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

fix: ensure policy resource name when pushing REST APIs #7192

Merged
merged 5 commits into from
Apr 29, 2021
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
27 changes: 27 additions & 0 deletions packages/amplify-e2e-core/src/categories/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1275,3 +1275,30 @@ export function addAuthUserPoolOnlyNoOAuth(cwd: string, settings: AddAuthUserPoo
});
});
}

export function updateAuthAddAdminQueries(projectDir: string, groupName: string = 'adminQueriesGroup'): Promise<void> {
return new Promise((resolve, reject) => {
spawn(getCLIPath(), ['update', 'auth'], { cwd: projectDir, stripColors: true })
.wait('What do you want to do?')
.send(KEY_DOWN_ARROW)
.send(KEY_DOWN_ARROW)
.send(KEY_DOWN_ARROW)
.send(KEY_DOWN_ARROW)
.sendCarriageReturn() // Create or update Admin queries API
.wait('Do you want to restrict access to the admin queries API to a specific Group')
.sendConfirmYes()
.wait('Select the group to restrict access with')
.sendCarriageReturn() // Enter a custom group
.wait('Provide a group name')
.send(groupName)
.sendCarriageReturn()
.sendEof()
.run((err: Error) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
2 changes: 2 additions & 0 deletions packages/amplify-e2e-tests/src/__tests__/api_2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
initJSProjectWithProfile,
listAttachedRolePolicies,
listRolePolicies,
updateAuthAddAdminQueries,
} from 'amplify-e2e-core';
import * as path from 'path';
import { existsSync } from 'fs';
Expand Down Expand Up @@ -402,6 +403,7 @@ describe('amplify add api (REST)', () => {
allowGuestUsers: false,
});
await addRestApi(projRoot, { existingLambda: true });
await updateAuthAddAdminQueries(projRoot);
await amplifyPushUpdate(projRoot);

const amplifyMeta = getProjectMeta(projRoot);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,6 @@ export function consolidateApiGatewayPolicies(context: $TSContext, stackName: st
const apis = amplifyMeta?.api ?? {};

Object.keys(apis).forEach(resourceName => {
if (resourceName === 'AdminQueries') {
return;
}

const resource = apis[resourceName];
const apiParams = loadApiWithPrivacyParams(context, resourceName, resource);

Expand Down Expand Up @@ -220,7 +216,7 @@ function createApiGatewayAuthResources(context: $TSContext, stackName: string, a
}

export function loadApiWithPrivacyParams(context: $TSContext, name: string, resource: any): object | undefined {
if (resource.providerPlugin !== ProviderName || resource.service !== 'API Gateway') {
if (resource.providerPlugin !== ProviderName || resource.service !== 'API Gateway' || name === 'AdminQueries') {
return;
}

Expand All @@ -239,6 +235,7 @@ function updateExistingApiCfn(context: $TSContext, api: $TSObject): void {
const resourceDir = getResourceDirPath(context, 'api', resourceName);
const cfnTemplate = path.join(resourceDir, `${resourceName}-cloudformation-template.json`);
const paramsFile = path.join(resourceDir, 'parameters.json');
const apiParamsFile = path.join(resourceDir, API_PARAMS_FILE);
const cfn: any = JSONUtilities.readJson(cfnTemplate, { throwIfNotExist: false }) ?? {};
const parameterJson = JSONUtilities.readJson(paramsFile, { throwIfNotExist: false }) ?? {};
const parameters = cfn?.Parameters ?? {};
Expand All @@ -248,6 +245,12 @@ function updateExistingApiCfn(context: $TSContext, api: $TSObject): void {
for (const parameterName in parameters) {
if (parameterName === AUTH_ROLE_NAME || parameterName === UNAUTH_ROLE_NAME) {
delete parameters[parameterName];
modified = true;
}
}

for (const parameterName in parameterJson as any) {
if (parameterName === AUTH_ROLE_NAME || parameterName === UNAUTH_ROLE_NAME) {
delete parameterJson[parameterName];
modified = true;
}
Expand All @@ -271,8 +274,18 @@ function updateExistingApiCfn(context: $TSContext, api: $TSObject): void {
}
}

if (Array.isArray(api.params.paths)) {
api.params.paths.forEach(path => {
if (!path.policyResourceName) {
path.policyResourceName = String(path.name).replace(/{[a-zA-Z0-9\-]+}/g, '*');
modified = true;
}
});
}

if (modified) {
JSONUtilities.writeJson(cfnTemplate, cfn);
JSONUtilities.writeJson(paramsFile, parameterJson);
JSONUtilities.writeJson(apiParamsFile, api.params);
}
}