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

Provide the ability to add an existing Secret to the Construct #639

Open
bradley-curran opened this issue Sep 13, 2024 · 1 comment
Open

Comments

@bradley-curran
Copy link

The current implementation doesn't allow access to the secretsmanager Secret at all. AFAIK there's no way to add a resource policy to the Secret via this implementation.

Would it be possible to create a variation of this Construct that takes an existing Secret as a prop? That way engineers can configure the Secret in the way that they need, while still using the key generation capabilities of the Custom Resource.

@truelecter
Copy link

I've stumbled accross this issue too and turns out, CDK can actually create Secret Resource policy on existing secret with secretsmanager.ResourcePolicy construct:

import * as iam from "aws-cdk-lib/aws-iam";
import * as secretsmanager from "aws-cdk-lib/aws-secretsmanager";
import { KeyPair } from "cdk-ec2-key-pair";

const bastionKey = new KeyPair(this, "BastionKeyPair", {
  keyPairName: "bastion/db",
  description: "Key pair for RDS Bastion",
});

const bastionPK = secretsmanager.Secret.fromSecretCompleteArn(this, "BastionPK", bastionKey.privateKeyArn);
bastionKey.grantReadOnPrivateKey(externalAmazonSharingRole);
const bastionPKPolicy = new secretsmanager.ResourcePolicy(this, "BastionPKPolicy", {
  secret: bastionPK
});

bastionPKPolicy.document.addStatements(new iam.PolicyStatement({
  actions: [
    "secretsmanager:GetSecretValue",
    "secretsmanager:DescribeSecret",
  ],
  principals: [new iam.ArnPrincipal(principalArn)],
  resources: [bastionPK.secretArn],
}));

This is, however, a workaround and it would be much better if this construct was able to manage secret resource policy (ideally public and private keys KMS as well), as current behaviour of grantReadOnPrivateKey and grantReadOnPublicKey is slightly misleading as it only modifies policy of grantee (which is noop in case of cross-account principals) and unlike official CDK constructs does not add any entry to the resource policy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants