Skip to content

Commit 664d52e

Browse files
committed
feat(ebe): use KMS tls cert if mtls mode is enabled
Ticket: WP-4353
1 parent 3e57e75 commit 664d52e

4 files changed

Lines changed: 37 additions & 11 deletions

File tree

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,29 @@ Both modes use the same TLS configuration variables:
5959

6060
**Option 1: Certificate Files**
6161

62-
- `TLS_KEY_PATH` - Path to private key file
63-
- `TLS_CERT_PATH` - Path to certificate file
62+
- `TLS_KEY_PATH` - Path to private key file (used for both inbound mTLS server and outbound mTLS client to KMS)
63+
- `TLS_CERT_PATH` - Path to certificate file (used for both inbound mTLS server and outbound mTLS client to KMS)
6464

6565
**Option 2: Environment Variables**
6666

67-
- `TLS_KEY` - Private key content (PEM format)
68-
- `TLS_CERT` - Certificate content (PEM format)
67+
- `TLS_KEY` - Private key content (PEM format, used for both inbound and outbound)
68+
- `TLS_CERT` - Certificate content (PEM format, used for both inbound and outbound)
6969

7070
#### mTLS Settings (when TLS_MODE=mtls)
7171

7272
- `MTLS_REQUEST_CERT` - Request client certificates (default: true)
7373
- `ALLOW_SELF_SIGNED` - Allow self-signed certificates (default: false)
7474
- `MTLS_ALLOWED_CLIENT_FINGERPRINTS` - Comma-separated list of allowed client certificate fingerprints (optional)
7575

76+
#### Outbound mTLS to KMS
77+
78+
- When `TLS_MODE=mtls`, outbound mTLS to KMS is enabled by default.
79+
- The same `TLS_CERT` and `TLS_KEY` are used as the client certificate and key for outbound mTLS requests to KMS.
80+
- `KMS_TLS_CERT_PATH` - Path to the CA certificate to verify the KMS server (required when outbound mTLS is enabled).
81+
- If `TLS_MODE=disabled`, outbound mTLS to KMS is also disabled by default.
82+
83+
> **Note:** If you want to use a different client certificate for KMS, you will need to extend the configuration. By default, the same cert/key is used for both inbound and outbound mTLS.
84+
7685
### Logging and Debug
7786

7887
- `HTTP_LOGFILE` - Path to HTTP request log file (optional, used by Morgan for HTTP access logs)

src/initConfig.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,13 @@ function enclavedEnvConfig(): Partial<EnclavedConfig> {
8787
port: Number(readEnvVar('ENCLAVED_EXPRESS_PORT')),
8888
bind: readEnvVar('BIND'),
8989
ipc: readEnvVar('IPC'),
90-
httpLoggerFile: readEnvVar('HTTP_LOGFILE'),
90+
httpLoggerFile: readEnvVar('HTTP_LOGFILE') || 'logs/http-access.log',
9191
timeout: Number(readEnvVar('TIMEOUT')),
9292
keepAliveTimeout: Number(readEnvVar('KEEP_ALIVE_TIMEOUT')),
9393
headersTimeout: Number(readEnvVar('HEADERS_TIMEOUT')),
9494
// KMS settings
9595
kmsUrl,
96+
kmsTlsCertPath: readEnvVar('KMS_TLS_CERT_PATH'),
9697
// mTLS settings
9798
keyPath: readEnvVar('TLS_KEY_PATH'),
9899
crtPath: readEnvVar('TLS_CERT_PATH'),
@@ -123,6 +124,7 @@ function mergeEnclavedConfigs(...configs: Partial<EnclavedConfig>[]): EnclavedCo
123124
keepAliveTimeout: get('keepAliveTimeout'),
124125
headersTimeout: get('headersTimeout'),
125126
kmsUrl: get('kmsUrl'),
127+
kmsTlsCertPath: get('kmsTlsCertPath'),
126128
keyPath: get('keyPath'),
127129
crtPath: get('crtPath'),
128130
tlsKey: get('tlsKey'),
@@ -164,6 +166,19 @@ function configureEnclavedMode(): EnclavedConfig {
164166
logger.info('Using TLS certificate from environment variable');
165167
}
166168

169+
if (!config.kmsTlsCertPath) {
170+
throw new Error('KMS TLS CERT is required when TLS mode is MTLS');
171+
}
172+
if (config.kmsTlsCertPath) {
173+
try {
174+
config.kmsTlsCert = fs.readFileSync(config.kmsTlsCertPath, 'utf-8');
175+
logger.info(`Successfully loaded KMS TLS certificate from file: ${config.kmsTlsCertPath}`);
176+
} catch (e) {
177+
const err = e instanceof Error ? e : new Error(String(e));
178+
throw new Error(`Failed to read KMS TLS certificate from kmsTlsCert: ${err.message}`);
179+
}
180+
}
181+
167182
// Validate that certificates are properly loaded when TLS is enabled
168183
validateTlsCertificates(config);
169184
}

src/kms/kmsClient.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import debug from 'debug';
22
import * as superagent from 'superagent';
3-
import { EnclavedConfig, isMasterExpressConfig } from '../shared/types';
3+
import { EnclavedConfig, isMasterExpressConfig, TlsMode } from '../shared/types';
44
import { PostKeyKmsSchema, PostKeyParams, PostKeyResponse } from './types/postKey';
55
import { GetKeyKmsSchema, GetKeyParams, GetKeyResponse } from './types/getKey';
66
import {
@@ -25,14 +25,17 @@ export class KmsClient {
2525
if (isMasterExpressConfig(cfg)) {
2626
throw new Error('Configuration is not in enclaved express mode');
2727
}
28-
2928
if (!cfg.kmsUrl) {
3029
throw new Error('KMS URL not configured. Please set KMS_URL in your environment.');
3130
}
3231

3332
this.url = cfg.kmsUrl;
34-
if (cfg.kmsTlsMode === 'enabled' && cfg.kmsTlsCert) {
35-
this.agent = new https.Agent({ ca: cfg.kmsTlsCert });
33+
if (cfg.tlsMode === TlsMode.MTLS && cfg.kmsTlsCert) {
34+
this.agent = new https.Agent({
35+
ca: cfg.kmsTlsCert,
36+
cert: cfg.tlsCert,
37+
key: cfg.tlsKey,
38+
});
3639
}
3740
debugLogger('kmsClient initialized with URL: %s', this.url);
3841
}

src/shared/types/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ export interface EnclavedConfig extends BaseConfig {
2727
appMode: AppMode.ENCLAVED;
2828
// KMS settings
2929
kmsUrl: string;
30-
kmsTlsMode?: 'enabled' | 'disabled';
31-
kmsTlsCert?: string;
3230
kmsTlsCertPath?: string;
31+
kmsTlsCert?: string;
3332
// mTLS settings
3433
keyPath?: string;
3534
crtPath?: string;

0 commit comments

Comments
 (0)