|
1 | 1 | // noinspection ES6PreferShortImport
|
2 |
| -import {Logger} from './logging'; |
| 2 | +import { Logger } from './logging'; |
3 | 3 | import fs from 'fs';
|
4 | 4 | import path from 'path';
|
5 |
| -import {getRelTopLevelPath} from "./version"; |
6 | 5 |
|
7 |
| -const FALLBACK_INTERNAL_ROOT_CERTS = path.join(__dirname, getRelTopLevelPath(), 'certs/internal.pem'); |
8 |
| -const FALLBACK_SYSTEM_ROOT_CERTS = path.join(__dirname, getRelTopLevelPath(), 'certs/system.pem'); |
| 6 | +const CERTIFICATES_FOLDER = 'certs' |
| 7 | +const RELATIVE_PATH = process.env.TEST_ENVIRONMENT ? '../' : './' |
| 8 | +const RESOLVED_PATH = path.join(__dirname, RELATIVE_PATH, CERTIFICATES_FOLDER) |
| 9 | +const FALLBACK_INTERNAL_ROOT_CERTS = path.join(RESOLVED_PATH, 'internal.pem'); |
| 10 | +const FALLBACK_SYSTEM_ROOT_CERTS = path.join(RESOLVED_PATH, 'system.pem'); |
9 | 11 |
|
10 | 12 | function makeInternalRootCertificates() {
|
11 |
| - const internalRootCertificates = fs.readFileSync(FALLBACK_INTERNAL_ROOT_CERTS); |
| 13 | + if (!fs.existsSync(FALLBACK_INTERNAL_ROOT_CERTS) |
| 14 | + || !fs.existsSync(FALLBACK_SYSTEM_ROOT_CERTS)) { |
| 15 | + throw new Error(certificateNotFoundMessage) |
| 16 | + } |
| 17 | + |
| 18 | + const internalRootCertificates = fs.readFileSync(FALLBACK_INTERNAL_ROOT_CERTS) |
| 19 | + const fallbackSystemRootCertificates = fs.readFileSync(FALLBACK_SYSTEM_ROOT_CERTS) |
12 | 20 |
|
13 |
| - let systemRootCertificates; |
| 21 | + let systemRootCertificates: Buffer; |
14 | 22 | const tls = require('tls');
|
15 | 23 | const nodeRootCertificates = tls.rootCertificates as string[] | undefined;
|
16 | 24 | if (nodeRootCertificates && nodeRootCertificates.length > 0) {
|
17 | 25 | systemRootCertificates = Buffer.from(nodeRootCertificates.join('\n'));
|
18 | 26 | } else {
|
19 |
| - systemRootCertificates = fs.readFileSync(FALLBACK_SYSTEM_ROOT_CERTS); |
| 27 | + systemRootCertificates = fallbackSystemRootCertificates; |
20 | 28 | }
|
21 | 29 |
|
22 | 30 | return Buffer.concat([internalRootCertificates, systemRootCertificates]);
|
@@ -47,6 +55,12 @@ export function makeSslCredentials(endpoint: string, logger: Logger, sslCredenti
|
47 | 55 | return makeDefaultSslCredentials();
|
48 | 56 | }
|
49 | 57 |
|
| 58 | +const certificateNotFoundMessage = `No certificate found |
| 59 | +It seems that you are using grpcs (secure) endpoint in a bundled environment. |
| 60 | +Either provide YDB_SSL_ROOT_CERTIFICATES_FILE environment variable |
| 61 | +or copy contents of ydb-nodejs-sdk/certs to ./certs path relative to the bundled file |
| 62 | +` |
| 63 | + |
50 | 64 | export interface ISslCredentials {
|
51 | 65 | rootCertificates?: Buffer,
|
52 | 66 | clientPrivateKey?: Buffer,
|
|
0 commit comments