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/__init__.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"""pubsec: the commands and deploy need types that hold a secret value, a
|
|
2
|
+
write token or a private key in memory, apart from pubrules, which only reads
|
|
3
|
+
about secrets."""
|
|
4
|
+
|
|
5
|
+
import re as _re
|
|
6
|
+
from importlib import metadata as _metadata
|
|
7
|
+
from pathlib import Path as _Path
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _version() -> str:
|
|
11
|
+
"""pyproject.toml's version, the one place pubrel sets it: read from the
|
|
12
|
+
file in a source checkout, else from the installed package's metadata."""
|
|
13
|
+
source = _Path(__file__).resolve().parents[2] / "pyproject.toml"
|
|
14
|
+
if source.is_file():
|
|
15
|
+
text = source.read_text(encoding="utf-8")
|
|
16
|
+
if _re.search(r'^name = "pubsec"$', text, _re.M):
|
|
17
|
+
return _re.search(r'^version = "([^"]+)"$', text, _re.M)[1]
|
|
18
|
+
return _metadata.version("pubsec")
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
__version__ = _version()
|
pubsec/app_secrets.py
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
"""`pubsec app-secrets`: send what this repository's pubrules.json names to it.
|
|
2
|
+
|
|
3
|
+
A repository names the values, so pubsec names nothing of anyone's. `github_app`
|
|
4
|
+
is one GitHub App's three values, or a list of apps; `secrets` is any others:
|
|
5
|
+
|
|
6
|
+
"github_app": {
|
|
7
|
+
"id": {"from": "some-app-id", "to": "SOME_APP_ID"},
|
|
8
|
+
"installation_id": {"from": "some-app-installation-id", "to": "SOME_APP_INSTALLATION_ID"},
|
|
9
|
+
"key": {"from": "some-app-key", "to": "SOME_APP_KEY"}
|
|
10
|
+
},
|
|
11
|
+
"secrets": [
|
|
12
|
+
{"from": "some-token", "to": "SOME_TOKEN", "kind": "secret"},
|
|
13
|
+
{"from": "some-user", "to": "SOME_USER", "kind": "variable"},
|
|
14
|
+
{"from": "some-deploy-key", "to": "SOME_DEPLOY_KEY", "kind": "secret", "environment": "some-env"}
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
`from` is the secret's name in the Bitwarden project ./with-secrets reads, and
|
|
18
|
+
`to` its name on GitHub. An app's id and installation id go as variables and its
|
|
19
|
+
key as a secret; `kind` says for the others, and `environment`, if given, names the
|
|
20
|
+
GitHub environment one goes in, which must exist. They all go to the repository
|
|
21
|
+
`origin` points at and no other. Run it through with-secrets, which puts the
|
|
22
|
+
project's secrets in its environment:
|
|
23
|
+
|
|
24
|
+
./with-secrets ./pubsec app-secrets --check # checks only, sends nothing
|
|
25
|
+
./with-secrets ./pubsec app-secrets
|
|
26
|
+
|
|
27
|
+
It writes nothing until every one is there and looks right, so a mistake never
|
|
28
|
+
leaves a repository half updated. It prints names and lengths, never a value, and
|
|
29
|
+
a value reaches gh on its standard input, never its command line.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
from __future__ import annotations
|
|
33
|
+
|
|
34
|
+
import json
|
|
35
|
+
import os
|
|
36
|
+
import re
|
|
37
|
+
import subprocess
|
|
38
|
+
import sys
|
|
39
|
+
from typing import Any
|
|
40
|
+
|
|
41
|
+
BLOCK = "github_app"
|
|
42
|
+
OTHERS = "secrets"
|
|
43
|
+
KINDS = {"secret", "variable"}
|
|
44
|
+
# Each of the three: how it goes to GitHub, and what its value looks like.
|
|
45
|
+
SLOTS = {"id": "variable", "installation_id": "variable", "key": "secret"}
|
|
46
|
+
FROM = re.compile(r"[A-Za-z0-9][A-Za-z0-9_.-]*")
|
|
47
|
+
TO = re.compile(r"[A-Z_][A-Z0-9_]*")
|
|
48
|
+
ENVIRONMENT = re.compile(r"[A-Za-z0-9][A-Za-z0-9_.-]*")
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def app_problems(block: Any) -> list[str]:
|
|
52
|
+
"""What is wrong with one app's three values: one phrase each."""
|
|
53
|
+
if not isinstance(block, dict):
|
|
54
|
+
return ["it must be an object, with id, installation_id and key"]
|
|
55
|
+
out = [f"{key} is not a setting; it takes id, installation_id and key"
|
|
56
|
+
for key in sorted(block.keys() - SLOTS.keys())]
|
|
57
|
+
for slot in SLOTS:
|
|
58
|
+
given = block.get(slot)
|
|
59
|
+
if not isinstance(given, dict) or set(given) != {"from", "to"}:
|
|
60
|
+
out.append(f"{slot} needs \"from\", its Bitwarden name, and \"to\", its GitHub name")
|
|
61
|
+
continue
|
|
62
|
+
if not isinstance(given["from"], str) or not FROM.fullmatch(given["from"]):
|
|
63
|
+
out.append(f"{slot}'s \"from\" is not a secret's name")
|
|
64
|
+
name = given["to"]
|
|
65
|
+
if not isinstance(name, str) or not TO.fullmatch(name) or name.startswith("GITHUB_"):
|
|
66
|
+
out.append(f"{slot}'s \"to\" must be upper case letters, digits and _, "
|
|
67
|
+
f"not starting GITHUB_")
|
|
68
|
+
return out
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def problems(block: Any) -> list[str]:
|
|
72
|
+
"""What is wrong with a github_app block, one app or a list of them."""
|
|
73
|
+
if isinstance(block, list) and block:
|
|
74
|
+
return [problem for app in block for problem in app_problems(app)]
|
|
75
|
+
if isinstance(block, list):
|
|
76
|
+
return ["it lists no apps"]
|
|
77
|
+
return app_problems(block)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def other_problems(entries: Any) -> list[str]:
|
|
81
|
+
"""What is wrong with a secrets list: one phrase each, none when sound."""
|
|
82
|
+
if not isinstance(entries, list):
|
|
83
|
+
return ["it must be a list of from, to and kind"]
|
|
84
|
+
out = []
|
|
85
|
+
for entry in entries:
|
|
86
|
+
if not isinstance(entry, dict) or set(entry) - {"environment"} != {"from", "to", "kind"}:
|
|
87
|
+
out.append(f"{json.dumps(entry)} needs \"from\", \"to\" and \"kind\", and may have \"environment\"")
|
|
88
|
+
continue
|
|
89
|
+
label = entry["to"] if isinstance(entry["to"], str) else json.dumps(entry["to"])
|
|
90
|
+
if not isinstance(entry["from"], str) or not FROM.fullmatch(entry["from"]):
|
|
91
|
+
out.append(f"{label}'s \"from\" is not a secret's name")
|
|
92
|
+
if not isinstance(entry["to"], str) or not TO.fullmatch(entry["to"]) or entry["to"].startswith("GITHUB_"):
|
|
93
|
+
out.append(f"{label}'s \"to\" must be upper case letters, digits and _, not starting GITHUB_")
|
|
94
|
+
if not isinstance(entry["kind"], str) or entry["kind"] not in KINDS:
|
|
95
|
+
out.append(f"{label}'s \"kind\" must be secret or variable")
|
|
96
|
+
if "environment" in entry and not (isinstance(entry["environment"], str) and ENVIRONMENT.fullmatch(entry["environment"])):
|
|
97
|
+
out.append(f"{label}'s \"environment\" is not an environment's name")
|
|
98
|
+
return out
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def plan(config: dict) -> list[dict]:
|
|
102
|
+
"""Everything the config names: from, to, kind, and what its value must look like."""
|
|
103
|
+
block = config.get(BLOCK, [])
|
|
104
|
+
apps = block if isinstance(block, list) else [block]
|
|
105
|
+
out = [{"from": app[slot]["from"], "to": app[slot]["to"], "kind": kind, "check": slot}
|
|
106
|
+
for app in apps for slot, kind in SLOTS.items()]
|
|
107
|
+
return out + [{**entry, "check": None} for entry in config.get(OTHERS, [])]
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def config_problems(config: dict) -> list[str]:
|
|
111
|
+
"""What is wrong with both blocks, including one name used twice."""
|
|
112
|
+
out = []
|
|
113
|
+
if BLOCK in config:
|
|
114
|
+
out += [f"{BLOCK}: {p}" for p in problems(config[BLOCK])]
|
|
115
|
+
if OTHERS in config:
|
|
116
|
+
out += [f"{OTHERS}: {p}" for p in other_problems(config[OTHERS])]
|
|
117
|
+
if out:
|
|
118
|
+
return out
|
|
119
|
+
names = [(item["kind"], item["to"], item.get("environment", "")) for item in plan(config)]
|
|
120
|
+
return [f"two of them have the same \"to\", {to}" for kind, to, _ in sorted(set(names)) if names.count((kind, to, _)) > 1]
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def wrong_value(slot: str | None, value: str) -> str | None:
|
|
124
|
+
"""How a value isn't what it should be, without saying what it is."""
|
|
125
|
+
if not value:
|
|
126
|
+
return "is empty"
|
|
127
|
+
if slot in ("id", "installation_id") and not (value.isascii() and value.isdigit()):
|
|
128
|
+
return "is not an id: only digits are expected"
|
|
129
|
+
if slot == "key" and not (re.search(r"-----BEGIN [A-Z ]*PRIVATE KEY-----", value)
|
|
130
|
+
and re.search(r"-----END [A-Z ]*PRIVATE KEY-----", value)):
|
|
131
|
+
return "is not a private key: it needs its BEGIN and END lines"
|
|
132
|
+
return None
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def env_name(name: str) -> str:
|
|
136
|
+
"""The name with-secrets exports a secret under."""
|
|
137
|
+
return name.upper().replace("-", "_")
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def send(kind: str, name: str, repo: str, value: str, environment: str | None = None) -> str | None:
|
|
141
|
+
"""Set one secret or variable in `repo` through gh, the value on its standard
|
|
142
|
+
input; what gh said if it refused, else None."""
|
|
143
|
+
done = subprocess.run(["gh", kind, "set", name, "-R", repo,
|
|
144
|
+
*(["--env", environment] if environment else [])],
|
|
145
|
+
input=value, text=True, capture_output=True, check=False)
|
|
146
|
+
return done.stderr.strip() if done.returncode != 0 else None
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def origin() -> str:
|
|
150
|
+
"""OWNER/NAME of the GitHub repository `origin` points at."""
|
|
151
|
+
proc = subprocess.run(["git", "remote", "get-url", "origin"], capture_output=True, text=True, check=False)
|
|
152
|
+
m = re.search(r"github\.com[:/]([A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?)(?:\.git)?/?$", proc.stdout.strip())
|
|
153
|
+
if not m:
|
|
154
|
+
sys.exit("pubsec app-secrets: origin is not a GitHub repository")
|
|
155
|
+
return m.group(1)
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def run(config: dict, check: bool) -> int:
|
|
159
|
+
if BLOCK not in config and OTHERS not in config:
|
|
160
|
+
print(f"pubsec app-secrets: pubrules.json has no {BLOCK} block or {OTHERS} list")
|
|
161
|
+
return 1
|
|
162
|
+
if bad := config_problems(config):
|
|
163
|
+
print(f"pubsec app-secrets: pubrules.json: {'; '.join(bad)}")
|
|
164
|
+
return 1
|
|
165
|
+
items = plan(config)
|
|
166
|
+
if not items:
|
|
167
|
+
print(f"pubsec app-secrets: pubrules.json's {OTHERS} list is empty")
|
|
168
|
+
return 1
|
|
169
|
+
failed = False
|
|
170
|
+
values = {}
|
|
171
|
+
print("Names and values")
|
|
172
|
+
for item in items:
|
|
173
|
+
name = item["from"]
|
|
174
|
+
value = os.environ.get(env_name(name))
|
|
175
|
+
if value is None:
|
|
176
|
+
print(f" MISSING {name}")
|
|
177
|
+
failed = True
|
|
178
|
+
elif (wrong := wrong_value(item["check"], value)) is not None:
|
|
179
|
+
print(f" BAD {name} {wrong} ({len(value)} characters)")
|
|
180
|
+
failed = True
|
|
181
|
+
else:
|
|
182
|
+
print(f" ok {name} ({len(value)} characters)")
|
|
183
|
+
values[name] = value
|
|
184
|
+
if failed:
|
|
185
|
+
print("Nothing was written. Fix those in Bitwarden, or the names in pubrules.json, and run "
|
|
186
|
+
"this through ./with-secrets, which reads them.")
|
|
187
|
+
return 1
|
|
188
|
+
if check:
|
|
189
|
+
print(f"All {len(items)} look right. --check: nothing was written.")
|
|
190
|
+
return 0
|
|
191
|
+
repo = origin()
|
|
192
|
+
for item in items:
|
|
193
|
+
environment = item.get("environment")
|
|
194
|
+
where = f"{repo}, environment {environment}" if environment else repo
|
|
195
|
+
if (refused := send(item["kind"], item["to"], repo, values[item["from"]], environment)) is not None:
|
|
196
|
+
print(f" FAILED {item['kind']} {item['to']} in {where}: {refused}")
|
|
197
|
+
failed = True
|
|
198
|
+
else:
|
|
199
|
+
print(f" sent {item['kind']} {item['to']} in {where}")
|
|
200
|
+
if failed:
|
|
201
|
+
print("Fix what gh said and run this again: it only overwrites.")
|
|
202
|
+
return 1
|
|
203
|
+
print(f"Done: {len(items)} values in {repo}.")
|
|
204
|
+
return 0
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Bitwarden's Python SDK and what it needs, by hash, on every platform, for the
|
|
2
|
+
# commands that write secrets (bitwarden.py). It is not a dependency of pubrules: only
|
|
3
|
+
# those commands fetch it. Regenerate with:
|
|
4
|
+
# uv pip compile --universal --generate-hashes --python-version 3.11 --no-header --no-annotate
|
|
5
|
+
|
|
6
|
+
bitwarden-sdk==2.1.0 \
|
|
7
|
+
--hash=sha256:2c5de50af003b7b2d16a9a690d3111480d1b1962eb25f39e416812d6d9d4e4d6 \
|
|
8
|
+
--hash=sha256:659b7e3faaab067f38556ffaaa7edf217e581047234f9a748770fa2926d2664c \
|
|
9
|
+
--hash=sha256:8374ef97e481c17a0b65e1b8d700df492cb384ff5a4888bd2c697c37457fc332 \
|
|
10
|
+
--hash=sha256:b6f6b2624e340307891edc20a2860ec0b7c2140683eec2ce2e20d1076ffe9268 \
|
|
11
|
+
--hash=sha256:cdf85d309aecf21c563c9245322d49a344135761d03cffa43b8703f509bc0df7
|
|
12
|
+
python-dateutil==4.0.0 \
|
|
13
|
+
--hash=sha256:a5e246f79eb80475e691bf93862cead98decd8a498cf53ba0516fcdc0f50dc1c \
|
|
14
|
+
--hash=sha256:d7295ff14fdc0f92f662100bfb6c551d160ca090f2939f1ec5b9794265c03f06
|
|
15
|
+
tzdata==2026.6 \
|
|
16
|
+
--hash=sha256:0a10eec6e13ef16b627fabed9a5f2ba61a62b1c67f327305f4e7e412a70b4aec \
|
|
17
|
+
--hash=sha256:85d2cd9c09f81f0bb29db27759fc83af4f1e620efb814f8c5bf8b4cc4feece6d
|
pubsec/bitwarden.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"""Writing a secret to Bitwarden Secrets Manager with Bitwarden's own SDK, and
|
|
2
|
+
finding the secret to write.
|
|
3
|
+
|
|
4
|
+
The bws CLI takes a value only on its command line, where every local user can
|
|
5
|
+
read it for as long as the write takes. The SDK takes it in memory, so a command
|
|
6
|
+
that writes something secret comes here. The secret to write is found by name in
|
|
7
|
+
the SDK's identifiers, which hold an id, a name and the projects and no value:
|
|
8
|
+
`bws secret list` gives every value of the project, which a lookup does not need.
|
|
9
|
+
|
|
10
|
+
The SDK is not a dependency of pubsec, which has none, and it has compiled wheels
|
|
11
|
+
for only some platforms. `ensure` installs it
|
|
12
|
+
for this run alone, from bitwarden-sdk.lock, by hash, as what it is given is a write
|
|
13
|
+
token and a private key. uv checks the hashes only with --require-hashes on `uv pip
|
|
14
|
+
install`: `uvx --with-requirements` installs a wrong one without a word.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import atexit
|
|
20
|
+
import shutil
|
|
21
|
+
import subprocess
|
|
22
|
+
import sys
|
|
23
|
+
import tempfile
|
|
24
|
+
from importlib import resources
|
|
25
|
+
from uuid import UUID
|
|
26
|
+
|
|
27
|
+
LOCK = "bitwarden-sdk.lock"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class WriteFailed(Exception):
|
|
31
|
+
"""A lookup or a write Bitwarden refused, with hidden values taken out of its message."""
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def scrub(text: str, hidden: tuple[str, ...]) -> str:
|
|
35
|
+
"""`text` with every hidden value, and each line of one, taken out."""
|
|
36
|
+
for value in hidden:
|
|
37
|
+
for part in [value, *value.splitlines()]:
|
|
38
|
+
if part:
|
|
39
|
+
text = text.replace(part, "[hidden]")
|
|
40
|
+
return text
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def ensure() -> str | None:
|
|
44
|
+
"""Return nothing once the SDK can be imported, or say why it can't."""
|
|
45
|
+
try:
|
|
46
|
+
import bitwarden_sdk # noqa: F401, PLC0415
|
|
47
|
+
except ImportError:
|
|
48
|
+
pass
|
|
49
|
+
else:
|
|
50
|
+
return None
|
|
51
|
+
if shutil.which("uv") is None:
|
|
52
|
+
return "this command fetches Bitwarden's SDK with uv, which is not installed: curl -LsSf https://astral.sh/uv/install.sh | sh"
|
|
53
|
+
target = tempfile.mkdtemp(prefix="pubsec-bitwarden-")
|
|
54
|
+
atexit.register(shutil.rmtree, target, ignore_errors=True)
|
|
55
|
+
with resources.as_file(resources.files("pubsec") / LOCK) as lock:
|
|
56
|
+
done = subprocess.run(["uv", "pip", "install", "--quiet", "--require-hashes", "--python", sys.executable,
|
|
57
|
+
"--target", target, "-r", str(lock)], capture_output=True, text=True, check=False)
|
|
58
|
+
if done.returncode != 0:
|
|
59
|
+
return f"Bitwarden's SDK could not be installed: {done.stderr.strip()}"
|
|
60
|
+
sys.path.insert(0, target)
|
|
61
|
+
try:
|
|
62
|
+
import bitwarden_sdk # noqa: F401, PLC0415
|
|
63
|
+
except ImportError as error:
|
|
64
|
+
return f"Bitwarden's SDK was installed but can't be imported: {error}"
|
|
65
|
+
return None
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
# The SDK's client, logged in, by token: one login a process for each.
|
|
69
|
+
_clients: dict[str, object] = {}
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _client(token: str): # noqa: ANN202 - the SDK's client, logged in
|
|
73
|
+
if token in _clients:
|
|
74
|
+
return _clients[token]
|
|
75
|
+
from bitwarden_sdk import BitwardenClient # noqa: PLC0415
|
|
76
|
+
try:
|
|
77
|
+
client = BitwardenClient()
|
|
78
|
+
answer = client.auth().login_access_token(token)
|
|
79
|
+
except Exception as error: # noqa: BLE001 # the SDK raises bare Exceptions, and they may quote what it was given
|
|
80
|
+
raise WriteFailed(scrub(str(error), (token,)) or type(error).__name__) from None
|
|
81
|
+
if not answer.success:
|
|
82
|
+
raise WriteFailed(scrub(str(answer.error_message), (token,)))
|
|
83
|
+
_clients[token] = client
|
|
84
|
+
return client
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def names(token: str, organization: str, project: str) -> dict[str, list[str]]:
|
|
88
|
+
"""Each name in the project and its secrets' ids. Only identifiers are
|
|
89
|
+
asked for, so no secret's value reaches this process."""
|
|
90
|
+
client = _client(token)
|
|
91
|
+
try:
|
|
92
|
+
answer = client.secrets().list(organization)
|
|
93
|
+
except Exception as error: # noqa: BLE001 # as in _client
|
|
94
|
+
raise WriteFailed(scrub(str(error), (token,)) or type(error).__name__) from None
|
|
95
|
+
if not answer.success or answer.data is None:
|
|
96
|
+
raise WriteFailed(scrub(str(answer.error_message), (token,)))
|
|
97
|
+
out: dict[str, list[str]] = {}
|
|
98
|
+
for secret in answer.data.data:
|
|
99
|
+
if project.lower() in {str(p).lower() for p in secret.project_ids or []}:
|
|
100
|
+
out.setdefault(secret.key, []).append(str(secret.id))
|
|
101
|
+
return out
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def values(token: str, ids: list[str]) -> dict[str, str]:
|
|
105
|
+
"""The values of the secrets `ids`, by id: those, and no other."""
|
|
106
|
+
if not ids:
|
|
107
|
+
return {}
|
|
108
|
+
client = _client(token)
|
|
109
|
+
try:
|
|
110
|
+
answer = client.secrets().get_by_ids(ids)
|
|
111
|
+
except Exception as error: # noqa: BLE001 # as in _client
|
|
112
|
+
raise WriteFailed(scrub(str(error), (token,)) or type(error).__name__) from None
|
|
113
|
+
if not answer.success or answer.data is None:
|
|
114
|
+
raise WriteFailed(scrub(str(answer.error_message), (token,)))
|
|
115
|
+
return {str(secret.id): secret.value for secret in answer.data.data}
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def write(token: str, organization: str, project: str, name: str, value: str, note: str,
|
|
119
|
+
secret: str | None = None) -> None:
|
|
120
|
+
"""Create the secret `name`, or update `secret`, in the project. The token and the value
|
|
121
|
+
stay in this process."""
|
|
122
|
+
hidden = (token, value)
|
|
123
|
+
client = _client(token)
|
|
124
|
+
try:
|
|
125
|
+
if secret is None:
|
|
126
|
+
answer = client.secrets().create(UUID(organization), name, value, note, [UUID(project)])
|
|
127
|
+
else:
|
|
128
|
+
answer = client.secrets().update(organization, secret, name, value, note, [UUID(project)])
|
|
129
|
+
except Exception as error: # noqa: BLE001 # as in _client
|
|
130
|
+
raise WriteFailed(scrub(str(error), hidden) or type(error).__name__) from None
|
|
131
|
+
if not answer.success:
|
|
132
|
+
raise WriteFailed(scrub(str(answer.error_message), hidden))
|
pubsec/cli.py
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"""`pubsec`: the commands that hold a secret value, a write token or a private key.
|
|
2
|
+
|
|
3
|
+
pubsec run [--cwd DIR] -- COMMAND [ARGS...]
|
|
4
|
+
pubsec app-secrets [--check]
|
|
5
|
+
pubsec github-app create [--replace] [--dry-run] MANIFEST
|
|
6
|
+
pubsec service-account-key create --account EMAIL --secret NAME [--bitwarden-project NAME] [--replace] [--dry-run]
|
|
7
|
+
pubsec signing-key backup [--under DIR] [--dry-run]
|
|
8
|
+
pubsec signing-key restore [NAME ...] [--under DIR] [--dry-run]
|
|
9
|
+
pubsec types
|
|
10
|
+
pubsec --version
|
|
11
|
+
|
|
12
|
+
A repository runs its pinned pubsec as ./pubsec, which pubrules's `sync` writes
|
|
13
|
+
beside ./pubrules, and its ./with-secrets runs `./pubsec run`. The deploy need
|
|
14
|
+
types (`pubsec types`) are installed for pubdeploy's walk, which loads them by
|
|
15
|
+
their `sustenaut.needs` entry points.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from __future__ import annotations
|
|
19
|
+
|
|
20
|
+
import argparse
|
|
21
|
+
import json
|
|
22
|
+
import os
|
|
23
|
+
import sys
|
|
24
|
+
import tempfile
|
|
25
|
+
from pathlib import Path
|
|
26
|
+
|
|
27
|
+
from . import __version__
|
|
28
|
+
|
|
29
|
+
CONFIG = "pubrules.json"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def read_config(root: Path) -> dict:
|
|
33
|
+
path = root / CONFIG
|
|
34
|
+
if not path.is_file():
|
|
35
|
+
sys.exit(f"pubsec: no {CONFIG} here")
|
|
36
|
+
try:
|
|
37
|
+
config = json.loads(path.read_text(encoding="utf-8"))
|
|
38
|
+
except ValueError:
|
|
39
|
+
sys.exit(f"pubsec: {CONFIG} is not JSON")
|
|
40
|
+
if not isinstance(config, dict):
|
|
41
|
+
sys.exit(f"pubsec: {CONFIG} is not a JSON object")
|
|
42
|
+
return config
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def confined(path: Path, what: str) -> Path:
|
|
46
|
+
"""`path`, resolved, when it lies in the working directory or a temporary
|
|
47
|
+
directory (the system's, or the Actions runner's RUNNER_TEMP), so a
|
|
48
|
+
mistyped or malicious argument cannot reach a file elsewhere; otherwise
|
|
49
|
+
pubsec stops, naming `what`."""
|
|
50
|
+
bases = [Path.cwd(), Path(tempfile.gettempdir())]
|
|
51
|
+
if os.environ.get("RUNNER_TEMP"):
|
|
52
|
+
bases.append(Path(os.environ["RUNNER_TEMP"]))
|
|
53
|
+
resolved = path.resolve()
|
|
54
|
+
if any(resolved.is_relative_to(base.resolve()) for base in bases):
|
|
55
|
+
return resolved
|
|
56
|
+
sys.exit(f"pubsec: {what} ({path}) must be in the working directory or a temporary directory")
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def types() -> int:
|
|
60
|
+
"""The need types pubsec offers, as `pubdeploy types` lists them."""
|
|
61
|
+
from .needs.adopt import bitwarden_adopt # noqa: PLC0415
|
|
62
|
+
from .needs.bitwarden import bitwarden_generated, bitwarden_secret # noqa: PLC0415
|
|
63
|
+
from .needs.gcp import service_account_key, workload_identity # noqa: PLC0415
|
|
64
|
+
from .needs.github import deploy_settings, github_secret, github_variable # noqa: PLC0415
|
|
65
|
+
from .needs.k8s import secret # noqa: PLC0415
|
|
66
|
+
offered = (bitwarden_secret, bitwarden_generated, bitwarden_adopt, service_account_key, workload_identity, github_secret,
|
|
67
|
+
github_variable, deploy_settings, secret)
|
|
68
|
+
rows = [("type", "grade", "budget", "offline", "fulfil", "keys")]
|
|
69
|
+
for kind in sorted(offered, key=lambda k: k.type):
|
|
70
|
+
required = set(kind.schema.get("required", []))
|
|
71
|
+
keys = [f"{k}{'' if k in required else '?'}" for k in kind.schema.get("properties", {})]
|
|
72
|
+
rows.append((kind.type, kind.grade, f"{kind.budget:g} s", "yes" if kind.offline else "no",
|
|
73
|
+
f"{kind.fulfil_budget:g} s", ", ".join(keys) or "-"))
|
|
74
|
+
widths = [max(len(r[i]) for r in rows) for i in range(len(rows[0]) - 1)]
|
|
75
|
+
for row in rows:
|
|
76
|
+
print(" ".join(cell.ljust(w) for cell, w in zip(row, widths)) + " " + row[-1])
|
|
77
|
+
print(f"{len(offered)} types, pubsec {__version__}'s, for pubdeploy")
|
|
78
|
+
return 0
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def main(argv: list[str] | None = None) -> int:
|
|
82
|
+
given = sys.argv[1:] if argv is None else argv
|
|
83
|
+
if given[:1] == ["run"]:
|
|
84
|
+
# Its own parser: everything after -- is the command, with its own options.
|
|
85
|
+
from .run import main as run # noqa: PLC0415
|
|
86
|
+
return run(given)
|
|
87
|
+
parser = argparse.ArgumentParser(prog="pubsec", description=__doc__.split("\n")[0])
|
|
88
|
+
parser.add_argument("--version", action="version", version=f"pubsec {__version__}")
|
|
89
|
+
commands = parser.add_subparsers(dest="command", required=True)
|
|
90
|
+
commands.add_parser("run", help="run one command with with-secrets.json's Bitwarden secrets in its "
|
|
91
|
+
"environment: run [--cwd DIR] -- COMMAND")
|
|
92
|
+
p = commands.add_parser("app-secrets", help="send the secrets this repository's pubrules.json "
|
|
93
|
+
"names to it, through ./with-secrets")
|
|
94
|
+
p.add_argument("--check", action="store_true", help="check the values, and send nothing")
|
|
95
|
+
p = commands.add_parser("github-app", help="make a GitHub App from a manifest, and put its "
|
|
96
|
+
"values in Bitwarden")
|
|
97
|
+
github_app = p.add_subparsers(dest="github_app_command", required=True)
|
|
98
|
+
p = github_app.add_parser("create", help="go through GitHub's app-manifest flow, in a browser, "
|
|
99
|
+
"then write the app's id, key and installation id")
|
|
100
|
+
p.add_argument("manifest", type=Path, metavar="MANIFEST",
|
|
101
|
+
help="the app's manifest, with a pubrules section naming its owner, "
|
|
102
|
+
"Bitwarden project and secret prefix")
|
|
103
|
+
p.add_argument("--replace", action="store_true",
|
|
104
|
+
help="overwrite secrets that already have those names, instead of refusing")
|
|
105
|
+
p.add_argument("--dry-run", action="store_true", help="show the plan, and do nothing")
|
|
106
|
+
p = commands.add_parser("service-account-key", help="make a Google Cloud service account key, and put it "
|
|
107
|
+
"in Bitwarden, with no file or command line holding it")
|
|
108
|
+
service_account_key = p.add_subparsers(dest="service_account_key_command", required=True)
|
|
109
|
+
p = service_account_key.add_parser("create", help="make a new key beside the account's others, and write it "
|
|
110
|
+
"to a secret")
|
|
111
|
+
p.add_argument("--account", required=True, metavar="EMAIL", help="the service account")
|
|
112
|
+
p.add_argument("--secret", required=True, metavar="NAME", help="the secret to write")
|
|
113
|
+
p.add_argument("--bitwarden-project", metavar="NAME",
|
|
114
|
+
help="the Bitwarden project; by default, the one with-secrets.json names")
|
|
115
|
+
p.add_argument("--replace", action="store_true",
|
|
116
|
+
help="overwrite a secret that already has that name, instead of refusing")
|
|
117
|
+
p.add_argument("--dry-run", action="store_true", help="show the plan, and do nothing")
|
|
118
|
+
p = commands.add_parser("signing-key", help="back up the signing keys pubrules.json declares to Bitwarden")
|
|
119
|
+
signing_key = p.add_subparsers(dest="signing_key_command", required=True)
|
|
120
|
+
p = signing_key.add_parser("backup", help="write each declared key on this machine that its Bitwarden "
|
|
121
|
+
"project lacks, never overwriting one")
|
|
122
|
+
p.add_argument("--under", type=Path, metavar="DIR",
|
|
123
|
+
help="every repository directly under DIR, not this one")
|
|
124
|
+
p.add_argument("--dry-run", action="store_true", help="say what would be written, and ask for nothing")
|
|
125
|
+
p = signing_key.add_parser("restore", help="write each declared key this machine lacks from its Bitwarden "
|
|
126
|
+
"project, never over one already here")
|
|
127
|
+
p.add_argument("names", nargs="*", metavar="NAME", help="only these keys, by their Bitwarden names")
|
|
128
|
+
p.add_argument("--under", type=Path, metavar="DIR",
|
|
129
|
+
help="every repository directly under DIR, not this one")
|
|
130
|
+
p.add_argument("--dry-run", action="store_true", help="say what would be restored, and ask for nothing")
|
|
131
|
+
commands.add_parser("types", help="the deploy need types pubsec offers pubdeploy's walk")
|
|
132
|
+
args = parser.parse_args(given)
|
|
133
|
+
root = Path.cwd()
|
|
134
|
+
if args.command == "app-secrets":
|
|
135
|
+
from .app_secrets import run as app_secrets # noqa: PLC0415
|
|
136
|
+
return app_secrets(read_config(root), args.check)
|
|
137
|
+
if args.command == "github-app":
|
|
138
|
+
from .github_app import run as github_app # noqa: PLC0415
|
|
139
|
+
return github_app(confined(args.manifest, "the manifest"), args.replace, args.dry_run)
|
|
140
|
+
if args.command == "service-account-key":
|
|
141
|
+
from .service_account_key import run as service_account_key # noqa: PLC0415
|
|
142
|
+
return service_account_key(root, args.account, args.secret, args.bitwarden_project, args.replace,
|
|
143
|
+
args.dry_run)
|
|
144
|
+
if args.command == "signing-key":
|
|
145
|
+
under = args.under.expanduser() if args.under else None
|
|
146
|
+
if args.signing_key_command == "restore":
|
|
147
|
+
from .signing_keys import restore # noqa: PLC0415
|
|
148
|
+
return restore(under, args.names, args.dry_run)
|
|
149
|
+
from .signing_keys import run as signing_key_backup # noqa: PLC0415
|
|
150
|
+
return signing_key_backup(under, args.dry_run)
|
|
151
|
+
return types()
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
if __name__ == "__main__":
|
|
155
|
+
sys.exit(main())
|