-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from snyk-labs/feat/mt-support
feat: Multitenant EU and AU support
- Loading branch information
Showing
9 changed files
with
293 additions
and
124 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "snyk-tags" | ||
version = "2.1.0" | ||
version = "2.2.0" | ||
description = "CLI tool designed to manage tags and attributes at scale" | ||
authors = ["EricFernandezSnyk <[email protected]>"] | ||
license = "MIT" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,11 +20,14 @@ | |
|
||
|
||
# Reach to the API and generate tokens | ||
def create_client(token: str) -> httpx.Client: | ||
return httpx.Client( | ||
base_url="https://snyk.io/api/v1", | ||
headers={"Authorization": f"token {token}", "Content-Type": "application/json"}, | ||
def create_client(token: str, tenant: str) -> httpx.Client: | ||
base_url = ( | ||
f"https://api.{tenant}.snyk.io/v1" | ||
if tenant in ["eu", "au"] | ||
else "https://api.snyk.io/v1" | ||
) | ||
headers = {"Authorization": f"token {token}"} | ||
return httpx.Client(base_url=base_url, headers=headers) | ||
|
||
|
||
# Apply attributes to a specific project | ||
|
@@ -66,19 +69,20 @@ def apply_attributes_to_project( | |
criticality = typer.style(criticality, bold=True, fg=typer.colors.MAGENTA) | ||
environment = typer.style(environment, bold=True, fg=typer.colors.MAGENTA) | ||
lifecycle = typer.style(lifecycle, bold=True, fg=typer.colors.MAGENTA) | ||
|
||
if req.status_code == 200: | ||
logging.info( | ||
f"Successfully added {criticality},{environment},{lifecycle} attributes to Project: {project_name}." | ||
) | ||
if req.status_code == 422: | ||
elif req.status_code == 422: | ||
logging.warning( | ||
f"Data {attribute_data} cannot be processed, make sure you have written the correct values (refer to help or Readme) and that they are in low caps. Error message: {req.json()}." | ||
) | ||
if req.status_code == 404: | ||
elif req.status_code == 404: | ||
logging.error( | ||
f"Project not found, likely a READ-ONLY project. Project: {project_name}. Error message: {req.json()}." | ||
) | ||
if req.status_code == 500: | ||
elif req.status_code == 500: | ||
logging.error( | ||
f"Error message: {req.json()}. Please contact [email protected]." | ||
) | ||
|
@@ -93,25 +97,33 @@ def apply_attributes_to_projects( | |
criticality: list, | ||
environment: list, | ||
lifecycle: list, | ||
tenant: str, | ||
) -> None: | ||
with create_client(token=token) as client: | ||
with create_client(token=token, tenant=tenant) as client: | ||
for org_id in org_ids: | ||
client_v3 = SnykClient(token=token) | ||
projects = client_v3.organizations.get(org_id).projects.all() | ||
base_url = ( | ||
f"https://api.{tenant}.snyk.io/rest" | ||
if tenant in ["eu", "au"] | ||
else "https://api.snyk.io/rest" | ||
) | ||
client_v3 = SnykClient( | ||
token=token, url=base_url, version="2023-08-31~experimental" | ||
) | ||
projects = client_v3.get(f"/orgs/{org_id}/projects").json() | ||
|
||
badname = 0 | ||
rightname = 0 | ||
|
||
for project in projects: | ||
if project.name.startswith(name): | ||
for project in projects["data"]: | ||
if project["attributes"]["name"].startswith(name): | ||
apply_attributes_to_project( | ||
client=client, | ||
org_id=org_id, | ||
project_id=project.id, | ||
project_id=project["id"], | ||
criticality=criticality, | ||
environment=environment, | ||
lifecycle=lifecycle, | ||
project_name=project.name, | ||
project_name=project["attributes"]["name"], | ||
) | ||
rightname = 1 | ||
else: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.