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

aws:ecs: fromSecretsManager causing cyclic dependency when secret creation is done in a separate stack #26511

Closed
surecloud-Awalia opened this issue Jul 26, 2023 · 2 comments
Labels
@aws-cdk/aws-ecs Related to Amazon Elastic Container bug This issue is a bug. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.

Comments

@surecloud-Awalia
Copy link

surecloud-Awalia commented Jul 26, 2023

Describe the bug

As per best practices, we have the secret manager's secret creation in a different stack, and we want to use this secret as an environment variable for an ecs task, when this ecs task and it's related task execution role is created in a different stack.

Expected Behavior

Stacks are created successfully.

Current Behavior

Error: secret stack depends on ecs stack. secret stack -> ecs stack/task execution role. adding this dependency will would create a cyclic reference.

Since we want secret stack to be created first and then we pass the secret to the ecs stack, we get a cyclic dependency when we try to set the secret as env variable, because the function tries to add policy to the secret's kms key which allows access to the ecs task's execution role to access the secret.

Reproduction Steps

const SecretStack = new SecretStack(app, `${environmentName}-secret`, {
env: {region: environment.region},
environment,
});

const ecsStack = new ecsStack(app, `${environmentName}-ecs`, {
env: {region: environment.region},
environment,
SecretStack.ecsSecrets.mySecret,
});

// secret stack

export class SecretStack extends Stack {
readonly ecsSecrets: Secret;

constructor(scope: Construct, id: string, props: StackProps) {

    super(scope, id, props);
    const secretKmsKey = new Key(this, 'secret-kms-key');
    this.ecsSecrets = new Secret(this, `${id}-ecs-secret`, {
         encryptionKey: secretKmsKey,
         generateSecretString: {
            generateStringKey: 'client_secret',
            secretStringTemplate: JSON.stringify({
                string_1: `https://auth.${props.zoneName}`,
                client_id: 'change_me',
            }),
        },
    });

 }

}

// secret being used in ecs stack

export interface ecsStackProps extends StackProps {
readonly ecsSecrets: Secret;
}

export class EcsStack extends Stack {

constructor(scope: Construct, id: string, props: ecsStackProps) {

    super(scope, id, props);

    let secretObject;
    if (props.secrets) {
        secretObject = Object.fromEntries(this.convertSecretToECSSecret(props.ecsSecrets).entries());
    }

    const ecsSecretMap = new Map<string, Secret>;
    props.ecsSecret.forEach((secretValue, secretKey) =>
        ecsSecretMap.set(secretKey, Secret.fromSecretsManager(secretValue))
    );

}

private convertSecretToECSSecret(secretsManagerSecretMap: Map<string, ISecret>) {

    const ecsSecretMap = new Map<string, Secret>;
    secretsManagerSecretMap.forEach((secretValue, secretKey) =>
        ecsSecretMap.set(secretKey, Secret.fromSecretsManager(secretValue))
    );
    return ecsSecretMap;
}

}

Possible Solution

We get an option where fromSecretsManager function gives an optional parameter which enables us to either create or not create the policy statement automatically for the KMS key.

Additional Information/Context

No response

CDK CLI Version

2.85.0

Framework Version

No response

Node.js Version

18.16.0

OS

Windows 10

Language

Typescript

Language Version

No response

Other information

No response

@surecloud-Awalia surecloud-Awalia added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jul 26, 2023
@github-actions github-actions bot added the @aws-cdk/aws-ecs Related to Amazon Elastic Container label Jul 26, 2023
@peterwoodworth
Copy link
Contributor

This reproduction code provided is a bit confusing, there are some clear type and compiler errors, fromSecretsManager isn't a method on any of our constructs, and I don't believe you're creating any resources in the EcsStack code provided.

@peterwoodworth peterwoodworth added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed needs-triage This issue or PR still needs to be triaged. labels Jul 26, 2023
@github-actions
Copy link

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

@github-actions github-actions bot added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Jul 28, 2023
@github-actions github-actions bot added closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. and removed closing-soon This issue will automatically close in 4 days unless further comments are made. labels Aug 2, 2023
@github-actions github-actions bot closed this as completed Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-ecs Related to Amazon Elastic Container bug This issue is a bug. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.
Projects
None yet
Development

No branches or pull requests

2 participants