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 7 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
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/contentstack-bootstrap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"test:report": "nyc --reporter=lcov mocha \"test/**/*.test.js\""
},
"dependencies": {
"@contentstack/cli-cm-seed": "~1.6.1",
"@contentstack/cli-cm-seed": "~1.6.2",
cs-raj marked this conversation as resolved.
Show resolved Hide resolved
"@contentstack/cli-command": "~1.2.15",
"@contentstack/cli-utilities": "~1.5.5",
"inquirer": "8.2.4",
Expand Down
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,11 @@ 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
cliux.print(`error: Management token or stack API key is invalid. ${checkManagementTokenValidity?.message||""}`, {color: "red"});
process.exit(1);
cs-raj marked this conversation as resolved.
Show resolved Hide resolved
}
} else if (managementTokenAlias) {
this.error('Provided management token alias not found in your config.!');
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-seed/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@contentstack/cli-cm-seed",
"description": "create a Stack from existing content types, entries, assets, etc.",
"version": "1.6.1",
"version": "1.6.2",
"author": "Contentstack",
"bugs": "https://github.com/contentstack/cli/issues",
"dependencies": {
Expand Down
19 changes: 19 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,21 @@ 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) {
cliux.print(`Failed to check the validity of the Management token. ${error}`, {color:"red"});
process.exit(1)
cs-raj marked this conversation as resolved.
Show resolved Hide resolved
}
}
2 changes: 1 addition & 1 deletion packages/contentstack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@contentstack/cli-cm-export-to-csv": "~1.5.0",
"@contentstack/cli-cm-import": "~1.10.1",
"@contentstack/cli-cm-migrate-rte": "~1.4.14",
"@contentstack/cli-cm-seed": "~1.6.1",
"@contentstack/cli-cm-seed": "~1.6.2",
"@contentstack/cli-command": "~1.2.15",
"@contentstack/cli-config": "~1.4.14",
"@contentstack/cli-launch": "~1.0.14",
Expand Down
4 changes: 2 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading