@indigoai-us/hq-cli 5.8.6 → 5.10.0
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.
- package/CHANGELOG.md +44 -0
- package/dist/commands/cloud-demote.d.ts +90 -0
- package/dist/commands/cloud-demote.js +193 -0
- package/dist/commands/run.d.ts +3 -0
- package/dist/commands/run.js +119 -0
- package/dist/commands/secrets.d.ts +3 -9
- package/dist/commands/secrets.js +5 -56
- package/dist/index.js +7 -2
- package/dist/run/discover-schemas.d.ts +12 -0
- package/dist/run/discover-schemas.js +64 -0
- package/dist/run/hq-plugin.d.ts +26 -0
- package/dist/run/hq-plugin.js +144 -0
- package/dist/utils/vault-api.d.ts +10 -0
- package/dist/utils/vault-api.js +58 -0
- package/package.json +7 -3
- package/src/commands/cloud-demote.test.ts +401 -0
- package/src/commands/cloud-demote.ts +277 -0
- package/src/commands/run.env-local.test.ts +84 -0
- package/src/commands/run.ts +137 -0
- package/src/commands/secrets.ts +4 -88
- package/src/index.ts +6 -0
- package/src/run/__fixtures__/discover-schemas/example.env.schema +4 -0
- package/src/run/discover-schemas.test.ts +153 -0
- package/src/run/discover-schemas.ts +79 -0
- package/src/run/hq-plugin.test.ts +125 -0
- package/src/run/hq-plugin.ts +174 -0
- package/src/run/varlock-shape.test.ts +57 -0
- package/src/utils/vault-api.ts +80 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
|
|
2
|
+
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="17c9937b-a219-5b2d-a120-0acbf96f945a")}catch(e){}}();
|
|
3
|
+
import * as fs from 'node:fs';
|
|
4
|
+
import * as path from 'node:path';
|
|
5
|
+
const SLUG_RE = /^# @hqCompany\("([^"]+)"\)/m;
|
|
6
|
+
function parseSlug(filePath) {
|
|
7
|
+
try {
|
|
8
|
+
const content = fs.readFileSync(filePath, 'utf8');
|
|
9
|
+
const m = SLUG_RE.exec(content);
|
|
10
|
+
return m ? m[1] : null;
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export function discoverSchemas(cwd) {
|
|
17
|
+
const schemaPaths = [];
|
|
18
|
+
const envLocalPaths = [];
|
|
19
|
+
let dir = path.resolve(cwd);
|
|
20
|
+
while (true) {
|
|
21
|
+
const schemaPath = path.join(dir, '.env.schema');
|
|
22
|
+
if (fs.existsSync(schemaPath)) {
|
|
23
|
+
schemaPaths.push(schemaPath);
|
|
24
|
+
const localPath = path.join(dir, '.env.local');
|
|
25
|
+
if (fs.existsSync(localPath)) {
|
|
26
|
+
envLocalPaths.push(localPath);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const isGitRoot = fs.existsSync(path.join(dir, '.git'));
|
|
30
|
+
if (isGitRoot)
|
|
31
|
+
break;
|
|
32
|
+
const parent = path.dirname(dir);
|
|
33
|
+
if (parent === dir)
|
|
34
|
+
break; // filesystem root
|
|
35
|
+
dir = parent;
|
|
36
|
+
}
|
|
37
|
+
// cwd-closest is pushed last during walk-up, so reversing puts it last.
|
|
38
|
+
// (During walk-up cwd is checked first → pushed first → reversed → ends up last.)
|
|
39
|
+
schemaPaths.reverse();
|
|
40
|
+
envLocalPaths.reverse();
|
|
41
|
+
let companySlug = null;
|
|
42
|
+
let companySlugPath = null;
|
|
43
|
+
let conflict = null;
|
|
44
|
+
for (const schemaPath of schemaPaths) {
|
|
45
|
+
const slug = parseSlug(schemaPath);
|
|
46
|
+
if (slug == null)
|
|
47
|
+
continue;
|
|
48
|
+
if (companySlug == null) {
|
|
49
|
+
companySlug = slug;
|
|
50
|
+
companySlugPath = schemaPath;
|
|
51
|
+
}
|
|
52
|
+
else if (companySlug !== slug) {
|
|
53
|
+
conflict = {
|
|
54
|
+
paths: [companySlugPath, schemaPath],
|
|
55
|
+
slugs: [companySlug, slug],
|
|
56
|
+
};
|
|
57
|
+
companySlug = null;
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return { schemaPaths, envLocalPaths, companySlug, conflict };
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=discover-schemas.js.map
|
|
64
|
+
//# debugId=17c9937b-a219-5b2d-a120-0acbf96f945a
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export interface InstallHqPluginOpts {
|
|
2
|
+
companyOverride?: string;
|
|
3
|
+
resolveCompanyUid: (slug: string) => Promise<string>;
|
|
4
|
+
fetchBatch: (uid: string, names: string[]) => Promise<{
|
|
5
|
+
secrets: Array<{
|
|
6
|
+
name: string;
|
|
7
|
+
value: string;
|
|
8
|
+
}>;
|
|
9
|
+
errors: Array<{
|
|
10
|
+
name: string;
|
|
11
|
+
code: string;
|
|
12
|
+
message?: string;
|
|
13
|
+
}>;
|
|
14
|
+
}>;
|
|
15
|
+
}
|
|
16
|
+
export interface PluginState {
|
|
17
|
+
schemaCompanySlug: string | null;
|
|
18
|
+
uid: string | null;
|
|
19
|
+
errorsByName: Map<string, {
|
|
20
|
+
code: string;
|
|
21
|
+
message?: string;
|
|
22
|
+
}>;
|
|
23
|
+
}
|
|
24
|
+
export declare function installHqPlugin(graph: any, opts: InstallHqPluginOpts): PluginState;
|
|
25
|
+
export declare function prewarmHqSecrets(graph: any, opts: InstallHqPluginOpts, state: PluginState): Promise<void>;
|
|
26
|
+
//# sourceMappingURL=hq-plugin.d.ts.map
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
|
|
2
|
+
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="01fee323-1f63-5411-bfc9-ff49e241f712")}catch(e){}}();
|
|
3
|
+
import { ResolutionError } from 'varlock/plugin-lib';
|
|
4
|
+
import { readCache, writeCache } from '../utils/secrets-cache.js';
|
|
5
|
+
export function installHqPlugin(graph /* EnvGraph */, opts) {
|
|
6
|
+
const pluginState = {
|
|
7
|
+
schemaCompanySlug: null,
|
|
8
|
+
uid: null,
|
|
9
|
+
errorsByName: new Map(),
|
|
10
|
+
};
|
|
11
|
+
// varlock@1.0.0's plugin-lib.js omits the Resolver export (d.ts/JS mismatch);
|
|
12
|
+
// extract it at runtime from any already-registered built-in resolver's prototype.
|
|
13
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
14
|
+
let RuntimeResolver;
|
|
15
|
+
try {
|
|
16
|
+
const fns = graph.registeredResolverFunctions;
|
|
17
|
+
const first = Object.values(fns)[0];
|
|
18
|
+
if (first == null)
|
|
19
|
+
throw new Error('registeredResolverFunctions is empty');
|
|
20
|
+
const proto = Object.getPrototypeOf(first);
|
|
21
|
+
if (proto == null || typeof proto.prototype?.process !== 'function') {
|
|
22
|
+
throw new Error('prototype has no process method');
|
|
23
|
+
}
|
|
24
|
+
RuntimeResolver = proto;
|
|
25
|
+
}
|
|
26
|
+
catch (e) {
|
|
27
|
+
throw new Error('varlock Resolver base class extraction failed — the varlock@1.0.0 d.ts/JS mismatch ' +
|
|
28
|
+
'may have been resolved; switch to `import { Resolver } from "varlock/plugin-lib"`. ' +
|
|
29
|
+
`Underlying: ${e instanceof Error ? e.message : String(e)}`);
|
|
30
|
+
}
|
|
31
|
+
// HqResolver is declared INSIDE installHqPlugin so its static def.resolve
|
|
32
|
+
// closes over `pluginState`. Module-scope declaration is forbidden — resolve()
|
|
33
|
+
// would hit `ReferenceError: pluginState is not defined`.
|
|
34
|
+
class HqResolver extends RuntimeResolver {
|
|
35
|
+
static def = {
|
|
36
|
+
name: 'hq',
|
|
37
|
+
impliesSensitive: true,
|
|
38
|
+
argsSchema: { type: 'array', arrayMaxLength: 1 },
|
|
39
|
+
resolve: async function () {
|
|
40
|
+
// Cache-only read. `pluginState` is captured by this inner-class closure;
|
|
41
|
+
// `prewarmHqSecrets(graph, opts, state)` populates `state.uid` and
|
|
42
|
+
// `state.errorsByName` before `graph.resolveEnvValues()` calls us.
|
|
43
|
+
const explicit = this.arrArgs?.[0]?.staticValue;
|
|
44
|
+
const secretName = (typeof explicit === 'string' && explicit) ? explicit : this._ownerKey;
|
|
45
|
+
if (!secretName) {
|
|
46
|
+
throw new ResolutionError('hq() resolver could not determine secret name (missing owner key)');
|
|
47
|
+
}
|
|
48
|
+
const err = pluginState.errorsByName.get(secretName);
|
|
49
|
+
if (err) {
|
|
50
|
+
if (err.code === 'forbidden') {
|
|
51
|
+
throw new ResolutionError(`No read permission for secret "${secretName}" — ask an admin to share it via \`hq secrets share ${secretName} --with <you> --permission read\``);
|
|
52
|
+
}
|
|
53
|
+
if (err.code === 'not_found') {
|
|
54
|
+
throw new ResolutionError(`Secret "${secretName}" does not exist in company`);
|
|
55
|
+
}
|
|
56
|
+
throw new ResolutionError(`Failed to load secret "${secretName}": ${err.message ?? err.code}`);
|
|
57
|
+
}
|
|
58
|
+
// Sentinel-check style throughout: `readCache` returns `string | null`
|
|
59
|
+
// (verified at `hq/packages/hq-cli/src/utils/secrets-cache.ts:45`); `pluginState.uid`
|
|
60
|
+
// is `string | null` per `PluginState`. Use `== null` (covers null AND undefined defensively)
|
|
61
|
+
// for both — do not mix in truthy checks like `if (!x)`, which would silently swallow a
|
|
62
|
+
// legitimate empty-string value if the contract ever loosened.
|
|
63
|
+
if (pluginState.uid == null) {
|
|
64
|
+
throw new ResolutionError('Internal error: prewarmHqSecrets was not called before resolveEnvValues');
|
|
65
|
+
}
|
|
66
|
+
const cached = readCache(pluginState.uid, secretName); // string | null
|
|
67
|
+
if (cached == null) {
|
|
68
|
+
throw new ResolutionError(`Internal error: pre-warm did not populate cache for "${secretName}"`);
|
|
69
|
+
}
|
|
70
|
+
return cached;
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
// Captured during process(parent); used by resolve() to fall back to the var key.
|
|
74
|
+
_ownerKey;
|
|
75
|
+
process(parent) {
|
|
76
|
+
super.process(parent);
|
|
77
|
+
if (parent != null && typeof parent.key === 'string') {
|
|
78
|
+
this._ownerKey = parent.key;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
// varlock@1.0.0's env-spec parser rejects dots in decorator names ([a-zA-Z0-9_] only),
|
|
83
|
+
// so `@hq.company` is not valid syntax. We register as `@hqCompany` (camelCase) instead.
|
|
84
|
+
// Schema files must use `# @hqCompany("slug")` followed by a blank line so the parser
|
|
85
|
+
// treats it as a file-level root decorator rather than an item decorator for the next var.
|
|
86
|
+
graph.registerRootDecorator({
|
|
87
|
+
name: 'hqCompany',
|
|
88
|
+
isFunction: true,
|
|
89
|
+
process: (decoratorValue) => {
|
|
90
|
+
const slug = decoratorValue.arrArgs?.[0]?.staticValue;
|
|
91
|
+
return typeof slug === 'string' ? slug : null;
|
|
92
|
+
},
|
|
93
|
+
execute: (slug) => {
|
|
94
|
+
if (slug)
|
|
95
|
+
pluginState.schemaCompanySlug = slug;
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
graph.registerResolver(HqResolver);
|
|
99
|
+
// Returned so `prewarmHqSecrets(graph, opts, state)` can read schemaCompanySlug
|
|
100
|
+
// and write `uid` + `errorsByName`. The state is held by the closure; the returned handle
|
|
101
|
+
// is purely for the prewarm helper.
|
|
102
|
+
return pluginState;
|
|
103
|
+
}
|
|
104
|
+
export async function prewarmHqSecrets(graph /* EnvGraph */, opts, state) {
|
|
105
|
+
const queue = [];
|
|
106
|
+
for (const [key, item] of Object.entries(graph.configSchema)) {
|
|
107
|
+
const resolver = item.valueResolver;
|
|
108
|
+
if (resolver?.def?.name === 'hq') {
|
|
109
|
+
const explicit = resolver.arrArgs?.[0]?.staticValue;
|
|
110
|
+
const secretName = (typeof explicit === 'string' && explicit) ? explicit : key;
|
|
111
|
+
queue.push({ key, secretName });
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (queue.length === 0) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
let slug;
|
|
118
|
+
if (state.schemaCompanySlug) {
|
|
119
|
+
slug = state.schemaCompanySlug;
|
|
120
|
+
}
|
|
121
|
+
else if (opts.companyOverride) {
|
|
122
|
+
slug = opts.companyOverride;
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
throw new Error('@hqCompany("...") not declared and --company not passed');
|
|
126
|
+
}
|
|
127
|
+
const uid = await opts.resolveCompanyUid(slug);
|
|
128
|
+
const uniqueNames = [...new Set(queue.map((q) => q.secretName))];
|
|
129
|
+
if (uniqueNames.length > 100) {
|
|
130
|
+
throw new Error(`hq run supports at most 100 hq() resolvers per schema; got ${uniqueNames.length}`);
|
|
131
|
+
}
|
|
132
|
+
const result = await opts.fetchBatch(uid, uniqueNames);
|
|
133
|
+
for (const s of result.secrets) {
|
|
134
|
+
writeCache(uid, s.name, s.value);
|
|
135
|
+
}
|
|
136
|
+
const errorsByName = new Map();
|
|
137
|
+
for (const e of result.errors) {
|
|
138
|
+
errorsByName.set(e.name, { code: e.code, message: e.message });
|
|
139
|
+
}
|
|
140
|
+
state.errorsByName = errorsByName;
|
|
141
|
+
state.uid = uid;
|
|
142
|
+
}
|
|
143
|
+
//# sourceMappingURL=hq-plugin.js.map
|
|
144
|
+
//# debugId=01fee323-1f63-5411-bfc9-ff49e241f712
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface VaultApiOptions {
|
|
2
|
+
token: string;
|
|
3
|
+
path: string;
|
|
4
|
+
method?: string;
|
|
5
|
+
body?: Record<string, unknown>;
|
|
6
|
+
query?: Record<string, string>;
|
|
7
|
+
}
|
|
8
|
+
export declare function vaultApiFetch(opts: VaultApiOptions): Promise<Response>;
|
|
9
|
+
export declare function getCompanyUid(token: string, companySlug: string | undefined): Promise<string>;
|
|
10
|
+
//# sourceMappingURL=vault-api.d.ts.map
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
|
|
2
|
+
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="4e482805-d129-5563-a77b-97b80410154b")}catch(e){}}();
|
|
3
|
+
import { DEFAULT_VAULT_API_URL } from './cognito-session.js';
|
|
4
|
+
export async function vaultApiFetch(opts) {
|
|
5
|
+
const url = new URL(opts.path, DEFAULT_VAULT_API_URL);
|
|
6
|
+
if (opts.query) {
|
|
7
|
+
for (const [k, v] of Object.entries(opts.query)) {
|
|
8
|
+
url.searchParams.set(k, v);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
return fetch(url.toString(), {
|
|
12
|
+
method: opts.method ?? 'GET',
|
|
13
|
+
headers: {
|
|
14
|
+
Authorization: `Bearer ${opts.token}`,
|
|
15
|
+
'Content-Type': 'application/json',
|
|
16
|
+
},
|
|
17
|
+
body: opts.body ? JSON.stringify(opts.body) : undefined,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
async function resolveCompanyUid(token, slug) {
|
|
21
|
+
const res = await vaultApiFetch({
|
|
22
|
+
token,
|
|
23
|
+
path: `/entity/by-slug/company/${encodeURIComponent(slug)}`,
|
|
24
|
+
});
|
|
25
|
+
if (!res.ok) {
|
|
26
|
+
const body = await res.json().catch(() => ({}));
|
|
27
|
+
throw new Error(`Failed to resolve company slug '${slug}': ${body.error ?? res.statusText}`);
|
|
28
|
+
}
|
|
29
|
+
const data = (await res.json());
|
|
30
|
+
return data.entity.uid;
|
|
31
|
+
}
|
|
32
|
+
async function resolveCompanyFromMemberships(token) {
|
|
33
|
+
const res = await vaultApiFetch({
|
|
34
|
+
token,
|
|
35
|
+
path: '/membership/me',
|
|
36
|
+
});
|
|
37
|
+
if (!res.ok) {
|
|
38
|
+
throw new Error("Failed to fetch memberships — run `hq login` and try again");
|
|
39
|
+
}
|
|
40
|
+
const data = (await res.json());
|
|
41
|
+
const active = data.memberships.filter((m) => m.status === 'active');
|
|
42
|
+
if (active.length === 0) {
|
|
43
|
+
throw new Error('No active company memberships found. Use --company <slug> to specify.');
|
|
44
|
+
}
|
|
45
|
+
if (active.length === 1) {
|
|
46
|
+
return active[0].companyUid;
|
|
47
|
+
}
|
|
48
|
+
const uids = active.map((m) => m.companyUid).join(', ');
|
|
49
|
+
throw new Error(`Multiple companies found (${uids}). Use --company <slug> to specify which one.`);
|
|
50
|
+
}
|
|
51
|
+
export async function getCompanyUid(token, companySlug) {
|
|
52
|
+
if (companySlug) {
|
|
53
|
+
return resolveCompanyUid(token, companySlug);
|
|
54
|
+
}
|
|
55
|
+
return resolveCompanyFromMemberships(token);
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=vault-api.js.map
|
|
58
|
+
//# debugId=4e482805-d129-5563-a77b-97b80410154b
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@indigoai-us/hq-cli",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.10.0",
|
|
4
4
|
"description": "HQ by Indigo management CLI — modules and cloud sync",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"bin": {
|
|
@@ -21,7 +21,8 @@
|
|
|
21
21
|
"commander": "^12.1.0",
|
|
22
22
|
"js-yaml": "^4.1.0",
|
|
23
23
|
"simple-git": "^3.27.0",
|
|
24
|
-
"semver": "^7.6.3"
|
|
24
|
+
"semver": "^7.6.3",
|
|
25
|
+
"varlock": "1.0.0"
|
|
25
26
|
},
|
|
26
27
|
"devDependencies": {
|
|
27
28
|
"@types/js-yaml": "^4.0.9",
|
|
@@ -42,5 +43,8 @@
|
|
|
42
43
|
"cloud"
|
|
43
44
|
],
|
|
44
45
|
"license": "MIT",
|
|
45
|
-
"type": "module"
|
|
46
|
+
"type": "module",
|
|
47
|
+
"engines": {
|
|
48
|
+
"node": ">=22.0.0"
|
|
49
|
+
}
|
|
46
50
|
}
|