@contrail/cli 3.6.0-alpha-8-alpha.694fd70 → 3.6.0-alpha.693d785
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/README.md +45 -151
- package/dist/api/feature-flags.d.ts +3 -0
- package/dist/api/feature-flags.js +29 -0
- package/dist/api/types.d.ts +0 -35
- package/dist/api/types.js +1 -49
- package/dist/api/users.d.ts +1 -0
- package/dist/api/users.js +10 -1
- package/dist/auth/login.d.ts +4 -0
- package/dist/auth/login.js +167 -93
- package/dist/auth/login.spec.js +305 -0
- package/dist/auth/sso.d.ts +36 -0
- package/dist/auth/sso.js +168 -0
- package/dist/auth/sso.spec.js +138 -0
- package/dist/basecommand.d.ts +1 -3
- package/dist/basecommand.js +2 -2
- package/dist/commands/app/getApiKey.js +2 -2
- package/dist/commands/config/set.js +1 -1
- package/dist/commands/feature-flags/export.d.ts +19 -0
- package/dist/commands/feature-flags/export.js +66 -0
- package/dist/commands/feature-flags/export.spec.js +109 -0
- package/dist/commands/feature-flags/import.d.ts +42 -0
- package/dist/commands/feature-flags/import.js +191 -0
- package/dist/commands/feature-flags/import.spec.js +194 -0
- package/dist/commands/instance/export.d.ts +9 -0
- package/dist/commands/instance/export.js +18 -3
- package/dist/commands/instance/export.spec.js +32 -0
- package/dist/commands/instance/import.d.ts +3 -0
- package/dist/commands/instance/import.js +5 -0
- package/dist/commands/instance/import.spec.js +4 -0
- package/dist/commands/login.d.ts +12 -0
- package/dist/commands/login.js +25 -0
- package/dist/common.d.ts +1 -3
- package/dist/common.js +1 -13
- package/dist/helpers/config/config.d.ts +5 -4
- package/dist/helpers/config/config.js +10 -12
- package/dist/helpers/file-helpers.d.ts +0 -1
- package/dist/helpers/file-helpers.js +1 -18
- package/dist/logger.js +1 -4
- package/node_modules/@contrail/entity-compare/package.json +3 -6
- package/node_modules/@contrail/loader/lib/common/conditional-columns.js +1 -1
- package/node_modules/@contrail/loader/lib/constants.d.ts +8 -0
- package/node_modules/@contrail/loader/lib/constants.js +9 -1
- package/node_modules/@contrail/loader/lib/entities-v2/linesheet-v2/item-processor.d.ts +4 -0
- package/node_modules/@contrail/loader/lib/entities-v2/linesheet-v2/item-processor.js +57 -0
- package/node_modules/@contrail/loader/lib/entities-v2/linesheet-v2/item-processor.spec.js +492 -34
- package/node_modules/@contrail/loader/lib/pipeline/validate.js +2 -52
- package/node_modules/@contrail/loader/lib/pipeline/validate.spec.js +0 -85
- package/node_modules/@contrail/loader/package.json +2 -2
- package/node_modules/@contrail/sdk/.github/workflows/publish-to-npm.yml +4 -1
- package/node_modules/@contrail/sdk/lib/auth/login.js +1 -1
- package/node_modules/@contrail/sdk/lib/config/config.d.ts +0 -3
- package/node_modules/@contrail/sdk/lib/config/config.js +7 -13
- package/node_modules/@contrail/sdk/lib/core/request.js +2 -6
- package/node_modules/@contrail/sdk/lib/entities/entities.client.d.ts +0 -2
- package/node_modules/@contrail/sdk/lib/entities/entities.client.js +1 -3
- package/node_modules/@contrail/sdk/lib/entities/entity.resource.registry.js +0 -1
- package/node_modules/@contrail/sdk/package.json +1 -1
- package/oclif.manifest.json +75 -365
- package/package.json +15 -5
- package/dist/commands/entities/create.d.ts +0 -48
- package/dist/commands/entities/create.js +0 -639
- package/dist/commands/entities/create.spec.js +0 -541
- package/dist/commands/entities/delete.d.ts +0 -19
- package/dist/commands/entities/delete.js +0 -134
- package/dist/commands/entities/get.d.ts +0 -24
- package/dist/commands/entities/get.js +0 -273
- package/dist/commands/entities/grammar.spec.js +0 -80
- package/dist/commands/entities/output.spec.js +0 -72
- package/dist/commands/entities/update.d.ts +0 -31
- package/dist/commands/entities/update.js +0 -157
- package/dist/helpers/entities/assortment-items.d.ts +0 -10
- package/dist/helpers/entities/assortment-items.js +0 -84
- package/dist/helpers/entities/batchFetch.d.ts +0 -8
- package/dist/helpers/entities/batchFetch.js +0 -49
- package/dist/helpers/entities/index.d.ts +0 -3
- package/dist/helpers/entities/index.js +0 -6
- package/dist/helpers/entities/items.d.ts +0 -10
- package/dist/helpers/entities/items.js +0 -102
- package/dist/helpers/entities/project-items.d.ts +0 -3
- package/dist/helpers/entities/project-items.js +0 -65
- package/dist/helpers/propertySpecifications/dateRanges.d.ts +0 -38
- package/dist/helpers/propertySpecifications/dateRanges.js +0 -89
- package/dist/helpers/propertySpecifications/dateRanges.spec.js +0 -95
- package/dist/helpers/propertySpecifications/propertySpecifications.d.ts +0 -40
- package/dist/helpers/propertySpecifications/propertySpecifications.js +0 -109
- package/dist/helpers/propertySpecifications/propertySpecifications.spec.d.ts +0 -1
- package/dist/helpers/propertySpecifications/propertySpecifications.spec.js +0 -373
- package/dist/logger.spec.d.ts +0 -1
- package/dist/logger.spec.js +0 -12
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/.github/pull_request_template.md +0 -31
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/.github/workflows/check-code-formatting.yml +0 -34
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/.github/workflows/empty.yml +0 -10
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/.github/workflows/generate-sdk-docs.yml +0 -32
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/.github/workflows/publish-to-npm.yml +0 -117
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/.github/workflows/sdk-lib.yml +0 -27
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/.prettierrc.yaml +0 -2
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/README.md +0 -72
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/home-hero.svg +0 -11
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/assets/assets.d.ts +0 -9
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/assets/assets.js +0 -54
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/assets/index.d.ts +0 -1
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/assets/index.js +0 -17
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/auth/auth.d.ts +0 -10
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/auth/auth.js +0 -6
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/auth/index.d.ts +0 -2
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/auth/index.js +0 -18
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/auth/login.d.ts +0 -10
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/auth/login.js +0 -209
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/config/config.d.ts +0 -48
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/config/config.js +0 -42
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/config/index.d.ts +0 -1
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/config/index.js +0 -17
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/content/content.d.ts +0 -12
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/content/content.js +0 -70
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/content/index.d.ts +0 -1
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/content/index.js +0 -17
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/core/index.d.ts +0 -1
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/core/index.js +0 -17
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/core/request.d.ts +0 -15
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/core/request.js +0 -241
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/entities/entities.client.d.ts +0 -101
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/entities/entities.client.js +0 -306
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/entities/entity-reference-expander.d.ts +0 -9
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/entities/entity-reference-expander.js +0 -86
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/entities/entity-reference.d.ts +0 -6
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/entities/entity-reference.js +0 -19
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/entities/entity.resource.registry.d.ts +0 -5
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/entities/entity.resource.registry.js +0 -59
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/entities/index.d.ts +0 -3
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/entities/index.js +0 -19
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/files/file-type-helper.d.ts +0 -3
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/files/file-type-helper.js +0 -24
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/files/files.d.ts +0 -19
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/files/files.js +0 -151
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/files/index.d.ts +0 -1
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/files/index.js +0 -17
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/index.d.ts +0 -10
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/index.js +0 -26
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/notifications/index.d.ts +0 -1
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/notifications/index.js +0 -17
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/notifications/notifications-dispatcher.d.ts +0 -16
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/notifications/notifications-dispatcher.js +0 -37
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/policies/index.d.ts +0 -1
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/policies/index.js +0 -17
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/policies/policies-cache.d.ts +0 -12
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/policies/policies-cache.js +0 -55
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/policies/policies.client.d.ts +0 -9
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/policies/policies.client.js +0 -74
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/types/index.d.ts +0 -2
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/types/index.js +0 -18
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/types/sub-types-helper.d.ts +0 -18
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/types/sub-types-helper.js +0 -98
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/types/types-cache.d.ts +0 -19
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/types/types-cache.js +0 -73
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/types/types.client.d.ts +0 -42
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/lib/types/types.client.js +0 -217
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/package.json +0 -64
- package/node_modules/@contrail/org-config-migration/node_modules/@contrail/sdk/scripts/replicate-bug.js +0 -69
- package/node_modules/@contrail/sdk/scripts/replicate-bug.js +0 -69
- /package/dist/{commands/entities/create.spec.d.ts → auth/sso.spec.d.ts} +0 -0
- /package/dist/commands/{entities/grammar.spec.d.ts → feature-flags/export.spec.d.ts} +0 -0
- /package/dist/commands/{entities/output.spec.d.ts → feature-flags/import.spec.d.ts} +0 -0
- /package/dist/{helpers/propertySpecifications/dateRanges.spec.d.ts → commands/instance/export.spec.d.ts} +0 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
export declare const LOOPBACK_PORT = 7731;
|
|
3
|
+
export declare const LOOPBACK_CALLBACK_URL: string;
|
|
4
|
+
export interface SsoClient {
|
|
5
|
+
domain: string;
|
|
6
|
+
clientId: string;
|
|
7
|
+
}
|
|
8
|
+
export interface SsoTokens {
|
|
9
|
+
accessToken: string;
|
|
10
|
+
refreshToken: string;
|
|
11
|
+
}
|
|
12
|
+
export interface PkceChallenge {
|
|
13
|
+
verifier: string;
|
|
14
|
+
challenge: string;
|
|
15
|
+
state: string;
|
|
16
|
+
}
|
|
17
|
+
export type SsoDomainCheck = {
|
|
18
|
+
kind: 'cognito';
|
|
19
|
+
identityProvider: string;
|
|
20
|
+
} | {
|
|
21
|
+
kind: 'legacy';
|
|
22
|
+
} | {
|
|
23
|
+
kind: 'none';
|
|
24
|
+
};
|
|
25
|
+
export declare function fetchSsoDomainCheck(email: string, apiGateway: string): Promise<SsoDomainCheck>;
|
|
26
|
+
export declare function createPkceChallenge(): PkceChallenge;
|
|
27
|
+
export declare function buildAuthorizeUrl(client: SsoClient, identityProvider: string, redirectUri: string, pkce: PkceChallenge): string;
|
|
28
|
+
export declare function parsePastedSignInCode(pasted: string, expectedState: string): string;
|
|
29
|
+
export interface LoopbackListener {
|
|
30
|
+
code: Promise<string>;
|
|
31
|
+
}
|
|
32
|
+
export declare function listenForLoopbackCode(expectedState: string): Promise<LoopbackListener>;
|
|
33
|
+
export declare function canOpenBrowser(env?: NodeJS.ProcessEnv, platform?: NodeJS.Platform): boolean;
|
|
34
|
+
export declare function openBrowser(url: string): Promise<void>;
|
|
35
|
+
export declare function exchangeCodeForTokens(client: SsoClient, code: string, redirectUri: string, verifier: string): Promise<SsoTokens>;
|
|
36
|
+
export declare function refreshSsoAccessToken(client: SsoClient, refreshToken: string): Promise<string>;
|
package/dist/auth/sso.js
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.refreshSsoAccessToken = exports.exchangeCodeForTokens = exports.openBrowser = exports.canOpenBrowser = exports.listenForLoopbackCode = exports.parsePastedSignInCode = exports.buildAuthorizeUrl = exports.createPkceChallenge = exports.fetchSsoDomainCheck = exports.LOOPBACK_CALLBACK_URL = exports.LOOPBACK_PORT = void 0;
|
|
4
|
+
const node_crypto_1 = require("node:crypto");
|
|
5
|
+
const node_http_1 = require("node:http");
|
|
6
|
+
const node_child_process_1 = require("node:child_process");
|
|
7
|
+
exports.LOOPBACK_PORT = 7731;
|
|
8
|
+
exports.LOOPBACK_CALLBACK_URL = `http://localhost:${exports.LOOPBACK_PORT}/callback`;
|
|
9
|
+
const BROWSER_SIGN_IN_TIMEOUT_MS = 5 * 60 * 1000;
|
|
10
|
+
const OAUTH_SCOPES = ['openid', 'email', 'api.vibeiq.default/read', 'api.vibeiq.default/write'];
|
|
11
|
+
async function fetchSsoDomainCheck(email, apiGateway) {
|
|
12
|
+
const domain = email.split('@').pop().toLowerCase();
|
|
13
|
+
const response = await fetch(`${apiGateway}/sso-login/domain-check/${encodeURIComponent(domain)}`);
|
|
14
|
+
if (!response.ok) {
|
|
15
|
+
throw new Error(`SSO lookup for ${domain} failed (${response.status}).`);
|
|
16
|
+
}
|
|
17
|
+
const body = await response.text();
|
|
18
|
+
if (!body) {
|
|
19
|
+
return { kind: 'none' };
|
|
20
|
+
}
|
|
21
|
+
const { identityProvider } = JSON.parse(body);
|
|
22
|
+
return identityProvider ? { kind: 'cognito', identityProvider } : { kind: 'legacy' };
|
|
23
|
+
}
|
|
24
|
+
exports.fetchSsoDomainCheck = fetchSsoDomainCheck;
|
|
25
|
+
function createPkceChallenge() {
|
|
26
|
+
const verifier = (0, node_crypto_1.randomBytes)(32).toString('base64url');
|
|
27
|
+
return {
|
|
28
|
+
verifier,
|
|
29
|
+
challenge: (0, node_crypto_1.createHash)('sha256').update(verifier).digest('base64url'),
|
|
30
|
+
state: (0, node_crypto_1.randomBytes)(16).toString('base64url'),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
exports.createPkceChallenge = createPkceChallenge;
|
|
34
|
+
function buildAuthorizeUrl(client, identityProvider, redirectUri, pkce) {
|
|
35
|
+
const params = new URLSearchParams({
|
|
36
|
+
response_type: 'code',
|
|
37
|
+
client_id: client.clientId,
|
|
38
|
+
redirect_uri: redirectUri,
|
|
39
|
+
scope: OAUTH_SCOPES.join(' '),
|
|
40
|
+
identity_provider: identityProvider,
|
|
41
|
+
code_challenge: pkce.challenge,
|
|
42
|
+
code_challenge_method: 'S256',
|
|
43
|
+
state: pkce.state,
|
|
44
|
+
});
|
|
45
|
+
return `${client.domain}/oauth2/authorize?${params}`;
|
|
46
|
+
}
|
|
47
|
+
exports.buildAuthorizeUrl = buildAuthorizeUrl;
|
|
48
|
+
function parsePastedSignInCode(pasted, expectedState) {
|
|
49
|
+
const [code, state] = pasted.trim().split('#');
|
|
50
|
+
if (!code || state !== expectedState) {
|
|
51
|
+
throw new Error('That sign-in code is not from this login attempt. Run the command again.');
|
|
52
|
+
}
|
|
53
|
+
return code;
|
|
54
|
+
}
|
|
55
|
+
exports.parsePastedSignInCode = parsePastedSignInCode;
|
|
56
|
+
function listenForLoopbackCode(expectedState) {
|
|
57
|
+
let resolveCode;
|
|
58
|
+
let rejectCode;
|
|
59
|
+
const code = new Promise((resolve, reject) => {
|
|
60
|
+
resolveCode = resolve;
|
|
61
|
+
rejectCode = reject;
|
|
62
|
+
});
|
|
63
|
+
const server = (0, node_http_1.createServer)((request, response) => {
|
|
64
|
+
const url = new URL(request.url, exports.LOOPBACK_CALLBACK_URL);
|
|
65
|
+
if (url.pathname !== '/callback') {
|
|
66
|
+
response.writeHead(404).end();
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (url.searchParams.get('state') !== expectedState) {
|
|
70
|
+
response.writeHead(400, { 'Content-Type': 'text/plain' }).end('Unknown sign-in attempt.');
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const error = url.searchParams.get('error_description') ?? url.searchParams.get('error');
|
|
74
|
+
const receivedCode = url.searchParams.get('code');
|
|
75
|
+
const signedIn = !error && Boolean(receivedCode);
|
|
76
|
+
const page = signedIn
|
|
77
|
+
? 'Sign-in received. Return to your terminal.'
|
|
78
|
+
: 'Sign-in failed. Return to your terminal.';
|
|
79
|
+
response
|
|
80
|
+
.writeHead(signedIn ? 200 : 400, { 'Content-Type': 'text/plain', Connection: 'close' })
|
|
81
|
+
.end(page, () => stopListening(() => {
|
|
82
|
+
if (error) {
|
|
83
|
+
rejectCode(new Error(`SSO sign-in failed: ${error}`));
|
|
84
|
+
}
|
|
85
|
+
else if (!signedIn) {
|
|
86
|
+
rejectCode(new Error('SSO sign-in returned an unexpected response. Run the command again.'));
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
resolveCode(receivedCode);
|
|
90
|
+
}
|
|
91
|
+
}));
|
|
92
|
+
});
|
|
93
|
+
const timeout = setTimeout(() => stopListening(() => rejectCode(new Error('Timed out waiting for the browser sign-in.'))), BROWSER_SIGN_IN_TIMEOUT_MS);
|
|
94
|
+
function stopListening(settle) {
|
|
95
|
+
clearTimeout(timeout);
|
|
96
|
+
server.close();
|
|
97
|
+
settle();
|
|
98
|
+
}
|
|
99
|
+
return new Promise((resolve, reject) => {
|
|
100
|
+
server.once('error', (error) => {
|
|
101
|
+
clearTimeout(timeout);
|
|
102
|
+
reject(error.code === 'EADDRINUSE'
|
|
103
|
+
? new Error(`Port ${exports.LOOPBACK_PORT} is in use. Run contrail login --no-browser instead.`)
|
|
104
|
+
: error);
|
|
105
|
+
});
|
|
106
|
+
server.listen(exports.LOOPBACK_PORT, 'localhost', () => resolve({ code }));
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
exports.listenForLoopbackCode = listenForLoopbackCode;
|
|
110
|
+
function canOpenBrowser(env = process.env, platform = process.platform) {
|
|
111
|
+
const isRemoteShell = Boolean(env.SSH_CONNECTION || env.SSH_TTY);
|
|
112
|
+
const hasLinuxDisplay = Boolean(env.DISPLAY || env.WAYLAND_DISPLAY);
|
|
113
|
+
return !isRemoteShell && (platform !== 'linux' || hasLinuxDisplay);
|
|
114
|
+
}
|
|
115
|
+
exports.canOpenBrowser = canOpenBrowser;
|
|
116
|
+
function openBrowser(url) {
|
|
117
|
+
const [command, args] = buildOpenBrowserCommand(url, process.platform);
|
|
118
|
+
return new Promise((resolve, reject) => {
|
|
119
|
+
const browserProcess = (0, node_child_process_1.spawn)(command, args, { stdio: 'ignore', detached: true });
|
|
120
|
+
browserProcess.once('spawn', () => {
|
|
121
|
+
browserProcess.unref();
|
|
122
|
+
resolve();
|
|
123
|
+
});
|
|
124
|
+
browserProcess.once('error', reject);
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
exports.openBrowser = openBrowser;
|
|
128
|
+
function buildOpenBrowserCommand(url, platform) {
|
|
129
|
+
if (platform === 'darwin') {
|
|
130
|
+
return ['open', [url]];
|
|
131
|
+
}
|
|
132
|
+
if (platform === 'win32') {
|
|
133
|
+
return ['rundll32', ['url.dll,FileProtocolHandler', url]];
|
|
134
|
+
}
|
|
135
|
+
return ['xdg-open', [url]];
|
|
136
|
+
}
|
|
137
|
+
async function exchangeCodeForTokens(client, code, redirectUri, verifier) {
|
|
138
|
+
const tokens = await postToTokenEndpoint(client, {
|
|
139
|
+
grant_type: 'authorization_code',
|
|
140
|
+
client_id: client.clientId,
|
|
141
|
+
code,
|
|
142
|
+
redirect_uri: redirectUri,
|
|
143
|
+
code_verifier: verifier,
|
|
144
|
+
});
|
|
145
|
+
return { accessToken: tokens.access_token, refreshToken: tokens.refresh_token };
|
|
146
|
+
}
|
|
147
|
+
exports.exchangeCodeForTokens = exchangeCodeForTokens;
|
|
148
|
+
async function refreshSsoAccessToken(client, refreshToken) {
|
|
149
|
+
const tokens = await postToTokenEndpoint(client, {
|
|
150
|
+
grant_type: 'refresh_token',
|
|
151
|
+
client_id: client.clientId,
|
|
152
|
+
refresh_token: refreshToken,
|
|
153
|
+
});
|
|
154
|
+
return tokens.access_token;
|
|
155
|
+
}
|
|
156
|
+
exports.refreshSsoAccessToken = refreshSsoAccessToken;
|
|
157
|
+
async function postToTokenEndpoint(client, form) {
|
|
158
|
+
const response = await fetch(`${client.domain}/oauth2/token`, {
|
|
159
|
+
method: 'POST',
|
|
160
|
+
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
161
|
+
body: new URLSearchParams(form),
|
|
162
|
+
});
|
|
163
|
+
const body = await response.json();
|
|
164
|
+
if (!response.ok) {
|
|
165
|
+
throw new Error(`SSO token request failed (${response.status} ${body.error}).`);
|
|
166
|
+
}
|
|
167
|
+
return body;
|
|
168
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const node_crypto_1 = require("node:crypto");
|
|
4
|
+
const sso_1 = require("./sso");
|
|
5
|
+
const ssoClient = {
|
|
6
|
+
domain: 'https://dev-vibeiq.auth.us-east-1.amazoncognito.com',
|
|
7
|
+
clientId: 'cli-client',
|
|
8
|
+
};
|
|
9
|
+
describe('createPkceChallenge', () => {
|
|
10
|
+
it('should derive the challenge as the base64url SHA-256 of the verifier', () => {
|
|
11
|
+
const pkce = (0, sso_1.createPkceChallenge)();
|
|
12
|
+
expect(pkce.challenge).toBe((0, node_crypto_1.createHash)('sha256').update(pkce.verifier).digest('base64url'));
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
describe('buildAuthorizeUrl', () => {
|
|
16
|
+
it('should send the user straight to their identity provider with a PKCE challenge', () => {
|
|
17
|
+
const url = new URL((0, sso_1.buildAuthorizeUrl)(ssoClient, 'sso-vibeiq-com', sso_1.LOOPBACK_CALLBACK_URL, {
|
|
18
|
+
verifier: 'verifier',
|
|
19
|
+
challenge: 'challenge',
|
|
20
|
+
state: 'state',
|
|
21
|
+
}));
|
|
22
|
+
expect(url.origin + url.pathname).toBe(`${ssoClient.domain}/oauth2/authorize`);
|
|
23
|
+
expect(Object.fromEntries(url.searchParams)).toEqual({
|
|
24
|
+
response_type: 'code',
|
|
25
|
+
client_id: 'cli-client',
|
|
26
|
+
redirect_uri: 'http://localhost:7731/callback',
|
|
27
|
+
scope: 'openid email api.vibeiq.default/read api.vibeiq.default/write',
|
|
28
|
+
identity_provider: 'sso-vibeiq-com',
|
|
29
|
+
code_challenge: 'challenge',
|
|
30
|
+
code_challenge_method: 'S256',
|
|
31
|
+
state: 'state',
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
describe('parsePastedSignInCode', () => {
|
|
36
|
+
it('should return the code when the pasted state matches this attempt', () => {
|
|
37
|
+
expect((0, sso_1.parsePastedSignInCode)(' the-code#the-state \n', 'the-state')).toBe('the-code');
|
|
38
|
+
});
|
|
39
|
+
it('should reject a code from another attempt', () => {
|
|
40
|
+
expect(() => (0, sso_1.parsePastedSignInCode)('the-code#other-state', 'the-state')).toThrow('That sign-in code is not from this login attempt.');
|
|
41
|
+
});
|
|
42
|
+
it('should reject a code pasted without its state', () => {
|
|
43
|
+
expect(() => (0, sso_1.parsePastedSignInCode)('the-code', 'the-state')).toThrow('That sign-in code is not from this login attempt.');
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
describe('canOpenBrowser', () => {
|
|
47
|
+
it('should open a browser on a local Mac', () => {
|
|
48
|
+
expect((0, sso_1.canOpenBrowser)({}, 'darwin')).toBe(true);
|
|
49
|
+
});
|
|
50
|
+
it('should not open a browser over SSH', () => {
|
|
51
|
+
expect((0, sso_1.canOpenBrowser)({ SSH_CONNECTION: '10.0.0.1 22 10.0.0.2 22' }, 'darwin')).toBe(false);
|
|
52
|
+
});
|
|
53
|
+
it('should not open a browser on Linux without a display', () => {
|
|
54
|
+
expect((0, sso_1.canOpenBrowser)({}, 'linux')).toBe(false);
|
|
55
|
+
});
|
|
56
|
+
it('should open a browser on a Linux desktop', () => {
|
|
57
|
+
expect((0, sso_1.canOpenBrowser)({ WAYLAND_DISPLAY: 'wayland-0' }, 'linux')).toBe(true);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
describe('fetchSsoDomainCheck', () => {
|
|
61
|
+
const originalFetch = global.fetch;
|
|
62
|
+
afterEach(() => {
|
|
63
|
+
global.fetch = originalFetch;
|
|
64
|
+
});
|
|
65
|
+
function stubDomainCheckFetch(body, status = 200) {
|
|
66
|
+
global.fetch = jest.fn().mockResolvedValue(new Response(body, { status }));
|
|
67
|
+
}
|
|
68
|
+
it('should name the Cognito identity provider for an SSO domain', async () => {
|
|
69
|
+
stubDomainCheckFetch(JSON.stringify({ identityProvider: 'sso-vibeiq-com' }));
|
|
70
|
+
await expect((0, sso_1.fetchSsoDomainCheck)('Dev@VibeIQ.com', 'https://api')).resolves.toEqual({
|
|
71
|
+
kind: 'cognito',
|
|
72
|
+
identityProvider: 'sso-vibeiq-com',
|
|
73
|
+
});
|
|
74
|
+
expect(global.fetch).toHaveBeenCalledWith('https://api/sso-login/domain-check/vibeiq.com');
|
|
75
|
+
});
|
|
76
|
+
it('should report a legacy SSO domain', async () => {
|
|
77
|
+
stubDomainCheckFetch(JSON.stringify({ identityProvider: '', url: 'https://api/sso-login/login/acme.com' }));
|
|
78
|
+
await expect((0, sso_1.fetchSsoDomainCheck)('dev@acme.com', 'https://api')).resolves.toEqual({
|
|
79
|
+
kind: 'legacy',
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
it('should report no SSO for an empty response', async () => {
|
|
83
|
+
stubDomainCheckFetch('');
|
|
84
|
+
await expect((0, sso_1.fetchSsoDomainCheck)('dev@example.com', 'https://api')).resolves.toEqual({
|
|
85
|
+
kind: 'none',
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
it('should fail when the lookup fails', async () => {
|
|
89
|
+
stubDomainCheckFetch('{}', 502);
|
|
90
|
+
await expect((0, sso_1.fetchSsoDomainCheck)('dev@example.com', 'https://api')).rejects.toThrow('SSO lookup for example.com failed (502).');
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
describe('listenForLoopbackCode', () => {
|
|
94
|
+
it('should resolve with the code the browser redirects back with', async () => {
|
|
95
|
+
const listener = await (0, sso_1.listenForLoopbackCode)('the-state');
|
|
96
|
+
const response = await fetch(`${sso_1.LOOPBACK_CALLBACK_URL}?code=the-code&state=the-state`);
|
|
97
|
+
expect(response.status).toBe(200);
|
|
98
|
+
await expect(listener.code).resolves.toBe('the-code');
|
|
99
|
+
});
|
|
100
|
+
it('should keep waiting for this attempt after a redirect from another attempt', async () => {
|
|
101
|
+
const listener = await (0, sso_1.listenForLoopbackCode)('the-state');
|
|
102
|
+
const staleResponse = await fetch(`${sso_1.LOOPBACK_CALLBACK_URL}?code=old-code&state=old-state`);
|
|
103
|
+
await fetch(`${sso_1.LOOPBACK_CALLBACK_URL}?code=the-code&state=the-state`);
|
|
104
|
+
expect(staleResponse.status).toBe(400);
|
|
105
|
+
await expect(listener.code).resolves.toBe('the-code');
|
|
106
|
+
});
|
|
107
|
+
it('should reject a redirect for this attempt that carries no code', async () => {
|
|
108
|
+
const listener = await (0, sso_1.listenForLoopbackCode)('the-state');
|
|
109
|
+
const rejection = expect(listener.code).rejects.toThrow('SSO sign-in returned an unexpected response.');
|
|
110
|
+
const response = await fetch(`${sso_1.LOOPBACK_CALLBACK_URL}?state=the-state`);
|
|
111
|
+
expect(response.status).toBe(400);
|
|
112
|
+
await rejection;
|
|
113
|
+
});
|
|
114
|
+
it('should reject when the identity provider reports an error', async () => {
|
|
115
|
+
const listener = await (0, sso_1.listenForLoopbackCode)('the-state');
|
|
116
|
+
const rejection = expect(listener.code).rejects.toThrow('SSO sign-in failed: User cancelled');
|
|
117
|
+
await fetch(`${sso_1.LOOPBACK_CALLBACK_URL}?error=access_denied&error_description=User%20cancelled&state=the-state`);
|
|
118
|
+
await rejection;
|
|
119
|
+
});
|
|
120
|
+
it('should tell the user to paste a code instead when the port is taken', async () => {
|
|
121
|
+
const listener = await (0, sso_1.listenForLoopbackCode)('the-state');
|
|
122
|
+
await expect((0, sso_1.listenForLoopbackCode)('another-state')).rejects.toThrow('Port 7731 is in use. Run contrail login --no-browser instead.');
|
|
123
|
+
await fetch(`${sso_1.LOOPBACK_CALLBACK_URL}?code=the-code&state=the-state`);
|
|
124
|
+
await listener.code;
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
describe('exchangeCodeForTokens', () => {
|
|
128
|
+
const originalFetch = global.fetch;
|
|
129
|
+
afterEach(() => {
|
|
130
|
+
global.fetch = originalFetch;
|
|
131
|
+
});
|
|
132
|
+
it('should name the Cognito error when the exchange is refused', async () => {
|
|
133
|
+
global.fetch = jest
|
|
134
|
+
.fn()
|
|
135
|
+
.mockResolvedValue(new Response(JSON.stringify({ error: 'invalid_grant' }), { status: 400 }));
|
|
136
|
+
await expect((0, sso_1.exchangeCodeForTokens)(ssoClient, 'the-code', sso_1.LOOPBACK_CALLBACK_URL, 'verifier')).rejects.toThrow('SSO token request failed (400 invalid_grant).');
|
|
137
|
+
});
|
|
138
|
+
});
|
package/dist/basecommand.d.ts
CHANGED
|
@@ -8,9 +8,7 @@ export declare abstract class BaseCommand extends Command {
|
|
|
8
8
|
logLevel: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
9
9
|
};
|
|
10
10
|
protected flags: any;
|
|
11
|
-
protected args:
|
|
12
|
-
[key: string]: any;
|
|
13
|
-
};
|
|
11
|
+
protected args: any;
|
|
14
12
|
init(): Promise<void>;
|
|
15
13
|
setLogLevel(logLevel: string, config?: any): void;
|
|
16
14
|
private confirmEnv;
|
package/dist/basecommand.js
CHANGED
|
@@ -17,7 +17,7 @@ class BaseCommand extends core_1.Command {
|
|
|
17
17
|
...baseFlags_1.baseFlags,
|
|
18
18
|
};
|
|
19
19
|
flags;
|
|
20
|
-
args
|
|
20
|
+
args;
|
|
21
21
|
async init() {
|
|
22
22
|
await super.init();
|
|
23
23
|
if (!this.ctor?.flags)
|
|
@@ -60,7 +60,7 @@ class BaseCommand extends core_1.Command {
|
|
|
60
60
|
const data = (0, lodash_1.pick)(config, ['orgSlug', 'apiGateway']);
|
|
61
61
|
let env = '';
|
|
62
62
|
if (data.apiGateway) {
|
|
63
|
-
env =
|
|
63
|
+
env = (0, config_1.findEnvironmentName)(data.apiGateway);
|
|
64
64
|
}
|
|
65
65
|
data.environment = env;
|
|
66
66
|
delete data.apiGateway;
|
|
@@ -35,7 +35,7 @@ class GetAPIKeyApp extends basecommand_1.BaseCommand {
|
|
|
35
35
|
},
|
|
36
36
|
];
|
|
37
37
|
const answers = await inquirer_1.default.prompt(questions);
|
|
38
|
-
|
|
38
|
+
logger_1.default.info(`App API key: (app:${answers.appApiKey})`);
|
|
39
39
|
}
|
|
40
40
|
else {
|
|
41
41
|
const appFound = installedApps.find((x) => x.app.identifier === this.flags.appIdentifier);
|
|
@@ -43,7 +43,7 @@ class GetAPIKeyApp extends basecommand_1.BaseCommand {
|
|
|
43
43
|
logger_1.default.info('You do not have such an app installed');
|
|
44
44
|
return;
|
|
45
45
|
}
|
|
46
|
-
|
|
46
|
+
logger_1.default.info(`App API key: (app:${appFound.apiKey.apiKey})`);
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
}
|
|
@@ -15,7 +15,7 @@ class SetConfig extends basecommand_1.BaseCommand {
|
|
|
15
15
|
environment: core_1.Flags.string({
|
|
16
16
|
description: 'The Environment',
|
|
17
17
|
required: false,
|
|
18
|
-
options: ['dev', 'development', 'prod', 'production'
|
|
18
|
+
options: ['dev', 'development', 'prod', 'production'],
|
|
19
19
|
}),
|
|
20
20
|
apiUserToken: core_1.Flags.string({
|
|
21
21
|
description: 'The API User Token',
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { BaseCommand } from '../../basecommand';
|
|
2
|
+
export declare const FEATURE_FLAGS_FILE = "feature-flags.yml";
|
|
3
|
+
export default class ExportFeatureFlags extends BaseCommand {
|
|
4
|
+
static description: string;
|
|
5
|
+
static examples: string[];
|
|
6
|
+
static flags: {
|
|
7
|
+
outDir: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
8
|
+
overwrite: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
9
|
+
alwaysConfirmEnv: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
10
|
+
logLevel: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
11
|
+
};
|
|
12
|
+
run(): Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Writes the org's enabled feature flags to $outDir/feature-flags.yml as a list of
|
|
16
|
+
* `- featureName: X` entries. An org with no flags enabled still gets a file (an empty
|
|
17
|
+
* list), so the export stays a faithful snapshot of what is turned on.
|
|
18
|
+
*/
|
|
19
|
+
export declare function exportFeatureFlags(outDir: string, overwrite: boolean): Promise<void>;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.exportFeatureFlags = exports.FEATURE_FLAGS_FILE = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const path = tslib_1.__importStar(require("path"));
|
|
6
|
+
const chalk_1 = tslib_1.__importDefault(require("chalk"));
|
|
7
|
+
const fs_1 = tslib_1.__importDefault(require("fs"));
|
|
8
|
+
const login_1 = require("../../auth/login");
|
|
9
|
+
const core_1 = require("@oclif/core");
|
|
10
|
+
const file_helpers_1 = require("../../helpers/file-helpers");
|
|
11
|
+
const error_helpers_1 = require("../../helpers/error-helpers");
|
|
12
|
+
const feature_flags_1 = require("../../api/feature-flags");
|
|
13
|
+
const basecommand_1 = require("../../basecommand");
|
|
14
|
+
exports.FEATURE_FLAGS_FILE = 'feature-flags.yml';
|
|
15
|
+
class ExportFeatureFlags extends basecommand_1.BaseCommand {
|
|
16
|
+
static description = 'Export the enabled feature flags for your Org';
|
|
17
|
+
static examples = [
|
|
18
|
+
`$ contrail feature-flags export
|
|
19
|
+
`,
|
|
20
|
+
];
|
|
21
|
+
static flags = {
|
|
22
|
+
...basecommand_1.BaseCommand.baseFlags,
|
|
23
|
+
outDir: core_1.Flags.string({
|
|
24
|
+
char: 'd',
|
|
25
|
+
description: 'The directory to which to export the feature flags. Defaults to the current directory',
|
|
26
|
+
required: false,
|
|
27
|
+
default: '.',
|
|
28
|
+
}),
|
|
29
|
+
overwrite: core_1.Flags.boolean({
|
|
30
|
+
char: 'f',
|
|
31
|
+
description: 'Replaces any existing export files in outDir',
|
|
32
|
+
required: false,
|
|
33
|
+
default: false,
|
|
34
|
+
}),
|
|
35
|
+
};
|
|
36
|
+
async run() {
|
|
37
|
+
try {
|
|
38
|
+
await (0, login_1.authUser)();
|
|
39
|
+
this.log(`${chalk_1.default.blue.bold('START')} Getting Feature Flags.`);
|
|
40
|
+
this.log(`${chalk_1.default.yellow.bold('FEATURE FLAGS')} Exporting`);
|
|
41
|
+
await exportFeatureFlags(this.flags.outDir, this.flags.overwrite);
|
|
42
|
+
this.log(`${chalk_1.default.yellow.bold('FEATURE FLAGS')} Exported`);
|
|
43
|
+
}
|
|
44
|
+
catch (e) {
|
|
45
|
+
this.log(`${chalk_1.default.red.bold('FAIL')} Feature Flags Export Failed. ${(0, error_helpers_1.describeError)(e)}`);
|
|
46
|
+
process.exitCode = 1;
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
this.log(`${chalk_1.default.green.bold('COMPLETE')} Feature Flags Export Complete.`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.default = ExportFeatureFlags;
|
|
53
|
+
/**
|
|
54
|
+
* Writes the org's enabled feature flags to $outDir/feature-flags.yml as a list of
|
|
55
|
+
* `- featureName: X` entries. An org with no flags enabled still gets a file (an empty
|
|
56
|
+
* list), so the export stays a faithful snapshot of what is turned on.
|
|
57
|
+
*/
|
|
58
|
+
async function exportFeatureFlags(outDir, overwrite) {
|
|
59
|
+
const featureFlags = await (0, feature_flags_1.getFeatureFlags)();
|
|
60
|
+
const featureNames = [
|
|
61
|
+
...new Set((featureFlags ?? []).map((flag) => flag?.featureName).filter(Boolean)),
|
|
62
|
+
].sort();
|
|
63
|
+
fs_1.default.mkdirSync(path.resolve(outDir), { recursive: true });
|
|
64
|
+
await (0, file_helpers_1.saveData)(featureNames.map((featureName) => ({ featureName })), path.join(outDir, exports.FEATURE_FLAGS_FILE), overwrite);
|
|
65
|
+
}
|
|
66
|
+
exports.exportFeatureFlags = exportFeatureFlags;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const fs_1 = tslib_1.__importDefault(require("fs"));
|
|
5
|
+
const export_1 = tslib_1.__importStar(require("./export"));
|
|
6
|
+
const file_helpers_1 = require("../../helpers/file-helpers");
|
|
7
|
+
const feature_flags_1 = require("../../api/feature-flags");
|
|
8
|
+
const login_1 = require("../../auth/login");
|
|
9
|
+
jest.mock('../../helpers/file-helpers');
|
|
10
|
+
jest.mock('../../api/feature-flags');
|
|
11
|
+
jest.mock('../../auth/login');
|
|
12
|
+
const mocked = (fn) => fn;
|
|
13
|
+
describe('exportFeatureFlags', () => {
|
|
14
|
+
let mkdirSyncSpy;
|
|
15
|
+
beforeEach(() => {
|
|
16
|
+
jest.clearAllMocks();
|
|
17
|
+
mkdirSyncSpy = jest.spyOn(fs_1.default, 'mkdirSync').mockReturnValue(undefined);
|
|
18
|
+
mocked(file_helpers_1.saveData).mockResolvedValue(undefined);
|
|
19
|
+
});
|
|
20
|
+
afterEach(() => {
|
|
21
|
+
mkdirSyncSpy.mockRestore();
|
|
22
|
+
});
|
|
23
|
+
it('writes the enabled flags sorted, without org specific fields', async () => {
|
|
24
|
+
mocked(feature_flags_1.getFeatureFlags).mockResolvedValue([
|
|
25
|
+
{ id: '1', orgId: 'org-a', featureName: 'SHOWROOM' },
|
|
26
|
+
{ id: '2', orgId: 'org-a', featureName: 'DEV_TABS' },
|
|
27
|
+
]);
|
|
28
|
+
await (0, export_1.exportFeatureFlags)('out-dir', true);
|
|
29
|
+
expect(mocked(file_helpers_1.saveData)).toHaveBeenCalledWith([{ featureName: 'DEV_TABS' }, { featureName: 'SHOWROOM' }], 'out-dir/feature-flags.yml', true);
|
|
30
|
+
});
|
|
31
|
+
it('still writes a file when no flags are enabled, so the export is a full snapshot', async () => {
|
|
32
|
+
mocked(feature_flags_1.getFeatureFlags).mockResolvedValue([]);
|
|
33
|
+
await (0, export_1.exportFeatureFlags)('out-dir', false);
|
|
34
|
+
expect(mocked(file_helpers_1.saveData)).toHaveBeenCalledWith([], 'out-dir/feature-flags.yml', false);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
describe('ExportFeatureFlags command', () => {
|
|
38
|
+
let mkdirSyncSpy;
|
|
39
|
+
let logs;
|
|
40
|
+
// oclif's init() parses argv and reads the on-disk config, so drive run() with flags set directly
|
|
41
|
+
const runCommand = async () => {
|
|
42
|
+
const command = new export_1.default([], {});
|
|
43
|
+
command.flags = { outDir: 'out-dir', overwrite: true };
|
|
44
|
+
jest.spyOn(command, 'log').mockImplementation((message) => logs.push(String(message)));
|
|
45
|
+
await command.run();
|
|
46
|
+
};
|
|
47
|
+
beforeEach(() => {
|
|
48
|
+
jest.clearAllMocks();
|
|
49
|
+
logs = [];
|
|
50
|
+
process.exitCode = undefined;
|
|
51
|
+
mkdirSyncSpy = jest.spyOn(fs_1.default, 'mkdirSync').mockReturnValue(undefined);
|
|
52
|
+
mocked(login_1.authUser).mockResolvedValue({});
|
|
53
|
+
mocked(file_helpers_1.saveData).mockResolvedValue(undefined);
|
|
54
|
+
mocked(feature_flags_1.getFeatureFlags).mockResolvedValue([]);
|
|
55
|
+
});
|
|
56
|
+
afterEach(() => {
|
|
57
|
+
mkdirSyncSpy.mockRestore();
|
|
58
|
+
process.exitCode = undefined;
|
|
59
|
+
});
|
|
60
|
+
it('leaves the exit code alone on success', async () => {
|
|
61
|
+
await runCommand();
|
|
62
|
+
expect(process.exitCode).toBeUndefined();
|
|
63
|
+
expect(logs.join('\n')).toContain('Feature Flags Export Complete');
|
|
64
|
+
});
|
|
65
|
+
const failures = [
|
|
66
|
+
[
|
|
67
|
+
'authentication fails',
|
|
68
|
+
() => {
|
|
69
|
+
mocked(login_1.authUser).mockRejectedValue(new Error('boom'));
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
[
|
|
73
|
+
'the API read fails',
|
|
74
|
+
() => {
|
|
75
|
+
mocked(feature_flags_1.getFeatureFlags).mockRejectedValue(new Error('boom'));
|
|
76
|
+
},
|
|
77
|
+
],
|
|
78
|
+
[
|
|
79
|
+
'the directory cannot be created',
|
|
80
|
+
() => {
|
|
81
|
+
mkdirSyncSpy.mockImplementation(() => {
|
|
82
|
+
throw new Error('boom');
|
|
83
|
+
});
|
|
84
|
+
},
|
|
85
|
+
],
|
|
86
|
+
[
|
|
87
|
+
'the file write fails',
|
|
88
|
+
() => {
|
|
89
|
+
mocked(file_helpers_1.saveData).mockRejectedValue(new Error('boom'));
|
|
90
|
+
},
|
|
91
|
+
],
|
|
92
|
+
];
|
|
93
|
+
it.each(failures)('exits non-zero when %s, so automation does not read it as a success', async (_, fail) => {
|
|
94
|
+
fail();
|
|
95
|
+
await runCommand();
|
|
96
|
+
expect(process.exitCode).toBe(1);
|
|
97
|
+
const output = logs.join('\n');
|
|
98
|
+
expect(output).toContain('Feature Flags Export Failed');
|
|
99
|
+
expect(output).not.toContain('Feature Flags Export Complete');
|
|
100
|
+
});
|
|
101
|
+
it('reports the API message rather than a bare "Error"', async () => {
|
|
102
|
+
// the SDK's ApiError has an undefined message and keeps the body in details
|
|
103
|
+
mocked(feature_flags_1.getFeatureFlags).mockRejectedValue({
|
|
104
|
+
details: '{"message":"Forbidden resource","statusCode":403}',
|
|
105
|
+
});
|
|
106
|
+
await runCommand();
|
|
107
|
+
expect(logs.join('\n')).toContain('Forbidden resource');
|
|
108
|
+
});
|
|
109
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { BaseCommand } from '../../basecommand';
|
|
2
|
+
export declare const REQUIRED_PLATFORM_ROLES: string[];
|
|
3
|
+
export default class ImportFeatureFlags extends BaseCommand {
|
|
4
|
+
static description: string;
|
|
5
|
+
static examples: string[];
|
|
6
|
+
static flags: {
|
|
7
|
+
dir: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
8
|
+
alwaysConfirmEnv: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
9
|
+
logLevel: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
10
|
+
};
|
|
11
|
+
run(): Promise<void>;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The feature flags API only lets a platform admin/manager create or delete flags, and a
|
|
15
|
+
* missing role surfaces as a generic 403 partway through the run. Checking up front stops
|
|
16
|
+
* the command before it half-applies the file.
|
|
17
|
+
*/
|
|
18
|
+
export declare function assertCanManageFeatureFlags(): Promise<void>;
|
|
19
|
+
/**
|
|
20
|
+
* The `instance import` variant: feature flags are one step of a much larger config copy that
|
|
21
|
+
* org admins without a platform role are expected to run, so a missing role (or roles we could
|
|
22
|
+
* not read) logs and skips instead of failing the whole import. Errors from the flag upload
|
|
23
|
+
* itself still propagate — only the permission check is soft here.
|
|
24
|
+
*
|
|
25
|
+
* `contrail feature-flags import` deliberately does the opposite and fails, because there the
|
|
26
|
+
* flags are the entire point of the command.
|
|
27
|
+
*/
|
|
28
|
+
export declare function uploadFeatureFlagsIfPermitted(dir: string): Promise<void>;
|
|
29
|
+
/**
|
|
30
|
+
* Makes the target org's enabled feature flags match $dir/feature-flags.yml: enables every
|
|
31
|
+
* flag in the file and disables any flag the target has that the file does not, the same
|
|
32
|
+
* way groups import reconciles groups. Feature flags are not editable in place, so there is
|
|
33
|
+
* no update step.
|
|
34
|
+
*
|
|
35
|
+
* Failures are collected and rethrown so an import never reports success while the org is
|
|
36
|
+
* missing flags.
|
|
37
|
+
*
|
|
38
|
+
* A file that lists no flags at all is skipped rather than applied: reconciling against an
|
|
39
|
+
* empty list would disable every flag in the target org, which is almost never what a copy
|
|
40
|
+
* is meant to do. Flags that should be off in the target have to be disabled by hand.
|
|
41
|
+
*/
|
|
42
|
+
export declare function uploadFeatureFlags(dir: string): Promise<void>;
|