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: allow apiKeyAuthorizationMode to be undefined if defaultAuthorizationMode is apiKey #2329

Merged
merged 1 commit into from
Dec 17, 2024
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
6 changes: 6 additions & 0 deletions .changeset/itchy-flowers-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@aws-amplify/backend-data': patch
'@aws-amplify/backend': patch
---

Allow apiKeyAuthorizationMode to be undefined if defaultAuthorizationMode is apiKey
21 changes: 21 additions & 0 deletions packages/backend-data/src/convert_authorization_modes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ void describe('convertAuthorizationModesToCDK', () => {
defaultAuthorizationMode: 'API_KEY',
apiKeyConfig: {
expires: Duration.days(7),
description: undefined,
},
iamConfig: {
enableIamAuthorizationMode: true,
Expand All @@ -103,6 +104,26 @@ void describe('convertAuthorizationModesToCDK', () => {
);
});

void it('defaults api key expiry if default auth mode is api key and apiKeyConfig is undefined', () => {
const expectedOutput: CDKAuthorizationModes = {
defaultAuthorizationMode: 'API_KEY',
apiKeyConfig: {
expires: Duration.days(7),
description: undefined,
},
iamConfig: {
enableIamAuthorizationMode: true,
},
};

assert.deepStrictEqual(
convertAuthorizationModesToCDK(getInstancePropsStub, undefined, {
defaultAuthorizationMode: 'apiKey',
}),
expectedOutput
);
});

void it('defaults to user pool auth if a user pool is present in provided auth resources', () => {
const expectedOutput: CDKAuthorizationModes = {
defaultAuthorizationMode: 'AMAZON_COGNITO_USER_POOLS',
Expand Down
11 changes: 7 additions & 4 deletions packages/backend-data/src/convert_authorization_modes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const buildConstructFactoryProvidedAuthConfig = (
const convertApiKeyAuthConfigToCDK = ({
description,
expiresInDays = DEFAULT_API_KEY_EXPIRATION_DAYS,
}: ApiKeyAuthorizationModeProps): CDKApiKeyAuthorizationConfig => ({
}: ApiKeyAuthorizationModeProps = {}): CDKApiKeyAuthorizationConfig => ({
description,
expires: Duration.days(expiresInDays),
});
Expand Down Expand Up @@ -207,9 +207,12 @@ export const convertAuthorizationModesToCDK = (
const cdkAuthorizationMode = convertAuthorizationModeToCDK(
defaultAuthorizationMode
);
const apiKeyConfig = authModes?.apiKeyAuthorizationMode
? convertApiKeyAuthConfigToCDK(authModes.apiKeyAuthorizationMode)
: computeApiKeyAuthFromResource(authResources, authModes);
const apiKeyConfig =
authModes?.apiKeyAuthorizationMode ||
// If default auth mode is apiKey, don't require apiKeyAuthorizationMode to be defined
defaultAuthorizationMode === 'apiKey'
? convertApiKeyAuthConfigToCDK(authModes?.apiKeyAuthorizationMode)
: computeApiKeyAuthFromResource(authResources, authModes);
const userPoolConfig = computeUserPoolAuthFromResource(authResources);
const identityPoolConfig = computeIdentityPoolAuthFromResource(authResources);
const lambdaConfig = authModes?.lambdaAuthorizationMode
Expand Down
Loading