mozbridge-cli 0.1.0__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.
- mozbridge_cli/__init__.py +3 -0
- mozbridge_cli/__main__.py +4 -0
- mozbridge_cli/api.py +274 -0
- mozbridge_cli/auth.py +179 -0
- mozbridge_cli/build.py +108 -0
- mozbridge_cli/compose.py +181 -0
- mozbridge_cli/config.py +76 -0
- mozbridge_cli/link.py +90 -0
- mozbridge_cli/local_build.py +140 -0
- mozbridge_cli/main.py +821 -0
- mozbridge_cli/runtime_secrets.py +140 -0
- mozbridge_cli/session.py +156 -0
- mozbridge_cli-0.1.0.dist-info/METADATA +351 -0
- mozbridge_cli-0.1.0.dist-info/RECORD +16 -0
- mozbridge_cli-0.1.0.dist-info/WHEEL +4 -0
- mozbridge_cli-0.1.0.dist-info/entry_points.txt +2 -0
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"""Fetch + parse runtime build secrets for `mozbridge publish --local`.
|
|
2
|
+
|
|
3
|
+
Talks to `GET /api/v1/projects/cicd/runtime-secrets`
|
|
4
|
+
(backend/app/services/cicd_runtime_secrets.py, routed at
|
|
5
|
+
backend/app/features/projects/router.py:get_cicd_runtime_secrets) — a
|
|
6
|
+
completely DIFFERENT credential than the CLI's login session:
|
|
7
|
+
|
|
8
|
+
MOZBRIDGE_BUILD_TOKEN — a project-scoped ServiceToken with the 'build'
|
|
9
|
+
scope, authenticated by `_get_project_and_scopes_from_pipeline_token`.
|
|
10
|
+
A 'build' scope caller gets registry/build credentials and NEVER a
|
|
11
|
+
deploy host key (see that function's own docstring) — this is the
|
|
12
|
+
load-bearing security property that makes it safe to install this CLI
|
|
13
|
+
on a developer's laptop. Minting this token is a human action taken out
|
|
14
|
+
of band via the Mozbridge dashboard/API (existing create_service_token
|
|
15
|
+
mechanism) — this module only ever READS it from the environment.
|
|
16
|
+
|
|
17
|
+
This module must never be handed the login session's access_token, and
|
|
18
|
+
api.py's trigger_build/trigger_prebuilt_build (which DO use the login
|
|
19
|
+
session) must never be handed MOZBRIDGE_BUILD_TOKEN. See main.py's
|
|
20
|
+
`_publish_local` for how the two credentials are kept apart end to end.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
from __future__ import annotations
|
|
24
|
+
|
|
25
|
+
import re
|
|
26
|
+
|
|
27
|
+
import httpx
|
|
28
|
+
|
|
29
|
+
from . import config
|
|
30
|
+
|
|
31
|
+
BUILD_TOKEN_ENV_VAR = "MOZBRIDGE_BUILD_TOKEN"
|
|
32
|
+
|
|
33
|
+
RUNTIME_SECRETS_PATH = f"{config.PROJECTS_PATH}/cicd/runtime-secrets"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class RuntimeSecretsError(Exception):
|
|
37
|
+
"""Raised when the runtime-secrets endpoint can't be reached or fails."""
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
# One `export KEY=VALUE` (or bare `KEY=VALUE`) per line — exactly what
|
|
41
|
+
# cicd_runtime_secrets.get_pipeline_secrets emits: `f"export {k}={_shell_quote(v)}"`
|
|
42
|
+
# joined with newlines. The `export ` prefix is optional in this regex only
|
|
43
|
+
# so the parser stays correct even if the backend response format loses it
|
|
44
|
+
# some day; the real response always has it.
|
|
45
|
+
_EXPORT_LINE_RE = re.compile(r"^(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)=(.*)$")
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _unquote_shell_value(raw: str) -> str:
|
|
49
|
+
"""Reverse cicd_runtime_secrets._shell_quote: `"'" + value.replace("'",
|
|
50
|
+
"'\\''") + "'"` — i.e. real POSIX-sh single-quoting, where an embedded
|
|
51
|
+
single quote becomes close-quote, escaped-literal-quote, reopen-quote
|
|
52
|
+
(`'\\''`). Walks the string rather than regexing it, since that
|
|
53
|
+
three-quote escape sequence is genuine (if unusual) shell syntax and
|
|
54
|
+
deserves a real parser instead of a pattern that happens to work on one
|
|
55
|
+
fixture.
|
|
56
|
+
|
|
57
|
+
Every value this backend ever emits is fully wrapped in single quotes
|
|
58
|
+
(see `_shell_quote` — there is no bare/unquoted-word case), so a `raw`
|
|
59
|
+
that doesn't start with `'` is returned unchanged rather than guessing
|
|
60
|
+
at a quoting style this backend doesn't use.
|
|
61
|
+
"""
|
|
62
|
+
if not raw:
|
|
63
|
+
return ""
|
|
64
|
+
if raw[0] != "'":
|
|
65
|
+
return raw
|
|
66
|
+
|
|
67
|
+
parts: list[str] = []
|
|
68
|
+
i = 0
|
|
69
|
+
n = len(raw)
|
|
70
|
+
while i < n:
|
|
71
|
+
if raw[i] != "'":
|
|
72
|
+
# Anything between a closing quote and the next opening quote
|
|
73
|
+
# that ISN'T the `\'` escape (handled below) shouldn't occur in
|
|
74
|
+
# real output — skip defensively rather than raising on a
|
|
75
|
+
# response this CLI doesn't control.
|
|
76
|
+
i += 1
|
|
77
|
+
continue
|
|
78
|
+
i += 1 # step past the opening quote
|
|
79
|
+
end = raw.find("'", i)
|
|
80
|
+
if end == -1:
|
|
81
|
+
# Unterminated quote — best-effort: take the rest as literal.
|
|
82
|
+
parts.append(raw[i:])
|
|
83
|
+
break
|
|
84
|
+
parts.append(raw[i:end])
|
|
85
|
+
i = end + 1
|
|
86
|
+
if raw[i : i + 2] == "\\'":
|
|
87
|
+
parts.append("'")
|
|
88
|
+
i += 2
|
|
89
|
+
return "".join(parts)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def parse_runtime_secrets(text: str) -> dict[str, str]:
|
|
93
|
+
"""Parse the runtime-secrets endpoint's shell-sourceable response text
|
|
94
|
+
into a plain dict. Blank lines and comment lines (`#...`) are skipped;
|
|
95
|
+
any line that isn't `[export ]KEY=VALUE` is ignored rather than raising
|
|
96
|
+
— this is a real shell script, and being lenient about lines this
|
|
97
|
+
parser doesn't need is safer than a hard failure on a backend addition.
|
|
98
|
+
"""
|
|
99
|
+
result: dict[str, str] = {}
|
|
100
|
+
for line in text.splitlines():
|
|
101
|
+
stripped = line.strip()
|
|
102
|
+
if not stripped or stripped.startswith("#"):
|
|
103
|
+
continue
|
|
104
|
+
match = _EXPORT_LINE_RE.match(stripped)
|
|
105
|
+
if not match:
|
|
106
|
+
continue
|
|
107
|
+
key, raw_value = match.group(1), match.group(2)
|
|
108
|
+
result[key] = _unquote_shell_value(raw_value)
|
|
109
|
+
return result
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def fetch_runtime_secrets(client: httpx.Client, build_token: str, *, env: str = "prod") -> dict[str, str]:
|
|
113
|
+
"""GET /api/v1/projects/cicd/runtime-secrets?env=<env>, Bearer <build_token>.
|
|
114
|
+
|
|
115
|
+
No X-Organization-Id header is sent — unlike every human-session call
|
|
116
|
+
in api.py, `_get_project_and_scopes_from_pipeline_token` resolves both
|
|
117
|
+
the project and the caller's scopes from the token alone.
|
|
118
|
+
"""
|
|
119
|
+
url = f"{config.API_BASE_URL}{RUNTIME_SECRETS_PATH}"
|
|
120
|
+
try:
|
|
121
|
+
resp = client.get(url, params={"env": env}, headers={"Authorization": f"Bearer {build_token}"})
|
|
122
|
+
except httpx.HTTPError as exc:
|
|
123
|
+
raise RuntimeSecretsError(f"Could not reach Mozbridge API for runtime secrets: {exc}") from exc
|
|
124
|
+
|
|
125
|
+
if resp.status_code == 401:
|
|
126
|
+
raise RuntimeSecretsError(
|
|
127
|
+
f"{BUILD_TOKEN_ENV_VAR} was rejected (401 Unauthorized). It must be an "
|
|
128
|
+
"active, project-scoped service token with the 'build' scope. Create one "
|
|
129
|
+
"via the Mozbridge dashboard/API (Project Settings -> Service Tokens), "
|
|
130
|
+
f"then `export {BUILD_TOKEN_ENV_VAR}=<token>` and try again."
|
|
131
|
+
)
|
|
132
|
+
if not (200 <= resp.status_code < 300):
|
|
133
|
+
try:
|
|
134
|
+
detail = resp.json().get("detail")
|
|
135
|
+
except ValueError:
|
|
136
|
+
detail = None
|
|
137
|
+
detail = detail or resp.text or f"HTTP {resp.status_code}"
|
|
138
|
+
raise RuntimeSecretsError(f"Could not fetch runtime secrets: {detail}")
|
|
139
|
+
|
|
140
|
+
return parse_runtime_secrets(resp.text)
|
mozbridge_cli/session.py
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
"""Local session cache: read/write ~/.config/mozbridge/session.json.
|
|
2
|
+
|
|
3
|
+
Security posture mirrors .llm/plans/_archived/ssh-broker.md's checklist for
|
|
4
|
+
its analogous CLI ("stores user private keys with 0600 and in a directory
|
|
5
|
+
with 0700") — applied here to session tokens instead of a private key.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import json
|
|
11
|
+
import os
|
|
12
|
+
import stat
|
|
13
|
+
import time
|
|
14
|
+
from dataclasses import asdict, dataclass
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
|
|
17
|
+
from . import config
|
|
18
|
+
|
|
19
|
+
DIR_MODE = 0o700
|
|
20
|
+
FILE_MODE = 0o600
|
|
21
|
+
|
|
22
|
+
# Non-interactive/CI auth: a project-scoped ServiceToken (same primitive as
|
|
23
|
+
# MOZBRIDGE_BUILD_TOKEN in runtime_secrets.py — created out of band via the
|
|
24
|
+
# existing create_service_token mechanism, never minted by this CLI) used
|
|
25
|
+
# directly as the bearer token for EVERY command instead of the on-disk
|
|
26
|
+
# session file. This is a completely separate credential/purpose from
|
|
27
|
+
# MOZBRIDGE_BUILD_TOKEN, which authenticates only `publish --local`'s
|
|
28
|
+
# registry-secrets fetch — the two must never be read from, or written to,
|
|
29
|
+
# the same place. See main.py's `_load_and_refresh_session` for the single
|
|
30
|
+
# choke point where this takes precedence, and `login_cmd` for why
|
|
31
|
+
# `mozbridge login` refuses outright when this is set (mutually exclusive
|
|
32
|
+
# auth modes, same as gh CLI's GH_TOKEN precedence over a stored session).
|
|
33
|
+
TOKEN_ENV_VAR = "MOZBRIDGE_TOKEN"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclass
|
|
37
|
+
class Session:
|
|
38
|
+
access_token: str
|
|
39
|
+
refresh_token: str | None
|
|
40
|
+
id_token: str | None
|
|
41
|
+
expires_at: float # unix timestamp, computed from expires_in at write time
|
|
42
|
+
scope: str | None = None
|
|
43
|
+
token_type: str = "Bearer"
|
|
44
|
+
|
|
45
|
+
def is_expired(self, *, leeway_seconds: int = 30) -> bool:
|
|
46
|
+
return time.time() >= (self.expires_at - leeway_seconds)
|
|
47
|
+
|
|
48
|
+
def to_dict(self) -> dict:
|
|
49
|
+
return asdict(self)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _ensure_config_dir() -> Path:
|
|
53
|
+
d = config.config_dir()
|
|
54
|
+
d.mkdir(parents=True, exist_ok=True)
|
|
55
|
+
os.chmod(d, DIR_MODE)
|
|
56
|
+
return d
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def save_session(session: Session) -> Path:
|
|
60
|
+
"""Write the session file atomically with 0600 perms, dir with 0700."""
|
|
61
|
+
_ensure_config_dir()
|
|
62
|
+
path = config.session_path()
|
|
63
|
+
tmp_path = path.with_suffix(".tmp")
|
|
64
|
+
|
|
65
|
+
# Create with restrictive perms from the start (avoid a window where the
|
|
66
|
+
# file is briefly world/group readable), then atomically replace.
|
|
67
|
+
fd = os.open(tmp_path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, FILE_MODE)
|
|
68
|
+
try:
|
|
69
|
+
with os.fdopen(fd, "w") as f:
|
|
70
|
+
json.dump(session.to_dict(), f, indent=2)
|
|
71
|
+
f.write("\n")
|
|
72
|
+
except BaseException:
|
|
73
|
+
try:
|
|
74
|
+
tmp_path.unlink(missing_ok=True)
|
|
75
|
+
except OSError:
|
|
76
|
+
pass
|
|
77
|
+
raise
|
|
78
|
+
|
|
79
|
+
os.chmod(tmp_path, FILE_MODE)
|
|
80
|
+
os.replace(tmp_path, path)
|
|
81
|
+
os.chmod(path, FILE_MODE)
|
|
82
|
+
return path
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def load_session() -> Session | None:
|
|
86
|
+
"""Return the cached session, or None if not logged in / file is unreadable."""
|
|
87
|
+
path = config.session_path()
|
|
88
|
+
if not path.exists():
|
|
89
|
+
return None
|
|
90
|
+
try:
|
|
91
|
+
data = json.loads(path.read_text())
|
|
92
|
+
except (OSError, json.JSONDecodeError):
|
|
93
|
+
return None
|
|
94
|
+
try:
|
|
95
|
+
return Session(
|
|
96
|
+
access_token=data["access_token"],
|
|
97
|
+
refresh_token=data.get("refresh_token"),
|
|
98
|
+
id_token=data.get("id_token"),
|
|
99
|
+
expires_at=float(data["expires_at"]),
|
|
100
|
+
scope=data.get("scope"),
|
|
101
|
+
token_type=data.get("token_type", "Bearer"),
|
|
102
|
+
)
|
|
103
|
+
except (KeyError, TypeError, ValueError):
|
|
104
|
+
return None
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def delete_session() -> bool:
|
|
108
|
+
"""Remove the cached session file. Idempotent — returns whether a file was removed."""
|
|
109
|
+
path = config.session_path()
|
|
110
|
+
try:
|
|
111
|
+
path.unlink()
|
|
112
|
+
return True
|
|
113
|
+
except FileNotFoundError:
|
|
114
|
+
return False
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def session_from_token_response(token_response: dict) -> Session:
|
|
118
|
+
"""Build a Session from a Logto token-endpoint response, computing expires_at now."""
|
|
119
|
+
expires_in = float(token_response.get("expires_in", 3600))
|
|
120
|
+
return Session(
|
|
121
|
+
access_token=token_response["access_token"],
|
|
122
|
+
refresh_token=token_response.get("refresh_token"),
|
|
123
|
+
id_token=token_response.get("id_token"),
|
|
124
|
+
expires_at=time.time() + expires_in,
|
|
125
|
+
scope=token_response.get("scope"),
|
|
126
|
+
token_type=token_response.get("token_type", "Bearer"),
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def token_from_env() -> str | None:
|
|
131
|
+
"""Return the CI bearer token from MOZBRIDGE_TOKEN, or None if unset/empty.
|
|
132
|
+
|
|
133
|
+
When this returns non-None, callers must use it directly as the bearer
|
|
134
|
+
token and skip load_session()/save_session()/refresh entirely — there is
|
|
135
|
+
no on-disk session file involved in this auth path at all, so the
|
|
136
|
+
0600/0700 permission checks (check_permissions_ok) never come into play
|
|
137
|
+
either.
|
|
138
|
+
"""
|
|
139
|
+
value = os.environ.get(TOKEN_ENV_VAR)
|
|
140
|
+
return value if value else None
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def check_permissions_ok() -> bool:
|
|
144
|
+
"""True if the config dir is 0700 and the session file (if present) is 0600."""
|
|
145
|
+
d = config.config_dir()
|
|
146
|
+
if not d.exists():
|
|
147
|
+
return True
|
|
148
|
+
dir_mode = stat.S_IMODE(d.stat().st_mode)
|
|
149
|
+
if dir_mode != DIR_MODE:
|
|
150
|
+
return False
|
|
151
|
+
path = config.session_path()
|
|
152
|
+
if path.exists():
|
|
153
|
+
file_mode = stat.S_IMODE(path.stat().st_mode)
|
|
154
|
+
if file_mode != FILE_MODE:
|
|
155
|
+
return False
|
|
156
|
+
return True
|
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: mozbridge-cli
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Command-line client for the Mozbridge deployment platform: SSO login, build, publish, status, and rollback for any project hosted on Mozbridge.
|
|
5
|
+
Project-URL: Homepage, https://mozbridge.com
|
|
6
|
+
Project-URL: Documentation, https://github.com/jessin01/mozbridge/blob/main/cli/docs/commands.md
|
|
7
|
+
Project-URL: Repository, https://github.com/jessin01/mozbridge
|
|
8
|
+
Author: Mozbridge
|
|
9
|
+
License: Proprietary
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Environment :: Console
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Operating System :: MacOS
|
|
14
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Topic :: Software Development :: Build Tools
|
|
20
|
+
Requires-Python: >=3.10
|
|
21
|
+
Requires-Dist: httpx>=0.27
|
|
22
|
+
Requires-Dist: pyyaml>=6.0
|
|
23
|
+
Requires-Dist: typer>=0.12
|
|
24
|
+
Provides-Extra: dev
|
|
25
|
+
Requires-Dist: pytest>=8.0; extra == 'dev'
|
|
26
|
+
Requires-Dist: respx>=0.21; extra == 'dev'
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# mozbridge-cli
|
|
30
|
+
|
|
31
|
+
A laptop CLI for the Mozbridge deployment platform. You sign in once via
|
|
32
|
+
browser-based SSO (Logto's device-authorization flow — no password, no
|
|
33
|
+
long-lived API key typed into a dotfile), link a local directory to a
|
|
34
|
+
Mozbridge org + project, and push builds from there. It never holds a
|
|
35
|
+
production credential of its own: every request it makes rides the same
|
|
36
|
+
human Logto session `mozbridge login` cached, hitting the same REST API the
|
|
37
|
+
Mozbridge web dashboard uses.
|
|
38
|
+
|
|
39
|
+
macOS-only for v1 — there are no Windows code paths.
|
|
40
|
+
|
|
41
|
+
## Install
|
|
42
|
+
|
|
43
|
+
There is no published package yet (no PyPI entry, no `mozbridge-cli` on any
|
|
44
|
+
index) — install from this checkout:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
pip install -e cli/
|
|
48
|
+
# or, for an isolated global install:
|
|
49
|
+
pipx install ./cli
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Either one registers a `mozbridge` console script (from
|
|
53
|
+
`cli/pyproject.toml`'s `[project.scripts]`: `mozbridge =
|
|
54
|
+
"mozbridge_cli.main:app_main"`). Confirm it's on your `PATH`:
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
mozbridge --help
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Requires Python >= 3.10.
|
|
61
|
+
|
|
62
|
+
## Quickstart
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
mozbridge login # opens a browser to sign in
|
|
66
|
+
cd ~/code/my-project
|
|
67
|
+
mozbridge link --org acme --project web-app # bind this directory
|
|
68
|
+
mozbridge publish # build + upload + trigger a build
|
|
69
|
+
mozbridge status # watch it land
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Run `mozbridge link` with no flags to pick org/project from a numbered list
|
|
73
|
+
interactively instead of naming slugs up front.
|
|
74
|
+
|
|
75
|
+
## Commands
|
|
76
|
+
|
|
77
|
+
```
|
|
78
|
+
mozbridge login # device-code SSO via Logto
|
|
79
|
+
mozbridge whoami # show the signed-in identity (refreshes the token if needed)
|
|
80
|
+
mozbridge logout # delete the cached local session
|
|
81
|
+
mozbridge link [--org SLUG] [--project SLUG] # link this directory to a Mozbridge org + project
|
|
82
|
+
mozbridge build # zip the current directory (does not upload)
|
|
83
|
+
mozbridge publish # build + upload + trigger a build on the linked project
|
|
84
|
+
mozbridge diff # preview what `publish` would do right now — makes no mutating call
|
|
85
|
+
mozbridge status # show the linked project + its 5 most recent deployments
|
|
86
|
+
mozbridge rollback [--to ID] [--yes] # roll back the linked project's deployment
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Full flag-by-flag reference, example output, and every documented failure
|
|
90
|
+
mode: **[docs/commands.md](docs/commands.md)**.
|
|
91
|
+
|
|
92
|
+
## How project linking works
|
|
93
|
+
|
|
94
|
+
`mozbridge link` writes `.mozbridge/link.json` in the **current directory**
|
|
95
|
+
— not in the CLI's own `~/.config/mozbridge`. It records which org/project
|
|
96
|
+
that specific repo checkout is bound to:
|
|
97
|
+
|
|
98
|
+
```json
|
|
99
|
+
{
|
|
100
|
+
"org_id": 1,
|
|
101
|
+
"org_slug": "acme",
|
|
102
|
+
"project_id": 10,
|
|
103
|
+
"project_slug": "web-app"
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
This is per-repo working-tree state, not per-user CLI state — a fresh clone
|
|
108
|
+
of the same repo on another machine has no link file until `mozbridge link`
|
|
109
|
+
is run there too. **It is not committed.** `link` best-effort appends
|
|
110
|
+
`.mozbridge/` to an existing `.gitignore` in the current directory (and
|
|
111
|
+
tells you so); if there's no `.gitignore` there yet, it prints a note asking
|
|
112
|
+
you to add the entry yourself rather than creating one. Either way: make
|
|
113
|
+
sure `.mozbridge/` is actually gitignored in real repos, since the file
|
|
114
|
+
contains real org/project identifiers.
|
|
115
|
+
|
|
116
|
+
`build`, `publish`, `status`, and `rollback` all read this file and fail
|
|
117
|
+
with a clear message if it's missing (see
|
|
118
|
+
[docs/commands.md](docs/commands.md) for the exact text).
|
|
119
|
+
|
|
120
|
+
## How multi-component publish works
|
|
121
|
+
|
|
122
|
+
Most real Mozbridge tenants are multi-service (a backend + a frontend +
|
|
123
|
+
workers), not a single whole-directory image. `mozbridge publish` detects
|
|
124
|
+
this from a Compose file in the current directory and, when it finds one,
|
|
125
|
+
publishes each buildable service as its own image — one
|
|
126
|
+
`create_source_upload` → `PUT` → `trigger_build` cycle per component —
|
|
127
|
+
instead of the single zip-the-whole-directory flow.
|
|
128
|
+
|
|
129
|
+
**Discovery order** (Docker Compose's own file-discovery order, checked in
|
|
130
|
+
this exact sequence, first match wins): `docker-compose.yml`,
|
|
131
|
+
`compose.yaml`, `docker-compose.yaml`.
|
|
132
|
+
|
|
133
|
+
**What counts as a "component"**: any top-level service in that file with a
|
|
134
|
+
`build:` key — either the short form (`build: ./backend`) or the mapping
|
|
135
|
+
form (`build: {context: ..., dockerfile: ...}`). A service with only an
|
|
136
|
+
`image:` key (no `build:`) is not a component and is skipped — that's how a
|
|
137
|
+
`redis:` or `postgres:` service in the same compose file is correctly
|
|
138
|
+
ignored.
|
|
139
|
+
|
|
140
|
+
**Shared-context de-duplication**: two components whose resolved build
|
|
141
|
+
context is the same directory — even if spelled differently in YAML (`.`
|
|
142
|
+
vs `./`) — are zipped and uploaded exactly once, then each triggers its own
|
|
143
|
+
build reusing that one `upload_id`. This is genuinely a byte-savings and
|
|
144
|
+
correctness behavior, not just a nicety: two services building from the
|
|
145
|
+
same monorepo root would otherwise ship two identical uploads.
|
|
146
|
+
|
|
147
|
+
**Per-component image naming**: each component's image name is computed
|
|
148
|
+
client-side as
|
|
149
|
+
`ghcr.io/{GHCR_ORG}/mozbridge-tenants/{org_slug}/{project_slug}-{service_name}`
|
|
150
|
+
(the same base the backend computes by default for a single-component
|
|
151
|
+
publish, with `-{service_name}` appended so N components never collide on
|
|
152
|
+
one image).
|
|
153
|
+
|
|
154
|
+
**Partial failure**: every component is attempted even if an earlier one
|
|
155
|
+
fails — one broken service's build failing does not stop the others from
|
|
156
|
+
being attempted. At the end, `publish` prints a summary line
|
|
157
|
+
(`Publish summary: N succeeded, M failed (of TOTAL)`) followed by one `OK`
|
|
158
|
+
or `FAIL` line per component, and exits non-zero if anything failed.
|
|
159
|
+
|
|
160
|
+
**No compose file, or none of its services build**: `publish` falls back
|
|
161
|
+
unchanged to the original single-component flow — zip the whole current
|
|
162
|
+
directory, one upload, one `trigger_build` call, with no `image_name`,
|
|
163
|
+
`context_path`, or `dockerfile_path` override sent, so the backend's own
|
|
164
|
+
defaults apply exactly as they always have. This is the fallback path, not
|
|
165
|
+
a separate mode you opt into — an ordinary non-Compose project (a single
|
|
166
|
+
Dockerfile app, or no Dockerfile at all) publishes exactly as it did before
|
|
167
|
+
multi-component support existed.
|
|
168
|
+
|
|
169
|
+
## Security model
|
|
170
|
+
|
|
171
|
+
- **Device-flow SSO, not a stored password or long-lived API key.**
|
|
172
|
+
`mozbridge login` runs Logto's OAuth 2.0 Device Authorization Grant (RFC
|
|
173
|
+
8628) against a real, provisioned Logto Native (public) client. You
|
|
174
|
+
approve the login in a browser; the CLI never sees or handles your
|
|
175
|
+
credentials directly.
|
|
176
|
+
- **Session cache**: the resulting access/refresh/ID tokens are cached at
|
|
177
|
+
`~/.config/mozbridge/session.json`. The directory is created `0700` and
|
|
178
|
+
the file is written `0600` on every save (verified against real
|
|
179
|
+
filesystem state, not mocked — see `cli/tests/test_session_permissions.py`),
|
|
180
|
+
the same posture the project's SSH-broker design used for private key
|
|
181
|
+
material. An expired access token is refreshed transparently before any
|
|
182
|
+
command that needs one; if refresh itself fails (dead/revoked refresh
|
|
183
|
+
token, or none was ever cached) you're told to `mozbridge login` again
|
|
184
|
+
rather than being left in a half-authenticated state.
|
|
185
|
+
- **What the CLI does NOT hold**: no service-token, no deploy credential, no
|
|
186
|
+
SSH key, no registry password. Every request uses the plain human Logto
|
|
187
|
+
bearer token `login` cached, sent exactly like a browser session would
|
|
188
|
+
send it.
|
|
189
|
+
- **A real, documented platform gap — stated plainly, not alarmingly**:
|
|
190
|
+
today, request-level authorization for mutating project actions (publish,
|
|
191
|
+
rollback) beyond ordinary org/project membership is not yet enforced on
|
|
192
|
+
the backend for human-authenticated callers. Concretely:
|
|
193
|
+
`require_deploy_scope` (`backend/app/features/identity/token_scopes.py`)
|
|
194
|
+
is built on `require_token_scopes(...)`, whose check body returns
|
|
195
|
+
immediately — a complete no-op — for any caller that is not a
|
|
196
|
+
`mz_`-prefixed ServiceToken (`is_service_identity(user)` is `False` for a
|
|
197
|
+
plain human Logto token). The real gate a human session hits is ordinary
|
|
198
|
+
org/project membership (`get_current_org` + `get_valid_project`), not a
|
|
199
|
+
deploy-scope check. The `rollback/{deployment_id}` route has no scope
|
|
200
|
+
dependency on it at all. This is a **backend finding**, not something
|
|
201
|
+
introduced by or specific to this CLI — it applies equally to the web
|
|
202
|
+
dashboard's own session tokens, and it was surfaced (independently,
|
|
203
|
+
twice) while building this CLI's `publish` and `rollback` commands. It's
|
|
204
|
+
tracked as a known platform gap in `.llm/plans/gap-register.md` and in
|
|
205
|
+
the session notes at the end of `.llm/plans/mozbridge-agent.md` — not
|
|
206
|
+
something this doc oversells or that the CLI works around.
|
|
207
|
+
|
|
208
|
+
## What's NOT built yet
|
|
209
|
+
|
|
210
|
+
- **`mozbridge deploy`** — the platform-orchestrated deploy path
|
|
211
|
+
(`/cicd/deploy`). `publish` uses a different, already-working mechanism
|
|
212
|
+
(`create_source_upload` + `trigger_build` with `source_type="upload"`)
|
|
213
|
+
that builds fresh source into a new image — it does not go through
|
|
214
|
+
`/cicd/deploy` today.
|
|
215
|
+
- Build-time secrets for multi-component publish.
|
|
216
|
+
- Windows support.
|
|
217
|
+
|
|
218
|
+
`mozbridge diff` (preview what `publish` would do, no mutating calls) is
|
|
219
|
+
now built — see [docs/commands.md](docs/commands.md#mozbridge-diff). It
|
|
220
|
+
compares local components against the most recent deployment *record*
|
|
221
|
+
only, not the actually-deployed compose content — the platform doesn't
|
|
222
|
+
expose that anywhere yet, so a full semantic compose diff is still not
|
|
223
|
+
built.
|
|
224
|
+
|
|
225
|
+
See `.llm/plans/mozbridge-agent.md` (the "Session progress and
|
|
226
|
+
reconciliation" section at the end) for the full roadmap and how the CLI's
|
|
227
|
+
scope compares to that plan's original acceptance criteria — not
|
|
228
|
+
duplicated here.
|
|
229
|
+
|
|
230
|
+
## CI / headless use
|
|
231
|
+
|
|
232
|
+
Every command above assumes an interactive human at a terminal —
|
|
233
|
+
`mozbridge login` opens a browser. A CI pipeline has neither, so instead of
|
|
234
|
+
that stored session, set `MOZBRIDGE_TOKEN` in the pipeline environment:
|
|
235
|
+
|
|
236
|
+
```bash
|
|
237
|
+
export MOZBRIDGE_TOKEN=<service-token>
|
|
238
|
+
mozbridge link --org acme --project web-app
|
|
239
|
+
mozbridge publish
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
When `MOZBRIDGE_TOKEN` is set, it is used **directly as the bearer token for
|
|
243
|
+
every command** — `whoami`, `link`, `status`, `rollback`, `diff`, and both
|
|
244
|
+
`publish` and `publish --local`'s upload/registration calls — instead of the
|
|
245
|
+
on-disk session `mozbridge login` produces. It's checked before anything
|
|
246
|
+
session-file-related, so nothing under `~/.config/mozbridge/` is read,
|
|
247
|
+
written, or permission-checked in this path at all. Because of that,
|
|
248
|
+
`mozbridge login` itself refuses cleanly when `MOZBRIDGE_TOKEN` is set
|
|
249
|
+
(these are mutually exclusive auth modes — a CI token in the environment
|
|
250
|
+
means "don't attempt an interactive login") — unset it first if you actually
|
|
251
|
+
want to run the device flow.
|
|
252
|
+
|
|
253
|
+
**Where the token comes from**: a project-scoped Mozbridge `ServiceToken`,
|
|
254
|
+
created out of band via the Mozbridge dashboard/API
|
|
255
|
+
(`create_service_token`) — the CLI never creates or mints one itself. Set it
|
|
256
|
+
as your CI platform's secret store, never as a `--token` flag (it would leak
|
|
257
|
+
into shell history and process listings) and never committed to the repo.
|
|
258
|
+
|
|
259
|
+
**`MOZBRIDGE_TOKEN` vs `MOZBRIDGE_BUILD_TOKEN` — do not confuse these**:
|
|
260
|
+
|
|
261
|
+
| | `MOZBRIDGE_TOKEN` | `MOZBRIDGE_BUILD_TOKEN` |
|
|
262
|
+
|---|---|---|
|
|
263
|
+
| Authenticates | **every** command, as a project-scoped identity | only `publish --local`'s one `GET /cicd/runtime-secrets` call |
|
|
264
|
+
| Replaces | the entire `mozbridge login` session | nothing — it's used alongside your regular session, which still does the actual build registration |
|
|
265
|
+
| Scope needed | whatever the platform grants a CI/service identity for the commands you run (link/publish/status/rollback) | the `build` scope specifically (registry/build credentials only, never a deploy host key) |
|
|
266
|
+
|
|
267
|
+
They are two separate credentials for two separate purposes and are never
|
|
268
|
+
read from, or substituted for, one another anywhere in this CLI — see
|
|
269
|
+
`session.py` and `runtime_secrets.py`'s module docstrings.
|
|
270
|
+
|
|
271
|
+
**A realistic Woodpecker pipeline step**:
|
|
272
|
+
```yaml
|
|
273
|
+
steps:
|
|
274
|
+
publish:
|
|
275
|
+
image: python:3.12-slim
|
|
276
|
+
environment:
|
|
277
|
+
MOZBRIDGE_TOKEN:
|
|
278
|
+
from_secret: mozbridge_token
|
|
279
|
+
commands:
|
|
280
|
+
- pip install --quiet mozbridge-cli # or: pip install -e ./cli, per your setup
|
|
281
|
+
- mozbridge link --org acme --project web-app
|
|
282
|
+
- mozbridge publish
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
A GitHub Actions step looks the same, just spelled with `env:` and
|
|
286
|
+
`${{ secrets.MOZBRIDGE_TOKEN }}`:
|
|
287
|
+
```yaml
|
|
288
|
+
- name: Publish to Mozbridge
|
|
289
|
+
env:
|
|
290
|
+
MOZBRIDGE_TOKEN: ${{ secrets.MOZBRIDGE_TOKEN }}
|
|
291
|
+
run: |
|
|
292
|
+
mozbridge link --org acme --project web-app
|
|
293
|
+
mozbridge publish
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
`mozbridge link` still needs to run once per checkout (it writes
|
|
297
|
+
`.mozbridge/link.json` into the working directory, which a fresh CI checkout
|
|
298
|
+
never has) — there is no way to skip it by pre-seeding the link file today,
|
|
299
|
+
so it belongs in the same pipeline step, before `publish`.
|
|
300
|
+
|
|
301
|
+
## Configuration (advanced / override)
|
|
302
|
+
|
|
303
|
+
| Env var | Default | Purpose |
|
|
304
|
+
|---|---|---|
|
|
305
|
+
| `MOZBRIDGE_LOGTO_ENDPOINT` | `https://auth.mozbridge.com` | Logto tenant endpoint |
|
|
306
|
+
| `MOZBRIDGE_LOGTO_CLIENT_ID` | `swdtsuiz6dgzack1g75x3` | Logto Native app client id (public client) |
|
|
307
|
+
| `MOZBRIDGE_PUBLIC_API_URL` (falls back to `PUBLIC_API_URL`) | `https://api.mozbridge.com` | Mozbridge API base |
|
|
308
|
+
| `MOZBRIDGE_GHCR_ORG` | `jessin01` | GHCR org used to compute default image names for multi-component publish |
|
|
309
|
+
| `MOZBRIDGE_CONFIG_DIR` | `~/.config/mozbridge` | Where the session file lives (mainly for tests) |
|
|
310
|
+
| `MOZBRIDGE_TOKEN` | unset | CI/headless auth — see [CI / headless use](#ci--headless-use) above |
|
|
311
|
+
|
|
312
|
+
All of these have real, working defaults — the CLI works out of the box
|
|
313
|
+
against production Mozbridge. The env vars exist for pointing at a
|
|
314
|
+
non-production instance (and for test isolation).
|
|
315
|
+
|
|
316
|
+
## Troubleshooting
|
|
317
|
+
|
|
318
|
+
**"Not logged in. Run `mozbridge login` first."** — no session file
|
|
319
|
+
exists yet at `~/.config/mozbridge/session.json` (or `whoami`/`link`/etc.
|
|
320
|
+
was run before ever logging in). Run `mozbridge login`.
|
|
321
|
+
|
|
322
|
+
**"Session expired and cannot be refreshed. Run `mozbridge login` again."**
|
|
323
|
+
— your cached session has no refresh token. Re-run `mozbridge login`.
|
|
324
|
+
|
|
325
|
+
**"Session refresh failed: ... — run `mozbridge login` again."** — the
|
|
326
|
+
access token was expired and the cached refresh token was rejected by
|
|
327
|
+
Logto (revoked, or itself expired). Re-run `mozbridge login`.
|
|
328
|
+
|
|
329
|
+
**"Session is no longer valid. Run `mozbridge login` again."** (from
|
|
330
|
+
`whoami` specifically) — the API returned 401 even with a token that looked
|
|
331
|
+
valid client-side (e.g. revoked server-side). Re-run `mozbridge login`.
|
|
332
|
+
|
|
333
|
+
**"This directory is not linked to a Mozbridge project. Run `mozbridge
|
|
334
|
+
link` first."** — `build`/`publish`/`status`/`rollback` all require a
|
|
335
|
+
`.mozbridge/link.json` in the current directory. Run `mozbridge link`
|
|
336
|
+
(from the same directory you'll run these commands in).
|
|
337
|
+
|
|
338
|
+
**A failed `publish`** — each step (`create_source_upload`, the `PUT`
|
|
339
|
+
upload, `trigger_build`) surfaces the backend's own error `detail` text
|
|
340
|
+
verbatim, prefixed with what it was trying to do, e.g.:
|
|
341
|
+
|
|
342
|
+
```
|
|
343
|
+
Could not upload the build source: Zip too large
|
|
344
|
+
Could not create a source upload session: Not a member of this organization
|
|
345
|
+
Could not trigger the build: <backend detail text>
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
For a multi-component publish, a failure on one component does not stop
|
|
349
|
+
the others — you get a per-component `OK`/`FAIL` summary at the end (see
|
|
350
|
+
[docs/commands.md](docs/commands.md#publish) for the exact shape) and the
|
|
351
|
+
process exits non-zero if anything failed.
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
mozbridge_cli/__init__.py,sha256=g2NRvPMhAVHryZBlc7aqhnjsFYQUO6ttcZqOQUscOS4,108
|
|
2
|
+
mozbridge_cli/__main__.py,sha256=x_rJ4JJtmcsX3-bdJcHN8TanBF7RNPtolGfIZjW3Zbo,70
|
|
3
|
+
mozbridge_cli/api.py,sha256=bdKWjhjD-MvuRcu3NNGQ0yonSC44cEP3yjRdzijTun8,11139
|
|
4
|
+
mozbridge_cli/auth.py,sha256=U4ffalUjmDxEivOtn4c3rvpiIs-YDAlJcCVpCTB11sA,5715
|
|
5
|
+
mozbridge_cli/build.py,sha256=E9p9hLSqPG2kOOoAkkfIywIMePU_3QiUMW4PpbQm80E,3336
|
|
6
|
+
mozbridge_cli/compose.py,sha256=n8i7d2BiNRwYtZ84sDNno5dDbeMihp9YIDjihYOaCgA,7212
|
|
7
|
+
mozbridge_cli/config.py,sha256=DzVBM5sB-3lbB6OozePnJBsbG2U_CGreY95SK8sqUAc,3365
|
|
8
|
+
mozbridge_cli/link.py,sha256=JbqYMOPprVeaS58kEjHgR7do2GooqQQEUwqb1oIpOLg,2824
|
|
9
|
+
mozbridge_cli/local_build.py,sha256=3QNTOb6NMZaR5M5Zdg_V4aqt9o6Fpax97CBTcq_lo9Q,5555
|
|
10
|
+
mozbridge_cli/main.py,sha256=FCQLmnPiqSRpLKxEtFZ_XA1VHPAykk7srHPFDVMgQq8,33277
|
|
11
|
+
mozbridge_cli/runtime_secrets.py,sha256=q-8_Xf3NTI7S7qxD-LQ9p1s6pazn3XK2DwYzEly0wCo,5921
|
|
12
|
+
mozbridge_cli/session.py,sha256=Fu7IAt-aOm7w78dTHSlmE9zJf3yhc28-dX-VxpocRbY,5275
|
|
13
|
+
mozbridge_cli-0.1.0.dist-info/METADATA,sha256=Ua5Ars3fISrOlY4pfcWkONm4j7lP20c2Q4PEosIYeoA,16733
|
|
14
|
+
mozbridge_cli-0.1.0.dist-info/WHEEL,sha256=zOwg4jB6zX2kU910N-cMawjivD6tO8NEWvE12je1bVk,87
|
|
15
|
+
mozbridge_cli-0.1.0.dist-info/entry_points.txt,sha256=HWKL7SM6k13b49p6l8y7iTee0FaSSaugyQ52wgw9BiY,58
|
|
16
|
+
mozbridge_cli-0.1.0.dist-info/RECORD,,
|