@contrail/cli 3.5.6 → 3.5.7-alpha.30ba14d
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 +1 -1
- package/dist/auth/login.js +29 -21
- package/dist/auth/login.spec.js +85 -1
- package/oclif.manifest.json +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -17,7 +17,7 @@ $ npm install -g @contrail/cli
|
|
|
17
17
|
$ contrail COMMAND
|
|
18
18
|
running command...
|
|
19
19
|
$ contrail (--version)
|
|
20
|
-
@contrail/cli/3.5.
|
|
20
|
+
@contrail/cli/3.5.7-alpha.30ba14d linux-x64 node-v24.22.1
|
|
21
21
|
$ contrail --help [COMMAND]
|
|
22
22
|
USAGE
|
|
23
23
|
$ contrail COMMAND
|
package/dist/auth/login.js
CHANGED
|
@@ -37,9 +37,10 @@ async function authUser() {
|
|
|
37
37
|
};
|
|
38
38
|
(0, sdk_1.setConfig)(sdk_1.Auth.config);
|
|
39
39
|
try {
|
|
40
|
-
return await
|
|
40
|
+
return await signInWithStoredTokens();
|
|
41
41
|
}
|
|
42
42
|
catch (e) {
|
|
43
|
+
logger_1.default.debug(`stored tokens rejected: ${e?.message}`);
|
|
43
44
|
if (isNetworkConnectionError(e)) {
|
|
44
45
|
logger_1.default.error("Encountered a network connection issue. Please ensure you're connected to the internet and try again");
|
|
45
46
|
process.exit(1);
|
|
@@ -161,30 +162,32 @@ async function setAuthContext(user, params) {
|
|
|
161
162
|
(0, sdk_1.setConfig)(sdk_1.Auth.config);
|
|
162
163
|
return refreshCurrentUserOrg();
|
|
163
164
|
}
|
|
164
|
-
async function
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
sdk_1.Auth.config = {
|
|
172
|
-
apiUserToken: newVibeAccessToken,
|
|
173
|
-
apiRefreshToken: config.apiRefreshToken,
|
|
174
|
-
apiGateway: config.apiGateway,
|
|
175
|
-
orgSlug: config.orgSlug,
|
|
176
|
-
};
|
|
177
|
-
console.log('fetched new access token from refresh token', newVibeAccessToken);
|
|
178
|
-
}
|
|
179
|
-
catch (e) {
|
|
180
|
-
throw Error('Could not get refresh token');
|
|
165
|
+
async function signInWithStoredTokens() {
|
|
166
|
+
try {
|
|
167
|
+
await (0, sdk_1.getCurrentUser)();
|
|
168
|
+
}
|
|
169
|
+
catch (e) {
|
|
170
|
+
if (isNetworkConnectionError(e)) {
|
|
171
|
+
throw e;
|
|
181
172
|
}
|
|
173
|
+
logger_1.default.debug('access token rejected, refreshing it...');
|
|
174
|
+
await refreshAccessToken();
|
|
182
175
|
}
|
|
176
|
+
return refreshCurrentUserOrg();
|
|
177
|
+
}
|
|
178
|
+
async function refreshCurrentUserOrg() {
|
|
179
|
+
logger_1.default.debug('refreshing current user org...');
|
|
180
|
+
await (0, sdk_1.getCurrentUser)();
|
|
183
181
|
// TODO: To uncomment when telemetry backend is developed.
|
|
184
182
|
// const current_Version = getVersionFromPackageJson();
|
|
185
183
|
// await checkTelemetry(current_Version);
|
|
186
184
|
return returnAndStoreConfig(sdk_1.Auth.config);
|
|
187
185
|
}
|
|
186
|
+
async function refreshAccessToken() {
|
|
187
|
+
const newVibeAccessToken = await requestRefreshedAccessToken(sdk_1.Auth.config.apiRefreshToken, sdk_1.Auth.config.apiGateway);
|
|
188
|
+
sdk_1.Auth.config = { ...sdk_1.Auth.config, apiUserToken: newVibeAccessToken };
|
|
189
|
+
(0, sdk_1.setConfig)(sdk_1.Auth.config);
|
|
190
|
+
}
|
|
188
191
|
function getApiGateway(config) {
|
|
189
192
|
return config?.apiGateway ?? DEFAULT_API_GATEWAY;
|
|
190
193
|
}
|
|
@@ -214,9 +217,14 @@ async function requestRefreshedAccessToken(vibeRefreshToken, apiGateway) {
|
|
|
214
217
|
},
|
|
215
218
|
body: JSON.stringify({ refreshToken: vibeRefreshToken }),
|
|
216
219
|
});
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
}
|
|
220
|
+
if (!response.ok) {
|
|
221
|
+
throw new Error(`Token refresh failed with status ${response.status}`);
|
|
222
|
+
}
|
|
223
|
+
const data = await response.json();
|
|
224
|
+
if (!data.accessToken) {
|
|
225
|
+
throw new Error('Token refresh response did not contain an access token');
|
|
226
|
+
}
|
|
227
|
+
return data.accessToken;
|
|
220
228
|
}
|
|
221
229
|
function getVersionFromPackageJson() {
|
|
222
230
|
// Get the path to the package.json file
|
package/dist/auth/login.spec.js
CHANGED
|
@@ -24,7 +24,7 @@ jest.mock('../helpers/config/config', () => ({
|
|
|
24
24
|
}));
|
|
25
25
|
jest.mock('../logger', () => ({
|
|
26
26
|
__esModule: true,
|
|
27
|
-
default: { debug: jest.fn(), info: jest.fn() },
|
|
27
|
+
default: { debug: jest.fn(), info: jest.fn(), error: jest.fn() },
|
|
28
28
|
}));
|
|
29
29
|
const DEV_GATEWAY = 'https://api.dev.vibeiq.com/dev/api';
|
|
30
30
|
function restoreEnv(name, value) {
|
|
@@ -125,6 +125,90 @@ describe('authUser with email and password', () => {
|
|
|
125
125
|
expect(config_1.storeConfig).not.toHaveBeenCalled();
|
|
126
126
|
});
|
|
127
127
|
});
|
|
128
|
+
describe('authUser with stored tokens', () => {
|
|
129
|
+
const originalFetch = global.fetch;
|
|
130
|
+
const originalEmail = process.env.CONTRAIL_CLI_EMAIL;
|
|
131
|
+
const originalPassword = process.env.CONTRAIL_CLI_PASSWORD;
|
|
132
|
+
const storedConfig = {
|
|
133
|
+
apiUserToken: 'expired-access',
|
|
134
|
+
apiRefreshToken: 'stored-refresh',
|
|
135
|
+
apiGateway: DEV_GATEWAY,
|
|
136
|
+
orgSlug: 'adam-zone-1',
|
|
137
|
+
};
|
|
138
|
+
let acceptedTokens;
|
|
139
|
+
let fetchMock;
|
|
140
|
+
let exitSpy;
|
|
141
|
+
function acceptOnlyTokensGivenToTheSdk() {
|
|
142
|
+
sdk_1.getCurrentUser.mockImplementation(async () => {
|
|
143
|
+
const token = sdk_1.setConfig.mock.calls.at(-1)?.[0]?.apiUserToken;
|
|
144
|
+
if (!acceptedTokens.includes(token)) {
|
|
145
|
+
throw new Error('Failed to fetch current user. Unauthorized');
|
|
146
|
+
}
|
|
147
|
+
return { id: 'user-1' };
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
beforeEach(() => {
|
|
151
|
+
jest.clearAllMocks();
|
|
152
|
+
process.env.CONTRAIL_CLI_EMAIL = 'dev@vibeiq.com';
|
|
153
|
+
process.env.CONTRAIL_CLI_PASSWORD = 'secret';
|
|
154
|
+
config_1.getConfig.mockResolvedValue(storedConfig);
|
|
155
|
+
acceptedTokens = ['refreshed-access', 'vibe-access'];
|
|
156
|
+
acceptOnlyTokensGivenToTheSdk();
|
|
157
|
+
mockAuthenticateUser.mockImplementation((_details, callbacks) => callbacks.onSuccess({ idToken: { jwtToken: 'cognito-id-token' } }));
|
|
158
|
+
fetchMock = jest.fn();
|
|
159
|
+
global.fetch = fetchMock;
|
|
160
|
+
});
|
|
161
|
+
afterEach(() => {
|
|
162
|
+
exitSpy?.mockRestore();
|
|
163
|
+
global.fetch = originalFetch;
|
|
164
|
+
restoreEnv('CONTRAIL_CLI_EMAIL', originalEmail);
|
|
165
|
+
restoreEnv('CONTRAIL_CLI_PASSWORD', originalPassword);
|
|
166
|
+
});
|
|
167
|
+
it('should refresh an expired access token without asking for a password', async () => {
|
|
168
|
+
fetchMock.mockResolvedValueOnce(jsonResponse(200, { accessToken: 'refreshed-access' }));
|
|
169
|
+
const context = await (0, login_1.authUser)();
|
|
170
|
+
expect(fetchMock).toHaveBeenCalledWith(`${DEV_GATEWAY}/auth/jwt_refresh/`, expect.objectContaining({ body: JSON.stringify({ refreshToken: 'stored-refresh' }) }));
|
|
171
|
+
expect(context).toEqual({ ...storedConfig, apiUserToken: 'refreshed-access' });
|
|
172
|
+
expect(config_1.storeConfig).toHaveBeenCalledWith(context);
|
|
173
|
+
});
|
|
174
|
+
it('should keep a still-valid access token without refreshing it', async () => {
|
|
175
|
+
acceptedTokens = ['expired-access'];
|
|
176
|
+
const context = await (0, login_1.authUser)();
|
|
177
|
+
expect(context).toEqual(storedConfig);
|
|
178
|
+
});
|
|
179
|
+
it('should fall back to the password sign-in when the refresh token is rejected', async () => {
|
|
180
|
+
fetchMock
|
|
181
|
+
.mockResolvedValueOnce(jsonResponse(401, { message: 'Unauthorized' }))
|
|
182
|
+
.mockResolvedValueOnce(jsonResponse(200, { accessToken: 'vibe-access', refreshToken: 'vibe-refresh' }));
|
|
183
|
+
const context = await (0, login_1.authUser)();
|
|
184
|
+
expect(context).toEqual({
|
|
185
|
+
...storedConfig,
|
|
186
|
+
apiUserToken: 'vibe-access',
|
|
187
|
+
apiRefreshToken: 'vibe-refresh',
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
it('should report a rejected password sign-in without refreshing the token it was just issued', async () => {
|
|
191
|
+
config_1.getConfig.mockResolvedValue({ apiGateway: DEV_GATEWAY, orgSlug: 'adam-zone-1' });
|
|
192
|
+
acceptedTokens = [];
|
|
193
|
+
fetchMock
|
|
194
|
+
.mockResolvedValueOnce(jsonResponse(200, { accessToken: 'vibe-access', refreshToken: 'vibe-refresh' }))
|
|
195
|
+
.mockResolvedValueOnce(jsonResponse(400, { message: 'Bad Request' }));
|
|
196
|
+
await expect((0, login_1.authUser)()).rejects.toThrow('Failed to fetch current user. Unauthorized');
|
|
197
|
+
expect(config_1.storeConfig).not.toHaveBeenCalled();
|
|
198
|
+
});
|
|
199
|
+
it('should exit with a connection error instead of prompting when the network is down', async () => {
|
|
200
|
+
const networkError = Object.assign(new TypeError('fetch failed'), {
|
|
201
|
+
cause: { code: 'ENOTFOUND' },
|
|
202
|
+
});
|
|
203
|
+
sdk_1.getCurrentUser.mockRejectedValue(networkError);
|
|
204
|
+
exitSpy = jest.spyOn(process, 'exit').mockImplementation((code) => {
|
|
205
|
+
throw new Error(`exit ${code}`);
|
|
206
|
+
});
|
|
207
|
+
await expect((0, login_1.authUser)()).rejects.toThrow('exit 1');
|
|
208
|
+
expect(fetchMock).not.toHaveBeenCalled();
|
|
209
|
+
expect(mockAuthenticateUser).not.toHaveBeenCalled();
|
|
210
|
+
});
|
|
211
|
+
});
|
|
128
212
|
describe('getVersionFromPackageJson', () => {
|
|
129
213
|
it('should return the version value from package.json', () => {
|
|
130
214
|
const version = (0, login_1.getVersionFromPackageJson)();
|
package/oclif.manifest.json
CHANGED