Skip to content

Commit 837f3a1

Browse files
committed
Add support for RDS using a Secret in the installer
1 parent 0932eb6 commit 837f3a1

File tree

3 files changed

+48
-30
lines changed

3 files changed

+48
-30
lines changed

lib/database.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import * as rds from '@aws-cdk/aws-rds';
33
import * as cdk from '@aws-cdk/core';
44
import { SecretValue } from '@aws-cdk/core';
55
import * as ssm from '@aws-cdk/aws-ssm';
6-
import { ParameterGroup } from '@aws-cdk/aws-rds';
76

87
export interface DatabaseProps extends cdk.StackProps {
98
readonly clusterName: string;
@@ -17,18 +16,14 @@ export interface DatabaseProps extends cdk.StackProps {
1716

1817
export class Database extends cdk.Stack {
1918
readonly credentials: string
20-
readonly endpoint: string
21-
readonly username: string
22-
readonly port: string
23-
readonly database: string
24-
readonly region: string
2519

2620
constructor(scope: cdk.Construct, id: string, props: DatabaseProps) {
2721
super(scope, id, props);
2822

23+
const rdsVersion = rds.MysqlEngineVersion.VER_5_7;
2924
const parameterGroup = new rds.ParameterGroup(this, "DBParameterGroup", {
3025
engine: props.instanceEngine ?? rds.DatabaseInstanceEngine.mysql({
31-
version: rds.MysqlEngineVersion.VER_5_7,
26+
version: rdsVersion,
3227
}),
3328
parameters: {
3429
explicit_defaults_for_timestamp: "OFF"
@@ -37,13 +32,13 @@ export class Database extends cdk.Stack {
3732

3833
// TODO: remove when the gitpod helm chart supports using secrets from ssm
3934
this.credentials = ssm.StringParameter.valueForStringParameter(
40-
this, `/gitpod/cluster/${props.clusterName}/region/${props.vpc.stack.region}`, 1);
35+
this, `/gitpod/cluster/${props.clusterName}/region/${props.vpc.stack.region}`);
4136

4237
const instance = new rds.DatabaseInstance(this, 'Gitpod', {
4338
vpc: props.vpc,
44-
vpcPlacement: { subnetType: ec2.SubnetType.PRIVATE },
39+
vpcSubnets: { subnetType: ec2.SubnetType.PRIVATE_WITH_NAT },
4540
engine: props.instanceEngine ?? rds.DatabaseInstanceEngine.mysql({
46-
version: rds.MysqlEngineVersion.VER_5_7,
41+
version: rdsVersion,
4742
}),
4843
storageEncrypted: true,
4944
backupRetention: props.backupRetention ?? cdk.Duration.days(7),
@@ -67,10 +62,17 @@ export class Database extends cdk.Stack {
6762
// allow from the whole vpc cidr
6863
instance.connections.allowFrom(ec2.Peer.ipv4(props.vpc.vpcCidrBlock), ec2.Port.tcp(3306));
6964

70-
this.endpoint = instance.dbInstanceEndpointAddress;
71-
this.username = props.username;
72-
this.port = '3306';
73-
this.database = 'gitpod';
74-
this.region = props.vpc.stack.region;
65+
new cdk.CfnOutput(this, "MysqlEndpoint", {
66+
value: instance.dbInstanceEndpointAddress,
67+
exportName: "MysqlEndpoint",
68+
});
69+
new cdk.CfnOutput(this, "MysqlUsername", {
70+
value: props.username,
71+
exportName: "MysqlUsername",
72+
});
73+
new cdk.CfnOutput(this, "MysqlPort", {
74+
value: '3306',
75+
exportName: "MysqlPort",
76+
});
7577
}
7678
}

lib/services.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import * as cdk from '@aws-cdk/core'
33
import * as ec2 from '@aws-cdk/aws-ec2'
44

55
import { Database } from './database';
6-
import { Registry } from './registry';
6+
//import { Registry } from './registry';
77

88
export class ServicesStack extends cdk.Stack {
9-
//readonly database: Database
109
//readonly registry: Registry
1110

1211
constructor(scope: cdk.Construct, id: string, props: cdk.StackProps) {
@@ -17,16 +16,16 @@ export class ServicesStack extends cdk.Stack {
1716
vpcName: `eksctl-${process.env.CLUSTER_NAME}-cluster/VPC`,
1817
isDefault: false
1918
});
20-
/*
19+
2120
// create RDS database for gitpod
22-
this.database = new Database(this, 'RDS', {
21+
const database = new Database(this, 'RDS', {
2322
env: props.env,
2423
clusterName: `${process.env.CLUSTER_NAME}`,
2524
vpc,
2625
username: 'gitpod'
2726
})
28-
this.database.node.addDependency(vpc);
29-
*/
27+
database.node.addDependency(vpc);
28+
3029
// create permissions to access S3 buckets
3130
/*
3231
this.registry = new Registry(this, 'Registry', {

setup.sh

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,19 @@ function install() {
141141
# Restart tigera-operator
142142
kubectl delete pod -n tigera-operator -l k8s-app=tigera-operator > /dev/null 2>&1
143143

144-
# TODO: remove once we can reference a secret in the helm chart.
144+
MYSQL_GITPOD_USERNAME="gitpod"
145+
MYSQL_GITPOD_PASSWORD=$(openssl rand -hex 18)
146+
MYSQL_GITPOD_SECRET="mysql-gitpod-token"
147+
MYSQL_GITPOD_ENCRYPTION_KEY='[{"name":"general","version":1,"primary":true,"material":"4uGh1q8y2DYryJwrVMHs0kWXJlqvHWWt/KJuNi04edI="}]'
148+
145149
# generated password cannot excede 41 characters (RDS limitation)
146-
#SSM_KEY="/gitpod/cluster/${CLUSTER_NAME}/region/${AWS_REGION}"
147-
#${AWS_CMD} ssm put-parameter \
148-
# --overwrite \
149-
# --name "${SSM_KEY}" \
150-
# --type String \
151-
# --value "$(date +%s | sha256sum | base64 | head -c 35 ; echo)" \
152-
# --region "${AWS_REGION}" > /dev/null 2>&1
150+
SSM_KEY="/gitpod/cluster/${CLUSTER_NAME}/region/${AWS_REGION}"
151+
${AWS_CMD} ssm put-parameter \
152+
--overwrite \
153+
--name "${SSM_KEY}" \
154+
--type String \
155+
--value "${MYSQL_GITPOD_PASSWORD}" \
156+
--region "${AWS_REGION}" > /dev/null 2>&1
153157

154158
# deploy CDK stacks
155159
cdk deploy \
@@ -162,7 +166,7 @@ function install() {
162166
--outputs-file cdk-outputs.json \
163167
--all
164168

165-
# TLS termination is done in the ALB
169+
# TLS termination is done in the ALB.
166170
cat <<EOF | kubectl apply -f -
167171
apiVersion: cert-manager.io/v1
168172
kind: Certificate
@@ -181,12 +185,25 @@ spec:
181185
secretName: https-certificates
182186
EOF
183187

188+
echo "Create database secret..."
189+
kubectl create secret generic "${MYSQL_GITPOD_SECRET}" \
190+
--from-literal=encryptionKeys="${MYSQL_GITPOD_ENCRYPTION_KEY}" \
191+
--from-literal=host="$(jq -r '. | to_entries[] | select(.key | startswith("ServicesRDS")).value.MysqlEndpoint ' < cdk-outputs.json)" \
192+
--from-literal=password="${MYSQL_GITPOD_PASSWORD}" \
193+
--from-literal=port="3306" \
194+
--from-literal=username="${MYSQL_GITPOD_USERNAME}" \
195+
--dry-run=client -o yaml | \
196+
kubectl replace --force -f -
197+
184198
local CONFIG_FILE="${DIR}/gitpod-config.yaml"
185199
gitpod-installer init > "${CONFIG_FILE}"
186200

187201
yq e -i ".certificate.name = \"https-certificates\"" "${CONFIG_FILE}"
188202
yq e -i ".domain = \"${DOMAIN}\"" "${CONFIG_FILE}"
189203
yq e -i ".metadata.region = \"${AWS_REGION}\"" "${CONFIG_FILE}"
204+
yq e -i ".database.inCluster = false" "${CONFIG_FILE}"
205+
yq e -i ".database.external.certificate.kind = \"secret\"" "${CONFIG_FILE}"
206+
yq e -i ".database.external.certificate.name = \"${MYSQL_GITPOD_SECRET}\"" "${CONFIG_FILE}"
190207
yq e -i '.workspace.runtime.containerdRuntimeDir = "/var/lib/containerd/io.containerd.runtime.v2.task/k8s.io"' "${CONFIG_FILE}"
191208

192209
gitpod-installer \

0 commit comments

Comments
 (0)