Skip to content

Commit d9ebc84

Browse files
Merge pull request #1149 from dbartholomae/grant-read-to-key
feat: grant access to key
2 parents afe7917 + bee4c45 commit d9ebc84

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/SopsSecret.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
IGrantable,
55
PolicyStatement,
66
} from 'aws-cdk-lib/aws-iam';
7-
import { IKey } from 'aws-cdk-lib/aws-kms';
7+
import { IKey, ViaServicePrincipal } from 'aws-cdk-lib/aws-kms';
88
import {
99
ISecret,
1010
ISecretAttachmentTarget,
@@ -129,6 +129,15 @@ export class SopsSecret extends Construct implements ISecret {
129129
}
130130

131131
public grantRead(grantee: IGrantable, versionStages?: string[]): Grant {
132+
if (this.encryptionKey) {
133+
// @see https://docs.aws.amazon.com/kms/latest/developerguide/services-secrets-manager.html
134+
this.encryptionKey.grantDecrypt(
135+
new ViaServicePrincipal(
136+
`secretsmanager.${Stack.of(this).region}.amazonaws.com`,
137+
grantee.grantPrincipal,
138+
),
139+
);
140+
}
132141
return this.secret.grantRead(grantee, versionStages);
133142
}
134143
public grantWrite(_grantee: IGrantable): Grant {

src/SopsStringParameter.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,15 @@ export class SopsStringParameter extends Construct implements IStringParameter {
9292
});
9393
}
9494
grantRead(grantee: IGrantable): Grant {
95+
if (this.encryptionKey) {
96+
this.encryptionKey.grantDecrypt(grantee);
97+
}
9598
return this.parameter.grantRead(grantee);
9699
}
97100
grantWrite(grantee: IGrantable): Grant {
101+
if (this.encryptionKey) {
102+
this.encryptionKey.grantEncrypt(grantee);
103+
}
98104
return this.parameter.grantWrite(grantee);
99105
}
100106
applyRemovalPolicy(policy: RemovalPolicy): void {

0 commit comments

Comments
 (0)