Skip to content

Commit

Permalink
do not refetch license if signature header absents from a response (#…
Browse files Browse the repository at this point in the history
…79645)

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
mshustov and kibanamachine authored Oct 12, 2020
1 parent 6956498 commit ecbba93
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
40 changes: 40 additions & 0 deletions x-pack/plugins/licensing/public/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,46 @@ describe('licensing plugin', () => {
expect(coreSetup.http.get).toHaveBeenCalledTimes(1);
});

it('http interceptor does not trigger re-fetch if signature header is not present', async () => {
const sessionStorage = coreMock.createStorage();
plugin = new LicensingPlugin(coreMock.createPluginInitializerContext(), sessionStorage);

const coreSetup = coreMock.createSetup();

coreSetup.http.get.mockResolvedValue(licenseMock.createLicense({ signature: 'signature-1' }));

let registeredInterceptor: HttpInterceptor;
coreSetup.http.intercept.mockImplementation((interceptor: HttpInterceptor) => {
registeredInterceptor = interceptor;
return () => undefined;
});

await plugin.setup(coreSetup);
await plugin.start(coreStart);
expect(registeredInterceptor!.response).toBeDefined();

const httpResponse = {
response: {
headers: {
get(name: string) {
if (name === 'kbn-license-sig') {
return undefined;
}
throw new Error('unexpected header');
},
},
},
request: {
url: 'http://10.10.10.10:5601/api/hello',
},
};
expect(coreSetup.http.get).toHaveBeenCalledTimes(0);

await registeredInterceptor!.response!(httpResponse as any, null as any);

expect(coreSetup.http.get).toHaveBeenCalledTimes(0);
});

it('http interceptor does not trigger license re-fetch for anonymous pages', async () => {
const sessionStorage = coreMock.createStorage();
plugin = new LicensingPlugin(coreMock.createPluginInitializerContext(), sessionStorage);
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/licensing/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class LicensingPlugin implements Plugin<LicensingPluginSetup, LicensingPl
if (core.http.anonymousPaths.isAnonymous(window.location.pathname)) return httpResponse;
if (httpResponse.response) {
const signatureHeader = httpResponse.response.headers.get('kbn-license-sig');
if (this.prevSignature !== signatureHeader) {
if (typeof signatureHeader === 'string' && this.prevSignature !== signatureHeader) {
if (!httpResponse.request!.url.includes(this.infoEndpoint)) {
signatureUpdated$.next();
}
Expand Down

0 comments on commit ecbba93

Please sign in to comment.