-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
176 lines (149 loc) · 5.74 KB
/
run.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
'''
GitHub Actions secrets API
Reference: https://developer.github.com/v3/actions/secrets/
'''
import os
import sys
import argparse
import json
import requests
from nacl import encoding, public
BASEURL="https://api.github.com"
class githubSecrets(object):
def __init__(self):
parser = argparse.ArgumentParser(
description="GitHub Actions Secrets management",
usage='''<command> [args]
Available commands:
listSecrets - List all secrets (names only) from a repo
createSecret - Create or update a secret in a repo
deleteSecret - Delete a secret
Arguments:
--github_token - Access token with repo scope
--owner - GitHub owner
--repo - Git repository
--secret - Name of the secret to retrieve or create/update
--value - Secret value to create/update
'''
)
parser.add_argument('command', help="Command to run")
args = parser.parse_args(sys.argv[1:2])
if not hasattr(self, args.command):
print('Unrecognized command')
parser.print_help()
exit(1)
getattr(self, args.command)()
def listSecrets(self):
parser = argparse.ArgumentParser(
description="List secrets in repo(s)",
usage='''listSecrets [args]
Available arguments:
--github_token
--owner
--repo
'''
)
parser.add_argument('--github_token', required=True)
parser.add_argument('--owner', required=True)
parser.add_argument('--repo', required=True)
args = parser.parse_args(sys.argv[2:])
# print(f'List secrets for {args.owner}/{args.repo}')
global headers
headers = {"Authorization": f"token {args.github_token}"}
r = list_secrets(args.owner, args.repo)
print(json.dumps(r, indent=4, sort_keys=True))
def createSecret(self):
parser = argparse.ArgumentParser(
description="Create/Update secret",
usage='''createSecret [args]
Available arguments:
--github_token
--owner
--repo
--secret
--value
'''
)
parser.add_argument('--github_token', required=True)
parser.add_argument('--owner', required=True)
parser.add_argument('--repo', required=True)
parser.add_argument('--secret', required=True)
parser.add_argument('--value', required=True)
args = parser.parse_args(sys.argv[2:])
# print(f'Create/Update {args.secret} secret in {args.owner}/{args.repo}')
global headers
headers = {"Authorization": f"token {args.github_token}"}
r = create_secret(args.owner, args.repo, args.secret, args.value)
print(json.dumps(r, indent=4, sort_keys=True))
def deleteSecret(self):
parser = argparse.ArgumentParser(
description="Delete a secret",
usage='''createSecret [args]
Available arguments:
--github_token
--owner
--repo
--secret
'''
)
parser.add_argument('--github_token', required=True)
parser.add_argument('--owner', required=True)
parser.add_argument('--repo', required=True)
parser.add_argument('--secret', required=True)
args = parser.parse_args(sys.argv[2:])
# print(f'Deleting {args.secret} secret in {args.owner}/{args.repo}')
global headers
headers = {"Authorization": f"token {args.github_token}"}
r = delete_secret(args.owner, args.repo, args.secret)
print(json.dumps(r, indent=4, sort_keys=True))
def encrypt(key: str, secret_value: str) -> str:
"""Encrypt a Unicode string using the public key."""
public_key = public.PublicKey(key, encoding.Base64Encoder())
sealed_box = public.SealedBox(public_key)
encrypted = sealed_box.encrypt(secret_value.encode(), encoding.Base64Encoder())
return encrypted.decode("utf-8")
def get_pub_key(owner, repo):
url = f"{BASEURL}/repos/{owner}/{repo}/actions/secrets/public-key"
r = requests.get(url, headers=headers)
if r.status_code != 200:
sys.exit("Could not get public key")
return json.loads(r.text)
def list_secrets(owner, repo):
url = f"{BASEURL}/repos/{owner}/{repo}/actions/secrets"
r = requests.get(url, headers=headers)
if r.status_code != 200:
sys.exit("Could not list secrets")
return json.loads(r.text)
def get_secret(owner, repo, secret):
url = f"{BASEURL}/repos/{owner}/{repo}/actions/secrets/{secret}"
r = requests.get(url, headers=headers)
print(r.text)
if r.status_code != 200:
sys.exit("Could not get secret")
return json.loads(r.text)
def create_secret(owner, repo, secret, value):
url = f"{BASEURL}/repos/{owner}/{repo}/actions/secrets/{secret}"
pub_key = get_pub_key(owner, repo)
encrypted_value = encrypt(pub_key["key"], value)
data = {"encrypted_value": encrypted_value, "key_id": pub_key["key_id"]}
r = requests.put(url, headers=headers, data=json.dumps(data))
if r.status_code == 201:
print(f"Secret {secret} created")
elif r.status_code == 204:
print(f"Secret {secret} updated")
else:
sys.exit(f"Could not create/update secret {secret}. Response:\n{r.text}")
return r.ok
def delete_secret(owner, repo, secret):
url = f"{BASEURL}/repos/{owner}/{repo}/actions/secrets/{secret}"
r = requests.delete(url, headers=headers)
if r.status_code != 204:
sys.exit(f"Could not delete secret {secret}")
return {
"ok": r.ok,
"status_code": r.status_code,
"secret": secret,
"action": "delete"
}
if __name__ == "__main__":
githubSecrets()