Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/CS-41007 - added validation for management token in utilties #1138

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
isAuthenticated,
cliux,
doesBranchExist,
isManagementTokenValid
} = require('@contentstack/cli-utilities');
const util = require('../../util');
const config = require('../../util/config');
Expand Down Expand Up @@ -119,6 +120,10 @@ class ExportToCsvCommand extends Command {
apiKey: listOfTokens[managementTokenAlias].apiKey,
token: listOfTokens[managementTokenAlias].token,
};
const checkManagementTokenValidity = await isManagementTokenValid(stackAPIKey,listOfTokens[managementTokenAlias].token);
if(!checkManagementTokenValidity?.valid) {
cs-raj marked this conversation as resolved.
Show resolved Hide resolved
throw checkManagementTokenValidity.hasOwnProperty('valid')?(`error: Management token or stack API key is invalid. ${checkManagementTokenValidity?.message||""}`):checkManagementTokenValidity?.message||"";
}
} else if (managementTokenAlias) {
this.error('Provided management token alias not found in your config.!');
} else {
Expand Down
18 changes: 18 additions & 0 deletions packages/contentstack-utilities/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import authHandler from './auth-handler';
import { HttpClient, cliux, configHandler } from '.';
export const isAuthenticated = () => authHandler.isAuthenticated();
export const doesBranchExist = async (stack, branchName) => {
return stack
Expand All @@ -8,3 +9,20 @@ export const doesBranchExist = async (stack, branchName) => {
return error;
});
};

export const isManagementTokenValid = async (stackAPIKey, managementToken) => {
const httpClient = new HttpClient({ headers: { api_key: stackAPIKey, authorization: managementToken } });
try {
const response = (await httpClient.get(`${configHandler.get('region').cma}/v3/environments?limit=1`))?.data;
shafeeqd959 marked this conversation as resolved.
Show resolved Hide resolved

if (response?.environments) {
return { valid: true, message: `valid token and stack api key` }
} else if(response?.error_code) {
return { valid: false, message: response.error_message };
} else {
throw typeof response === "string"? response : "";
shafeeqd959 marked this conversation as resolved.
Show resolved Hide resolved
}
} catch (error) {
return { message:`Failed to check the validity of the Management token. ${error}`};
}
}
Loading