Skip to content

Commit c2059b7

Browse files
authored
Update Cloud SQL sample apps with SSL example (#2361)
* Update Cloud SQL sample apps with SSL example * Lint. * Update region tag. * Move certs folder into mysql\mysql. * Update Dockerfile WORKDIR and COPY certs. * Update mysql sslmode to verify-full. * Lint.
1 parent 9804b07 commit c2059b7

File tree

6 files changed

+74
-7
lines changed

6 files changed

+74
-7
lines changed

cloud-sql/mysql/mysql/Dockerfile

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
FROM node:10-slim
88

99
# Create and change to the app directory.
10-
WORKDIR /usr/src/app
10+
WORKDIR /app
1111

1212
# Copy application dependency manifests to the container image.
1313
# A wildcard is used to ensure both package.json AND package-lock.json are copied.
@@ -19,6 +19,9 @@ COPY package*.json ./
1919
# RUN npm ci --only=production
2020
RUN npm install --production
2121

22+
# Copy any certificates if present.
23+
COPY ./certs /app/certs
24+
2225
# Copy local code to the container image.
2326
COPY . ./
2427

cloud-sql/mysql/mysql/certs/.gitkeep

Whitespace-only changes.

cloud-sql/mysql/mysql/server.js

+33-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
const express = require('express');
1818
const mysql = require('promise-mysql');
19+
const fs = require('fs');
1920

2021
const app = express();
2122
app.set('view engine', 'pug');
@@ -41,13 +42,37 @@ const logger = winston.createLogger({
4142
transports: [new winston.transports.Console(), loggingWinston],
4243
});
4344

45+
// [START cloud_sql_mysql_mysql_create_tcp_sslcerts]
46+
const createTcpPoolSslCerts = async config => {
47+
// Extract host and port from socket address
48+
const dbSocketAddr = process.env.DB_HOST.split(':');
49+
50+
// Establish a connection to the database
51+
return mysql.createPool({
52+
user: process.env.DB_USER, // e.g. 'my-db-user'
53+
password: process.env.DB_PASS, // e.g. 'my-db-password'
54+
database: process.env.DB_NAME, // e.g. 'my-database'
55+
host: dbSocketAddr[0], // e.g. '127.0.0.1'
56+
port: dbSocketAddr[1], // e.g. '3306'
57+
ssl: {
58+
sslmode: 'verify-full',
59+
ca: fs.readFileSync(process.env.DB_ROOT_CERT), // e.g., '/path/to/my/server-ca.pem'
60+
key: fs.readFileSync(process.env.DB_KEY), // e.g. '/path/to/my/client-key.pem'
61+
cert: fs.readFileSync(process.env.DB_CERT), // e.g. '/path/to/my/client-cert.pem'
62+
},
63+
// ... Specify additional properties here.
64+
...config,
65+
});
66+
};
67+
// [END cloud_sql_mysql_mysql_create_tcp_sslcerts]
68+
4469
// [START cloud_sql_mysql_mysql_create_tcp]
4570
const createTcpPool = async config => {
4671
// Extract host and port from socket address
4772
const dbSocketAddr = process.env.DB_HOST.split(':');
4873

4974
// Establish a connection to the database
50-
return await mysql.createPool({
75+
return mysql.createPool({
5176
user: process.env.DB_USER, // e.g. 'my-db-user'
5277
password: process.env.DB_PASS, // e.g. 'my-db-password'
5378
database: process.env.DB_NAME, // e.g. 'my-database'
@@ -64,7 +89,7 @@ const createUnixSocketPool = async config => {
6489
const dbSocketPath = process.env.DB_SOCKET_PATH || '/cloudsql';
6590

6691
// Establish a connection to the database
67-
return await mysql.createPool({
92+
return mysql.createPool({
6893
user: process.env.DB_USER, // e.g. 'my-db-user'
6994
password: process.env.DB_PASS, // e.g. 'my-db-password'
7095
database: process.env.DB_NAME, // e.g. 'my-database'
@@ -106,9 +131,13 @@ const createPool = async () => {
106131
// [END cloud_sql_mysql_mysql_backoff]
107132
};
108133
if (process.env.DB_HOST) {
109-
return await createTcpPool(config);
134+
if (process.env.DB_ROOT_CERT) {
135+
return createTcpPoolSslCerts(config);
136+
} else {
137+
return createTcpPool(config);
138+
}
110139
} else {
111-
return await createUnixSocketPool(config);
140+
return createUnixSocketPool(config);
112141
}
113142
};
114143

cloud-sql/postgres/knex/Dockerfile

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
FROM node:10-slim
88

99
# Create and change to the app directory.
10-
WORKDIR /usr/src/app
10+
WORKDIR /app
1111

1212
# Copy application dependency manifests to the container image.
1313
# A wildcard is used to ensure both package.json AND package-lock.json are copied.
@@ -19,6 +19,9 @@ COPY package*.json ./
1919
# RUN npm ci --only=production
2020
RUN npm install --production
2121

22+
# Copy any certificates if present.
23+
COPY ./certs /app/certs
24+
2225
# Copy local code to the container image.
2326
COPY . ./
2427

cloud-sql/postgres/knex/certs/.gitkeep

Whitespace-only changes.

cloud-sql/postgres/knex/server.js

+33-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const process = require('process');
1919

2020
const express = require('express');
2121
const Knex = require('knex');
22+
const fs = require('fs');
2223

2324
const app = express();
2425
app.set('view engine', 'pug');
@@ -62,6 +63,33 @@ app.use(async (req, res, next) => {
6263
}
6364
});
6465

66+
// [START cloud_sql_postgres_knex_create_tcp_sslcerts]
67+
const createTcpPoolSslCerts = async config => {
68+
// Extract host and port from socket address
69+
const dbSocketAddr = process.env.DB_HOST.split(':'); // e.g. '127.0.0.1:5432'
70+
71+
// Establish a connection to the database
72+
return Knex({
73+
client: 'pg',
74+
connection: {
75+
user: process.env.DB_USER, // e.g. 'my-user'
76+
password: process.env.DB_PASS, // e.g. 'my-user-password'
77+
database: process.env.DB_NAME, // e.g. 'my-database'
78+
host: dbSocketAddr[0], // e.g. '127.0.0.1'
79+
port: dbSocketAddr[1], // e.g. '5432'
80+
ssl: {
81+
rejectUnauthorized: false,
82+
ca: fs.readFileSync(process.env.DB_ROOT_CERT), // e.g., '/path/to/my/server-ca.pem'
83+
key: fs.readFileSync(process.env.DB_KEY), // e.g. '/path/to/my/client-key.pem'
84+
cert: fs.readFileSync(process.env.DB_CERT), // e.g. '/path/to/my/client-cert.pem'
85+
},
86+
},
87+
// ... Specify additional properties here.
88+
...config,
89+
});
90+
};
91+
// [END cloud_sql_postgres_knex_create_tcp_sslcerts]
92+
6593
// [START cloud_sql_postgres_knex_create_tcp]
6694
const createTcpPool = async config => {
6795
// Extract host and port from socket address
@@ -140,7 +168,11 @@ const createPool = async () => {
140168
// [END cloud_sql_postgres_knex_backoff]
141169

142170
if (process.env.DB_HOST) {
143-
return createTcpPool(config);
171+
if (process.env.DB_ROOT_CERT) {
172+
return createTcpPoolSslCerts(config);
173+
} else {
174+
return createTcpPool(config);
175+
}
144176
} else {
145177
return createUnixSocketPool(config);
146178
}

0 commit comments

Comments
 (0)