我使用的是aws-cdk-lib (2.13.0)。下面是我的代码片段:
import { App, Stack } from 'aws-cdk-lib';
import { Secret } from 'aws-cdk-lib/aws-secretsmanager';
export class CognitoStack extends Stack {
constructor(scope: App) {
super(scope, 'cognito');
const secret = this.getSecret('google');
console.log({ secret });
}
public getSecret(path: string) {
const secret = Secret.fromSecretNameV2(this, `Secret${path}`, path);
console.log({ path, secret, secretArn: secret.secretArn, string: secret.secretValue.toString() });
return secret.secretValue.toJSON();
}
}生成的日志如下所示:
{
path: 'google',
secret: <ref *1> SecretBase {
node: Node {
host: [Circular *1],
_locked: false,
_children: {},
_context: {},
_metadata: [],
_dependencies: Set(0) {},
_validations: [Array],
id: 'Secretgoogle',
scope: [CognitoStack]
},
stack: CognitoStack {
node: [Node],
_missingContext: [],
_stackDependencies: {},
templateOptions: {},
_logicalIds: [LogicalIDs],
account: '${Token[AWS.AccountId.4]}',
region: '${Token[AWS.Region.8]}',
environment: 'aws://unknown-account/unknown-region',
terminationProtection: undefined,
_stackName: 'cognito',
tags: [TagManager],
artifactId: 'cognito',
templateFile: 'cognito.template.json',
_versionReportingEnabled: true,
synthesizer: [DefaultStackSynthesizer],
[Symbol(@aws-cdk/core.DependableTrait)]: [Object]
},
env: {
account: '${Token[AWS.AccountId.4]}',
region: '${Token[AWS.Region.8]}'
},
_physicalName: undefined,
_allowCrossEnvironment: false,
physicalName: '${Token[TOKEN.332]}',
encryptionKey: undefined,
secretName: 'google',
secretArn: 'arn:${Token[AWS.Partition.7]}:secretsmanager:${Token[AWS.Region.8]}:${Token[AWS.AccountId.4]}:secret:google',
autoCreatePolicy: false,
[Symbol(@aws-cdk/core.DependableTrait)]: { dependencyRoots: [Array] }
},
secretArn: 'arn:${Token[AWS.Partition.7]}:secretsmanager:${Token[AWS.Region.8]}:${Token[AWS.AccountId.4]}:secret:google',
string: '${Token[TOKEN.333]}'
}
{ secret: '<unresolved-token>' }
npx cdk diff sandbox-cognito的结果如下所示:
Stack sandbox-cognito
Resources
[~] AWS::Cognito::UserPoolIdentityProvider Google GoogleAF1E99FA
└─ [~] ProviderDetails
├─ [-] Removed: .client_id
└─ [-] Removed: .client_secret这意味着它正在删除我能够手动设置的client_id/client_机密。现在,我正在尝试从一个秘密加载值,它不起作用。
问题是我不能解析JSON (注意日志中的<unresolved-token> )。我认为它还没有解决,但我不知道如何解决.它正在尝试解析这个字符串文本:${Token[TOKEN.333]},而不是秘密值。我怎样才能得到秘密字符串的结果?
发布于 2022-03-06 10:16:28
将您现有的秘密导入为SecretValue。使用clientSecret:string方法将其传递给.toString()支柱。
// Existing secret as SecretValue. Or use Secret.fromSecretNameV2.
const secretVal = cdk.SecretValue.secretsManager('GoogleSecrets', {
jsonField: 'client-secret',
});
new cognito.UserPoolIdentityProviderGoogle(this, 'GoogleProvider', {
userPool,
// creates a dynamic reference which resolves to the actual secret value at deploy-time
clientSecret: secretVal.toString(),
clientId: 'my-id',
});解释
在生命周期中,SecretValue.toString()“解析”为不同的值:当您console.log它时,您得到一个(无用的)不透明占位符令牌值,如${Token[TOKEN.198]}。在同步时,CDK在模板中呈现一个CloudFormation 动态参考:
//my-stack.template.json
{"client_secret": "{{resolve:secretsmanager:arn:aws:secretsmanager:us-east-1:123456789012:secret:GoogleSecrets:SecretString:client-secret::}}"}在部署时,CloudFormation“解析”动态引用中的实际秘密值。
重要的是,实际的秘密值永远不会暴露在您的本地环境或模板工艺品中。
发布于 2022-08-08 20:00:59
使用secretValue.unsafeUnwrap(),这是使它与CDK一起工作的唯一方法。否则,它将失败,因为秘密数据在生成的yaml或json文件中可用,该文件由cdk .换句话说,云的形成。
https://stackoverflow.com/questions/71356632
复制相似问题