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

Migration: Codegen support for Gen2 reference auth from Gen1 import auth #14011

Merged
merged 10 commits into from
Nov 12, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
LoginOptions,
Scope,
} from '@aws-amplify/amplify-gen2-codegen';
import { AttributeMappingRule } from '@aws-amplify/amplify-gen2-codegen/src/auth/source_builder';
import { AttributeMappingRule, ReferenceAuth } from '@aws-amplify/amplify-gen2-codegen/src/auth/source_builder';
import {
LambdaConfigType,
IdentityProviderTypeType,
Expand Down Expand Up @@ -46,6 +46,7 @@ export interface AuthSynthesizerOptions {
identityGroups?: GroupType[];
webClient?: UserPoolClientType;
authTriggerConnections?: AuthTriggerConnectionSourceMap;
referenceAuth?: ReferenceAuth;
guestLogin?: boolean;
mfaConfig?: UserPoolMfaType;
totpConfig?: SoftwareTokenMfaConfigType;
Expand Down Expand Up @@ -243,6 +244,7 @@ export const getAuthDefinition = ({
webClient,
authTriggerConnections,
guestLogin,
referenceAuth,
mfaConfig,
totpConfig,
}: AuthSynthesizerOptions): AuthDefinition => {
Expand Down Expand Up @@ -360,5 +362,6 @@ export const getAuthDefinition = ({
oAuthFlows: webClient?.AllowedOAuthFlows,
readAttributes: webClient?.ReadAttributes,
writeAttributes: webClient?.WriteAttributes,
referenceAuth,
};
};
88 changes: 88 additions & 0 deletions packages/amplify-gen2-codegen/src/auth/source_builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
AuthDefinition,
AuthTriggerEvents,
EmailOptions,
ReferenceAuth,
renderAuthNode,
UserPoolMfaConfig,
} from './source_builder';
Expand Down Expand Up @@ -375,5 +376,92 @@ describe('render auth node', () => {
const source = printNodeArray(node);
assert.match(source, /defineAuth\(\{[\s\S]*attributeMapping:\s\{[\s\S]*fullname:\s"name"/);
});
it('renders attributeMapping if passed along with Google login', () => {
const authDefinition: AuthDefinition = {
loginOptions: {
googleLogin: true,
googleAttributes: { fullname: 'name' } as AttributeMappingRule,
},
};
const node = renderAuthNode(authDefinition);
const source = printNodeArray(node);
assert.match(source, /defineAuth\(\{[\s\S]*attributeMapping:\s\{[\s\S]*fullname:\s"name"/);
});
});
describe('reference auth', () => {
it(`renders successfully for imported userpool`, () => {
const referenceAuthProps: ReferenceAuth = {
userPoolId: 'userPoolId',
userPoolClientId: 'userPoolClientId',
groups: {
Admin: 'AdminRoleARN',
ReadOnly: 'ReadOnlyRoleARN',
},
};
const authDefinition: AuthDefinition = {
referenceAuth: referenceAuthProps,
};
const node = renderAuthNode(authDefinition);
const source = printNodeArray(node);
assert.match(source, /referenceAuth/);
assert.match(source, /userPoolId: "userPoolId"/);
assert.match(source, /userPoolClientId: "userPoolClientId"/);
assert.match(source, /groups:/);
assert.match(source, /Admin: "AdminRoleARN"/);
assert.match(source, /ReadOnly: "ReadOnlyRoleARN"/);
assert.doesNotMatch(source, /identityPoolId: "identityPoolId"/);
assert.doesNotMatch(source, /authRoleArn: "authRoleArn"/);
assert.doesNotMatch(source, /unauthRoleArn: "unauthRoleArn"/);
});

it(`renders successfully for imported identity pool`, () => {
const referenceAuthProps: ReferenceAuth = {
identityPoolId: 'identityPoolId',
authRoleArn: 'authRoleArn',
unauthRoleArn: 'unauthRoleArn',
};
const authDefinition: AuthDefinition = {
referenceAuth: referenceAuthProps,
};
const node = renderAuthNode(authDefinition);
const source = printNodeArray(node);
assert.match(source, /referenceAuth/);
assert.match(source, /identityPoolId: "identityPoolId"/);
assert.match(source, /authRoleArn: "authRoleArn"/);
assert.match(source, /unauthRoleArn: "unauthRoleArn"/);
assert.doesNotMatch(source, /userPoolId: "userPoolId"/);
assert.doesNotMatch(source, /userPoolClientId: "userPoolClientId"/);
assert.doesNotMatch(source, /groups:/);
assert.doesNotMatch(source, /Admin: "AdminRoleARN"/);
assert.doesNotMatch(source, /ReadOnly: "ReadOnlyRoleARN"/);
});

it(`renders successfully for imported userpool and identity pool`, () => {
const referenceAuthProps: ReferenceAuth = {
userPoolId: 'userPoolId',
userPoolClientId: 'userPoolClientId',
identityPoolId: 'identityPoolId',
authRoleArn: 'authRoleArn',
unauthRoleArn: 'unauthRoleArn',
groups: {
Admin: 'AdminRoleARN',
ReadOnly: 'ReadOnlyRoleARN',
},
};
const authDefinition: AuthDefinition = {
referenceAuth: referenceAuthProps,
};
const node = renderAuthNode(authDefinition);
const source = printNodeArray(node);
assert.match(source, /referenceAuth/);
assert.match(source, /userPoolId: "userPoolId"/);
assert.match(source, /userPoolClientId: "userPoolClientId"/);
assert.match(source, /identityPoolId: "identityPoolId"/);
assert.match(source, /authRoleArn: "authRoleArn"/);
assert.match(source, /unauthRoleArn: "unauthRoleArn"/);
assert.match(source, /groups:/);
assert.match(source, /Admin: "AdminRoleARN"/);
assert.match(source, /ReadOnly: "ReadOnlyRoleARN"/);
});
});
});
43 changes: 42 additions & 1 deletion packages/amplify-gen2-codegen/src/auth/source_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@
| 'userMigration'
| 'verifyAuthChallengeResponse';

export type ReferenceAuth = {
userPoolId?: string;
identityPoolId?: string;
authRoleArn?: string;
unauthRoleArn?: string;
userPoolClientId?: string;
groups?: Record<string, string>;
};

export interface AuthDefinition {
loginOptions?: LoginOptions;
groups?: Group[];
Expand All @@ -141,6 +150,7 @@
oAuthFlows?: string[];
readAttributes?: string[];
writeAttributes?: string[];
referenceAuth?: ReferenceAuth;
}

const factory = ts.factory;
Expand Down Expand Up @@ -462,9 +472,40 @@

export function renderAuthNode(definition: AuthDefinition): ts.NodeArray<ts.Node> {
const namedImports: { [importedPackageName: string]: Set<string> } = { '@aws-amplify/backend': new Set() };
const secretErrors: ts.Node[] = [];
let backendFunctionConstruct: string;
Fixed Show fixed Hide fixed
const refAuth = definition.referenceAuth;
if (refAuth) {
const referenceAuthProperties: Array<PropertyAssignment> = [];
namedImports['@aws-amplify/backend'].add('referenceAuth');
for (const [key, value] of Object.entries(refAuth)) {
if (value) {
referenceAuthProperties.push(
factory.createPropertyAssignment(
factory.createIdentifier(key),
typeof value === 'object'
? factory.createObjectLiteralExpression(
Object.entries(value).map(([_key, _value]) =>
factory.createPropertyAssignment(factory.createIdentifier(_key), factory.createStringLiteral(_value)),
),
true,
)
: factory.createStringLiteral(value),
),
);
}
}
return renderResourceTsFile({
exportedVariableName: factory.createIdentifier('auth'),
functionCallParameter: factory.createObjectLiteralExpression(referenceAuthProperties, true),
additionalImportedBackendIdentifiers: namedImports,
backendFunctionConstruct: 'referenceAuth',
postImportStatements: secretErrors,
});
}

namedImports['@aws-amplify/backend'].add('defineAuth');
const defineAuthProperties: Array<PropertyAssignment> = [];
const secretErrors: ts.Node[] = [];

const logInWithPropertyAssignment = createLogInWithPropertyAssignment(definition.loginOptions, secretErrors);
defineAuthProperties.push(logInWithPropertyAssignment);
Expand Down
14 changes: 9 additions & 5 deletions packages/amplify-gen2-codegen/src/backend/synthesizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import ts, {
ImportDeclaration,
VariableStatement,
} from 'typescript';
import { PolicyOverrides } from '../auth/source_builder.js';
import { PolicyOverrides, ReferenceAuth } from '../auth/source_builder.js';
import { BucketAccelerateStatus, BucketVersioningStatus } from '@aws-sdk/client-s3';
import { AccessPatterns, ServerSideEncryptionConfiguration } from '../storage/source_builder.js';
import assert from 'assert';
Expand All @@ -26,6 +26,7 @@ export interface BackendRenderParameters {
oAuthFlows?: string[];
readAttributes?: string[];
writeAttributes?: string[];
referenceAuth?: ReferenceAuth;
};
storage?: {
importFrom: string;
Expand Down Expand Up @@ -206,7 +207,7 @@ export class BackendSynthesizer {
factory.createVariableDeclarationList([backendVariable], ts.NodeFlags.Const),
);

if (renderArgs.auth?.userPoolOverrides) {
if (renderArgs.auth?.userPoolOverrides && !renderArgs?.auth?.referenceAuth) {
const cfnUserPoolVariableStatement = this.createVariableStatement(
this.createVariableDeclaration('cfnUserPool', 'auth.resources.cfnResources.cfnUserPool'),
);
Expand All @@ -233,7 +234,7 @@ export class BackendSynthesizer {
);
}

if (renderArgs.auth?.guestLogin === false || renderArgs.auth?.identityPoolName) {
if (renderArgs.auth?.guestLogin === false || (renderArgs.auth?.identityPoolName && !renderArgs?.auth?.referenceAuth)) {
const cfnIdentityPoolVariableStatement = this.createVariableStatement(
this.createVariableDeclaration('cfnIdentityPool', 'auth.resources.cfnResources.cfnIdentityPool'),
);
Expand All @@ -248,7 +249,10 @@ export class BackendSynthesizer {
}
}

if (renderArgs.auth?.oAuthFlows || renderArgs.auth?.readAttributes || renderArgs.auth?.writeAttributes) {
if (
(renderArgs.auth?.oAuthFlows || renderArgs.auth?.readAttributes || renderArgs.auth?.writeAttributes) &&
!renderArgs?.auth?.referenceAuth
) {
const cfnUserPoolClientVariableStatement = this.createVariableStatement(
this.createVariableDeclaration('cfnUserPoolClient', 'auth.resources.cfnResources.cfnUserPoolClient'),
);
Expand All @@ -274,7 +278,7 @@ export class BackendSynthesizer {
}
}

if (renderArgs.auth?.writeAttributes) {
if (renderArgs.auth?.writeAttributes && !renderArgs?.auth?.referenceAuth) {
nodes.push(
this.setPropertyValue(
factory.createIdentifier('cfnUserPoolClient'),
Expand Down
1 change: 1 addition & 0 deletions packages/amplify-gen2-codegen/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export const createGen2Renderer = ({
oAuthFlows: auth?.oAuthFlows,
readAttributes: auth?.readAttributes,
writeAttributes: auth?.writeAttributes,
referenceAuth: auth?.referenceAuth,
};
}

Expand Down
Loading
Loading