-
Notifications
You must be signed in to change notification settings - Fork 0
/
dirtySecretsStack.ts
178 lines (166 loc) · 5.53 KB
/
dirtySecretsStack.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import {
Stack,
StackProps,
Duration,
RemovalPolicy,
CfnOutput,
} from "aws-cdk-lib/core";
import { Construct } from "constructs";
import {
Table,
TableEncryption,
AttributeType,
BillingMode,
CfnTable,
} from "aws-cdk-lib/aws-dynamodb";
import {
PolicyDocument,
PolicyStatement,
Effect,
CfnRole,
} from "aws-cdk-lib/aws-iam";
import { RetentionDays, LogGroup } from "aws-cdk-lib/aws-logs";
import { NodejsFunction, OutputFormat } from "aws-cdk-lib/aws-lambda-nodejs";
import {
Alias,
Architecture,
Runtime,
FunctionUrlAuthType,
} from "aws-cdk-lib/aws-lambda";
export class DirtySecretsStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const tableName = "dirty-secrets";
const stack = Stack.of(this);
const region = stack.region;
const account = stack.account;
const secretsDDB = new Table(this, "SecretsTable", {
tableName,
partitionKey: { name: "principal", type: AttributeType.STRING },
sortKey: { name: "name", type: AttributeType.STRING },
encryption: TableEncryption.AWS_MANAGED,
billingMode: BillingMode.PAY_PER_REQUEST,
resourcePolicy: PolicyDocument.fromJson({
Version: "2012-10-17",
Statement: [
{
Sid: "DenyAllButLambdaRole",
Effect: "Deny",
Principal: "*",
Action: [
"dynamodb:GetItem",
"dynamodb:Query",
"dynamodb:BatchGetItem",
],
Resource: [
`arn:aws:dynamodb:${region}:${account}:table/${tableName}`,
`arn:aws:dynamodb:${region}:${account}:table/${tableName}/*`,
],
Condition: {
"ForAllValues:StringNotEquals": {
"dynamodb:LeadingKeys": ["${aws:PrincipalArn}"],
},
},
},
{
Sid: "DenyExfiltration",
Effect: "Deny",
Principal: "*",
Action: ["dynamodb:Scan"],
Resource: [
`arn:aws:dynamodb:${region}:${account}:table/${tableName}`,
`arn:aws:dynamodb:${region}:${account}:table/${tableName}/*`,
],
},
],
}),
});
const cfnDDB = secretsDDB.node.defaultChild as CfnTable;
cfnDDB.overrideLogicalId("SecretsDDBTable");
const functionName = "dirty-secrets-function";
const logGroup = new LogGroup(this, "LogGroup", {
retention: RetentionDays.ONE_DAY,
logGroupName: `/aws/lambda/${functionName}`,
removalPolicy: RemovalPolicy.DESTROY,
});
const dirtySecretsFunction = new NodejsFunction(
this,
"DirtySecretsFunction",
{
entry: "src/handler.mjs",
description:
"Function to illustrate using DynamoDB as a Secrets Manager",
functionName: "dirty-secrets-function",
runtime: Runtime.NODEJS_20_X,
architecture: Architecture.ARM_64,
timeout: Duration.seconds(3),
logGroup,
bundling: {
format: OutputFormat.ESM,
mainFields: ["module", "main"],
bundleAwsSDK: true,
minify: false,
metafile: false,
// rip out non-essential credential provider stuff
externalModules: [
"@aws-sdk/client-sso",
"@aws-sdk/client-sso-oidc",
"@smithy/credential-provider-imds",
"@aws-sdk/credential-provider-ini",
"@aws-sdk/credential-provider-http",
"@aws-sdk/credential-provider-process",
"@aws-sdk/credential-provider-sso",
"@aws-sdk/credential-provider-web-identity",
"@aws-sdk/token-providers",
],
},
}
);
dirtySecretsFunction.addToRolePolicy(
new PolicyStatement({
effect: Effect.ALLOW,
actions: ["dynamodb:GetItem"],
resources: [secretsDDB.tableArn],
conditions: {
"ForAllValues:StringEquals": {
"dynamodb:LeadingKeys": [dirtySecretsFunction.role?.roleArn],
},
},
})
);
// set lambda role name
let cfnLambdaRole = dirtySecretsFunction.role?.node.defaultChild as CfnRole;
cfnLambdaRole.overrideLogicalId("DirtySecretsFunctionRole");
cfnLambdaRole.addPropertyOverride(
"RoleName",
`${functionName}-lambda-role`
);
// add an alias to the lambda function
const alias = new Alias(this, "FunctionAlias", {
aliasName: "dev",
version: dirtySecretsFunction.currentVersion,
});
let url = alias.addFunctionUrl({
authType: FunctionUrlAuthType.NONE,
});
// Output the URL of the Lambda Function
new CfnOutput(this, "FunctionUrl", {
value: url.url,
description: "The URL of the Lambda Function",
});
new CfnOutput(this, "DDBCreateItemUrl", {
value: `https://${region}.console.aws.amazon.com/dynamodbv2/home?region=${region}#edit-item?itemMode=1&route=ROUTE_ITEM_EXPLORER&table=${tableName}`,
description: "The URL to add secrets to the DynamoDB Table",
});
new CfnOutput(this, "CLIGetItemCommand", {
value: `aws dynamodb get-item --key '{"partition":{"S":"${dirtySecretsFunction.role?.roleArn}"},"name": {"S":"secret"}}' --table-name ${tableName}`,
description:
"The CLI command to run to attempt to get a secret from the table, it will fail",
});
new CfnOutput(this, "FunctionPrincipal", {
value: `${dirtySecretsFunction.role?.roleArn}`,
description:
"The function principal to use when putting secrets in the table",
});
}
}