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(cognito): deprecate privateKey and add privateKeyValue as typed SecureValue #31409

Merged
merged 8 commits into from
Sep 13, 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
19 changes: 17 additions & 2 deletions packages/aws-cdk-lib/aws-cognito/lib/user-pool-idps/apple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Construct } from 'constructs';
import { UserPoolIdentityProviderProps } from './base';
import { CfnUserPoolIdentityProvider } from '../cognito.generated';
import { UserPoolIdentityProviderBase } from './private/user-pool-idp-base';
import { SecretValue } from '../../../core';

/**
* Properties to initialize UserPoolAppleIdentityProvider
Expand All @@ -22,8 +23,16 @@ export interface UserPoolIdentityProviderAppleProps extends UserPoolIdentityProv
readonly keyId: string;
/**
* The privateKey content for Apple APIs to authenticate the client.
*
* @deprecated use privateKeyValue
* @default none
*/
readonly privateKey: string;
readonly privateKey?: string;
/**
* The privateKey content for Apple APIs to authenticate the client.
* @default none
*/
readonly privateKeyValue?: SecretValue;
/**
* The list of apple permissions to obtain for getting access to the apple profile
* @see https://developer.apple.com/documentation/sign_in_with_apple/clientconfigi/3230955-scope
Expand All @@ -44,6 +53,12 @@ export class UserPoolIdentityProviderApple extends UserPoolIdentityProviderBase

const scopes = props.scopes ?? ['name'];

// Exactly one of the properties must be configured
if ((!props.privateKey && !props.privateKeyValue) ||
(props.privateKey && props.privateKeyValue)) {
throw new Error('Exactly one of "privateKey" or "privateKeyValue" must be configured.');
}

const resource = new CfnUserPoolIdentityProvider(this, 'Resource', {
userPoolId: props.userPool.userPoolId,
providerName: 'SignInWithApple', // must be 'SignInWithApple' when the type is 'SignInWithApple'
Expand All @@ -52,7 +67,7 @@ export class UserPoolIdentityProviderApple extends UserPoolIdentityProviderBase
client_id: props.clientId,
team_id: props.teamId,
key_id: props.keyId,
private_key: props.privateKey,
private_key: props.privateKeyValue ? props.privateKeyValue.unsafeUnwrap() : props.privateKey,
authorize_scopes: scopes.join(' '),
},
attributeMapping: super.configureAttributeMapping(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Template } from '../../../assertions';
import { Stack } from '../../../core';
import { Stack, SecretValue } from '../../../core';
import { ProviderAttribute, UserPool, UserPoolIdentityProviderApple } from '../../lib';

describe('UserPoolIdentityProvider', () => {
Expand Down Expand Up @@ -102,12 +102,51 @@ describe('UserPoolIdentityProvider', () => {
// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Cognito::UserPoolIdentityProvider', {
AttributeMapping: {
family_name: 'firstName',
given_name: 'lastName',
family_name: 'lastName',
given_name: 'firstName',
customAttr1: 'email',
customAttr2: 'sub',
},
});
});

// cannot assign both privateKey and privateKeyValue
test('cannot assign both privateKey and privateKeyValue', () => {
// GIVEN
const stack = new Stack();
const pool = new UserPool(stack, 'userpool');

expect(() => {
new UserPoolIdentityProviderApple(stack, 'userpoolidp', {
userPool: pool,
clientId: 'com.amzn.cdk',
teamId: 'CDKTEAMCDK',
keyId: 'XXXXXXXXXX',
privateKey: 'PRIV_KEY_CDK',
privateKeyValue: SecretValue.secretsManager('dummyId'),
});
}).toThrow('Exactly one of "privateKey" or "privateKeyValue" must be configured.');
});

// should support privateKeyValue
test('should support privateKeyValue', () => {
// GIVEN
const stack = new Stack();
const pool = new UserPool(stack, 'userpool');

new UserPoolIdentityProviderApple(stack, 'userpoolidp', {
userPool: pool,
clientId: 'com.amzn.cdk',
teamId: 'CDKTEAMCDK',
keyId: 'XXXXXXXXXX',
privateKeyValue: SecretValue.secretsManager('dummyId'),
});

Template.fromStack(stack).hasResourceProperties('AWS::Cognito::UserPoolIdentityProvider', {
ProviderDetails: {
private_key: '{{resolve:secretsmanager:dummyId:SecretString:::}}',
},
});
});
});
});