Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wrong token for cache #1769

Merged
merged 6 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
## Fixed Issues

- [core] Disable destination cache, when the JWT does not contain necessary information. For example, when using `IsolationStrategy.Tenant_User`, the JWT has to contain both tenant id and user id.
- [core] Provider token used in retrieving destination from cache.


# 1.51.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
import {
onlyIssuerServiceToken,
providerServiceToken,
providerUserJwt,
subscriberServiceToken,
subscriberUserJwt
} from '../../../../test/test-util/mocked-access-tokens';
Expand All @@ -25,7 +24,6 @@ import {
destinationName
} from '../../../../test/test-util/example-destination-service-responses';
import { wrapJwtInHeader } from '../jwt';
import * as destinationService from './destination-service';
import { DestinationConfiguration, parseDestination } from './destination';
import {
alwaysProvider,
Expand Down Expand Up @@ -128,90 +126,44 @@ describe('jwtType x selection strategy combinations. Possible values are {subscr
expect(destination).toBe(null);
}

describe('Combinations: subscriberUserToken x {alwaysSubscriber,alwaysProvider,subscriberFirst}', () => {
it('subscriberUserToken && alwaysSubscriberToken: should not send a request to retrieve remote provider destination and return subscriber destination.', async () => {
describe('userToken x {alwaysSubscriber,alwaysProvider,subscriberFirst}', () => {
it('alwaysSubscriber does not call provider', async () => {
const mocks = mockThingsForCombinations();

const actual = await fetchDestination(
const destination = await fetchDestination(
subscriberUserJwt,
alwaysSubscriber
);
expect(actual!.url).toBe(subscriberDestination.URL);

mocks.subscriberMocks.forEach(mock => expect(mock.isDone()).toBe(true));
mocks.providerMocks.forEach(mock => expect(mock.isDone()).toBe(false));
expect(destination!.url).toBe(subscriberDestination.URL);
});

it('subscriberUserToken && alwaysProvider: should not sed a request to retrieve remote subscriber destination and return provider destination', async () => {
it('alwaysPovider does not call subscriber', async () => {
const mocks = mockThingsForCombinations();

const actual = await fetchDestination(subscriberUserJwt, alwaysProvider);
assertSubscriberNotCalledAndProviderFound(mocks, actual!);
});

it('subscriberUserToken && subscriberFirst: should try subscriber first (found something), provider not called and return subscriber destination', async () => {
mockThingsForCombinations();

const requestSpy = jest.spyOn(
destinationService,
'fetchSubaccountDestinations'
);
const actual = await fetchDestination(subscriberUserJwt, subscriberFirst);
expect(requestSpy).toHaveBeenCalledTimes(1);
expect(requestSpy).toHaveBeenNthCalledWith(
1,
'https://destination.example.com',
subscriberServiceToken,
expect.anything()
);
expect(actual!.url).toBe(subscriberDestination.URL);
});

it('subscriberUserToken && subscriberFirst: should try subscriber first (found nothing), provider called and return provider destination', async () => {
mockThingsForCombinations(true, false);

const requestSpy = jest.spyOn(
destinationService,
'fetchSubaccountDestinations'
);
const actual = await fetchDestination(subscriberUserJwt, subscriberFirst);
expect(requestSpy).toHaveBeenCalledTimes(2);
expect(requestSpy).toHaveBeenNthCalledWith(
1,
'https://destination.example.com',
subscriberServiceToken,
expect.anything()
);
expect(requestSpy).toHaveBeenNthCalledWith(
2,
'https://destination.example.com',
providerServiceToken,
expect.anything()
const destination = await fetchDestination(
subscriberUserJwt,
alwaysProvider
);
expect(actual!.url).toBe(providerDestination.URL);
});
});

describe('providerUserToken x {alwaysSubscriber,alwaysProvider,subscriberFirst}', () => {
it('providerUserToken && alwaysSubscriber: should return null since the token does not match subscriber', async () => {
const mocks = mockThingsForCombinations();

const actual = await fetchDestination(providerUserJwt, alwaysSubscriber);
assertNothingCalledAndNullFound(mocks, actual);
mocks.subscriberMocks.forEach(mock => expect(mock.isDone()).toBe(false));
mocks.providerMocks.forEach(mock => expect(mock.isDone()).toBe(true));
expect(destination!.url).toBe(providerDestination.URL);
});

it('providerUserToken && alwaysProvider: should not send a request to retrieve remote subscriber destination and return provider destination.', async () => {
it('subscriberFirst does not call provider if subscriber is found', async () => {
const mocks = mockThingsForCombinations();

const actual = await fetchDestination(providerUserJwt, alwaysProvider);
assertSubscriberNotCalledAndProviderFound(mocks, actual!);
});

it('providerUserToken && subscriberFirst: should not sed a request to retrieve remote subscriber destination and return provider destination.', async () => {
const mocks = mockThingsForCombinations();
const destination = await fetchDestination(
subscriberUserJwt,
subscriberFirst
);

const actual = await fetchDestination(providerUserJwt, subscriberFirst);
assertSubscriberNotCalledAndProviderFound(mocks, actual!);
mocks.subscriberMocks.forEach(mock => expect(mock.isDone()).toBe(true));
mocks.providerMocks.forEach(mock => expect(mock.isDone()).toBe(false));
expect(destination!.url).toBe(subscriberDestination.URL);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
providerJwtBearerToken,
providerServiceToken,
providerUserJwt,
providerUserPayload,
subscriberServiceToken,
subscriberUserJwt
} from '../../../../test/test-util/mocked-access-tokens';
Expand Down Expand Up @@ -144,6 +145,24 @@ describe('caching destination integration tests', () => {
);
});

it('cache key contains user also for provider tokens', async () => {
jjtang1985 marked this conversation as resolved.
Show resolved Hide resolved
await getDestination('ProviderDest', {
userJwt: providerUserJwt,
useCache: true,
isolationStrategy: IsolationStrategy.Tenant_User
});
const cacheKeys = Object.keys(
(destinationCache.getCacheInstance() as any).cache
);
expect(cacheKeys[0]).toBe(
getDestinationCacheKeyStrict(
providerUserPayload,
'ProviderDest',
IsolationStrategy.Tenant_User
)
);
});

it('retrieved subscriber destinations are cached with tenant id using "Tenant" isolation type by default ', async () => {
await getDestination('SubscriberDest', {
userJwt: subscriberUserJwt,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -570,10 +570,6 @@ class DestinationFromServiceRetriever {
return false;
}

if (this.isProviderAndSubscriberSameTenant()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐛 !

return false;
}

return true;
}

Expand Down
8 changes: 3 additions & 5 deletions packages/core/test/test-util/mocked-access-tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@ const iat = Math.floor(Date.now() / 1000);
const providerServiceTokenPayload = {
iat,
iss: providerXsuaaUrl,
zid: TestTenants.PROVIDER,
user_id: 'service-prov'
zid: TestTenants.PROVIDER
};

export const providerServiceToken = signedJwt(providerServiceTokenPayload);

const subscriberServiceTokenPayload = {
iat,
iss: subscriberXsuaaUrl,
zid: TestTenants.SUBSCRIBER,
user_id: 'service-sub'
zid: TestTenants.SUBSCRIBER
};

export const subscriberServiceToken = signedJwt(subscriberServiceTokenPayload);
Expand Down Expand Up @@ -64,7 +62,7 @@ export const userApprovedSubscriberServiceToken = signedJwt(
userApprovedSubscriberTokenPayload
);

const providerUserPayload = {
export const providerUserPayload = {
iat,
iss: providerXsuaaUrl,
zid: TestTenants.PROVIDER,
Expand Down