Skip to content

Commit

Permalink
Delegate adding principal policy to base principal
Browse files Browse the repository at this point in the history
When the base principal is an identity principal, we want to add the policy there with the kms:ViaService condition applied.
  • Loading branch information
pergardebrink committed Dec 17, 2023
1 parent e7ecd70 commit 9b6555d
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/aws-cdk-lib/aws-kms/lib/via-service-principal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ export class ViaServicePrincipal extends iam.PrincipalBase {
this.basePrincipal = basePrincipal ? basePrincipal : new iam.AnyPrincipal();
}

public addToPrincipalPolicy(_statement: iam.PolicyStatement): iam.AddToPrincipalPolicyResult {
const conditions = Object.assign({}, _statement.conditions);

if (conditions.StringEquals) {
conditions.StringEquals = Object.assign({ 'kms:ViaService': this.serviceName }, conditions.StringEquals);
} else {
conditions.StringEquals = { 'kms:ViaService': this.serviceName };
}

return this.basePrincipal.addToPrincipalPolicy(_statement.copy({ conditions }));
}

public get policyFragment(): iam.PrincipalPolicyFragment {
// Make a copy of the base policyFragment to add a condition to it
const base = this.basePrincipal.policyFragment;
Expand Down

0 comments on commit 9b6555d

Please sign in to comment.