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

Added validation for management token before importing a stack data #1481

Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 13 additions & 4 deletions packages/contentstack-bootstrap/src/bootstrap/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ export const setupEnvironments = async (
.stack({ api_key: api_key })
.managementToken()
.create(managementBody);
if(!managementTokenResult.uid){
cliux.print(
`Info: Failed to generate a management token.\nNote: Management token is not available in your plan. Please contact the admin for support.`,
{
color: 'yellow',
},
);
if ((await continueBootstrapCommand()) === 'no') {
return;
}
}
}
if (Array.isArray(environmentResult.items) && environmentResult.items.length > 0) {
for (const environment of environmentResult.items) {
Expand Down Expand Up @@ -229,15 +240,13 @@ const envFileHandler = async (
filePath = pathValidator(path.join(sanitizePath(clonedDirectory), sanitizePath(fileName)));
content = `CONTENTSTACK_API_KEY=${environmentVariables.api_key}\nCONTENTSTACK_DELIVERY_TOKEN=${
environmentVariables.deliveryToken
}\n${
}\nCONTENTSTACK_BRANCH=main${
livePreviewEnabled
? `\nCONTENTSTACK_PREVIEW_TOKEN=${
environmentVariables.preview_token || `''`
}\nCONTENTSTACK_PREVIEW_HOST=${previewHost}\nCONTENTSTACK_APP_HOST=${appHost}\n`
: '\n'
}CONTENTSTACK_ENVIRONMENT=${environmentVariables.environment}\nCONTENTSTACK_API_HOST=${
customHost ? customHost : managementAPIHost
}${
}CONTENTSTACK_ENVIRONMENT=${environmentVariables.environment}${
!isUSRegion && !customHost ? '\nCONTENTSTACK_REGION=' + region.name : ''
}\nCONTENTSTACK_LIVE_PREVIEW=${livePreviewEnabled}\nCONTENTSTACK_LIVE_EDIT_TAGS=false\nCONTENTSTACK_API_HOST=${
customHost ? customHost : managementAPIHost
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-bootstrap/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const config: Configuration = {
stack: 'contentstack/stack-contentstack-angular-modularblock-example',
},
'compass-app': {
source: 'SunilLsagar/universal-demo',
source: 'SunilLsagar/universal-demo-latest',
stack: 'SunilLsagar/stack-universal-demo-latest',
master_locale: 'en',
},
Expand Down
51 changes: 51 additions & 0 deletions packages/contentstack-seed/src/seed/contentstack/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,33 @@ export interface Stack {
org_uid: string;
}

export interface ManagementToken {
response_code: string;
response_message: string;
}


export interface CreateStackOptions {
name: string;
description: string;
master_locale: string;
org_uid: string;
}

export interface createManagementTokenOptions{
name: string;
description: string;
expires_on: string;
scope: {
module: string;
acl: {
read: boolean;
write?: boolean;
};
branches?: string[];
}[];
}

export default class ContentstackClient {
instance: Promise<ContentstackManagementSDK.ContentstackClient>;

Expand Down Expand Up @@ -167,6 +187,37 @@ export default class ContentstackClient {
}
}

async createManagementToken(api_key: string, managementToken: any, options: createManagementTokenOptions): Promise<ManagementToken> {
try {
const client = await this.instance;
const body = {
token: {
name: options.name,
description: options.description,
scope: options.scope,
expires_on: options.expires_on,
},
};

const response = await client.stack({ api_key: api_key, management_token: managementToken }).managementToken().create(body);
return {
response_code: response.errorCode,
response_message: response.errorMessage
};
} catch (error: unknown) {
const typedError = error as { errorCode: string };

if (typedError.errorCode === '401') {
return {
response_code: '401',
response_message: 'You do not have access to create management tokens. Please try again or ask an Administrator for assistance.'
}
}
throw this.buildError(typedError);
}

}

private buildError(error: any) {
const message = error.errorMessage || error.response.data?.errorMessage || error.response.statusText;
const status = error.status;
Expand Down
39 changes: 38 additions & 1 deletion packages/contentstack-seed/src/seed/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,46 @@ export default class ContentModelSeeder {
let count;
const stack_details = await this.csClient.getStack(api_key);
if(this.options.master_locale != stack_details.master_locale){
cliux.print(`Compass app requires the master locale to be set to English (en).`);
cliux.print(`Compass app requires the master locale to be set to English (en).`,{
color: "yellow",
bold: true,
});
return false;
}
const managementBody = {
"name":"Checking roles for creating management token",
"description":"This is a compass app management token.",
"scope":[
{
"module":"content_type",
"acl":{
"read":true,
"write":true
}
},
{
"module":"branch",
"branches":[
"main"
],
"acl":{
"read":true
}
}
],
"expires_on": "3000-01-01",
"is_email_notification_enabled":false
}
let managementTokenResult = await this.csClient.createManagementToken(api_key, this.managementToken, managementBody);
if(managementTokenResult?.response_code == "161" || managementTokenResult?.response_code == "401"){
cliux.print(
`Info: Failed to generate a management token.\nNote: Management token is not available in your plan. Please contact the admin for support.`,
{
color: 'red',
},
);
return false;
}
count = await this.csClient.getContentTypeCount(api_key, this.managementToken);

if (count > 0 && this._options.skipStackConfirmation !== 'yes') {
Expand Down
Loading