11#!/usr/bin/env node
22
3- let fipsError : Error | undefined ;
43function enableFipsIfRequested ( ) : void {
5- if ( process . argv . includes ( "--tlsFIPSMode" ) ) {
6- // FIPS mode should be enabled before we run any other code, including any dependencies.
7- // We still wrap this into a function so we can also call it immediately after
8- // entering the snapshot main function.
4+ let fipsError : Error | undefined ;
5+ const tlsFIPSMode = process . argv . includes ( "--tlsFIPSMode" ) ;
6+
7+ if ( tlsFIPSMode ) {
98 try {
109 // eslint-disable-next-line
1110 require ( "crypto" ) . setFips ( 1 ) ;
1211 } catch ( err : unknown ) {
1312 fipsError ??= err as Error ;
1413 }
1514 }
15+
16+ if ( tlsFIPSMode ) {
17+ if ( ! fipsError && ! crypto . getFips ( ) ) {
18+ fipsError = new Error ( "FIPS mode not enabled despite requested due to unknown error." ) ;
19+ }
20+ }
21+
22+ if ( fipsError ) {
23+ if ( process . config . variables . node_shared_openssl ) {
24+ console . error (
25+ "Could not enable FIPS mode. Please ensure that your system OpenSSL installation supports FIPS."
26+ ) ;
27+ } else {
28+ console . error ( "Could not enable FIPS mode. This installation does not appear to support FIPS." ) ;
29+ }
30+ console . error ( "Error details:" ) ;
31+ console . error ( fipsError ) ;
32+ process . exit ( 1 ) ;
33+ }
1634}
1735
1836enableFipsIfRequested ( ) ;
@@ -28,7 +46,6 @@ import { systemCA } from "@mongodb-js/devtools-proxy-support";
2846async function main ( ) : Promise < void > {
2947 systemCA ( ) . catch ( ( ) => undefined ) ; // load system CA asynchronously as in mongosh
3048
31- assertFIPSMode ( ) ;
3249 assertHelpMode ( ) ;
3350 assertVersionMode ( ) ;
3451
@@ -105,27 +122,6 @@ main().catch((error: unknown) => {
105122 process . exit ( 1 ) ;
106123} ) ;
107124
108- function assertFIPSMode ( ) : void | never {
109- if ( config . tlsFIPSMode ) {
110- if ( ! fipsError && ! crypto . getFips ( ) ) {
111- fipsError = new Error ( "FIPS mode not enabled despite requested." ) ;
112- }
113- }
114-
115- if ( fipsError ) {
116- if ( process . config . variables . node_shared_openssl ) {
117- console . error (
118- "Could not enable FIPS mode. Please ensure that your system OpenSSL installation supports FIPS."
119- ) ;
120- } else {
121- console . error ( "Could not enable FIPS mode. This installation does not appear to support FIPS." ) ;
122- }
123- console . error ( "Error details:" ) ;
124- console . error ( fipsError ) ;
125- process . exit ( 1 ) ;
126- }
127- }
128-
129125function assertHelpMode ( ) : void | never {
130126 if ( config . help ) {
131127 console . log ( "For usage information refer to the README.md:" ) ;
0 commit comments