Merge pull request #17699 from codingdiaz/feat/tools-collator-auth
feat(explore): adds optional token manager to collator
This commit is contained in:
@@ -39,3 +39,18 @@ backend.add(searchPlugin());
|
||||
backend.add(searchModuleExploreCollator({ schedule }));
|
||||
backend.start();
|
||||
```
|
||||
|
||||
### Using Auth Middleware
|
||||
|
||||
If your Backstage instance uses service-to-service authentication you can pass an optional `tokenManager` to the collator factory. This will ensure that the collator makes authenticated requests to the explore backend.
|
||||
|
||||
```tsx
|
||||
indexBuilder.addCollator({
|
||||
schedule: every10MinutesSchedule,
|
||||
factory: ToolDocumentCollatorFactory.fromConfig(env.config, {
|
||||
discovery: env.discovery,
|
||||
logger: env.logger,
|
||||
tokenManager: env.tokenManager,
|
||||
}),
|
||||
});
|
||||
```
|
||||
|
||||
@@ -12,6 +12,7 @@ import { IndexableDocument } from '@backstage/plugin-search-common';
|
||||
import { Logger } from 'winston';
|
||||
import { PluginEndpointDiscovery } from '@backstage/backend-common';
|
||||
import { Readable } from 'stream';
|
||||
import { TokenManager } from '@backstage/backend-common';
|
||||
|
||||
// @public
|
||||
export interface ToolDocument extends IndexableDocument, ExploreTool {}
|
||||
@@ -35,5 +36,6 @@ export class ToolDocumentCollatorFactory implements DocumentCollatorFactory {
|
||||
export type ToolDocumentCollatorFactoryOptions = {
|
||||
discovery: PluginEndpointDiscovery;
|
||||
logger: Logger;
|
||||
tokenManager?: TokenManager;
|
||||
};
|
||||
```
|
||||
|
||||
@@ -14,7 +14,10 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { PluginEndpointDiscovery } from '@backstage/backend-common';
|
||||
import {
|
||||
PluginEndpointDiscovery,
|
||||
TokenManager,
|
||||
} from '@backstage/backend-common';
|
||||
import { Config } from '@backstage/config';
|
||||
import { ExploreTool } from '@backstage/plugin-explore-common';
|
||||
import {
|
||||
@@ -40,6 +43,7 @@ export interface ToolDocument extends IndexableDocument, ExploreTool {}
|
||||
export type ToolDocumentCollatorFactoryOptions = {
|
||||
discovery: PluginEndpointDiscovery;
|
||||
logger: Logger;
|
||||
tokenManager?: TokenManager;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -52,10 +56,12 @@ export class ToolDocumentCollatorFactory implements DocumentCollatorFactory {
|
||||
|
||||
private readonly discovery: PluginEndpointDiscovery;
|
||||
private readonly logger: Logger;
|
||||
private readonly tokenManager?: TokenManager;
|
||||
|
||||
private constructor(options: ToolDocumentCollatorFactoryOptions) {
|
||||
this.discovery = options.discovery;
|
||||
this.logger = options.logger;
|
||||
this.tokenManager = options.tokenManager;
|
||||
}
|
||||
|
||||
static fromConfig(
|
||||
@@ -87,7 +93,17 @@ export class ToolDocumentCollatorFactory implements DocumentCollatorFactory {
|
||||
|
||||
private async fetchTools() {
|
||||
const baseUrl = await this.discovery.getBaseUrl('explore');
|
||||
const response = await fetch(`${baseUrl}/tools`);
|
||||
|
||||
let headers = {};
|
||||
|
||||
if (this.tokenManager) {
|
||||
const { token } = await this.tokenManager.getToken();
|
||||
headers = {
|
||||
Authorization: `Bearer ${token}`,
|
||||
};
|
||||
}
|
||||
|
||||
const response = await fetch(`${baseUrl}/tools`, headers);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(
|
||||
|
||||
Reference in New Issue
Block a user