Skip to content

Commit

Permalink
refactor(cli): refactor api key prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
danielleadams committed Sep 8, 2021
1 parent 4b5580a commit de82d67
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,17 @@ describe('prompt to add Api Key', () => {
return { envName };
},
},
print: {
info: jest.fn(),
},
prompt: {
confirm: jest.fn(() => true),
},
} as unknown as $TSContext;

jest.spyOn(ctx.print, 'info');
jest.spyOn(ctx.prompt, 'confirm');
jest.spyOn(walkthrough, 'askApiKeyQuestions');
jest.spyOn(cfnApiArtifactHandler, 'getCfnApiArtifactHandler');

await promptToAddApiKey(ctx);

expect(ctx.print.info).toHaveBeenCalledWith(`
⚠️ WARNING: Global Sandbox Mode has been enabled, which requires a valid API key. If
you'd like to disable, remove ${chalk.green('"type AMPLIFY_GLOBAL @allow_public_data_access_with_api_key"')}
from your GraphQL schema and run 'amplify push' again. If you'd like to proceed with
sandbox mode disabled in '${ctx.amplify.getEnvInfo().envName}', do not create an API Key.
`);

expect(ctx.prompt.confirm).toHaveBeenCalledWith('Would you like to create an API Key?', true);
expect(walkthrough.askApiKeyQuestions).toHaveBeenCalledTimes(1);
expect(cfnApiArtifactHandler.getCfnApiArtifactHandler).toHaveBeenCalledTimes(1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import chalk from 'chalk';
import { $TSContext } from 'amplify-cli-core';
import { askApiKeyQuestions } from './service-walkthroughs/appSync-walkthrough';
import { authConfigToAppSyncAuthType } from './utils/auth-config-to-app-sync-auth-type-bi-di-mapper';
import { getCfnApiArtifactHandler } from './cfn-api-artifact-handler';
import { $TSContext } from 'amplify-cli-core';

export async function promptToAddApiKey(context: $TSContext): Promise<void> {
context.print.info(`
⚠️ WARNING: Global Sandbox Mode has been enabled, which requires a valid API key. If
you'd like to disable, remove ${chalk.green('"type AMPLIFY_GLOBAL @allow_public_data_access_with_api_key"')}
from your GraphQL schema and run 'amplify push' again. If you'd like to proceed with
sandbox mode disabled in '${context.amplify.getEnvInfo().envName}', do not create an API Key.
`);
if (await context.prompt.confirm('Would you like to create an API Key?', true)) {
const apiKeyConfig = await askApiKeyQuestions();
const authConfig = [apiKeyConfig];
Expand Down
4 changes: 0 additions & 4 deletions packages/amplify-cli/src/__tests__/commands/status.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,4 @@ describe('amplify status: ', () => {
//TBD: to move ViewResourceTableParams into a separate file for mocking instance functions.
expect(mockContextWithHelpSubcommandAndCLArgs.amplify.showStatusTable.mock.calls.length).toBe(0);
});

// it('status should show global sandbox warning when enabled', async () => {

// });
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"api": {
"ampapp": {
"service": "AppSync",
"providerPlugin": "awscloudformation",
"output": {
"authConfig": {
"defaultAuthentication": {
"authenticationType": "AWS_IAM"
},
"additionalAuthenticationProviders": [
{
"authenticationType": "API_KEY",
"apiKeyConfig": {
"apiKeyExpirationDays": 2,
"apiKeyExpirationDate": "2021-08-20T20:38:07.585Z",
"description": ""
}
}
]
},
"globalSandboxModeConfig": {
"dev": {
"enabled": true
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import chalk from 'chalk';
import { $TSContext } from 'amplify-cli-core';
import { promptToAddApiKey } from 'amplify-category-api';
import { globalSandboxModeEnabled, showGlobalSandboxModeWarning } from './show-global-sandbox-mode-warning';
import { apiKeyIsActive, hasApiKey } from './get-api-key-config';

export async function promptSandboxModeApiKey(context: $TSContext): Promise<void> {
if (globalSandboxModeEnabled(context)) {
if (!apiKeyIsActive() || !hasApiKey()) {
context.print.info(`
⚠️ WARNING: Global Sandbox Mode has been enabled, which requires a valid API key. If
you'd like to disable, remove ${chalk.green('"type AMPLIFY_GLOBAL @allow_public_data_access_with_api_key"')}
from your GraphQL schema and run 'amplify push' again. If you'd like to proceed with
sandbox mode disabled in '${context.amplify.getEnvInfo().envName}', do not create an API Key.
`);
await promptToAddApiKey(context);
} else showGlobalSandboxModeWarning(context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import { onCategoryOutputsChange } from './on-category-outputs-change';
import { initializeEnv } from '../../initialize-env';
import { getProviderPlugins } from './get-provider-plugins';
import { getEnvInfo } from './get-env-info';
import { globalSandboxModeEnabled, showGlobalSandboxModeWarning } from './show-global-sandbox-mode-warning';
import { apiKeyIsActive, hasApiKey } from './get-api-key-config';
import { EnvironmentDoesNotExistError, exitOnNextTick, stateManager, $TSAny, $TSContext } from 'amplify-cli-core';
import { promptToAddApiKey } from 'amplify-category-api';
import { promptSandboxModeApiKey } from './prompt-sandbox-mode-api-key';

export async function pushResources(
context: $TSContext,
Expand Down Expand Up @@ -70,10 +68,7 @@ export async function pushResources(
continueToPush = await context.amplify.confirmPrompt('Are you sure you want to continue?');
}

if (globalSandboxModeEnabled(context)) {
if (!apiKeyIsActive() || !hasApiKey()) await promptToAddApiKey(context);
else showGlobalSandboxModeWarning(context);
}
await promptSandboxModeApiKey(context);

if (continueToPush) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -879,24 +879,4 @@ describe('ModelTransformer: ', () => {
expect(out).toBeDefined();
validateModelSchema(parse(out.schema));
});

it('should support global sandbox mode', () => {
const validSchema = `
type AMPLIFY_GLOBAL @allow_public_data_access_with_api_key(in: \"dev\")
type Post @model {
id: ID!
title: String!
}
`;

const transformer = new GraphQLTransform({
transformers: [new ModelTransformer()],
featureFlags,
});
const out = transformer.transform(validSchema);
expect(out).toBeDefined();

parse(out.schema);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ export class GraphQLTransform {
aws_oidc: true,
aws_cognito_user_pools: true,
deprecated: true,
allow_public_data_access_with_api_key: true,
},
);
let allModelDefinitions = [...context.inputDocument.definitions];
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ directive @aws_api_key on FIELD_DEFINITION | OBJECT
directive @aws_iam on FIELD_DEFINITION | OBJECT
directive @aws_oidc on FIELD_DEFINITION | OBJECT
directive @aws_cognito_user_pools(cognito_groups: [String!]) on FIELD_DEFINITION | OBJECT
directive @allow_public_data_access_with_api_key(in: [String!]) on OBJECT
# Allows transformer libraries to deprecate directive arguments.
directive @deprecated(reason: String) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION | ENUM | ENUM_VALUE
Expand Down

0 comments on commit de82d67

Please sign in to comment.