-
Notifications
You must be signed in to change notification settings - Fork 178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(server): load secrets from file instead of env var #972
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f6bdf9c
refactor(server): load secrets from file instead of env var
iainsproat 6d2f94a
Fixes broken reference
iainsproat 74ce7f8
Fix typescript types
iainsproat c7d014f
Merge branch 'main' into 897-load-secrets-from-file
iainsproat 94452bc
Use nullish coalescing operator instead of ||
iainsproat 9e71c59
Merge branch 'main' into 897-load-secrets-from-file
iainsproat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,63 @@ | ||
const debug = require('debug')('speckle') | ||
|
||
const { getEmailPassword } = require('@/modules/shared/helpers/secretsHelper') | ||
const nodemailer = require('nodemailer') | ||
const modulesDebug = debug.extend('modules') | ||
const errorDebug = debug.extend('errors') | ||
|
||
/** @type {import('nodemailer').Transporter | undefined} */ | ||
let transporter = undefined | ||
|
||
const createJsonEchoTransporter = () => | ||
nodemailer.createTransport({ | ||
jsonTransport: true | ||
}) | ||
|
||
const initSmtpTransporter = async () => { | ||
try { | ||
const smtpTransporter = nodemailer.createTransport({ | ||
host: process.env.EMAIL_HOST, | ||
port: process.env.EMAIL_PORT || 587, | ||
secure: process.env.EMAIL_SECURE === 'true', | ||
auth: { | ||
user: process.env.EMAIL_USERNAME, | ||
pass: getEmailPassword() | ||
} | ||
}) | ||
await smtpTransporter.verify() | ||
return smtpTransporter | ||
} catch (e) { | ||
errorDebug('📧 Email provider is misconfigured, check config variables.', e) | ||
} | ||
} | ||
|
||
/** | ||
* @returns {import('nodemailer').Transporter | undefined} | ||
*/ | ||
async function initializeTransporter() { | ||
let newTransporter = undefined | ||
|
||
if (process.env.NODE_ENV === 'test') newTransporter = createJsonEchoTransporter() | ||
if (process.env.EMAIL === 'true') newTransporter = await initSmtpTransporter() | ||
|
||
if (!newTransporter) { | ||
modulesDebug( | ||
'📧 Email provider is not configured. Server functionality will be limited.' | ||
) | ||
} | ||
|
||
transporter = newTransporter | ||
return newTransporter | ||
} | ||
|
||
/** | ||
* @returns {import('nodemailer').Transporter | undefined} | ||
*/ | ||
function getTransporter() { | ||
return transporter | ||
} | ||
|
||
module.exports = { | ||
initializeTransporter, | ||
getTransporter | ||
} |
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,75 @@ | ||
import { MisconfiguredSecretError } from '@/modules/shared/errors' | ||
import { existsSync, readFile } from 'fs' | ||
import { join } from 'path' | ||
|
||
let redisUrl: string | ||
let postgresUrl: string | ||
let sessionSecret: string | ||
let emailPassword: string | ||
let s3SecretKey: string | ||
let googleClientSecret: string | ||
let githubClientSecret: string | ||
let azureADClientSecret: string | ||
let apolloKey: string | ||
|
||
export function getRedisUrl(): string { | ||
if (!redisUrl) redisUrl = getSecret('redis_url') | ||
return redisUrl | ||
} | ||
|
||
export function getPostgresUrl(): string { | ||
if (!postgresUrl) postgresUrl = getSecret('postgres_url') | ||
return postgresUrl | ||
} | ||
|
||
export function getSessionSecret(): string { | ||
if (!sessionSecret) sessionSecret = getSecret('session_secret') | ||
return sessionSecret | ||
} | ||
|
||
export function getEmailPassword(): string { | ||
if (!emailPassword) emailPassword = getSecret('email_password') | ||
return emailPassword | ||
} | ||
|
||
export function getS3SecretKey(): string { | ||
if (!s3SecretKey) s3SecretKey = getSecret('s3_secret_key') | ||
return s3SecretKey | ||
} | ||
|
||
export function getGoogleClientSecret(): string { | ||
if (!googleClientSecret) googleClientSecret = getSecret('google_client_secret') | ||
return googleClientSecret | ||
} | ||
|
||
export function getGitHubClientSecret(): string { | ||
if (!githubClientSecret) githubClientSecret = getSecret('github_client_secret') | ||
return githubClientSecret | ||
} | ||
|
||
export function getAzureADClientSecret(): string { | ||
if (!azureADClientSecret) azureADClientSecret = getSecret('azure_ad_client_secret') | ||
return azureADClientSecret | ||
} | ||
|
||
export function getApolloKey(): string { | ||
if (!apolloKey) apolloKey = getSecret('apollo_key') | ||
return apolloKey | ||
} | ||
|
||
function getSecret(secretName: string): string { | ||
const secretPath = join('/etc/secrets', secretName) | ||
if (existsSync(secretPath)) { | ||
readFile(secretPath, { encoding: 'utf-8' }, function (err, data) { | ||
if (!err) return data | ||
}) | ||
} | ||
|
||
if (process.env[secretName.toUpperCase()]) { | ||
return process.env[secretName.toUpperCase()] ?? '' | ||
} | ||
|
||
throw new MisconfiguredSecretError( | ||
`Neither ${secretPath} or ${secretName.toUpperCase()} env var were configured` | ||
) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(How?) Will this work on local development environments? Also I'm wondering if this shouldn't be an infra only solution (e.g. some cloud feature that stores some env vars separately, but when they reach the app theyre just env vars), not something that changes how our app reads environment config values? Just considering that this seems like a security feature only relevant for the prod env
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for challenging this, it led to a better solution.
To summarise what we discussed for future reference; the file won't exist on a dev environment, so this will fallback to using environment variables. It is backwards-compatible with our current dev & test setups.
On production the files would exist, so we wouldn't need the secrets in environment variables (secrets in env vars are relatively insecure).