Skip to content
Merged
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
19 changes: 11 additions & 8 deletions src/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
import json
from typing import Dict, Any, List
from datetime import datetime
import logging

logging.getLogger().setLevel(level=logging.INFO)
except ImportError:
import sys

print("Missing dependencies. Please reach @jboursier if needed.")
logging.error("Missing dependencies. Please reach @jboursier if needed.")
sys.exit(255)

from ghas_cli.utils import repositories, vulns, teams, issues, actions, roles, secrets
Expand Down Expand Up @@ -417,7 +420,7 @@ def repositories_archivable(
# 1. Get list repositories passed as argument
res = input_repos_list.readlines()

print(len(res))
logging.info(len(res))
for repo in res:
repo = repo.rstrip("\n")

Expand Down Expand Up @@ -793,7 +796,7 @@ def secret_alerts_export(
output_csv.write(
f"{secret['state']}, {secret['resolution']}, {secret['resolved_at']}, {secret['repository_full_name']}, {secret['url']}, {secret['secret_type']}, {secret['secret']}\n"
)
print(f"Retrieved {len(secrets_list)} secrets.")
logging.info(f"Retrieved {len(secrets_list)} secrets.")


##############
Expand Down Expand Up @@ -921,7 +924,7 @@ def roles_add(
name: str,
description: str,
base_role: str,
permission: List,
permissions: List,
organization: str,
token: str,
) -> None:
Expand Down Expand Up @@ -1073,7 +1076,7 @@ def mass_deploy(
with open("./templates/codeql.md", "r") as f:
template_codeql = f.read()

print(
logging.info(
f"Enabling Actions ({actions_enable}), Secret Scanner ({secretscanner}), Push Protection ({pushprotection}), Dependabot ({dependabot}), CodeQL ({codeql}), Dependency Reviewer ({reviewer}) to {len(repos_list)} repositories."
)

Expand All @@ -1091,7 +1094,7 @@ def mass_deploy(
reviewer_res = None
mend_res = 0

print(f"{repo}....", end="")
logging.info(f"{repo}....")

if actions_enable:
actions_res = actions.set_permissions(
Expand Down Expand Up @@ -1166,7 +1169,7 @@ def mass_deploy(
token=token,
)

print(
logging.info(
f"Done: {actions_res},{secretscanner_res}, {pushprotection_res}, {dependabot_res}, {codeql_res}, {reviewer_res}, {issue_secretscanner_res}, {issue_pushprotection_res}, {issue_dependabot_res}, {issue_codeql_res}, {mend_res}"
)
# CSV columns
Expand Down Expand Up @@ -1322,7 +1325,7 @@ def mass_set_developer_role(
perms = teams.get_repo_perms(team, repo.name, organization, token)
if "write" == perms[-1]:
write_perms.append([team, repo.name, perms[-1]])
print([team, repo.name, perms[-1]])
logging.info([team, repo.name, perms[-1]])
output_perms_list.write(f"{team}, {repo.name}, {perms[-1]}\n")

# Assign the Developer role
Expand Down
5 changes: 3 additions & 2 deletions src/ghas_cli/utils/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
#!/usr/bin/env python3

import json
import logging


def output_to_csv(alerts_per_repos: Dict, location: str) -> bool:
try:
with open(location, "w") as log_file:
log_file.write(json.dumps(alerts_per_repos))
except Exception as e:
print(str(e))
print(f"Failure to write the output to {location}")
logging.error(str(e))
logging.error(f"Failure to write the output to {location}")
return False
return True
13 changes: 10 additions & 3 deletions src/ghas_cli/utils/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from datetime import datetime
import time
import requests
import logging

# If the rate-limit is reached, sleep X seconds
SLEEP_1_MINUTE = 60
Expand All @@ -28,7 +29,7 @@ def get_github_headers(token: str) -> Dict:
def check_rate_limit(response: Any) -> bool:
if "0" == response.headers["x-ratelimit-remaining"]:
reset_time = datetime.fromtimestamp(int(response.headers["x-ratelimit-reset"]))
print(
logging.warn(
f"Rate limit reached: {response.headers['x-ratelimit-remaining']}/{response.headers['x-ratelimit-limit']} - {reset_time}"
)

Expand All @@ -37,37 +38,43 @@ def check_rate_limit(response: Any) -> bool:

if response.status_code == 403:
# This can be secondary rate limit or SSO error
print(response.json()["message"])
logging.warn(response.json()["message"])
return True

time.sleep(SLEEP_BETWEEN_REQUESTS)
return False


def check_unauthorized(response: Any):
if response.status_code == 401:
print(response.json()["message"])
logging.error(response.json()["message"])
return False
return True


def check_response(response: any):
check_rate_limit(response)
check_unauthorized(response)


def get(*args, **kwargs):
response = requests.get(*args, **kwargs)
check_response(response)
return response


def post(*args, **kwargs):
response = requests.post(*args, **kwargs)
check_response(response)
return response


def put(*args, **kwargs):
response = requests.put(*args, **kwargs)
check_response(response)
return response


def patch(*args, **kwargs):
response = requests.patch(*args, **kwargs)
check_response(response)
Expand Down
Loading