Skip to content

Commit b21f67a

Browse files
authored
Update SSL settings for Cloud SQL (kriasoft#239)
1 parent b2ef3df commit b21f67a

File tree

17 files changed

+72
-34
lines changed

17 files changed

+72
-34
lines changed

.vscode/settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
"envalid",
2424
"jsonb",
2525
"knexfile",
26+
"kriasoft",
27+
"pgappname",
2628
"pgdatabase",
2729
"pgdebug",
2830
"pghost",

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ infrastructure, using code-first **GraphQL API** and **PostgreSQL** backend.
1616

1717
---
1818

19-
This monorepo was bootstrapped with [Node.js API Starter Kit](https://github.com/kriasoft/nodejs-api-starter).
19+
This project was bootstrapped with [Node.js API Starter Kit](https://github.com/kriasoft/nodejs-api-starter).
2020
Be sure to join our [Discord channel](https://discord.com/invite/bSsv7XM) for assistance.
2121

22-
## Monorepo Structure
22+
## Directory Structure
2323

2424
`├──`[`.github`](.github) — GitHub configuration including CI/CD<br>
2525
`├──`[`.vscode`](.vscode) — VSCode settings including code snippets, recommended extensions etc.<br>

api/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ environment such as [Google Cloud Functions](https://cloud.google.com/functions)
66
or [Google Cloud Run](https://cloud.google.com/run).
77

88
This project was bootstrapped with [Node.js API Starter Kit](https://github.com/kriasoft/nodejs-api-starter).
9-
Be sure to join our [Discord channel](https://discord.com/invite/bSsv7XM) if you
10-
need some help.
9+
Be sure to join our [Discord channel](https://discord.com/invite/bSsv7XM) for
10+
assistance.
1111

1212
## Tech Stack
1313

api/scripts/deploy.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ import spawn from "cross-spawn";
1414
import minimist from "minimist";
1515

1616
import env from "../src/env";
17-
import pkg from "../package.json";
17+
import { name } from "../package.json";
1818

1919
const args = minimist(process.argv.slice(3));
20-
const project = process.env.GOOGLE_CLOUD_PROJECT;
21-
const region = process.env.GOOGLE_CLOUD_REGION;
2220
const version = args.version ?? os.userInfo().username;
2321

2422
const envVars = [
@@ -27,11 +25,11 @@ const envVars = [
2725
`APP_VERSION=${version}`,
2826
`APP_ENV=${env.APP_ENV}`,
2927
`JWT_SECRET=${env.JWT_SECRET}`,
30-
`JWT_EXPIRES=${env.JWT_EXPIRES}`,
31-
`PGHOST=/cloudsql/${project}:${region}:db`,
28+
`PGHOST=/cloudsql/${env.GOOGLE_CLOUD_SQL}`,
3229
`PGUSER=${env.PGUSER}`,
3330
`PGPASSWORD=${env.PGPASSWORD}`,
3431
`PGDATABASE=${env.PGDATABASE}`,
32+
`PGAPPNAME=${name}_${version}`,
3533
];
3634

3735
spawn.sync(
@@ -40,13 +38,13 @@ spawn.sync(
4038
`--project=${process.env.GOOGLE_CLOUD_PROJECT}`,
4139
`functions`,
4240
`deploy`,
43-
pkg.name,
41+
name,
4442
`--region=${process.env.GOOGLE_CLOUD_REGION}`,
4543
`--allow-unauthenticated`,
46-
`--entry-point=${pkg.name}`,
44+
`--entry-point=${name}`,
4745
`--memory=2GB`,
4846
`--runtime=nodejs12`,
49-
`--source=gs://${process.env.PKG_BUCKET}/api_${version}.zip`,
47+
`--source=gs://${process.env.PKG_BUCKET}/${name}_${version}.zip`,
5048
`--timeout=30`,
5149
`--set-env-vars=${envVars.join(",")}`,
5250
`--trigger-http`,

api/src/db.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ const db = knex({
1313
client: "pg",
1414

1515
connection: {
16-
ssl: env.PGSSLMODE !== "disable" && {
17-
rejectUnauthorized: false,
18-
cert: fs.readFileSync(env.PGSSLCERT, "utf8"),
19-
key: fs.readFileSync(env.PGSSLKEY, "utf8"),
20-
ca: fs.readFileSync(env.PGSSLROOTCERT, "utf8"),
16+
ssl: env.PGSSLMODE === "verify-ca" && {
17+
cert: fs.readFileSync(env.PGSSLCERT, "ascii"),
18+
key: fs.readFileSync(env.PGSSLKEY, "ascii"),
19+
ca: fs.readFileSync(env.PGSSLROOTCERT, "ascii"),
20+
servername: ((x) => `${x[0]}:${x[2]}`)(env.GOOGLE_CLOUD_SQL.split(":")),
2121
},
2222
},
2323

api/src/env.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default cleanEnv(
2323
JWT_SECRET: str(),
2424
JWT_EXPIRES: num({ default: 60 * 60 * 24 * 14 /* 2 weeks */ }),
2525

26+
GOOGLE_CLOUD_SQL: str({ default: "" }),
2627
PGHOST: str(),
2728
PGPORT: num({ default: 5432 }),
2829
PGUSER: str(),

api/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ if (!env.isProduction) {
4646
});
4747

4848
app.listen(env.PORT, () => {
49-
console.log(`API listening on http://localhost:${env.PORT}/`);
49+
const meta = `env: ${env.APP_ENV}, db: ${env.PGDATABASE}`;
50+
console.log(`API listening on http://localhost:${env.PORT}/ (${meta})`);
5051
require("../scripts/update-schema");
5152
});
5253
}

db/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ Migration and seed files plus some administration scripts that help to design
44
a PostgreSQL database.
55

66
This project was bootstrapped with [Node.js API Starter Kit](https://github.com/kriasoft/nodejs-api-starter).
7-
Be sure to join our [Discord channel](https://discord.com/invite/bSsv7XM) if you
8-
need some help.
7+
Be sure to join our [Discord channel](https://discord.com/invite/bSsv7XM) for
8+
assistance.
99

1010
## Directory Layout
1111

db/knexfile.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* @copyright 2016-present Kriasoft (https://git.io/vMINh)
66
*/
77

8+
const fs = require("fs");
9+
810
// Load environment variables (PGHOST, PGUSER, etc.)
911
require("env");
1012

@@ -14,7 +16,16 @@ require("env");
1416
module.exports = {
1517
client: "pg",
1618

17-
connection: {},
19+
connection: {
20+
ssl: process.env.PGSSLMODE === "verify-ca" && {
21+
cert: fs.readFileSync(process.env.PGSSLCERT, "ascii"),
22+
key: fs.readFileSync(process.env.PGSSLKEY, "ascii"),
23+
ca: fs.readFileSync(process.env.PGSSLROOTCERT, "ascii"),
24+
servername: ((x) => `${x[0]}:${x[2]}`)(
25+
process.env.GOOGLE_CLOUD_SQL.split(":"),
26+
),
27+
},
28+
},
1829

1930
pool: { min: 0, max: 1 },
2031

env/.env

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@ APP_NAME=example
66
APP_VERSION=latest
77

88
# Google Cloud Platform (GCP) defaults
9-
GOOGLE_CLOUD_PROJECT=
109
GOOGLE_CLOUD_REGION=us-central1
1110

1211
# Cloud storage bucket that is used for saving
1312
# application bundles (build articacts) during CI/CD workflows
1413
PKG_BUCKET=pkg.example.com
15-
16-
# Cloud storage bucket for user uploaded content and other assets
17-
STORAGE_BUCKET=s.example.com

0 commit comments

Comments
 (0)