pubsec 0.3.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pubsec/__init__.py +21 -0
- pubsec/app_secrets.py +204 -0
- pubsec/bitwarden-sdk.lock +17 -0
- pubsec/bitwarden.py +132 -0
- pubsec/cli.py +155 -0
- pubsec/github_app.py +516 -0
- pubsec/needs/__init__.py +216 -0
- pubsec/needs/adopt.py +127 -0
- pubsec/needs/bitwarden.py +121 -0
- pubsec/needs/gcp.py +246 -0
- pubsec/needs/github.py +220 -0
- pubsec/needs/k8s.py +261 -0
- pubsec/run.py +235 -0
- pubsec/service_account_key.py +161 -0
- pubsec/signing_keys.py +386 -0
- pubsec/walk.py +98 -0
- pubsec-0.3.1.dist-info/METADATA +322 -0
- pubsec-0.3.1.dist-info/RECORD +22 -0
- pubsec-0.3.1.dist-info/WHEEL +5 -0
- pubsec-0.3.1.dist-info/entry_points.txt +16 -0
- pubsec-0.3.1.dist-info/licenses/LICENSE +202 -0
- pubsec-0.3.1.dist-info/top_level.txt +1 -0
pubsec/github_app.py
ADDED
|
@@ -0,0 +1,516 @@
|
|
|
1
|
+
"""`pubsec github-app create`: make a GitHub App from a manifest, and put its
|
|
2
|
+
three values in Bitwarden, where `pubsec app-secrets` finds them (five with
|
|
3
|
+
`"keep_oauth_client": true`: its OAuth client id and secret too).
|
|
4
|
+
|
|
5
|
+
The manifest is the consuming repository's own, so pubsec names nothing of
|
|
6
|
+
anyone's. It is what GitHub's app-manifest flow takes, plus a `pubrules` section
|
|
7
|
+
that GitHub never sees (its name from when this was pubrules's):
|
|
8
|
+
|
|
9
|
+
{
|
|
10
|
+
"name": "some-app",
|
|
11
|
+
"url": "https://example.org",
|
|
12
|
+
"public": false,
|
|
13
|
+
"default_permissions": {"contents": "read"},
|
|
14
|
+
"pubrules": {
|
|
15
|
+
"org": "some-org", # or "user": "someone"
|
|
16
|
+
"bitwarden_project": "some-project",
|
|
17
|
+
"secret_prefix": "some-app"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
In order, stopping at the first thing wrong, so a refusal comes before an app
|
|
22
|
+
exists: show the plan (where --dry-run ends), ask for a Bitwarden access token
|
|
23
|
+
with write access, and look for the names in the project. Then a page on
|
|
24
|
+
127.0.0.1 posts the manifest to GitHub, GitHub redirects back with a code, and
|
|
25
|
+
the code becomes the app and its key. The browser opens the app's install page,
|
|
26
|
+
and the app's own key is used to watch for the installation, and to say which
|
|
27
|
+
repositories it reaches. Last, typing "yes"
|
|
28
|
+
writes `<prefix>-app-id`, `<prefix>-app-key` and `<prefix>-installation-id`, and
|
|
29
|
+
with `"keep_oauth_client": true` in the section, `<prefix>-client-id` and
|
|
30
|
+
`<prefix>-client-secret`: the client secret GitHub makes with the app, which
|
|
31
|
+
an app that signs people in needs, and which is otherwise dropped.
|
|
32
|
+
|
|
33
|
+
The token is only ever asked for, never read from a file or kept. It reaches bws,
|
|
34
|
+
which finds the project, in its environment, and Bitwarden's SDK, which looks the
|
|
35
|
+
names up by their identifiers alone. The key stays in this process's
|
|
36
|
+
memory, and is never printed or written to disk: Bitwarden's SDK writes the values
|
|
37
|
+
from memory, as bws can take one only on its command line.
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
from __future__ import annotations
|
|
41
|
+
|
|
42
|
+
import base64
|
|
43
|
+
import getpass
|
|
44
|
+
import hashlib
|
|
45
|
+
import hmac
|
|
46
|
+
import html
|
|
47
|
+
import itertools
|
|
48
|
+
import json
|
|
49
|
+
import os
|
|
50
|
+
import re
|
|
51
|
+
import secrets
|
|
52
|
+
import shutil
|
|
53
|
+
import subprocess
|
|
54
|
+
import threading
|
|
55
|
+
import time
|
|
56
|
+
import urllib.error
|
|
57
|
+
import urllib.parse
|
|
58
|
+
import urllib.request
|
|
59
|
+
import webbrowser
|
|
60
|
+
from dataclasses import dataclass
|
|
61
|
+
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
62
|
+
from pathlib import Path
|
|
63
|
+
from typing import Any
|
|
64
|
+
|
|
65
|
+
from .app_secrets import wrong_value
|
|
66
|
+
from .bitwarden import WriteFailed, ensure, scrub, write
|
|
67
|
+
from .bitwarden import names as stored_names
|
|
68
|
+
|
|
69
|
+
SECTION = "pubrules"
|
|
70
|
+
SETTINGS = {"org", "user", "bitwarden_project", "secret_prefix", "keep_oauth_client"}
|
|
71
|
+
OWNER = re.compile(r"[A-Za-z0-9][A-Za-z0-9-]*")
|
|
72
|
+
# So that ./with-secrets can export each of the three: it skips a name that
|
|
73
|
+
# is not a letter and then letters, digits and "_" once "-" becomes "_".
|
|
74
|
+
PREFIX = re.compile(r"[a-z][a-z0-9-]*")
|
|
75
|
+
POLL_SECONDS = 2
|
|
76
|
+
# RSASSA-PKCS1-v1_5's DigestInfo for SHA-256 (RFC 8017, 9.2).
|
|
77
|
+
SHA256_INFO = bytes.fromhex("3031300d060960864801650304020105000420")
|
|
78
|
+
BWS_MISSING = ("bws is not installed: get it from https://github.com/bitwarden/sdk-sm/releases "
|
|
79
|
+
"(or cargo install bws --locked).")
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
class Stop(Exception):
|
|
83
|
+
"""Something that ends the run, with what to tell the person running it."""
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class BwsFailed(Stop):
|
|
87
|
+
"""bws said no, in words that hold neither the token nor a value."""
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
@dataclass(frozen=True)
|
|
91
|
+
class Plan:
|
|
92
|
+
"""What the manifest's `pubrules` section says, and what it sends GitHub."""
|
|
93
|
+
|
|
94
|
+
manifest: dict
|
|
95
|
+
owner: str
|
|
96
|
+
org: bool
|
|
97
|
+
project: str
|
|
98
|
+
prefix: str
|
|
99
|
+
# Also keep the app's OAuth client (its id and the secret GitHub makes with it), for an app
|
|
100
|
+
# that signs people in: the manifest's `"keep_oauth_client": true`.
|
|
101
|
+
oauth: bool = False
|
|
102
|
+
|
|
103
|
+
@property
|
|
104
|
+
def names(self) -> dict[str, str]:
|
|
105
|
+
slots = ("app-id", "app-key", "installation-id") + (("client-id", "client-secret") if self.oauth else ())
|
|
106
|
+
return {slot: f"{self.prefix}-{slot}" for slot in slots}
|
|
107
|
+
|
|
108
|
+
def apps_url(self) -> str:
|
|
109
|
+
"""Where the owner's apps are: an organization's, or the signed-in user's."""
|
|
110
|
+
account = f"organizations/{self.owner}/" if self.org else ""
|
|
111
|
+
return f"https://github.com/{account}settings/apps"
|
|
112
|
+
|
|
113
|
+
def installation_url(self, installation: int) -> str:
|
|
114
|
+
"""Where the owner chooses which repositories an installation reaches."""
|
|
115
|
+
account = f"organizations/{self.owner}/" if self.org else ""
|
|
116
|
+
return f"https://github.com/{account}settings/installations/{installation}"
|
|
117
|
+
|
|
118
|
+
def new_app_url(self) -> str:
|
|
119
|
+
return f"{self.apps_url()}/new"
|
|
120
|
+
|
|
121
|
+
def settings_url(self, slug: str) -> str:
|
|
122
|
+
return f"{self.apps_url()}/{urllib.parse.quote(slug, safe='')}"
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def problems(manifest: Any) -> list[str]:
|
|
126
|
+
"""What is wrong with a manifest: one phrase each, none when sound."""
|
|
127
|
+
if not isinstance(manifest, dict):
|
|
128
|
+
return ["it must be an object"]
|
|
129
|
+
out = [f'it needs "{field}"' for field in ("name", "url")
|
|
130
|
+
if not (isinstance(manifest.get(field), str) and manifest[field])]
|
|
131
|
+
if "redirect_url" in manifest:
|
|
132
|
+
out.append('"redirect_url" is not its to set: pubsec points it at itself')
|
|
133
|
+
section = manifest.get(SECTION)
|
|
134
|
+
if not isinstance(section, dict):
|
|
135
|
+
return [*out, f'it needs a "{SECTION}" section']
|
|
136
|
+
out += [f'{key} is not a {SECTION} setting' for key in sorted(section.keys() - SETTINGS)]
|
|
137
|
+
owners = [key for key in ("org", "user") if key in section]
|
|
138
|
+
if len(owners) != 1:
|
|
139
|
+
out.append(f'{SECTION} names its owner with one of "org" and "user"')
|
|
140
|
+
elif not (isinstance(section[owners[0]], str) and OWNER.fullmatch(section[owners[0]])):
|
|
141
|
+
out.append(f'{SECTION}\'s "{owners[0]}" is not an account\'s name')
|
|
142
|
+
project = section.get("bitwarden_project")
|
|
143
|
+
if not (isinstance(project, str) and project.strip()):
|
|
144
|
+
out.append(f'{SECTION} needs "bitwarden_project", the project\'s name or id')
|
|
145
|
+
if not isinstance(section.get("keep_oauth_client", False), bool):
|
|
146
|
+
out.append(f'{SECTION}\'s "keep_oauth_client" must be true or false')
|
|
147
|
+
prefix = section.get("secret_prefix")
|
|
148
|
+
if not (isinstance(prefix, str) and PREFIX.fullmatch(prefix)):
|
|
149
|
+
out.append(f'{SECTION}\'s "secret_prefix" must be lower case letters, digits and -, '
|
|
150
|
+
"starting with a letter")
|
|
151
|
+
return out
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def load(path: Path) -> Plan:
|
|
155
|
+
try:
|
|
156
|
+
manifest = json.loads(path.read_text(encoding="utf-8"))
|
|
157
|
+
except OSError as error:
|
|
158
|
+
raise Stop(f"cannot read {path}: {error.strerror}") from None
|
|
159
|
+
except ValueError as error:
|
|
160
|
+
raise Stop(f"{path} is not JSON: {error}") from None
|
|
161
|
+
if bad := problems(manifest):
|
|
162
|
+
raise Stop(f"{path}: {'; '.join(bad)}")
|
|
163
|
+
section = manifest.pop(SECTION)
|
|
164
|
+
org = "org" in section
|
|
165
|
+
return Plan(manifest, section["org" if org else "user"], org, section["bitwarden_project"],
|
|
166
|
+
section["secret_prefix"], section.get("keep_oauth_client", False))
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def bws(token: str, *args: str, hide: tuple[str, ...] = ()) -> str:
|
|
170
|
+
"""Run bws with the token in its environment, and return what it printed."""
|
|
171
|
+
done = subprocess.run(["bws", *args], env={**os.environ, "BWS_ACCESS_TOKEN": token},
|
|
172
|
+
capture_output=True, text=True, check=False)
|
|
173
|
+
if done.returncode != 0:
|
|
174
|
+
raise BwsFailed(scrub(done.stderr, (token, *hide)).strip() or f"bws exited {done.returncode}")
|
|
175
|
+
return done.stdout
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def project_id(token: str, plan: Plan) -> tuple[str, str]:
|
|
179
|
+
"""The project's id and its organization's, which the SDK needs and can't look up."""
|
|
180
|
+
projects = _listed(bws(token, "project", "list", "--output", "json", "--color", "no"))
|
|
181
|
+
found = [p for p in projects if plan.project in (p.get("id"), p.get("name"))]
|
|
182
|
+
if len(found) != 1:
|
|
183
|
+
sees = ", ".join(sorted(str(p.get("name")) for p in projects)) or "none"
|
|
184
|
+
raise Stop(f"this token's machine account sees {len(found)} projects named {plan.project}, "
|
|
185
|
+
f"not one. It sees: {sees}.")
|
|
186
|
+
return found[0]["id"], found[0]["organizationId"]
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def _listed(printed: str) -> list[dict]:
|
|
190
|
+
try:
|
|
191
|
+
listed = json.loads(printed)
|
|
192
|
+
except ValueError:
|
|
193
|
+
raise Stop("bws printed something other than JSON") from None
|
|
194
|
+
if not isinstance(listed, list):
|
|
195
|
+
raise Stop("bws printed something other than a list")
|
|
196
|
+
return listed
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def targets(plan: Plan, existing: dict[str, list[str]], replace: bool) -> dict[str, str | None]:
|
|
200
|
+
"""Each name and the secret to edit, None to create it; stops if that isn't clear."""
|
|
201
|
+
names = list(plan.names.values())
|
|
202
|
+
if not replace and (taken := [name for name in names if name in existing]):
|
|
203
|
+
raise Stop(f"already in project {plan.project}: {', '.join(taken)}. "
|
|
204
|
+
"No app was made. --replace overwrites them.")
|
|
205
|
+
if twice := [name for name in names if len(existing.get(name, [])) > 1]:
|
|
206
|
+
raise Stop(f"more than one secret in project {plan.project} has the name "
|
|
207
|
+
f"{', '.join(twice)}, so which to replace is not clear. No app was made.")
|
|
208
|
+
return {name: existing[name][0] if name in existing else None for name in names}
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def github(method: str, path: str, what: str, bearer: str | None = None) -> Any:
|
|
212
|
+
"""One call to GitHub's API. `what` names it in errors: a path can hold the one-time code."""
|
|
213
|
+
base = os.environ.get("PUBSEC_GITHUB_API_URL", "https://api.github.com").rstrip("/")
|
|
214
|
+
headers = {"Accept": "application/vnd.github+json", "X-GitHub-Api-Version": "2022-11-28",
|
|
215
|
+
"User-Agent": "pubsec"}
|
|
216
|
+
if bearer:
|
|
217
|
+
headers["Authorization"] = f"Bearer {bearer}"
|
|
218
|
+
request = urllib.request.Request(f"{base}{path}", method=method, headers=headers,
|
|
219
|
+
data=b"" if method == "POST" else None)
|
|
220
|
+
try:
|
|
221
|
+
with urllib.request.urlopen(request, timeout=30) as response:
|
|
222
|
+
return json.loads(response.read())
|
|
223
|
+
except urllib.error.HTTPError as error:
|
|
224
|
+
try:
|
|
225
|
+
said = json.loads(error.read()).get("message", "")
|
|
226
|
+
except (ValueError, AttributeError):
|
|
227
|
+
said = ""
|
|
228
|
+
raise Stop(f"GitHub answered {what} with {error.code} {said}".rstrip()) from None
|
|
229
|
+
except (urllib.error.URLError, TimeoutError, ValueError) as error:
|
|
230
|
+
raise Stop(f"could not reach GitHub for {what}: {error}") from None
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def await_code(plan: Plan) -> str:
|
|
234
|
+
"""Send the browser through GitHub's form, and return the code it comes back with."""
|
|
235
|
+
state = secrets.token_urlsafe(32)
|
|
236
|
+
arrived = threading.Event()
|
|
237
|
+
code: list[str] = []
|
|
238
|
+
page: list[bytes] = []
|
|
239
|
+
|
|
240
|
+
class Handler(BaseHTTPRequestHandler):
|
|
241
|
+
def log_message(self, format, *args): # noqa: A002
|
|
242
|
+
"""The request line holds the code: it stays out of the terminal."""
|
|
243
|
+
|
|
244
|
+
def answer(self, status: int, body: bytes) -> None:
|
|
245
|
+
self.send_response(status)
|
|
246
|
+
self.send_header("Content-Type", "text/html; charset=utf-8")
|
|
247
|
+
self.send_header("Content-Length", str(len(body)))
|
|
248
|
+
self.send_header("Cache-Control", "no-store")
|
|
249
|
+
self.send_header("Referrer-Policy", "no-referrer")
|
|
250
|
+
self.end_headers()
|
|
251
|
+
self.wfile.write(body)
|
|
252
|
+
|
|
253
|
+
def do_GET(self): # noqa: N802
|
|
254
|
+
# Only what this machine's browser addresses to this server, so a
|
|
255
|
+
# site cannot reach it by rebinding a name of its own to 127.0.0.1.
|
|
256
|
+
if self.headers.get("Host") != f"127.0.0.1:{self.server.server_address[1]}":
|
|
257
|
+
self.answer(403, b"Not for this host.")
|
|
258
|
+
return
|
|
259
|
+
url = urllib.parse.urlsplit(self.path)
|
|
260
|
+
if url.path == "/":
|
|
261
|
+
self.answer(200, page[0])
|
|
262
|
+
return
|
|
263
|
+
if url.path != "/callback":
|
|
264
|
+
self.answer(404, b"Nothing here.")
|
|
265
|
+
return
|
|
266
|
+
query = urllib.parse.parse_qs(url.query)
|
|
267
|
+
given = query.get("state", [""])[0]
|
|
268
|
+
if not hmac.compare_digest(given.encode(), state.encode()) or not query.get("code"):
|
|
269
|
+
self.answer(400, b"This did not come from the form pubsec sent. Still waiting.")
|
|
270
|
+
return
|
|
271
|
+
code.append(query["code"][0])
|
|
272
|
+
self.answer(200, b"GitHub has made the app. Go back to the terminal.")
|
|
273
|
+
arrived.set()
|
|
274
|
+
|
|
275
|
+
server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
|
|
276
|
+
port = server.server_address[1]
|
|
277
|
+
sent = json.dumps({**plan.manifest, "redirect_url": f"http://127.0.0.1:{port}/callback"})
|
|
278
|
+
action = f"{plan.new_app_url()}?{urllib.parse.urlencode({'state': state})}"
|
|
279
|
+
page.append(f"""<!doctype html><meta charset="utf-8"><title>pubsec github-app</title>
|
|
280
|
+
<form method="post" action="{html.escape(action)}">
|
|
281
|
+
<input type="hidden" name="manifest" value="{html.escape(sent)}">
|
|
282
|
+
<button>Create the GitHub App</button></form>
|
|
283
|
+
<script>document.forms[0].submit()</script>
|
|
284
|
+
""".encode())
|
|
285
|
+
threading.Thread(target=server.serve_forever, daemon=True).start()
|
|
286
|
+
try:
|
|
287
|
+
local = f"http://127.0.0.1:{port}/"
|
|
288
|
+
print(f"Opening {local} , which sends the manifest to GitHub. Open it yourself if no browser "
|
|
289
|
+
"comes up, and check and confirm the app there.", flush=True)
|
|
290
|
+
webbrowser.open(local)
|
|
291
|
+
arrived.wait()
|
|
292
|
+
return code[0]
|
|
293
|
+
finally:
|
|
294
|
+
server.shutdown()
|
|
295
|
+
server.server_close()
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
def rsa_numbers(pem: str) -> tuple[int, int]:
|
|
299
|
+
"""The modulus and private exponent of a PKCS#1 key, the form GitHub gives."""
|
|
300
|
+
|
|
301
|
+
def items(data: bytes):
|
|
302
|
+
at = 0
|
|
303
|
+
while at < len(data):
|
|
304
|
+
size = data[at + 1]
|
|
305
|
+
at += 2
|
|
306
|
+
if size & 0x80:
|
|
307
|
+
count = size & 0x7f
|
|
308
|
+
size = int.from_bytes(data[at:at + count])
|
|
309
|
+
at += count
|
|
310
|
+
yield data[at:at + size]
|
|
311
|
+
at += size
|
|
312
|
+
|
|
313
|
+
try:
|
|
314
|
+
if "BEGIN RSA PRIVATE KEY" not in pem:
|
|
315
|
+
raise ValueError
|
|
316
|
+
der = base64.b64decode("".join(line for line in pem.splitlines() if not line.startswith("-----")))
|
|
317
|
+
numbers = [int.from_bytes(item) for item in items(next(items(der)))]
|
|
318
|
+
return numbers[1], numbers[3]
|
|
319
|
+
except (ValueError, IndexError, StopIteration):
|
|
320
|
+
raise Stop("GitHub's key is not a PKCS#1 RSA key, so pubsec cannot sign with it") from None
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
def app_jwt(app_id: int, pem: str, now: int | None = None) -> str:
|
|
324
|
+
"""A token for GitHub's /app endpoints, RS256 with no dependency: this signs
|
|
325
|
+
a few requests on the owner's own machine, so its timing shows nobody anything."""
|
|
326
|
+
issued = int(time.time()) if now is None else now
|
|
327
|
+
|
|
328
|
+
def part(data: bytes) -> str:
|
|
329
|
+
return base64.urlsafe_b64encode(data).rstrip(b"=").decode()
|
|
330
|
+
|
|
331
|
+
claims = {"iat": issued - 60, "exp": issued + 540, "iss": str(app_id)}
|
|
332
|
+
signed = (part(json.dumps({"alg": "RS256", "typ": "JWT"}).encode()) + "."
|
|
333
|
+
+ part(json.dumps(claims).encode()))
|
|
334
|
+
modulus, exponent = rsa_numbers(pem)
|
|
335
|
+
size = (modulus.bit_length() + 7) // 8
|
|
336
|
+
digest = SHA256_INFO + hashlib.sha256(signed.encode()).digest()
|
|
337
|
+
padded = b"\x00\x01" + b"\xff" * (size - len(digest) - 3) + b"\x00" + digest
|
|
338
|
+
signature = pow(int.from_bytes(padded), exponent, modulus).to_bytes(size)
|
|
339
|
+
return f"{signed}.{part(signature)}"
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
def await_installation(plan: Plan, app: dict) -> dict:
|
|
343
|
+
"""Open the install page, and wait until the owner has installed the app.
|
|
344
|
+
|
|
345
|
+
An error only says so and waits on: giving up would throw away a key that
|
|
346
|
+
has been seen once. Ctrl-C ends it, and the run has already said what's left.
|
|
347
|
+
"""
|
|
348
|
+
slug = urllib.parse.quote(app["slug"], safe="")
|
|
349
|
+
url = f"https://github.com/apps/{slug}/installations/new"
|
|
350
|
+
print(f"Opening {url} : install the app on {plan.owner}. Waiting for that (Ctrl-C to stop).", flush=True)
|
|
351
|
+
webbrowser.open(url)
|
|
352
|
+
said = None
|
|
353
|
+
while True:
|
|
354
|
+
try:
|
|
355
|
+
installs = github("GET", "/app/installations?per_page=100", "the list of installations",
|
|
356
|
+
bearer=app_jwt(app["id"], app["pem"]))
|
|
357
|
+
except Stop as error:
|
|
358
|
+
if str(error) != said:
|
|
359
|
+
print(f" {error}. Still trying.", flush=True)
|
|
360
|
+
said = str(error)
|
|
361
|
+
else:
|
|
362
|
+
for install in installs:
|
|
363
|
+
if str(install.get("account", {}).get("login", "")).casefold() == plan.owner.casefold():
|
|
364
|
+
return install
|
|
365
|
+
time.sleep(POLL_SECONDS)
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
def selected_repositories(app: dict, installation: int) -> list[str]:
|
|
369
|
+
"""The repositories an installation reaches, which only its own token can
|
|
370
|
+
list. That token lasts an hour and goes no further than this function."""
|
|
371
|
+
token = github("POST", f"/app/installations/{installation}/access_tokens",
|
|
372
|
+
"the request for an installation token", bearer=app_jwt(app["id"], app["pem"]))["token"]
|
|
373
|
+
names: list[str] = []
|
|
374
|
+
for page in itertools.count(1):
|
|
375
|
+
listed = github("GET", f"/installation/repositories?per_page=100&page={page}",
|
|
376
|
+
"the list of repositories", bearer=token)["repositories"]
|
|
377
|
+
names += [repository["full_name"] for repository in listed]
|
|
378
|
+
if len(listed) < 100:
|
|
379
|
+
return names
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
def report_reach(plan: Plan, app: dict, install: dict) -> None:
|
|
383
|
+
"""Say which repositories the app reached. Nothing here stops the run: the
|
|
384
|
+
key is not written yet, and the reach can be changed at any time."""
|
|
385
|
+
print(f"Installed on {plan.owner}, installation id {install['id']}.")
|
|
386
|
+
selection = install.get("repository_selection")
|
|
387
|
+
narrow = f"Change that at {plan.installation_url(install['id'])}"
|
|
388
|
+
if selection == "selected":
|
|
389
|
+
try:
|
|
390
|
+
names = selected_repositories(app, install["id"])
|
|
391
|
+
except (Stop, KeyError, TypeError) as error:
|
|
392
|
+
print(f"It reaches selected repositories, but pubsec could not list them: {error}. {narrow}")
|
|
393
|
+
return
|
|
394
|
+
print(f"It reaches {len(names)} selected repositories: {', '.join(sorted(names)) or 'none'}. {narrow}")
|
|
395
|
+
elif selection == "all":
|
|
396
|
+
print(f"It reaches ALL of {plan.owner}'s repositories, and any made later. {narrow}")
|
|
397
|
+
else:
|
|
398
|
+
print(f"GitHub did not say which repositories it reaches. Check at {plan.installation_url(install['id'])}")
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
def make_app(plan: Plan) -> dict:
|
|
402
|
+
code = await_code(plan)
|
|
403
|
+
app = github("POST", f"/app-manifests/{urllib.parse.quote(code, safe='')}/conversions",
|
|
404
|
+
"the manifest's code")
|
|
405
|
+
if not isinstance(app, dict) or not all(app.get(field) for field in ("id", "slug", "pem")):
|
|
406
|
+
raise Stop("GitHub's answer has no app id, slug and key")
|
|
407
|
+
if (wrong := wrong_value("key", str(app["pem"]))) is not None:
|
|
408
|
+
raise Stop(f"GitHub's key {wrong}")
|
|
409
|
+
rsa_numbers(app["pem"])
|
|
410
|
+
return app
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
def ask_hidden(prompt: str) -> str:
|
|
414
|
+
try:
|
|
415
|
+
return getpass.getpass(prompt).strip()
|
|
416
|
+
except EOFError:
|
|
417
|
+
return ""
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
def write_all(token: str, organization: str, project: str, values: dict[str, str],
|
|
421
|
+
ids: dict[str, str | None]) -> list[str]:
|
|
422
|
+
"""Write each value. On a failure, a key that was seen once is worth another
|
|
423
|
+
token: the ones left are tried until they go or nobody offers one. Returns the ones left."""
|
|
424
|
+
left = list(values)
|
|
425
|
+
while True:
|
|
426
|
+
for name in list(left):
|
|
427
|
+
try:
|
|
428
|
+
write(token, organization, project, name, values[name], "", ids[name])
|
|
429
|
+
except WriteFailed as error:
|
|
430
|
+
print(f" FAILED {name}: {error}")
|
|
431
|
+
else:
|
|
432
|
+
print(f" wrote {name}")
|
|
433
|
+
left.remove(name)
|
|
434
|
+
if not left:
|
|
435
|
+
return []
|
|
436
|
+
print(f"{len(left)} not written: {', '.join(left)}.", flush=True)
|
|
437
|
+
token = ask_hidden("Another Bitwarden access token to try those with (Enter to stop): ")
|
|
438
|
+
if not token:
|
|
439
|
+
return left
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
def create(path: Path, replace: bool, dry_run: bool) -> int:
|
|
443
|
+
if shutil.which("bws") is None:
|
|
444
|
+
raise Stop(BWS_MISSING)
|
|
445
|
+
plan = load(path)
|
|
446
|
+
print(f"App: {plan.manifest['name']}, owned by {plan.owner} "
|
|
447
|
+
f"({'organization' if plan.org else 'user'})")
|
|
448
|
+
print(f"Bitwarden project: {plan.project}")
|
|
449
|
+
print("Secrets:")
|
|
450
|
+
for name in plan.names.values():
|
|
451
|
+
print(f" {name}")
|
|
452
|
+
if dry_run:
|
|
453
|
+
print("--dry-run: no app was made and nothing was written.")
|
|
454
|
+
return 0
|
|
455
|
+
if why := ensure():
|
|
456
|
+
raise Stop(why)
|
|
457
|
+
token = ask_hidden(f"Bitwarden access token with write access to {plan.project} "
|
|
458
|
+
"(hidden, and not kept): ")
|
|
459
|
+
if not token:
|
|
460
|
+
raise Stop("no access token, so no app was made")
|
|
461
|
+
project, organization = project_id(token, plan)
|
|
462
|
+
try:
|
|
463
|
+
existing = stored_names(token, organization, project)
|
|
464
|
+
except WriteFailed as error:
|
|
465
|
+
raise Stop(f"Bitwarden would not list the names in {plan.project}: {error}") from None
|
|
466
|
+
ids = targets(plan, existing, replace)
|
|
467
|
+
|
|
468
|
+
app = make_app(plan)
|
|
469
|
+
print(f"GitHub made the app {app['slug']}, id {app['id']}. If this run ends before its key is "
|
|
470
|
+
f"written, the key is gone: make a new one, or delete the app, at {plan.settings_url(app['slug'])}",
|
|
471
|
+
flush=True)
|
|
472
|
+
oauth = plan.oauth and bool(app.get("client_id") and app.get("client_secret"))
|
|
473
|
+
if plan.oauth and not oauth:
|
|
474
|
+
print(f"GitHub's answer has no OAuth client id and secret, so they are not written: make a client "
|
|
475
|
+
f"secret at {plan.settings_url(app['slug'])}")
|
|
476
|
+
kept = ("client_secret",) if oauth else ()
|
|
477
|
+
if unkept := [field for field in ("client_secret", "webhook_secret") if app.get(field) and field not in kept]:
|
|
478
|
+
print(f"Not kept: {' and '.join(unkept)}. Make new ones in the app's settings if it needs them.")
|
|
479
|
+
install = await_installation(plan, app)
|
|
480
|
+
report_reach(plan, app, install)
|
|
481
|
+
|
|
482
|
+
names = plan.names
|
|
483
|
+
values = {names["app-id"]: str(app["id"]), names["app-key"]: app["pem"],
|
|
484
|
+
names["installation-id"]: str(install["id"])}
|
|
485
|
+
if oauth:
|
|
486
|
+
values |= {names["client-id"]: str(app["client_id"]), names["client-secret"]: str(app["client_secret"])}
|
|
487
|
+
print(f"To write in project {plan.project}:")
|
|
488
|
+
for name, secret in ids.items():
|
|
489
|
+
if name not in values:
|
|
490
|
+
continue
|
|
491
|
+
print(f" {'replace' if secret else 'create '} {name}")
|
|
492
|
+
try:
|
|
493
|
+
answer = input('Type "yes" to write them: ')
|
|
494
|
+
except EOFError:
|
|
495
|
+
answer = ""
|
|
496
|
+
if answer.strip() != "yes":
|
|
497
|
+
print(f"Nothing was written, and the key is gone with this run. Make a new one, or delete the "
|
|
498
|
+
f"app, at {plan.settings_url(app['slug'])}")
|
|
499
|
+
return 1
|
|
500
|
+
if left := write_all(token, organization, project, values, ids):
|
|
501
|
+
print(f"Not written: {', '.join(left)}. Any key among them is gone with this run: make a new "
|
|
502
|
+
f"one at {plan.settings_url(app['slug'])}")
|
|
503
|
+
return 1
|
|
504
|
+
print(f"Done: {len(values)} secrets in project {plan.project}.")
|
|
505
|
+
return 0
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
def run(path: Path, replace: bool, dry_run: bool) -> int:
|
|
509
|
+
try:
|
|
510
|
+
return create(path, replace, dry_run)
|
|
511
|
+
except Stop as error:
|
|
512
|
+
print(f"pubsec github-app: {error}")
|
|
513
|
+
return 1
|
|
514
|
+
except KeyboardInterrupt:
|
|
515
|
+
print("\nStopped. Nothing more was written.")
|
|
516
|
+
return 130
|