Skip to content
This repository was archived by the owner on Nov 9, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ Options:
--sync Forces a sync regardless of cache status
--github-token TEXT GitHub access token [env var: GITHUB_TOKEN;
required]
--github-url TEXT GitHub Base URL as:'https://hostname/api/v3', if
not set here will load from ENV VAR named in snyk-
sync.yaml [env var: GITHUB_URL]
--help Show this message and exit.

Commands:
Expand Down Expand Up @@ -163,9 +166,19 @@ docker push ghcr.io/snyk-playground/snyk-sync:latest
```
docker pull ghcr.io/snyk-playground/snyk-sync:latest
docker tag ghcr.io/snyk-playground/snyk-sync:latest snyk-sync:latest
docker run --rm -it -e GITHUB_TOKEN -e SNYK_TOKEN -v "${PWD}":/runtime snyk-sync:latest --sync target
docker run --rm -it -e GITHUB_TOKEN -e SNYK_TOKEN -v "${PWD}":/runtime snyk-sync:latest --sync targets
```

### Using a custom github url for Github Enterprise

A custom url can be set as an evironment variable: `export GITHUB_URL=https://hostname/api/v3`

```
docker run --rm -it -e GITHUB_TOKEN -e SNYK_TOKEN -e GITHUB_URL -v "${PWD}":/runtime snyk-sync:latest --sync targets
```

If environment variable not set, default (github.com) will be used. `github_url_env_name: GITHUB_URL` can be set in `snyk-sync.yml` to force checking for the custom url.

### Using a custom CA Root Certificate / proxies

If using a proxy, ensure that you are passing HTTP_PROXY and HTTPS_PROXY environment variables to the container runtimes.
Expand Down
1 change: 1 addition & 0 deletions conf/snyk-sync.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ default:
orgName: ie-playground
github_orgs:
- snyk-playground
github_url_env_name: GITHUB_URL
schema: 2
snyk:
groups:
Expand Down
16 changes: 12 additions & 4 deletions snyk_sync/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ def main(
envvar="GITHUB_TOKEN",
callback=settings_callback,
),
github_url: str = typer.Option(
default=None,
help="GitHub Base URL as:'https://hostname/api/v3', if not set here will load from ENV VAR named in snyk-sync.yaml",
envvar="GITHUB_URL",
callback=settings_callback,
),
):

# We keep this as the global settings hash
Expand Down Expand Up @@ -203,7 +209,11 @@ def sync(

GH_PAGE_LIMIT = 100

gh = Github(s.github_token, per_page=GH_PAGE_LIMIT)
if s.github_url is not None:
gh = Github(base_url=s.github_url, login_or_token=s.github_token, per_page=GH_PAGE_LIMIT)
else:
gh = Github(s.github_token, per_page=GH_PAGE_LIMIT)


client = SnykClient(str(s.snyk_token), user_agent=f"pysnyk/snyk_services/sync/{__version__}", tries=2, delay=1)

Expand Down Expand Up @@ -414,9 +424,7 @@ def targets(
load_conf()
tmp_watch: SnykWatchList = load_watchlist(s.cache_dir)
watchlist.repos = tmp_watch.repos

# print(f"{watchlist=}")


all_orgs = Orgs(cache=str(s.cache_dir), groups=s.snyk_groups)

all_orgs.load()
Expand Down
1 change: 0 additions & 1 deletion snyk_sync/models/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ def parse_branches(self, default_org: str, snyk_orgs: dict) -> List[Branch]:

# if we have any other values, we will update that also
if isinstance(v, dict):

if "orgName" in v.keys():
if v["orgName"] in snyk_orgs.keys():
org_name = v["orgName"]
Expand Down
1 change: 1 addition & 0 deletions snyk_sync/models/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Settings(BaseModel):
snyk_group: Optional[UUID4]
snyk_token: Optional[UUID4]
github_token: Optional[str]
github_url: Optional[str]
github_orgs: List[str] = list()
cache_timeout: Optional[float]
instance: Optional[str]
Expand Down
21 changes: 17 additions & 4 deletions snyk_sync/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ def newer(cached: str, remote: str) -> bool:
cache_ts = datetime.strptime(cached, "%Y-%m-%d %H:%M:%S")
remote_ts = datetime.strptime(remote, "%Y-%m-%d %H:%M:%S")

# print(cache_ts, remote_ts)

return bool(remote_ts < cache_ts)


Expand Down Expand Up @@ -231,7 +229,7 @@ def default_settings(
if token_env_name in environ.keys():
token = environ[token_env_name]
else:
raise Exception(f"Environment Variable {token_env_name} for {name}is not set properly and required")
raise Exception(f"Environment Variable {token_env_name} for {name} is not set properly and required")

return token

Expand All @@ -242,10 +240,25 @@ def default_settings(
if token_env_name in environ.keys():
token = environ[token_env_name]
else:
raise Exception(f"Environment Variable {token_env_name} for {name}is not set properly and required")
raise Exception(f"Environment Variable {token_env_name} for {name} is not set properly and required")

return token

if name == "github_url":

#if github_url_env_name not specified in snyk-snyc.yml, don't force check on env variable
if 'github_url_env_name' not in s.keys():
return None

github_url_env_name = s["github_url_env_name"]

if github_url_env_name in environ.keys():
github_url = environ[github_url_env_name]
else:
raise Exception(f"Environment Variable {github_url_env_name} for {name} is not set properly and required")

return github_url

if name == "default_org":
return s["default"]["orgName"]

Expand Down