|
8 | 8 | - PAT of appropriate scope (assumes the workflow token if not specified) |
9 | 9 | - Report scope ("enterprise", "organization", "repository") |
10 | 10 | - Enterprise slug OR organization name OR repository name |
| 11 | +- Features to run (comma separated list of "secretscanning", "codescanning", "dependabot") |
11 | 12 |
|
12 | 13 | Outputs: |
13 | 14 | - CSV file of secret scanning alerts |
14 | 15 | - CSV file of code scanning alerts |
15 | | -
|
16 | | -TODO: dependabot alerts |
| 16 | +- CSV file of Dependabot alerts |
17 | 17 | """ |
18 | 18 |
|
19 | 19 | # Import modules |
20 | 20 | from src import code_scanning, dependabot, enterprise, secret_scanning |
21 | 21 | import os |
22 | 22 |
|
| 23 | + |
| 24 | +# Define the available features |
| 25 | +FEATURES = ["secretscanning", "codescanning", "dependabot"] |
| 26 | + |
23 | 27 | # Read in config values |
24 | 28 | if os.environ.get("GITHUB_API_URL") is None: |
25 | 29 | api_endpoint = "https://api.github.com" |
|
46 | 50 | else: |
47 | 51 | scope_name = os.environ.get("SCOPE_NAME") |
48 | 52 |
|
| 53 | +if os.environ.get("FEATURES") is None: |
| 54 | + features = FEATURES |
| 55 | +else: |
| 56 | + if os.environ.get("FEATURES") == "all": |
| 57 | + features = FEATURES |
| 58 | + else: |
| 59 | + features = os.environ.get("FEATURES").split(",") |
| 60 | + for f in features: |
| 61 | + if f not in FEATURES: |
| 62 | + print( |
| 63 | + f"Invalid feature: {f}. Proceeding without. Valid features are: {FEATURES}" |
| 64 | + ) |
| 65 | + features.remove(f) |
| 66 | + |
| 67 | + |
49 | 68 | # Do the things! |
50 | 69 | if __name__ == "__main__": |
| 70 | + print("Starting GitHub security report...") |
| 71 | + # enterprise scope |
51 | 72 | if report_scope == "enterprise": |
52 | 73 | # secret scanning |
53 | | - secrets_list = secret_scanning.get_enterprise_secret_scanning_alerts( |
54 | | - api_endpoint, github_pat, scope_name |
55 | | - ) |
56 | | - secret_scanning.write_enterprise_secrets_list(secrets_list) |
57 | | - # code scanning |
58 | | - if enterprise.get_enterprise_version(api_endpoint) != "GHEC": |
59 | | - repo_list = enterprise.get_repo_report(url, github_pat) |
60 | | - cs_list = code_scanning.list_enterprise_server_code_scanning_alerts( |
61 | | - api_endpoint, github_pat, repo_list |
62 | | - ) |
63 | | - code_scanning.write_enterprise_server_cs_list(cs_list) |
64 | | - else: |
65 | | - cs_list = code_scanning.list_enterprise_cloud_code_scanning_alerts( |
| 74 | + if "secretscanning" in features: |
| 75 | + secrets_list = secret_scanning.get_enterprise_secret_scanning_alerts( |
66 | 76 | api_endpoint, github_pat, scope_name |
67 | 77 | ) |
68 | | - code_scanning.write_enterprise_cloud_cs_list(cs_list) |
| 78 | + secret_scanning.write_enterprise_secrets_list(secrets_list) |
| 79 | + # code scanning |
| 80 | + if "codescanning" in features: |
| 81 | + version = enterprise.get_enterprise_version(api_endpoint) |
| 82 | + # for GHES version 3.5 and 3.6 we need to loop through each repo and use the repo level api to get the code scanning alerts |
| 83 | + # for 3.7 and above we use the enterprise level api to get the code scanning alerts |
| 84 | + if version.startswith("3.5") or version.startswith("3.6"): |
| 85 | + repo_list = enterprise.get_repo_report(url, github_pat) |
| 86 | + cs_list = code_scanning.list_enterprise_server_code_scanning_alerts( |
| 87 | + api_endpoint, github_pat, repo_list |
| 88 | + ) |
| 89 | + code_scanning.write_enterprise_server_cs_list(cs_list) |
| 90 | + else: |
| 91 | + cs_list = code_scanning.list_enterprise_cloud_code_scanning_alerts( |
| 92 | + api_endpoint, github_pat, scope_name |
| 93 | + ) |
| 94 | + code_scanning.write_enterprise_cloud_cs_list(cs_list) |
69 | 95 | # dependabot alerts |
70 | | - if enterprise.get_enterprise_version(api_endpoint) == "GHEC": |
| 96 | + if "dependabot" in features: |
71 | 97 | dependabot_list = dependabot.list_enterprise_dependabot_alerts( |
72 | 98 | api_endpoint, github_pat, scope_name |
73 | 99 | ) |
74 | 100 | dependabot.write_org_or_enterprise_dependabot_list(dependabot_list) |
75 | | - else: |
76 | | - pass |
77 | | - |
| 101 | + # organization scope |
78 | 102 | elif report_scope == "organization": |
79 | 103 | # code scanning |
80 | | - cs_list = code_scanning.list_org_code_scanning_alerts( |
81 | | - api_endpoint, github_pat, scope_name |
82 | | - ) |
83 | | - code_scanning.write_org_cs_list(cs_list) |
| 104 | + if "codescanning" in features: |
| 105 | + cs_list = code_scanning.list_org_code_scanning_alerts( |
| 106 | + api_endpoint, github_pat, scope_name |
| 107 | + ) |
| 108 | + code_scanning.write_org_cs_list(cs_list) |
84 | 109 | # dependabot alerts |
85 | | - if enterprise.get_enterprise_version(api_endpoint) == "GHEC": |
| 110 | + if "dependabot" in features: |
86 | 111 | dependabot_list = dependabot.list_org_dependabot_alerts( |
87 | 112 | api_endpoint, github_pat, scope_name |
88 | 113 | ) |
89 | 114 | dependabot.write_org_or_enterprise_dependabot_list(dependabot_list) |
90 | | - else: |
91 | | - pass |
| 115 | + |
92 | 116 | # secret scanning |
93 | | - secrets_list = secret_scanning.get_org_secret_scanning_alerts( |
94 | | - api_endpoint, github_pat, scope_name |
95 | | - ) |
96 | | - secret_scanning.write_org_secrets_list(secrets_list) |
| 117 | + if "secretscanning" in features: |
| 118 | + secrets_list = secret_scanning.get_org_secret_scanning_alerts( |
| 119 | + api_endpoint, github_pat, scope_name |
| 120 | + ) |
| 121 | + secret_scanning.write_org_secrets_list(secrets_list) |
| 122 | + # repository scope |
97 | 123 | elif report_scope == "repository": |
98 | 124 | # code scanning |
99 | | - cs_list = code_scanning.list_repo_code_scanning_alerts( |
100 | | - api_endpoint, github_pat, scope_name |
101 | | - ) |
102 | | - code_scanning.write_repo_cs_list(cs_list) |
| 125 | + if "codescanning" in features: |
| 126 | + cs_list = code_scanning.list_repo_code_scanning_alerts( |
| 127 | + api_endpoint, github_pat, scope_name |
| 128 | + ) |
| 129 | + code_scanning.write_repo_cs_list(cs_list) |
103 | 130 | # dependabot alerts |
104 | | - if enterprise.get_enterprise_version(api_endpoint) == "GHEC": |
| 131 | + if "dependabot" in features: |
105 | 132 | dependabot_list = dependabot.list_repo_dependabot_alerts( |
106 | 133 | api_endpoint, github_pat, scope_name |
107 | 134 | ) |
108 | 135 | dependabot.write_repo_dependabot_list(dependabot_list) |
109 | | - else: |
110 | | - pass |
| 136 | + |
111 | 137 | # secret scanning |
112 | | - secrets_list = secret_scanning.get_repo_secret_scanning_alerts( |
113 | | - api_endpoint, github_pat, scope_name |
114 | | - ) |
115 | | - secret_scanning.write_repo_secrets_list(secrets_list) |
| 138 | + if "secretscanning" in features: |
| 139 | + secrets_list = secret_scanning.get_repo_secret_scanning_alerts( |
| 140 | + api_endpoint, github_pat, scope_name |
| 141 | + ) |
| 142 | + secret_scanning.write_repo_secrets_list(secrets_list) |
116 | 143 | else: |
117 | 144 | exit("invalid report scope") |
0 commit comments