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

Use fetch middlewares in event-source-polifyll (re: #23015) #24820

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ export class IdentityAuthInjectorFetchMiddleware implements FetchMiddleware {
return next(request);
};
}

async headers(): Promise<Record<string, string>> {
const { token } = await this.identityApi.getCredentials();
if (!token) {
return {};
}
return {
[this.headerName]: this.headerValue(token),
};
}
}

function buildMatcher(options: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,12 @@ export function createFetchApi(options: {

return {
fetch: result,
headers: () => {
return Promise.all(
middleware.map(m => (m.headers ? m.headers() : Promise.resolve({}))),
).then(headersList => {
return Object.assign({}, ...headersList);
});
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ export interface FetchMiddleware {
* call out to as part of the request cycle.
*/
apply(next: typeof fetch): typeof fetch;

/**
* Returns optional headers to be applied on the fetch request.
*/
headers?(): Promise<Record<string, string>>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type FetchApi = {
* The `fetch` implementation.
*/
fetch: typeof fetch;
headers?: () => Promise<Record<string, string>>;
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ export class MockFetchApi implements FetchApi {
get fetch(): typeof crossFetch {
return this.implementation.fetch;
}

get headers() {
return this.implementation.headers;
}
}

//
Expand Down
5 changes: 1 addition & 4 deletions plugins/techdocs/src/alpha.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
createApiFactory,
discoveryApiRef,
fetchApiRef,
identityApiRef,
} from '@backstage/core-plugin-api';
import {
compatWrapper,
Expand All @@ -55,14 +54,12 @@ const techDocsStorageApi = createApiExtension({
deps: {
configApi: configApiRef,
discoveryApi: discoveryApiRef,
identityApi: identityApiRef,
fetchApi: fetchApiRef,
},
factory: ({ configApi, discoveryApi, identityApi, fetchApi }) =>
factory: ({ configApi, discoveryApi, fetchApi }) =>
new TechDocsStorageClient({
configApi,
discoveryApi,
identityApi,
fetchApi,
}),
}),
Expand Down
11 changes: 1 addition & 10 deletions plugins/techdocs/src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
const storageApi = new TechDocsStorageClient({
configApi,
discoveryApi,
identityApi,
fetchApi,
});

Expand All @@ -72,7 +71,6 @@
const storageApi = new TechDocsStorageClient({
configApi,
discoveryApi,
identityApi,
fetchApi,
});

Expand All @@ -88,7 +86,6 @@
const storageApi = new TechDocsStorageClient({
configApi,
discoveryApi,
identityApi,
fetchApi,
});

Expand All @@ -113,7 +110,6 @@
const storageApi = new TechDocsStorageClient({
configApi,
discoveryApi,
identityApi,
fetchApi,
});

Expand All @@ -130,15 +126,14 @@

expect(MockedEventSource).toHaveBeenCalledWith(
'http://backstage:9191/api/techdocs/sync/default/Component/test-component',
{ withCredentials: true, headers: { Authorization: 'Bearer token' } },
{ withCredentials: true, headers: { authorization: 'Bearer token' } },

Check failure

Code scanning / CodeQL

Hard-coded credentials Critical

The hard-coded value "Bearer token" is used as
authorization header
.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is a test

);
});

it('should resolve to cached', async () => {
const storageApi = new TechDocsStorageClient({
configApi,
discoveryApi,
identityApi,
fetchApi,
});

Expand All @@ -160,7 +155,6 @@
const storageApi = new TechDocsStorageClient({
configApi,
discoveryApi,
identityApi,
fetchApi,
});

Expand All @@ -182,7 +176,6 @@
const storageApi = new TechDocsStorageClient({
configApi,
discoveryApi,
identityApi,
fetchApi,
});

Expand Down Expand Up @@ -212,7 +205,6 @@
const storageApi = new TechDocsStorageClient({
configApi,
discoveryApi,
identityApi,
fetchApi,
});

Expand All @@ -239,7 +231,6 @@
const storageApi = new TechDocsStorageClient({
configApi,
discoveryApi,
identityApi,
fetchApi,
});

Expand Down
15 changes: 5 additions & 10 deletions plugins/techdocs/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@

import { CompoundEntityRef } from '@backstage/catalog-model';
import { Config } from '@backstage/config';
import {
DiscoveryApi,
FetchApi,
IdentityApi,
} from '@backstage/core-plugin-api';
import { DiscoveryApi, FetchApi } from '@backstage/core-plugin-api';
import { NotFoundError, ResponseError } from '@backstage/errors';
import {
SyncResult,
Expand Down Expand Up @@ -124,18 +120,15 @@ export class TechDocsClient implements TechDocsApi {
export class TechDocsStorageClient implements TechDocsStorageApi {
public configApi: Config;
public discoveryApi: DiscoveryApi;
public identityApi: IdentityApi;
private fetchApi: FetchApi;

constructor(options: {
configApi: Config;
discoveryApi: DiscoveryApi;
identityApi: IdentityApi;
fetchApi: FetchApi;
}) {
this.configApi = options.configApi;
this.discoveryApi = options.discoveryApi;
this.identityApi = options.identityApi;
this.fetchApi = options.fetchApi;
}

Expand Down Expand Up @@ -213,13 +206,15 @@ export class TechDocsStorageClient implements TechDocsStorageApi {

const apiOrigin = await this.getApiOrigin();
const url = `${apiOrigin}/sync/${namespace}/${kind}/${name}`;
const { token } = await this.identityApi.getCredentials();
const headers: HeadersInit = this.fetchApi.headers
? await this.fetchApi.headers()
: {};

return new Promise((resolve, reject) => {
// Polyfill is used to add support for custom headers and auth
const source = new EventSourcePolyfill(url, {
withCredentials: true,
headers: token ? { Authorization: `Bearer ${token}` } : {},
headers: headers,
});

source.addEventListener('log', (e: any) => {
Expand Down
3 changes: 1 addition & 2 deletions plugins/techdocs/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,10 @@ export const techdocsPlugin = createPlugin({
identityApi: identityApiRef,
fetchApi: fetchApiRef,
},
factory: ({ configApi, discoveryApi, identityApi, fetchApi }) =>
factory: ({ configApi, discoveryApi, fetchApi }) =>
new TechDocsStorageClient({
configApi,
discoveryApi,
identityApi,
fetchApi,
}),
}),
Expand Down
Loading