Skip to content

Commit a146af2

Browse files
committed
chore: print warning if both mtls and mtlsKeyPair provided
1 parent 9990c6f commit a146af2

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

packages/connectivity/src/http-agent/http-agent.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,35 @@ describe('getAgentConfig', () => {
316316
expect(actual.passphrase).not.toBeDefined();
317317
expect(cacheSpy).toHaveBeenCalledTimes(1);
318318
});
319+
320+
it('logs a warning when both mtls is enabled and mtlsKeyPair is provided', async () => {
321+
process.env.CF_INSTANCE_CERT = 'cf-crypto/cf-cert';
322+
process.env.CF_INSTANCE_KEY = 'cf-crypto/cf-key';
323+
324+
const destination: HttpDestination = {
325+
url: 'https://example.com',
326+
name: 'test-destination',
327+
mtls: true,
328+
mtlsKeyPair: {
329+
cert: 'ias-cert',
330+
key: 'ias-key'
331+
}
332+
};
333+
334+
const logger = createLogger('http-agent');
335+
const warnSpy = jest.spyOn(logger, 'warn');
336+
337+
const actual = (await getAgentConfig(destination))['httpsAgent']
338+
.options;
339+
340+
expect(warnSpy).toHaveBeenCalledWith(
341+
"Destination test-destination has both 'mtlsKeyPair' (used by IAS) and 'mtls' (to use certs from cf) enabled. The 'mtlsKeyPair' will be used."
342+
);
343+
expect(actual.cert).toEqual('ias-cert');
344+
expect(actual.key).toEqual('ias-key');
345+
346+
warnSpy.mockRestore();
347+
});
319348
});
320349

321350
it('returns an object with key "httpsAgent" which is missing mTLS options when mtls is set to true but env variables do not include cert & key', async () => {

packages/connectivity/src/http-agent/http-agent.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ function getKeyStoreOptions(destination: Destination):
116116
if (
117117
// Only add certificates, when using ClientCertificateAuthentication (https://github.com/SAP/cloud-sdk-js/issues/3544)
118118
destination.authentication === 'ClientCertificateAuthentication' &&
119-
!mtlsIsEnabled(destination) &&
119+
!(mtlsIsEnabled(destination) || destination.mtlsKeyPair) &&
120120
destination.keyStoreName
121121
) {
122122
const certificate = selectCertificate(destination);
@@ -181,11 +181,18 @@ async function getMtlsOptions(
181181
} has mTLS enabled, but the required Cloud Foundry environment variables (CF_INSTANCE_CERT and CF_INSTANCE_KEY) are not defined. Note that 'inferMtls' only works on Cloud Foundry.`
182182
);
183183
}
184-
if (mtlsIsEnabled(destination)) {
185-
if (destination.mtlsKeyPair) {
186-
return destination.mtlsKeyPair;
184+
if (destination.mtlsKeyPair) {
185+
if (mtlsIsEnabled(destination)) {
186+
logger.warn(
187+
`Destination ${
188+
destination.name ? destination.name : ''
189+
} has both 'mtlsKeyPair' (used by IAS) and 'mtls' (to use certs from cf) enabled. The 'mtlsKeyPair' will be used.`
190+
);
187191
}
188192

193+
return destination.mtlsKeyPair;
194+
}
195+
if (mtlsIsEnabled(destination)) {
189196
if (registerDestinationCache.mtls.useMtlsCache) {
190197
return registerDestinationCache.mtls.getMtlsOptions();
191198
}
@@ -202,10 +209,9 @@ async function getMtlsOptions(
202209

203210
function mtlsIsEnabled(destination: Destination) {
204211
return (
205-
(destination.mtls &&
206-
process.env.CF_INSTANCE_CERT &&
207-
process.env.CF_INSTANCE_KEY) ||
208-
destination.mtlsKeyPair
212+
destination.mtls &&
213+
process.env.CF_INSTANCE_CERT &&
214+
process.env.CF_INSTANCE_KEY
209215
);
210216
}
211217

0 commit comments

Comments
 (0)