Skip to content

Commit 773a4bc

Browse files
authored
Database secret handling fix (#17)
1 parent 606cc15 commit 773a4bc

3 files changed

Lines changed: 36 additions & 47 deletions

File tree

.github/workflows/_validate.yml

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,22 @@ jobs:
6666
- name: Build
6767
run: pnpm build
6868

69-
- name: Make directory for build artifacts
70-
run: mkdir -p output/build-artifacts
71-
72-
- name: Spotlight app performance budget
73-
run: pnpm --filter @flexion/forms-spotlight size:ci > output/build-artifacts/spotlight-size-output.txt
74-
75-
- name: Design package performance budget
76-
run: pnpm --filter @flexion/forms-design size:ci > output/build-artifacts/design-size-output.txt
77-
78-
- name: Upload size:ci results
79-
uses: actions/upload-artifact@v4
80-
with:
81-
name: size-limit-results
82-
path: |
83-
output/build-artifacts/spotlight-size-output.txt
84-
output/build-artifacts/design-size-output.txt
69+
# - name: Make directory for build artifacts
70+
# run: mkdir -p output/build-artifacts
71+
72+
# - name: Spotlight app performance budget
73+
# run: pnpm --filter @flexion/forms-spotlight size:ci > output/build-artifacts/spotlight-size-output.txt
74+
75+
# - name: Design package performance budget
76+
# run: pnpm --filter @flexion/forms-design size:ci > output/build-artifacts/design-size-output.txt
77+
78+
# - name: Upload size:ci results
79+
# uses: actions/upload-artifact@v4
80+
# with:
81+
# name: size-limit-results
82+
# path: |
83+
# output/build-artifacts/spotlight-size-output.txt
84+
# output/build-artifacts/design-size-output.txt
8585

8686
- name: Lint source code
8787
shell: bash

apps/sandbox/src/index.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,31 @@ import { createCustomServer } from './server.js';
66
const port = process.env.PORT || 4321;
77

88
const getAppRunnerSecrets = async () => {
9-
const dbSecretStr = process.env.DB_SECRET;
9+
const dbSecretArn = process.env.DB_SECRET;
1010
const dbHost = process.env.DB_HOST;
1111
const dbPort = process.env.DB_PORT;
1212
const dbName = process.env.DB_NAME;
1313

14-
if (!dbSecretStr || !dbHost || !dbPort || !dbName) {
14+
if (!dbSecretArn || !dbHost || !dbPort || !dbName) {
1515
console.error(
1616
'Missing required environment variables: DB_SECRET, DB_HOST, DB_PORT, DB_NAME'
1717
);
1818
return;
1919
}
2020

21+
// Fetch the secret from AWS Secrets Manager using the ARN
22+
const vault = getAWSSecretsManagerVault();
23+
const dbSecretStr = await vault.getSecret(dbSecretArn);
24+
25+
if (!dbSecretStr) {
26+
console.error('Failed to retrieve secret from Secrets Manager');
27+
return;
28+
}
29+
2130
const dbSecret = JSON.parse(dbSecretStr);
2231
if (!dbSecret.username || !dbSecret.password) {
2332
console.error(
24-
'`DB_SECRET` environment variable is missing username or password'
33+
'Secret from Secrets Manager is missing username or password'
2534
);
2635
return;
2736
}

infra/cdktf/src/lib/aws/sandbox-stack.ts

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import { Route } from '../../../.gen/providers/aws/route';
1010
import { SecurityGroup } from '../../../.gen/providers/aws/security-group';
1111
import { DbSubnetGroup } from '../../../.gen/providers/aws/db-subnet-group';
1212
import { DbInstance } from '../../../.gen/providers/aws/db-instance';
13-
import { SecretsmanagerSecret } from '../../../.gen/providers/aws/secretsmanager-secret';
14-
import { SecretsmanagerSecretVersion } from '../../../.gen/providers/aws/secretsmanager-secret-version';
1513
import { EcrRepository } from '../../../.gen/providers/aws/ecr-repository';
1614
import { ApprunnerVpcConnector } from '../../../.gen/providers/aws/apprunner-vpc-connector';
1715
import { ApprunnerService } from '../../../.gen/providers/aws/apprunner-service';
@@ -20,8 +18,6 @@ import { IamRolePolicy } from '../../../.gen/providers/aws/iam-role-policy';
2018
import { IamRolePolicyAttachment } from '../../../.gen/providers/aws/iam-role-policy-attachment';
2119
import { DataAwsAvailabilityZones } from '../../../.gen/providers/aws/data-aws-availability-zones';
2220

23-
import { getDatabaseSecretKey } from '@flexion/forms-infra-core';
24-
2521
interface SandboxStackConfig {
2622
environment: string;
2723
}
@@ -151,28 +147,8 @@ export class SandboxStack extends Construct {
151147
},
152148
});
153149

154-
// Generate random password for database
150+
// Database username (password will be managed by RDS in Secrets Manager)
155151
const dbUsername = 'postgres';
156-
const dbPassword = Fn.base64encode(
157-
Fn.uuid() // Use UUID for a secure random password
158-
);
159-
160-
// Database secret (only username and password - host/port/db passed as env vars)
161-
const dbSecret = new SecretsmanagerSecret(this, `${id}-db-secret`, {
162-
name: getDatabaseSecretKey(environment),
163-
description: `Database credentials for ${environment}`,
164-
tags: {
165-
Environment: environment,
166-
},
167-
});
168-
169-
new SecretsmanagerSecretVersion(this, `${id}-db-secret-version`, {
170-
secretId: dbSecret.id,
171-
secretString: Fn.jsonencode({
172-
username: dbUsername,
173-
password: dbPassword,
174-
}),
175-
});
176152

177153
// RDS Subnet Group
178154
const dbSubnetGroup = new DbSubnetGroup(this, `${id}-db-subnet-group`, {
@@ -184,7 +160,7 @@ export class SandboxStack extends Construct {
184160
},
185161
});
186162

187-
// RDS Instance
163+
// RDS Instance with AWS-managed password in Secrets Manager
188164
const rdsInstance = new DbInstance(this, `${id}-db`, {
189165
identifier: `${id}-db`,
190166
engine: 'postgres',
@@ -194,7 +170,7 @@ export class SandboxStack extends Construct {
194170
maxAllocatedStorage: 100,
195171
dbName: 'postgres',
196172
username: dbUsername,
197-
password: dbPassword,
173+
manageMasterUserPassword: true,
198174
dbSubnetGroupName: dbSubnetGroup.name,
199175
vpcSecurityGroupIds: [rdsSecurityGroup.id],
200176
publiclyAccessible: false,
@@ -340,7 +316,11 @@ export class SandboxStack extends Construct {
340316
DB_NAME: 'postgres',
341317
},
342318
runtimeEnvironmentSecrets: {
343-
DB_SECRET: dbSecret.arn,
319+
DB_SECRET: Fn.lookup(
320+
Fn.element(rdsInstance.masterUserSecret, 0),
321+
'secret_arn',
322+
''
323+
),
344324
},
345325
},
346326
},

0 commit comments

Comments
 (0)