-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Postgres database support (#293)
* Rename database contexts to their method (in memory, filesystem) rather than dev vs test * Add Postgres Testcontainer, implemented via a describeDatabase() test suite factory that will run tests via both Sqlite and Postgres * Wire describeDatabase up to each db gateway test, and create db helper module for dealing with date differences between sqlite (INTEGER) and Postgres (TIMESTAMP). * Add ability to share Postgres container across all the database package tests. * Get Postgres and its testability wired up to the DOJ demo app. The entrypoint still needs cloud.gov environment settings. * Add Postgres RDS to cloud.gov environment * temp: run the deploy on this branch for testing * Postgres Terraform config + app wiring * Fix aws-rds VCAP_SERVICES lookup path * on cloud, use ssl with postgres * Ignore self-signed cert errors with rejectUnauthorized. Revisit by seeing if we can install the RDS certs on the container * Hack in Postgres Lucia adapter * To satisfy Lucia, use TEXT for session and user ID types. We will want to move these queries into the database package so we can get test coverage over them. * Add allowlists for demo apps * Remove deploy on feature branch
- Loading branch information
1 parent
318d3ee
commit 7dd7dd8
Showing
106 changed files
with
1,491 additions
and
415 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import esbuild from 'esbuild'; | ||
|
||
esbuild | ||
.build({ | ||
bundle: true, | ||
entryPoints: ['./src/index.ts'], | ||
format: 'esm', | ||
minify: true, | ||
outdir: './dist', | ||
platform: 'node', | ||
sourcemap: true, | ||
target: 'esnext', | ||
}) | ||
.catch(() => process.exit(1)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,21 @@ | ||
import { createCustomServer } from './server.js'; | ||
|
||
const port = process.env.PORT || 4321; | ||
const app = await createCustomServer(); | ||
|
||
app.listen(port, () => { | ||
console.log(`Server running on http://localhost:${port}`); | ||
}); | ||
const getCloudGovServerSecrets = () => { | ||
if (process.env.VCAP_SERVICES === undefined) { | ||
throw new Error('VCAP_SERVICES not found'); | ||
} | ||
const services = JSON.parse(process.env.VCAP_SERVICES || '{}'); | ||
return { | ||
//loginGovClientSecret: services['user-provided']?.credentials?.SECRET_LOGIN_GOV_PRIVATE_KEY, | ||
dbUri: services['aws-rds'][0].credentials.uri as string, | ||
}; | ||
}; | ||
|
||
const secrets = getCloudGovServerSecrets(); | ||
createCustomServer({ dbUri: secrets?.dbUri }).then((server: any) => | ||
server.listen(port, () => { | ||
console.log(`Server running on http://localhost:${port}`); | ||
}) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,15 @@ | ||
import path, { dirname } from 'path'; | ||
import { fileURLToPath } from 'url'; | ||
import { | ||
createDatabaseGateway, | ||
createPostgresDatabaseContext, | ||
} from '@atj/database'; | ||
import { createServer } from '@atj/server'; | ||
|
||
const getDirname = () => dirname(fileURLToPath(import.meta.url)); | ||
|
||
export const createCustomServer = async (): Promise<any> => { | ||
const { createDevDatabaseContext, createDatabaseGateway } = await import( | ||
'@atj/database' | ||
); | ||
const { createServer } = await import('@atj/server'); | ||
|
||
const dbCtx = await createDevDatabaseContext( | ||
path.join(getDirname(), '../doj.db') | ||
export const createCustomServer = async (ctx: { | ||
dbUri: string; | ||
}): Promise<any> => { | ||
const db = createDatabaseGateway( | ||
await createPostgresDatabaseContext(ctx.dbUri, true) | ||
); | ||
const db = createDatabaseGateway(dbCtx); | ||
|
||
return createServer({ | ||
title: 'DOJ Form Service', | ||
|
@@ -23,16 +20,18 @@ export const createCustomServer = async (): Promise<any> => { | |
'urn:gov:gsa:openidconnect.profiles:sp:sso:gsa:tts-10x-atj-dev-server-doj', | ||
//clientSecret: '', // secrets.loginGovClientSecret, | ||
}, | ||
isUserAuthorized: async (email: string) => { | ||
return [ | ||
// 10x team members | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
// DOJ test users | ||
'[email protected]', | ||
'[email protected]', | ||
].includes(email); | ||
}, | ||
}); | ||
}; | ||
|
||
/* | ||
const getServerSecrets = () => { | ||
const services = JSON.parse(process.env.VCAP_SERVICES || '{}'); | ||
const loginClientSecret = | ||
services['user-provided']?.credentials?.SECRET_LOGIN_GOV_PRIVATE_KEY; | ||
return { | ||
loginGovClientSecret: loginClientSecret, | ||
}; | ||
}; | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"module": "ESNext", | ||
"emitDeclarationOnly": true, | ||
"module": "CommonJS", | ||
"moduleResolution": "NodeNext", | ||
"outDir": "./dist", | ||
"target": "es2022" | ||
"emitDeclarationOnly": true | ||
}, | ||
"include": ["./src/**/*"], | ||
"exclude": ["./dist"], | ||
"include": ["./src"], | ||
"references": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { defineConfig } from 'vitest/config'; | ||
import { getDatabaseTestContainerGlobalSetupPath } from '@atj/database'; | ||
|
||
import sharedTestConfig from '../../vitest.shared'; | ||
|
||
export default defineConfig({ | ||
...sharedTestConfig, | ||
test: { | ||
...sharedTestConfig.test, | ||
globalSetup: [getDatabaseTestContainerGlobalSetupPath()], | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,21 @@ | ||
import { createCustomServer } from './server'; | ||
import { createCustomServer } from './server.js'; | ||
|
||
const port = process.env.PORT || 4321; | ||
const app = await createCustomServer(); | ||
|
||
app.listen(port, () => { | ||
console.log(`Server running on http://localhost:${port}`); | ||
}); | ||
const getCloudGovServerSecrets = () => { | ||
if (process.env.VCAP_SERVICES === undefined) { | ||
throw new Error('VCAP_SERVICES not found'); | ||
} | ||
const services = JSON.parse(process.env.VCAP_SERVICES || '{}'); | ||
return { | ||
//loginGovClientSecret: services['user-provided']?.credentials?.SECRET_LOGIN_GOV_PRIVATE_KEY, | ||
dbUri: services['aws-rds'][0].credentials.uri as string, | ||
}; | ||
}; | ||
|
||
const secrets = getCloudGovServerSecrets(); | ||
createCustomServer({ dbUri: secrets?.dbUri }).then((server: any) => | ||
server.listen(port, () => { | ||
console.log(`Server running on http://localhost:${port}`); | ||
}) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,33 @@ | ||
import path, { dirname } from 'path'; | ||
import { fileURLToPath } from 'url'; | ||
import { | ||
createDatabaseGateway, | ||
createPostgresDatabaseContext, | ||
} from '@atj/database'; | ||
import { createServer } from '@atj/server'; | ||
|
||
const getDirname = () => dirname(fileURLToPath(import.meta.url)); | ||
|
||
export const createCustomServer = async (): Promise<any> => { | ||
const { createDevDatabaseContext, createDatabaseGateway } = await import( | ||
'@atj/database' | ||
); | ||
const { createServer } = await import('@atj/server'); | ||
|
||
const dbCtx = await createDevDatabaseContext( | ||
path.join(getDirname(), '../doj.db') | ||
export const createCustomServer = async (ctx: { | ||
dbUri: string; | ||
}): Promise<any> => { | ||
const db = createDatabaseGateway( | ||
await createPostgresDatabaseContext(ctx.dbUri, true) | ||
); | ||
const db = createDatabaseGateway(dbCtx); | ||
|
||
return createServer({ | ||
title: 'KS Courts Form Service', | ||
title: 'DOJ Form Service', | ||
db, | ||
loginGovOptions: { | ||
loginGovUrl: 'https://idp.int.identitysandbox.gov', | ||
clientId: | ||
'urn:gov:gsa:openidconnect.profiles:sp:sso:gsa:tts-10x-atj-dev-server-doj', | ||
//clientSecret: '', // secrets.loginGovClientSecret, | ||
}, | ||
isUserAuthorized: async (email: string) => { | ||
return [ | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
].includes(email); | ||
}, | ||
}); | ||
}; | ||
|
||
/* | ||
const getServerSecrets = () => { | ||
const services = JSON.parse(process.env.VCAP_SERVICES || '{}'); | ||
const loginClientSecret = | ||
services['user-provided']?.credentials?.SECRET_LOGIN_GOV_PRIVATE_KEY; | ||
return { | ||
loginGovClientSecret: loginClientSecret, | ||
}; | ||
}; | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,20 @@ | ||
import request from 'supertest'; | ||
import { beforeAll, describe, expect, test } from 'vitest'; | ||
import { describe, expect, test } from 'vitest'; | ||
import { describeDatabase } from '@atj/database/testing'; | ||
|
||
import { createCustomServer } from '../src/server'; | ||
|
||
describe('Kansas State Courts Form Service', () => { | ||
let app: any; | ||
|
||
beforeAll(async () => { | ||
app = await createCustomServer(); | ||
describe('DOJ Form Service', () => { | ||
test('avoid "No test suite found in file" error', async () => { | ||
expect(true).toBe(true); | ||
}); | ||
}); | ||
|
||
test('renders the home page', async () => { | ||
describeDatabase('Kansas State Courts Form Service', () => { | ||
test('renders the home page', async ({ db }) => { | ||
const app = await createCustomServer({ dbUri: db.ctx.connectionUri }); | ||
const response = await request(app).get('/'); | ||
expect(response.ok).toBe(true); | ||
expect(response.text).toMatch(/KS Courts Form Service/); | ||
expect(response.text).toMatch(/DOJ Form Service/); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.