Skip to content

Commit

Permalink
fix: basic authentication and onPrem Connectivity (#1547)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankEssenberger authored Aug 26, 2021
1 parent 8d8bcb9 commit 17320c0
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 64 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

## Fixed Issues

- [core] Fix failing destination retrieval for `OnPremise` proxy type and basic authentication.
- [openapi-generator] Fix generation of options per service configuration files to always use POSIX-style file path separators independent of operating system.


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ describe('connectivity-service', () => {

const input: Destination = {
url: 'https://example.com',
proxyType: 'OnPremise'
proxyType: 'OnPremise',
authentication: 'PrincipalPropagation'
};

const expected: Destination = {
url: 'https://example.com',
proxyType: 'OnPremise',
authentication: 'PrincipalPropagation',
proxyConfiguration: {
...mockedConnectivityServiceProxyConfig,
headers: {
Expand Down
52 changes: 39 additions & 13 deletions packages/core/src/connectivity/scp-cf/connectivity-service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { createLogger, ErrorWithCause } from '@sap-cloud-sdk/util';
import { Protocol } from './protocol';
import { ProxyConfiguration } from './connectivity-service-types';
import { Destination } from './destination/destination-service-types';
import {
AuthenticationType,
Destination
} from './destination/destination-service-types';
import { EnvironmentAccessor } from './environment-accessor';
import { Service } from './environment-accessor-types';
import { serviceToken } from './token-accessor';
import { decodeJwt, isUserToken, JwtPair } from './jwt';

const logger = createLogger({
package: 'core',
Expand All @@ -28,18 +32,30 @@ export function addProxyConfiguration(
): Promise<Destination> {
return Promise.resolve()
.then(() => proxyHostAndPort())
.then(hostAndPort => addHeaders(hostAndPort, jwt))
.then(hostAndPort =>
addHeaders(hostAndPort, destination.authentication, jwt)
)
.then(proxyConfiguration => ({
...destination,
proxyConfiguration
}));
}

// TODO: remove string argument in v2.0
export function addProxyConfigurationOnPrem(
destination: Destination,
jwt?: string
jwt: string | JwtPair | undefined
): Promise<Destination> {
return addProxyConfiguration(destination, jwt);
const jwtPair =
typeof jwt === 'string' ? { encoded: jwt, decoded: decodeJwt(jwt) } : jwt;
if (
destination.authentication === 'PrincipalPropagation' &&
!isUserToken(jwtPair)
) {
throw new Error('For principal propagation a user JWT is needed.');
}

return addProxyConfiguration(destination, jwtPair?.encoded);
}

interface HostAndPort {
Expand Down Expand Up @@ -73,6 +89,7 @@ function readConnectivityServiceBinding(): Service {

function addHeaders(
hostAndPort: HostAndPort,
authenticationType: AuthenticationType | undefined,
jwt?: string
): Promise<ProxyConfiguration> {
const connServiceBinding = readConnectivityServiceBinding();
Expand All @@ -81,7 +98,7 @@ function addHeaders(
.then(() => proxyAuthorizationHeader(connServiceBinding, jwt))
.then(proxyAuthHeader => ({
...proxyAuthHeader,
...sapConnectivityAuthenticationHeader(jwt)
...sapConnectivityAuthenticationHeader(authenticationType, jwt)
}))
.then(
headers =>
Expand Down Expand Up @@ -109,16 +126,25 @@ function proxyAuthorizationHeader(
}

function sapConnectivityAuthenticationHeader(
authenticationType: AuthenticationType | undefined,
jwt?: string
): Record<string, string> {
if (jwt) {
return {
'SAP-Connectivity-Authentication': `Bearer ${jwt}`
};
if (authenticationType === 'PrincipalPropagation') {
if (jwt) {
return {
'SAP-Connectivity-Authentication': `Bearer ${jwt}`
};
}
throw new Error(
`Unable to create "SAP-Connectivity-Authentication" header: no JWT found on the current request.
Connecting to on-premise systems via principle propagation is not possible.`
);
}
if (authenticationType === 'BasicAuthentication') {
logger.warn(
'You are connecting to an On-Premise system using basic authentication. For productive usage Principal propagation is recommended.'
);
}
logger.warn(
`Unable to create "SAP-Connectivity-Authentication" header: no JWT found on the current request.
Continuing without header. Connecting to on-premise systems may not be possible.`
);

return {};
}
Loading

0 comments on commit 17320c0

Please sign in to comment.