-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
cognito: Using a token for UserPoolIdentityProviderApple doesn't resolve at deploy #31378
Comments
According to this: aws-cdk/packages/aws-cdk-lib/aws-cognito/lib/user-pool-idps/apple.ts Lines 23 to 26 in bc4dbfd
It has to be the private_key content which would be used to as Json string for providerdetails. Looking at the provided sample in the CFN document:
Looks like the private_key has to be plain text in the template. I will reach out internally for clarifying. |
internal tracking: V1512372084 |
I guess you have three options here but the private key has to be stored in ssm parameter instead: Let me know if it works for you. // template parameter with string value(cdk creates a CfnParameter for you)
privateKey: ssm.StringParameter.valueForStringParameter(this, 'foo'),
// template parameter with SSM parameter value(cdk creates a CfnParameter for you)
privateKey: ssm.StringParameter.fromStringParameterAttributes(this, 'privateKey', { parameterName: 'foo', }).stringValue,
// dynamic reference with ssm-secure(cdk creates a dynamic reference for you)
privateKey: ssm.StringParameter. fromSecureStringParameterAttributes(this, 'privateKey', {
parameterName: 'foo',
encryptionKey: kms.Key.fromKeyArn(this, 'Key', 'arn:aws:kms:eu-central-1:987654321098:key/abcd1234-ab12-cd34-ef56-abcdef123456'),
}).stringValue,
// dynamic reference with a secretsmanager Secret(cdk creates a dynamic reference for you)
privateKey: '{{resolve:secretsmanager:secret-id:SecretString:json-key}}' As the private key is sensitive credentials, it's recommended using fromSecureStringParameterAttributes() or dynamic reference with secret manager secret support. Check aws-ssm doc for more details or this blog post(SSM Parameters in AWS CDK) for samples. |
@pahud Thanks for the reply! So, I tried this option first after saving the value as SecureString in SSM: privateKey: ssm.StringParameter. fromSecureStringParameterAttributes(this, 'privateKey', {
parameterName: 'foo',
encryptionKey: kms.Key.fromKeyArn(this, 'Key', 'arn:aws:kms:eu-central-1:987654321098:key/abcd1234-ab12-cd34-ef56-abcdef123456'),
}).stringValue But it threw this error: Failed to create ChangeSet cdk-deploy-change-set on CognitoStack: FAILED, SSM Secure reference is not supported in: [AWS::Cognito::UserPoolIdentityProvider/Properties/ProviderDetails/private_key] So then I saved the value as a String in SSM, and tried this option: privateKey: ssm.StringParameter.valueForStringParameter(this, 'foo') This didn't throw the same error, but it did throw this error: CREATE_FAILED | AWS::Cognito::UserPoolClient | UserPoolClient (UserPoolClient) Resource handler returned message: "The provider SignInWithApple does not exist for User Pool region_id (Service: CognitoIdentityProvider, Status Code: 400, Request ID: ...)" (RequestToken: ..., HandlerErrorCode: InvalidRequest) I haven't used AWS CDK before, but it seems like I covered the bases. Do you know what went wrong? |
I found this issue with a solution that worked. UserPoolClient.node.addDependency(provider)
Now it would be great to be able to use the SecureString parameter instead of just the regular String parameter, if possible. Unfortunately, based on this documentation, it seems like it might not be widely supported. If it's not possible, could you please shed some light on what some of the potential drawbacks to not using the SecureString might be? Is my Sign in with Apple private key at risk of exposure since its not in a SecureString or Secret? Thanks! |
Yes according to "Resources that support dynamic parameter patterns for secure strings" described in the doc, only a few services support that. I have cut an internal ticket to bring this to relevant team's attention. Unfortunately there isn't anything CDK can do as that is defined in CFN spec. I will report here when I have updates. |
OK please check out this sample below: Let's say you have a Secrets Manager Secret called "TheKeyNameInSecretsManager" with its secret value as JSON and "private_key" in the JSON payload as below: % aws secretsmanager get-secret-value --secret-id TheKeyNameInSecretsManager --query "SecretString"
"{\"private_key\":\"foo\"}" Now, our goal is to build a dynamic reference with secretsmanager as the service like private_key: "{{resolve:secretsmanager:TheKeyNameInSecretsManager:SecretString:private_key::}}" Your CDK code should look like this: const applePrivateKeySecret = secretsmanager.Secret.fromSecretNameV2(this, 'ApplePrivateKey', 'TheKeyNameInSecretsManager')
// create a random userpool
const userPool = new cognito.UserPool(this, 'UserPool', { })
new cognito.UserPoolIdentityProviderApple(this, 'UserPoolIdentityProviderApple',
{
clientId: 'com.myapp',
teamId: '123456',
keyId: '123456',
privateKey: SecretValue.secretsManager(applePrivateKeySecret.secretName, {
jsonField: 'private_key',
}).unsafeUnwrap().toString(),
userPool,
}) You can
In this case, your private_key would be stored in the secretsmanager Secret and cloudformation would reference it using dynamic references. I didn't really deploy it as I don't have a valid private_key but Let me know if it works for you. |
I think we need a PR to fix this. Just like
I think this has to be
|
Comments on closed issues and PRs are hard for our team to see. |
1 similar comment
Comments on closed issues and PRs are hard for our team to see. |
Describe the bug
I'm attempting to create a Cognito user pool using Apple as an identity provider. I created the secret key and manually saved it in Secrets Manager using the AWS console.
Originally, I attempted this:
It didn't work on deploy since the secret is exposed as a string.
I also tried saving the value as a
SecureString
in AWS Systems Manager and retrieving it like this:Which didn't give the same error regarding the value being exposed, but results in a
TypeError
(described just below), since apparently this token is unresolved when I attempt to deploy.So I attempted to create a lambda that could be called by my Cognito stack to retrieve the token from Secrets Manager:
The lambda code:
In my Cognito Stack, I retrieve the token and attempt to use it, but
UserPoolIdentityProviderApple
is seeminglyundefined
since the token never resolves.This line will always throw an error:
This comment on another issue seems to outline what I'm doing, more or less.
As far as I can tell, aren't tokens supposed to resolve on deployment? But this unresolved token is always causing the above error when I attempt to deploy.
What is the correct way to retrieve a secret from Secrets Manager to use in this context?
The Cognito Stack code:
Regression Issue
Last Known Working CDK Version
No response
Expected Behavior
I expected the token to resolve on deployment so I could use it to create an identity provider for Cognito.
Current Behavior
The token does not resolve and breaks the deployment. If I add a check
Token.isUnresolved()
, it will always be true.Reproduction Steps
See above code please.
Possible Solution
No response
Additional Information/Context
No response
CDK CLI Version
2.156.0 (build 2966832)
Framework Version
No response
Node.js Version
20.16.0
OS
Windows 10
Language
TypeScript
Language Version
TypeScript 5.5.3
Other information
No response
The text was updated successfully, but these errors were encountered: