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 2 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
18 changes: 16 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,15 @@ export interface UserPoolIdentityProviderAppleProps extends UserPoolIdentityProv
readonly keyId: string;
/**
* The privateKey content for Apple APIs to authenticate the client.
*
* @deprecated use privateKeyValue
*/
readonly privateKey: string;
readonly privateKey?: string;
/**
* The privateKey content for Apple APIs to authenticate the client.
*
*/
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 +52,12 @@ export class UserPoolIdentityProviderApple extends UserPoolIdentityProviderBase

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

//at least one of the properties must be configured
pahud marked this conversation as resolved.
Show resolved Hide resolved
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 +66,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 @@ -90,8 +90,8 @@ describe('UserPoolIdentityProvider', () => {
keyId: 'CDKKEYCDK1',
privateKey: 'PRIV_KEY_CDK',
attributeMapping: {
familyName: ProviderAttribute.APPLE_LAST_NAME,
givenName: ProviderAttribute.APPLE_FIRST_NAME,
familyName: ProviderAttribute.APPLE_FIRST_NAME,
pahud marked this conversation as resolved.
Show resolved Hide resolved
givenName: ProviderAttribute.APPLE_LAST_NAME,
custom: {
customAttr1: ProviderAttribute.APPLE_EMAIL,
customAttr2: ProviderAttribute.other('sub'),
Expand All @@ -109,5 +109,44 @@ describe('UserPoolIdentityProvider', () => {
},
});
});

// 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:::}}',
},
});
});
});
});
Loading