1+ /*
2+ WARNING: this file is imported by migrate-mongo which does not use babel. Avoid importing assets from the wider project as they'll use tilde imports.
3+ This file is copied verbatim into the production Docker image at src/secure-context.js. Imports will not be copied.
4+ If this grows in complexity and needs external dependencies, it will need to be folded into babel.
5+ */
16import tls from 'node:tls'
27
3- import { getTrustStoreCerts } from '~/src/helpers/secure-context/get-trust-store-certs.js'
4-
58/**
69 * @type {SecureContext }
710 */
811export let secureContext
912
1013/**
1114 * Prepares the TLS secure context
12- * @param {Server } server
1315 * @returns
16+ * @param {(arg: string) => void } loggerFn
1417 */
15- export function prepareSecureContext ( server ) {
18+ export function prepareSecureContext ( loggerFn ) {
1619 const originalCreateSecureContext = tls . createSecureContext
1720
1821 tls . createSecureContext = function ( options = { } ) {
1922 const trustStoreCerts = getTrustStoreCerts ( process . env )
2023
2124 if ( ! trustStoreCerts . length ) {
22- server . logger . info ( 'Could not find any TRUSTSTORE_ certificates' )
25+ loggerFn ( 'Could not find any TRUSTSTORE_ certificates' )
2326 }
2427
2528 const originalSecureContext = originalCreateSecureContext ( options )
@@ -37,6 +40,20 @@ export function prepareSecureContext(server) {
3740}
3841
3942/**
40- * @import { Server } from '@hapi/hapi'
43+ * Get base64 certs from all environment variables starting with TRUSTSTORE_
44+ * @param {NodeJS.ProcessEnv } envs
45+ * @returns {string[] }
46+ */
47+ export function getTrustStoreCerts ( envs ) {
48+ return Object . entries ( envs )
49+ . map ( ( [ key , value ] ) => key . startsWith ( 'TRUSTSTORE_' ) && value )
50+ . filter (
51+ /** @returns {envValue is string } */
52+ ( envValue ) => Boolean ( envValue )
53+ )
54+ . map ( ( envValue ) => Buffer . from ( envValue , 'base64' ) . toString ( ) . trim ( ) )
55+ }
56+
57+ /**
4158 * @import { SecureContext } from 'node:tls'
4259 */
0 commit comments