import https from 'https' ; import http from 'http' ; import { assoc , createLogger , last } from '@sap-cloud-sdk/util' ; import { Destination , DestinationCertificate , getProtocolOrDefault , Protocol } from '../scp-cf' ; import { addProxyConfigurationInternet , proxyAgent , proxyStrategy , ProxyStrategy } from '../scp-cf/destination' ; import { HttpAgentConfig , HttpsAgentConfig } from './agent-config' ; const logger = createLogger ( { package : 'connectivity' , messageContext : 'http-agent' } ) ; param destination returns export function getAgentConfig ( destination ) { const agentType = destination . proxyConfiguration ? AgentType . PROXY : AgentType . DEFAULT ; const certificateOptions = getCertificateOption ( destination ) ; if ( agentType === AgentType . PROXY ) { return createProxyAgent ( destination , certificateOptions ) ; } return createDefaultAgent ( destination , certificateOptions ) ; } enum AgentType { DEFAULT , PROXY } function createProxyAgent ( destination , options ) { if ( ! destination . proxyConfiguration ) { throw new Error ( `The destination proxy configuration: ${ destination . proxyConfiguration } is undefined.` ) ; } return proxyAgent ( destination , options ) ; } const trustAllOptions = ( destination ) => ( options ) : Record < string , any > => assoc ( 'rejectUnauthorized' , ! destination . isTrustingAllCertificates , options ) ; const certificateOptions = ( destination ) => ( options ) : Record < string , any > => { if ( destination . keyStoreName && destination . keyStorePassword ) { const certificate = selectCertificate ( destination ) ; logger . debug ( `Certificate with name "${ certificate . name }" selected.` ) ; return { ... options , pfx : Buffer . from ( certificate . content , 'base64' ) , passphrase : destination . keyStorePassword } ; } return options ; } ; internal param destination returns function getCertificateOption ( destination ) { if ( getProtocolOrDefault ( destination ) === Protocol . HTTP ) { if ( destination . isTrustingAllCertificates ) { logger . warn ( '"isTrustingAllCertificates" is not available for HTTP.' ) ; } return { } ; } if ( destination . isTrustingAllCertificates ) { logger . warn ( '"isTrustingAllCertificates" property in the provided destination is set to "true". This is highly discouraged in production.' ) ; } const options = trustAllOptions ( destination ) ( { } ) ; return certificateOptions ( destination ) ( options ) ; } const supportedCertificateFormats = [ 'p12' , 'pfx' ] ; function hasSupportedFormat ( certificate ) { const certificateFormat = last ( certificate . name . split ( '.' ) ) ; if ( certificateFormat ) { return supportedCertificateFormats . includes ( certificateFormat ) ; } return false ; } function selectCertificate ( destination ) { const certificate = destination . certificates . find ( c => c . name === destination . keyStoreName ) ; if ( ! certificate ) { throw Error ( `No certificate with name ${ destination . keyStoreName } could be found on the destination!` ) ; } if ( ! hasSupportedFormat ( certificate ) ) { const format = last ( certificate . name . split ( '.' ) ) ; throw Error ( `The format of the provided certificate '${ certificate . name }' is not supported. Supported formats are: ${ supportedCertificateFormats . join ( ', ' ) }. ${ format && [ 'jks' , 'keystore' ] . includes ( format ) ? "You can convert Java Keystores (.jks, .keystore) into PKCS#12 keystores using the JVM's keytool CLI: keytool -importkeystore -srckeystore your-keystore.jks -destkeystore your-keystore.p12 -deststoretype pkcs12" : '' }` ) ; } return certificate ; } internal function createDefaultAgent ( destination , options ) { if ( getProtocolOrDefault ( destination ) === Protocol . HTTPS ) { return { httpsAgent : new https . Agent ( options ) } ; } return { httpAgent : new http . Agent ( options ) } ; } internal param targetUri returns export function urlAndAgent ( targetUri ) { let destination = { url : targetUri , proxyType : 'Internet' } ; if ( proxyStrategy ( destination ) === ProxyStrategy . INTERNET_PROXY ) { destination = addProxyConfigurationInternet ( destination ) ; } return { baseURL : destination . url , ... getAgentConfig ( destination ) } ; }