@sequenceholdings/studio-cli 0.4.2 → 0.4.5

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.
@@ -1,63 +0,0 @@
1
- /**
2
- * Tiered environment discovery.
3
- *
4
- * The published package ships with only the `local` environment baked in.
5
- * Every other deployment URL is *discovered* from the authenticated
6
- * `GET /api/platform/environments` endpoint and cached on disk:
7
- *
8
- * - `anonymous` — no usable token (or the endpoint rejected it):
9
- * `local` only, no catalog cached.
10
- * - `sequence` — Sequence staff / trusted M2M: full catalog
11
- * (local, staging, production, banksouth) + preview targeting.
12
- * - `partner` — any other authenticated identity: local + their
13
- * deployment(s).
14
- *
15
- * This is a visibility/UX layer, not access control — every deployment
16
- * still enforces Auth0 + FGA per request. A user who knows a URL can
17
- * always add it to config.toml; the server rejecting their token is the
18
- * real gate.
19
- *
20
- * One bootstrap hostname is baked into the package for the discovery
21
- * call itself. All deployments share the same Auth0 audience, so any
22
- * identity's token validates there; the endpoint 401s anonymously.
23
- */
24
- export type EnvTier = 'anonymous' | 'sequence' | 'partner';
25
- export interface CatalogEnvironment {
26
- name: string;
27
- url: string;
28
- }
29
- export interface EnvCatalog {
30
- tier: Exclude<EnvTier, 'anonymous'>;
31
- environments: CatalogEnvironment[];
32
- /** Unix ms timestamp of the successful fetch. */
33
- fetchedAt: number;
34
- /**
35
- * Auth0 `sub` of the identity that fetched this catalog. Reads ignore the
36
- * cache when the current identity differs (logout, seqapi re-login as
37
- * someone else), so one user's tier never carries over to another.
38
- */
39
- subject: string;
40
- }
41
- /** The host the discovery call goes to. Override for tests / self-hosting. */
42
- export declare function bootstrapUrl(): string;
43
- export declare function catalogPath(): string;
44
- /**
45
- * Read the cached catalog from disk. Returns null when missing, malformed,
46
- * or fetched by a *different* identity than the one the CLI would
47
- * authenticate as right now — callers treat all of those as tier
48
- * `anonymous`. The subject check means a logout (token file removed) or a
49
- * re-login as someone else invalidates the cache immediately, without
50
- * waiting for the next discovery call.
51
- */
52
- export declare function readCachedCatalog(): Promise<EnvCatalog | null>;
53
- export declare function clearCatalog(): Promise<void>;
54
- /**
55
- * Fetch the environment catalog from the discovery endpoint and cache it.
56
- *
57
- * Returns the fresh catalog, or null when the caller is effectively
58
- * anonymous: no token available, or the endpoint rejected the token
59
- * (401/403 — the stale cache is cleared so visibility downgrades too).
60
- * Network/server errors leave any existing cache in place and rethrow so
61
- * callers can distinguish "offline" from "not authorized".
62
- */
63
- export declare function fetchCatalog(): Promise<EnvCatalog | null>;
@@ -1,121 +0,0 @@
1
- import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
2
- import { existsSync } from 'node:fs';
3
- import { homedir } from 'node:os';
4
- import { dirname, join } from 'node:path';
5
- import { authenticatedRequestUrl } from '@sequenceholdings/artifact-studio/deployment-validation';
6
- import { currentIdentitySubject, decodeJwtSub, tryGetAccessToken } from './auth.js';
7
- const DEFAULT_BOOTSTRAP_URL = 'https://atlas.seqholdings.com';
8
- /** The host the discovery call goes to. Override for tests / self-hosting. */
9
- export function bootstrapUrl() {
10
- return (process.env['SEQ_STUDIO_DISCOVERY_URL']?.trim().replace(/\/+$/, '') ||
11
- DEFAULT_BOOTSTRAP_URL);
12
- }
13
- export function catalogPath() {
14
- return join(homedir(), '.config', 'lattice', 'environments.json');
15
- }
16
- function isValidCatalog(value) {
17
- if (typeof value !== 'object' || value === null)
18
- return false;
19
- const candidate = value;
20
- return ((candidate.tier === 'sequence' || candidate.tier === 'partner') &&
21
- Array.isArray(candidate.environments) &&
22
- candidate.environments.every((env) => typeof env === 'object' &&
23
- env !== null &&
24
- typeof env.name === 'string' &&
25
- typeof env.url === 'string') &&
26
- typeof candidate.fetchedAt === 'number' &&
27
- typeof candidate.subject === 'string' &&
28
- candidate.subject.length > 0);
29
- }
30
- /**
31
- * Read the cached catalog from disk. Returns null when missing, malformed,
32
- * or fetched by a *different* identity than the one the CLI would
33
- * authenticate as right now — callers treat all of those as tier
34
- * `anonymous`. The subject check means a logout (token file removed) or a
35
- * re-login as someone else invalidates the cache immediately, without
36
- * waiting for the next discovery call.
37
- */
38
- export async function readCachedCatalog() {
39
- const path = catalogPath();
40
- if (!existsSync(path))
41
- return null;
42
- let catalog;
43
- try {
44
- const parsed = JSON.parse(await readFile(path, 'utf8'));
45
- catalog = isValidCatalog(parsed) ? parsed : null;
46
- }
47
- catch {
48
- return null;
49
- }
50
- if (!catalog)
51
- return null;
52
- const subject = await currentIdentitySubject();
53
- if (subject === null || subject !== catalog.subject)
54
- return null;
55
- return catalog;
56
- }
57
- async function saveCatalog(catalog) {
58
- const path = catalogPath();
59
- await mkdir(dirname(path), { recursive: true });
60
- await writeFile(path, JSON.stringify(catalog, null, 2) + '\n', {
61
- encoding: 'utf8',
62
- mode: 0o600,
63
- });
64
- }
65
- export async function clearCatalog() {
66
- await rm(catalogPath(), { force: true });
67
- }
68
- /**
69
- * Fetch the environment catalog from the discovery endpoint and cache it.
70
- *
71
- * Returns the fresh catalog, or null when the caller is effectively
72
- * anonymous: no token available, or the endpoint rejected the token
73
- * (401/403 — the stale cache is cleared so visibility downgrades too).
74
- * Network/server errors leave any existing cache in place and rethrow so
75
- * callers can distinguish "offline" from "not authorized".
76
- */
77
- export async function fetchCatalog() {
78
- const targetUrl = bootstrapUrl();
79
- const requestUrl = authenticatedRequestUrl({
80
- baseUrl: targetUrl,
81
- path: '/api/platform/environments',
82
- });
83
- // Fail closed when an M2M secret is configured but minting fails, so
84
- // headless callers (CI) see the real Auth0 error instead of a silent
85
- // downgrade to the anonymous tier.
86
- const token = await tryGetAccessToken({
87
- failClosedForM2m: true,
88
- targetUrl,
89
- });
90
- if (!token) {
91
- await clearCatalog();
92
- return null;
93
- }
94
- const response = await fetch(requestUrl, {
95
- headers: { Authorization: `Bearer ${token}` },
96
- redirect: 'manual',
97
- });
98
- if (response.status === 401 || response.status === 403) {
99
- await clearCatalog();
100
- return null;
101
- }
102
- if (!response.ok) {
103
- throw new Error(`Environment discovery failed (${response.status}) against ${targetUrl}.`);
104
- }
105
- const body = (await response.json());
106
- if ((body.tier !== 'sequence' && body.tier !== 'partner') ||
107
- !Array.isArray(body.environments)) {
108
- throw new Error('Environment discovery returned an unexpected response shape.');
109
- }
110
- const catalog = {
111
- tier: body.tier,
112
- environments: body.environments,
113
- fetchedAt: Date.now(),
114
- // Bind the cache to the identity that fetched it. `unknown` never
115
- // matches a real subject, so an undecodable token yields a catalog
116
- // that works for this invocation but is not trusted from disk later.
117
- subject: decodeJwtSub(token) ?? 'unknown',
118
- };
119
- await saveCatalog(catalog);
120
- return catalog;
121
- }