1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
7 const fs = require('fs');
8 const path = require('path');
9 const crypto = require('crypto');
10 const chalk = require('react-dev-utils/chalk');
11 const paths = require('./paths');
13 // Ensure the certificate and key provided are valid and if not
14 // throw an easy to debug error
15 function validateKeyAndCerts({ cert, key, keyFile, crtFile }) {
18 // publicEncrypt will throw an error with an invalid cert
19 encrypted = crypto.publicEncrypt(cert, Buffer.from('test'));
22 `The certificate "${chalk.yellow(crtFile)}" is invalid.\n${err.message}`
27 // privateDecrypt will throw an error with an invalid key
28 crypto.privateDecrypt(key, encrypted);
31 `The certificate key "${chalk.yellow(keyFile)}" is invalid.\n${
38 // Read file and throw an error if it doesn't exist
39 function readEnvFile(file, type) {
40 if (!fs.existsSync(file)) {
42 `You specified ${chalk.cyan(
44 )} in your env, but the file "${chalk.yellow(file)}" can't be found.`
47 return fs.readFileSync(file);
50 // Get the https config
51 // Return cert files if provided in env, otherwise just true or false
52 function getHttpsConfig() {
53 const { SSL_CRT_FILE, SSL_KEY_FILE, HTTPS } = process.env;
54 const isHttps = HTTPS === 'true';
56 if (isHttps && SSL_CRT_FILE && SSL_KEY_FILE) {
57 const crtFile = path.resolve(paths.appPath, SSL_CRT_FILE);
58 const keyFile = path.resolve(paths.appPath, SSL_KEY_FILE);
60 cert: readEnvFile(crtFile, 'SSL_CRT_FILE'),
61 key: readEnvFile(keyFile, 'SSL_KEY_FILE'),
64 validateKeyAndCerts({ ...config, keyFile, crtFile });
70 module.exports = getHttpsConfig;