vault-backend: accept interface, not concrete client
Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/plugin-vault-backend': minor
|
||||
---
|
||||
|
||||
Allow generic Vault clients to be passed into the builder
|
||||
@@ -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<this>;
|
||||
// (undocumented)
|
||||
protected readonly env: VaultEnvironment;
|
||||
setVaultClient(vaultClient: VaultClient): this;
|
||||
setVaultClient(vaultApi: VaultApi): this;
|
||||
}
|
||||
|
||||
// @public
|
||||
|
||||
@@ -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 });
|
||||
});
|
||||
|
||||
|
||||
@@ -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;
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user