@contrail/cli 3.5.6 → 3.5.7-alpha.4f4573d
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 +30 -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.4f4573d 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,33 @@ 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();
|
|
175
|
+
await (0, sdk_1.getCurrentUser)();
|
|
182
176
|
}
|
|
177
|
+
return returnAndStoreConfig(sdk_1.Auth.config);
|
|
178
|
+
}
|
|
179
|
+
async function refreshCurrentUserOrg() {
|
|
180
|
+
logger_1.default.debug('refreshing current user org...');
|
|
181
|
+
await (0, sdk_1.getCurrentUser)();
|
|
183
182
|
// TODO: To uncomment when telemetry backend is developed.
|
|
184
183
|
// const current_Version = getVersionFromPackageJson();
|
|
185
184
|
// await checkTelemetry(current_Version);
|
|
186
185
|
return returnAndStoreConfig(sdk_1.Auth.config);
|
|
187
186
|
}
|
|
187
|
+
async function refreshAccessToken() {
|
|
188
|
+
const newVibeAccessToken = await requestRefreshedAccessToken(sdk_1.Auth.config.apiRefreshToken, sdk_1.Auth.config.apiGateway);
|
|
189
|
+
sdk_1.Auth.config = { ...sdk_1.Auth.config, apiUserToken: newVibeAccessToken };
|
|
190
|
+
(0, sdk_1.setConfig)(sdk_1.Auth.config);
|
|
191
|
+
}
|
|
188
192
|
function getApiGateway(config) {
|
|
189
193
|
return config?.apiGateway ?? DEFAULT_API_GATEWAY;
|
|
190
194
|
}
|
|
@@ -214,9 +218,14 @@ async function requestRefreshedAccessToken(vibeRefreshToken, apiGateway) {
|
|
|
214
218
|
},
|
|
215
219
|
body: JSON.stringify({ refreshToken: vibeRefreshToken }),
|
|
216
220
|
});
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
}
|
|
221
|
+
if (!response.ok) {
|
|
222
|
+
throw new Error(`Token refresh failed with status ${response.status}`);
|
|
223
|
+
}
|
|
224
|
+
const data = await response.json();
|
|
225
|
+
if (!data.accessToken) {
|
|
226
|
+
throw new Error('Token refresh response did not contain an access token');
|
|
227
|
+
}
|
|
228
|
+
return data.accessToken;
|
|
220
229
|
}
|
|
221
230
|
function getVersionFromPackageJson() {
|
|
222
231
|
// 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