diff --git a/.changeset/khaki-doors-compare.md b/.changeset/khaki-doors-compare.md new file mode 100644 index 0000000000..a2c4de020a --- /dev/null +++ b/.changeset/khaki-doors-compare.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-vault-backend': minor +--- + +Allow generic Vault clients to be passed into the builder diff --git a/plugins/vault-backend/api-report.md b/plugins/vault-backend/api-report.md index 2b1fb14bd0..4890e585b4 100644 --- a/plugins/vault-backend/api-report.md +++ b/plugins/vault-backend/api-report.md @@ -33,12 +33,9 @@ export interface VaultApi { export class VaultBuilder { constructor(env: VaultEnvironment); build(): VaultBuilderReturn; - protected buildRouter(vaultClient: VaultClient): express.Router; static createBuilder(env: VaultEnvironment): VaultBuilder; enableTokenRenew(schedule?: TaskRunner): Promise; - // (undocumented) - protected readonly env: VaultEnvironment; - setVaultClient(vaultClient: VaultClient): this; + setVaultClient(vaultApi: VaultApi): this; } // @public diff --git a/plugins/vault-backend/src/service/VaultBuilder.ts b/plugins/vault-backend/src/service/VaultBuilder.ts index 788b90ff01..a61d99ac70 100644 --- a/plugins/vault-backend/src/service/VaultBuilder.ts +++ b/plugins/vault-backend/src/service/VaultBuilder.ts @@ -19,7 +19,7 @@ import { InputError } from '@backstage/errors'; import { Logger } from 'winston'; import express from 'express'; import Router from 'express-promise-router'; -import { VaultClient } from './vaultApi'; +import { VaultApi, VaultClient } from './vaultApi'; import { TaskRunner, PluginTaskScheduler } from '@backstage/backend-tasks'; import { errorHandler } from '@backstage/backend-common'; @@ -47,7 +47,7 @@ export type VaultBuilderReturn = { * @public */ export class VaultBuilder { - private vaultClient?: VaultClient; + private vaultApi?: VaultApi; /** * Creates a new instance of the VaultBuilder. @@ -58,7 +58,8 @@ export class VaultBuilder { static createBuilder(env: VaultEnvironment) { return new VaultBuilder(env); } - constructor(protected readonly env: VaultEnvironment) {} + + constructor(private readonly env: VaultEnvironment) {} /** * Builds the routes for Vault. @@ -79,9 +80,9 @@ export class VaultBuilder { }; } - this.vaultClient = this.vaultClient ?? new VaultClient(this.env); + this.vaultApi = this.vaultApi ?? new VaultClient(this.env); - const router = this.buildRouter(this.vaultClient); + const router = this.buildRouter(this.vaultApi); return { router: router, @@ -91,11 +92,11 @@ export class VaultBuilder { /** * Overwrites the current vault client. * - * @param vaultClient - The new Vault client + * @param vaultApi - The new Vault client * @returns */ - public setVaultClient(vaultClient: VaultClient) { - this.vaultClient = vaultClient; + public setVaultClient(vaultApi: VaultApi) { + this.vaultApi = vaultApi; return this; } @@ -116,8 +117,8 @@ export class VaultBuilder { id: 'refresh-vault-token', fn: async () => { this.env.logger.info('Renewing Vault token'); - const vaultClient = this.vaultClient ?? new VaultClient(this.env); - await vaultClient.renewToken(); + const vaultApi = this.vaultApi ?? new VaultClient(this.env); + await vaultApi.renewToken?.(); }, }); return this; @@ -126,10 +127,10 @@ export class VaultBuilder { /** * Builds the backend routes for Vault. * - * @param vaultClient - The Vault client used to list the secrets. + * @param vaultApi - The Vault client used to list the secrets. * @returns The generated backend router */ - protected buildRouter(vaultClient: VaultClient): express.Router { + private buildRouter(vaultApi: VaultApi): express.Router { const router = Router(); router.use(express.json()); @@ -143,7 +144,7 @@ export class VaultBuilder { throw new InputError(`Invalid path: ${path}`); } - const secrets = await vaultClient.listSecrets(path); + const secrets = await vaultApi.listSecrets(path); res.json({ items: secrets }); }); diff --git a/plugins/vault-backend/src/service/vaultApi.ts b/plugins/vault-backend/src/service/vaultApi.ts index 7510ff218e..b75ea76368 100644 --- a/plugins/vault-backend/src/service/vaultApi.ts +++ b/plugins/vault-backend/src/service/vaultApi.ts @@ -56,7 +56,7 @@ type RenewTokenResponse = { */ export interface VaultApi { /** - * Returns the URL to acces the Vault UI with the defined config. + * Returns the URL to access the Vault UI with the defined config. */ getFrontendSecretsUrl(): string; /**