-
Notifications
You must be signed in to change notification settings - Fork 5
/
github-secrets-manager.py
executable file
·391 lines (315 loc) · 12.3 KB
/
github-secrets-manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#!/usr/bin/env python3
import logging
import logging.handlers
import pkg_resources
import argparse
import yaml
from agithub.GitHub import GitHub
from pprint import pformat
from base64 import b64encode
from nacl import encoding, public
# version = pkg_resources.get_distribution('github-secrets-manager').version
# Small cache of repo pkeys to save some API calls
public_key_cache = {}
def read_secrets_file(filename):
"""Read the YAML configuration file"""
logging.debug("read_secrets_file")
secrets = {}
secrets = yaml.safe_load(open(filename))
return secrets
# https://developer.github.com/v3/actions/secrets/#create-or-update-a-secret-for-a-repository
def encrypt(public_key: str, secret_value: str) -> str:
"""Encrypt a Unicode string using the public key."""
public_key = public.PublicKey(public_key.encode("utf-8"), encoding.Base64Encoder())
sealed_box = public.SealedBox(public_key)
encrypted = sealed_box.encrypt(secret_value.encode("utf-8"))
return b64encode(encrypted).decode("utf-8")
""" Test if the path is an org or a repo"""
def is_repo(path):
if "/" in path:
return True
return False
"""Get the public key used to encrypt secrets for the org or a repo"""
def get_public_key(path, github_handle, target="actions"):
global public_key_cache
gh_status = 0
key = {"key_id": "", "key": ""}
cache_key_id = path + "/" + target
if cache_key_id in public_key_cache:
logging.debug("Public key cache hit for %s" % path)
key = public_key_cache[cache_key_id]
else:
logging.debug("Public key cache miss for %s" % path)
if is_repo(path):
owner, repo = path.split("/")
if owner and repo:
gh_status, data = (
github_handle.repos[owner][repo][target].secrets["public-key"].get()
)
else:
logging.error("unable to determine owner and repo from %s" % path)
else:
# org key
gh_status, data = (
github_handle.orgs[path][target].secrets["public-key"].get()
)
if gh_status == 200:
logging.debug("Successfully read private key for %s" % path)
public_key_cache[cache_key_id] = data
key = data
else:
logging.error("Error reading private key for %s : %d" % (path, gh_status))
return key
"""Check if a secret already exists on a repo or for the org"""
def secret_exists(path, secret_name, github_handle, target="actions"):
status = False
gh_status = 0
if is_repo(path):
owner, repo = path.split("/")
if owner and repo:
gh_status, data = (
github_handle.repos[owner][repo][target].secrets[secret_name].get()
)
else:
logging.error("unable to determine owner and repo from %s" % path)
else:
# This is an org secret
gh_status, data = github_handle.orgs[path][target].secrets[secret_name].get()
if gh_status == 200:
status = True
return status
"""Add or update a secret in a github repo or org"""
def upsert_secret(path, secret_name, secret_val, github_handle, target="actions"):
status = False
gh_status = 0
logger.info("Upserting path:%s sec:%s val:%s" % (path, secret_name, secret_val))
public_key = get_public_key(path, github_handle, target)
if public_key["key"]:
if public_key["key_id"]:
encrypted_secret = encrypt(public_key["key"], str(secret_val))
request_body = {
"encrypted_value": encrypted_secret,
"key_id": public_key["key_id"],
}
request_headers = {"Content-Type": "application/json"}
if is_repo(path):
owner, repo = path.split("/")
if owner and repo:
gh_status, data = (
github_handle.repos[owner][repo][target]
.secrets[secret_name]
.put(body=request_body, headers=request_headers)
)
else:
logging.error("unable to determine owner and repo from %s" % path)
else:
# org secret
request_body["visibility"] = (
"private" # this secret will only be visible to private repos in the org
)
gh_status, data = (
github_handle.orgs[path][target]
.secrets[secret_name]
.put(body=request_body, headers=request_headers)
)
if gh_status == 204 or gh_status == 201:
status = True
else:
logger.error("Error upserting secret %s : %d" % (path, gh_status))
else:
logging.error("No public key ID - unable to upsert secret")
else:
logging.error("No public key - unable to upsert secret")
return status
"""Remove a secret from a github repo or org"""
def remove_secret(path, secret_name, github_handle, target="actions"):
status = False
if secret_exists(path, secret_name, github_handle, target):
if is_repo(path):
owner, repo = path.split("/")
if owner and repo:
gh_status, data = (
github_handle.repos[owner][repo][target]
.secrets[secret_name]
.delete()
)
else:
logging.error("unable to determine owner and repo from %s" % path)
else:
# org secret
gh_status, data = (
github_handle.orgs[path][target].secrets[secret_name].delete()
)
if gh_status == 204:
status = True
else:
logger.error("Error removing secret %s : %d" % (path, gh_status))
else:
status = True # Treat as if it was a removal if secret does not exist
return status
def manage_secret(secret, github_handle, groups, target):
remove = False
repos = []
orgs = []
errors = 0
if secret and "name" in secret:
secret_name = secret["name"].strip()
logging.info("Secret found: %s" % secret_name)
if "value" in secret and secret["value"]:
secret_val = secret["value"]
else:
# We assume if there is no value, we are removing the secret
logging.info(
"No value defined for %s - removing parameter from all repos" % secret_name
)
remove = True
if "groups" in secret:
for group in secret["groups"]:
if group in groups:
repos.extend(groups[group])
else:
logging.info("No group defined for %s" % group)
if "orgs" in secret:
orgs.extend(secret["orgs"])
if "repos" in secret:
repos.extend(secret["repos"])
# Process any org values first
if orgs:
for org in orgs:
if remove:
if dryrun:
logging.info("DRYRUN: Removing %s from org %s" % (secret_name, org))
else:
if remove_secret(org, secret_name, github_handle, target):
logging.info(
"Successfully removed secret %s from org %s"
% (secret_name, org)
)
else:
logging.error(
"Unable to remove secret %s from org %s"
% (secret_name, org)
)
errors += 1
else:
if dryrun:
logging.info("DRYRUN: Adding %s to org %s" % (secret_name, org))
else:
if upsert_secret(
org, secret_name, secret_val, github_handle, target
):
logging.info(
"Successfully added/updated secret %s in org %s"
% (secret_name, org)
)
else:
logging.error(
"Unable to add/update secret %s in org %s"
% (secret_name, org)
)
errors += 1
if repos:
for repo in repos:
repo = repo.strip()
if (len(repos_filter) > 0 and repo in repos_filter) or len(
repos_filter
) == 0:
if remove:
if dryrun:
logging.info(
"DRYRUN: Removing %s from repo %s" % (secret_name, repo)
)
else:
if remove_secret(repo, secret_name, github_handle, target):
logging.info(
"Successfully removed secret %s from repo %s"
% (secret_name, repo)
)
else:
logging.error(
"Unable to remove secret %s from repo %s"
% (secret_name, repo)
)
errors += 1
else:
if dryrun:
logging.info("DRYRUN: Adding %s to %s" % (secret_name, repo))
else:
if upsert_secret(
repo, secret_name, secret_val, github_handle, target
):
logging.info(
"Successfully added/updated secret %s in repo %s"
% (secret_name, repo)
)
else:
logging.error(
"Unable to add/update secret %s in repo %s"
% (secret_name, repo)
)
errors += 1
return errors
if __name__ == "__main__":
secrets = {}
public_key_cache = {}
repos_filter = []
description = "Synchronize secrets with github repos\n"
parser = argparse.ArgumentParser(
description=description, formatter_class=argparse.RawTextHelpFormatter
)
parser.add_argument(
"--secrets-file", help="Secrets file", dest="secrets_filename", required=True
)
parser.add_argument(
"--github-pat", help="Github access token", dest="github_pat", required=True
)
parser.add_argument(
"--verbose", help="Turn on DEBUG logging", action="store_true", required=False
)
parser.add_argument(
"--repos",
help="Comma separated list of repos to be updated",
dest="repos_filter",
required=False,
)
parser.add_argument(
"--dryrun",
help="Do a dryrun - no changes will be performed",
dest="dryrun",
action="store_true",
default=False,
required=False,
)
args = parser.parse_args()
log_level = logging.INFO
if args.verbose:
print("Verbose logging selected")
log_level = logging.DEBUG
if args.repos_filter:
repos_filter = args.repos_filter.split(",")
# if set, make no changes and log only what would happen
dryrun = args.dryrun
# Setup some logging
logger = logging.getLogger()
logger.setLevel(log_level)
ch = logging.StreamHandler()
ch.setLevel(log_level)
console_formatter = logging.Formatter("%(levelname)8s: %(message)s")
ch.setFormatter(console_formatter)
logger.addHandler(ch)
# Read the yaml file
secrets = read_secrets_file(args.secrets_filename)
logging.debug(pformat(secrets))
# Initialize connection to Github API
github_handle = GitHub(token=args.github_pat)
groups = {}
if "groups" in secrets:
groups = secrets["groups"]
# Loop over each secret in the config file
# For each, determine if we are adding it to a repo or globally to an org
for secret in secrets["secrets"]:
manage_secret(secret, github_handle, groups, "actions")
if "dependabot" in secrets:
for secret in secrets["dependabot"]:
manage_secret(secret, github_handle, groups, "dependabot")
logging.info("Complete")