vault-backend: accept interface, not concrete client

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2023-03-14 15:58:09 +01:00
parent 2584660394
commit 5e959c9eb6
4 changed files with 21 additions and 18 deletions
@@ -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;
/**