|
| 1 | +import dotenv from "dotenv"; |
| 2 | +import joi from "joi"; |
| 3 | +import {CredentialsOptions} from "aws-sdk/lib/credentials"; |
| 4 | +import * as AWS from "aws-sdk"; |
| 5 | + |
| 6 | +dotenv.config({path: ".env"}); |
| 7 | + |
| 8 | +export interface Config { |
| 9 | + env: "development" | "test" | "production"; |
| 10 | + port: number; |
| 11 | + previewUrl: string; |
| 12 | + publishUrl: string; |
| 13 | + persistentBackend: "s3" | "blob" | "preview"; |
| 14 | + s3Bucket?: string; |
| 15 | + logLevel: "trace" | "info" | "debug" | "error"; |
| 16 | + phase?: "alpha" | "beta"; |
| 17 | + footerText?: string; |
| 18 | + isProd: boolean; |
| 19 | + isDev: boolean; |
| 20 | + isTest: boolean; |
| 21 | + lastCommit: string; |
| 22 | + lastTag: string; |
| 23 | + sessionTimeout: number; |
| 24 | + sessionCookiePassword: string; |
| 25 | + awsCredentials?: CredentialsOptions; |
| 26 | + authEnabled: boolean, |
| 27 | + ssoLoginUrl: string, |
| 28 | + ssoLogoutUrl: string, |
| 29 | + authServiceUrl: string, |
| 30 | + rsa256PublicKeyBase64: string, |
| 31 | + authCookieName: string, |
| 32 | +} |
| 33 | + |
| 34 | +// server-side storage expiration - defaults to 20 minutes |
| 35 | +const sessionSTimeoutInMilliseconds = 20 * 60 * 1000; |
| 36 | + |
| 37 | +// Define config schema |
| 38 | +const schema = joi.object({ |
| 39 | + port: joi.number().default(3000), |
| 40 | + env: joi |
| 41 | + .string() |
| 42 | + .valid("development", "test", "production") |
| 43 | + .default("development"), |
| 44 | + previewUrl: joi.string(), |
| 45 | + publishUrl: joi.string(), |
| 46 | + persistentBackend: joi.string().valid("s3", "blob", "preview").optional(), |
| 47 | + s3Bucket: joi.string().optional(), |
| 48 | + logLevel: joi |
| 49 | + .string() |
| 50 | + .valid("trace", "info", "debug", "error") |
| 51 | + .default("debug"), |
| 52 | + phase: joi.string().valid("alpha", "beta").optional(), |
| 53 | + footerText: joi.string().optional(), |
| 54 | + lastCommit: joi.string().default("undefined"), |
| 55 | + lastTag: joi.string().default("undefined"), |
| 56 | + sessionTimeout: joi.number().default(sessionSTimeoutInMilliseconds), |
| 57 | + sessionCookiePassword: joi.string().optional(), |
| 58 | + authEnabled: joi.string().required(), |
| 59 | + ssoLoginUrl: joi.string().optional(), |
| 60 | + ssoLogoutUrl: joi.string().optional(), |
| 61 | + authServiceUrl: joi.string().optional(), |
| 62 | + rsa256PublicKeyBase64: joi.string().optional(), |
| 63 | + authCookieName: joi.string().optional(), |
| 64 | +}); |
| 65 | + |
| 66 | +// Build config |
| 67 | +const config = { |
| 68 | + port: process.env.PORT, |
| 69 | + env: process.env.NODE_ENV, |
| 70 | + previewUrl: process.env.PREVIEW_URL || "http://localhost:3009", |
| 71 | + publishUrl: process.env.PUBLISH_URL || "http://localhost:3009", |
| 72 | + persistentBackend: process.env.PERSISTENT_BACKEND || "preview", |
| 73 | + s3Bucket: process.env.S3_BUCKET, |
| 74 | + logLevel: process.env.LOG_LEVEL || "error", |
| 75 | + phase: process.env.PHASE || "alpha", |
| 76 | + footerText: process.env.FOOTER_TEXT, |
| 77 | + lastCommit: process.env.LAST_COMMIT || process.env.LAST_COMMIT_GH, |
| 78 | + lastTag: process.env.LAST_TAG || process.env.LAST_TAG_GH, |
| 79 | + sessionTimeout: process.env.SESSION_TIMEOUT, |
| 80 | + sessionCookiePassword: process.env.SESSION_COOKIE_PASSWORD, |
| 81 | + authEnabled: process.env.AUTH_ENABLED, |
| 82 | + ssoLoginUrl: process.env.SSO_LOGIN_URL, |
| 83 | + ssoLogoutUrl: process.env.SSO_LOGOUT_URL, |
| 84 | + authServiceUrl: process.env.AUTH_SERVICE_URL, |
| 85 | + rsa256PublicKeyBase64: process.env.RSA256_PUBLIC_KEY_BASE64, |
| 86 | + authCookieName: process.env.AUTH_COOKIE_NAME, |
| 87 | +}; |
| 88 | + |
| 89 | +// Validate config |
| 90 | +const result = schema.validate( |
| 91 | + { |
| 92 | + ...config, |
| 93 | + }, |
| 94 | + {abortEarly: false} |
| 95 | +); |
| 96 | + |
| 97 | +// Throw if config is invalid |
| 98 | +if (result.error) { |
| 99 | + throw new Error(`The server config is invalid. ${result.error.message}`); |
| 100 | +} |
| 101 | + |
| 102 | +// Use the joi validated value |
| 103 | +const value: Config = result.value; |
| 104 | + |
| 105 | +/** |
| 106 | + * TODO:- replace this with a top-level await when upgraded to node 16 |
| 107 | + */ |
| 108 | +async function getAwsConfigCredentials(): Promise<CredentialsOptions | {}> { |
| 109 | + return new Promise(function (resolve, reject) { |
| 110 | + if (value.persistentBackend === "s3") { |
| 111 | + return AWS.config.getCredentials(async function (err) { |
| 112 | + if (err) { |
| 113 | + console.error("Error getting AWS credentials", err); |
| 114 | + reject(err); |
| 115 | + } else { |
| 116 | + resolve({ |
| 117 | + // @ts-ignore |
| 118 | + accessKeyId: AWS.config.credentials.accessKeyId, |
| 119 | + // @ts-ignore |
| 120 | + secretAccessKey: AWS.config.credentials.secretAccessKey, |
| 121 | + }); |
| 122 | + } |
| 123 | + }); |
| 124 | + } else { |
| 125 | + resolve({}); |
| 126 | + } |
| 127 | + }); |
| 128 | +} |
| 129 | + |
| 130 | +getAwsConfigCredentials() |
| 131 | + .then((awsConfig) => { |
| 132 | + // @ts-ignore |
| 133 | + value.awsCredentials = awsConfig; |
| 134 | + }) |
| 135 | + .catch((e) => { |
| 136 | + throw e; |
| 137 | + }); |
| 138 | + |
| 139 | +value.isProd = value.env === "production"; |
| 140 | +value.isDev = !value.isProd; |
| 141 | +value.isTest = value.env === "test"; |
| 142 | + |
| 143 | +export default value; |
0 commit comments