Skip to content

Commit 27a3f7b

Browse files
committed
Add secrets prop for sensitive data
1 parent f290989 commit 27a3f7b

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ export type WebServerService = {
131131
environment?:
132132
| aws.ecs.KeyValuePair[]
133133
| ((services: Services) => aws.ecs.KeyValuePair[]);
134+
secrets?: aws.ecs.Secret[];
134135
image: pulumi.Input<string>;
135136
port: pulumi.Input<number>;
136137
domain: pulumi.Input<string>;
@@ -181,6 +182,27 @@ const project = new studion.Project('demo-project', {
181182
});
182183
```
183184

185+
In order to pass sensitive information to the container use `secrets` instead of `environment`. AWS will fetch values from
186+
Secret Manager based on arn that is provided for the `valueFrom` field.
187+
188+
```ts
189+
const project = new studion.Project('demo-project', {
190+
environment: 'DEVELOPMENT',
191+
services: [
192+
{
193+
type: 'WEB_SERVER',
194+
serviceName: 'api',
195+
image: imageUri,
196+
port: 3000,
197+
domain: 'api.my-domain.com',
198+
secrets: [
199+
{ name: 'DB_PASSWORD', valueFrom: 'arn-of-the-secret-manager-secret' },
200+
],
201+
},
202+
],
203+
});
204+
```
205+
184206
### Database
185207

186208
AWS RDS Postgres instance.
@@ -331,6 +353,7 @@ export type WebServerArgs = {
331353
maxCount?: pulumi.Input<number>;
332354
size?: pulumi.Input<Size>;
333355
environment?: aws.ecs.KeyValuePair[];
356+
secrets?: aws.ecs.Secret[];
334357
healtCheckPath?: pulumi.Input<string>;
335358
taskExecutionRoleInlinePolicies?: pulumi.Input<
336359
pulumi.Input<RoleInlinePolicy>[]
@@ -444,3 +467,4 @@ const project = new studion.Project('demo-project', {
444467

445468
- [ ] Add worker service for executing tasks
446469
- [ ] Add MongoDB service
470+
- [ ] Make db username & password fields optional and autogenerate db username & password if they are not provided

src/components/web-server.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,16 @@ export type WebServerArgs = {
8282
*/
8383
size?: pulumi.Input<Size>;
8484
/**
85-
* The environment variables to pass to a container. Defaults to [].
85+
* The environment variables to pass to a container. Don't use this field for
86+
* sensitive information such as passwords, API keys, etc. For that purpose,
87+
* please use the `secrets` property.
88+
* Defaults to [].
8689
*/
8790
environment?: aws.ecs.KeyValuePair[];
91+
/**
92+
* The secrets to pass to the container. Defaults to [].
93+
*/
94+
secrets?: aws.ecs.Secret[];
8895
/**
8996
* Path for the health check request. Defaults to "/healtcheck".
9097
*/
@@ -107,6 +114,7 @@ const defaults = {
107114
maxCount: 10,
108115
size: 'small',
109116
environment: [],
117+
secrets: [],
110118
healtCheckPath: '/healtcheck',
111119
taskExecutionRoleInlinePolicies: [],
112120
taskRoleInlinePolicies: [],
@@ -267,6 +275,21 @@ export class WebServer extends pulumi.ComponentResource {
267275
{ parent: this },
268276
);
269277

278+
const secretManagerSecretsInlinePolicy = {
279+
name: `${name}-secret-manager-access`,
280+
policy: JSON.stringify({
281+
Version: '2012-10-17',
282+
Statement: [
283+
{
284+
Sid: 'AllowContainerToGetSecretManagerSecrets',
285+
Effect: 'Allow',
286+
Action: ['secretsmanager:GetSecretValue'],
287+
Resource: '*',
288+
},
289+
],
290+
}),
291+
};
292+
270293
const taskExecutionRole = new aws.iam.Role(
271294
`${name}-ecs-task-exec-role`,
272295
{
@@ -276,7 +299,10 @@ export class WebServer extends pulumi.ComponentResource {
276299
'arn:aws:iam::aws:policy/CloudWatchFullAccess',
277300
'arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryFullAccess',
278301
],
279-
inlinePolicies: argsWithDefaults.taskExecutionRoleInlinePolicies,
302+
inlinePolicies: [
303+
secretManagerSecretsInlinePolicy,
304+
...argsWithDefaults.taskExecutionRoleInlinePolicies,
305+
],
280306
},
281307
{ parent: this },
282308
);
@@ -344,11 +370,20 @@ export class WebServer extends pulumi.ComponentResource {
344370
argsWithDefaults.image,
345371
argsWithDefaults.port,
346372
argsWithDefaults.environment,
373+
argsWithDefaults.secrets,
347374
this.logGroup.name,
348375
awsRegion,
349376
])
350377
.apply(
351-
([containerName, image, port, environment, logGroup, region]) => {
378+
([
379+
containerName,
380+
image,
381+
port,
382+
environment,
383+
secrets,
384+
logGroup,
385+
region,
386+
]) => {
352387
return JSON.stringify([
353388
{
354389
readonlyRootFilesystem: false,
@@ -370,6 +405,7 @@ export class WebServer extends pulumi.ComponentResource {
370405
},
371406
},
372407
environment,
408+
secrets,
373409
},
374410
] as ContainerDefinition[]);
375411
},

0 commit comments

Comments
 (0)