diff --git a/plugins/vault-backend/README.md b/plugins/vault-backend/README.md index aac2b68f60..8c70cfd24a 100644 --- a/plugins/vault-backend/README.md +++ b/plugins/vault-backend/README.md @@ -115,7 +115,7 @@ In case you need to support different secret engines for entities of the catalog # ... annotations: vault.io/secrets-path: path/to/secrets -+ vault.io/secrets-engine: customSecretEngine # Optional. By default it uses 'secertEngine' value from configuration. ++ vault.io/secrets-engine: customSecretEngine # Optional. By default it uses the 'secretEngine' value from your app-config. ``` That will overwrite the default secret engine from the configuration. @@ -152,7 +152,7 @@ export default async function createPlugin( ## Features - List the secrets present in a certain path -- Use different secret engines for different components +- Use different secret engines for different entities - Open a link to view the secret - Open a link to edit the secret - Renew the token automatically with a defined periodicity diff --git a/plugins/vault-backend/src/service/VaultBuilder.ts b/plugins/vault-backend/src/service/VaultBuilder.ts index ec118d7648..9856476c20 100644 --- a/plugins/vault-backend/src/service/VaultBuilder.ts +++ b/plugins/vault-backend/src/service/VaultBuilder.ts @@ -149,7 +149,9 @@ export class VaultBuilder { throw new InputError(`Invalid engine: ${engine}`); } - const secrets = await vaultApi.listSecrets(path, engine); + const secrets = await vaultApi.listSecrets(path, { + secretEngine: engine, + }); res.json({ items: secrets }); }); diff --git a/plugins/vault-backend/src/service/vaultApi.test.ts b/plugins/vault-backend/src/service/vaultApi.test.ts index 1d037bb77a..47c7c5a252 100644 --- a/plugins/vault-backend/src/service/vaultApi.test.ts +++ b/plugins/vault-backend/src/service/vaultApi.test.ts @@ -98,7 +98,9 @@ describe('VaultApi', () => { }); it('should return secrets for custom engine', async () => { - const secrets = await api.listSecrets('test/success', 'kv'); + const secrets = await api.listSecrets('test/success', { + secretEngine: 'kv', + }); expect(secrets).toEqual(mockSecretsResult('kv')); }); diff --git a/plugins/vault-backend/src/service/vaultApi.ts b/plugins/vault-backend/src/service/vaultApi.ts index adcaf07d56..d785e135df 100644 --- a/plugins/vault-backend/src/service/vaultApi.ts +++ b/plugins/vault-backend/src/service/vaultApi.ts @@ -63,11 +63,13 @@ export interface VaultApi { /** * Returns a list of secrets used to show in a table. * @param secretPath - The path where the secrets are stored in Vault - * @param secretMount - The mount point of the secrets engine, optional, overrides default secret engine + * @param options - Additional options to be passed to the Vault API, allows to override vault default settings in app config file */ listSecrets( secretPath: string, - secretMount?: string | undefined, + options?: { + secretEngine?: string; + }, ): Promise; /** @@ -123,9 +125,11 @@ export class VaultClient implements VaultApi { async listSecrets( secretPath: string, - secretEngine?: string | undefined, + options?: { + secretEngine?: string; + }, ): Promise { - const mount = secretEngine || this.vaultConfig.secretEngine; + const mount = options?.secretEngine || this.vaultConfig.secretEngine; const listUrl = this.vaultConfig.kvVersion === 2 @@ -142,7 +146,9 @@ export class VaultClient implements VaultApi { if (secret.endsWith('/')) { secrets.push( ...(await this.limit(() => - this.listSecrets(`${secretPath}/${secret.slice(0, -1)}`, mount), + this.listSecrets(`${secretPath}/${secret.slice(0, -1)}`, { + secretEngine: mount, + }), )), ); } else { diff --git a/plugins/vault/README.md b/plugins/vault/README.md index 3fe87398ce..ce2df6e47a 100644 --- a/plugins/vault/README.md +++ b/plugins/vault/README.md @@ -67,7 +67,7 @@ metadata: # ... annotations: vault.io/secrets-path: path/to/secrets - vault.io/secret-engine: customSecretEngine # Optional. By default it uses 'secertEngine' value from configuration. + vault.io/secrets-engine: customSecretEngine # Optional. By default it uses the 'secretEngine' value from your app-config. ``` The path is relative to your secrets engine folder. So if you want to get the secrets for backstage and you have the following directory structure: diff --git a/plugins/vault/src/api.test.ts b/plugins/vault/src/api.test.ts index 03c75fbce3..eb122a92d1 100644 --- a/plugins/vault/src/api.test.ts +++ b/plugins/vault/src/api.test.ts @@ -87,9 +87,9 @@ describe('api', () => { }); it('should return secrets with custom engine', async () => { - expect(await api.listSecrets('test/success', 'kv')).toEqual( - mockSecretsResult.items, - ); + expect( + await api.listSecrets('test/success', { secretEngine: 'kv' }), + ).toEqual(mockSecretsResult.items); expect(fetchApiSpy).toHaveBeenCalledWith( `${mockBaseUrl}/v1/secrets/test%2Fsuccess?engine=kv`, expect.anything(), diff --git a/plugins/vault/src/api.ts b/plugins/vault/src/api.ts index fa6a65a77d..f01823ff22 100644 --- a/plugins/vault/src/api.ts +++ b/plugins/vault/src/api.ts @@ -46,11 +46,13 @@ export interface VaultApi { /** * Returns a list of secrets used to show in a table. * @param secretPath - The path where the secrets are stored in Vault - * @param secretMount - The mount point of the secrets engine, optional, overrides default secret engine + * @param options - Additional options to be passed to the Vault API, allows to override vault default settings in app config file */ listSecrets( secretPath: string, - secretMount?: string | undefined, + options?: { + secretEngine?: string; + }, ): Promise; } @@ -96,11 +98,14 @@ export class VaultClient implements VaultApi { async listSecrets( secretPath: string, - secretMount?: string | undefined, + options?: { + secretEngine?: string; + }, ): Promise { const query: { [key in string]: any } = {}; - if (secretMount) { - query.engine = secretMount; + const { secretEngine } = options || {}; + if (secretEngine) { + query.engine = secretEngine; } const result = await this.callApi<{ items: VaultSecret[] }>( diff --git a/plugins/vault/src/components/EntityVaultTable/EntityVaultTable.tsx b/plugins/vault/src/components/EntityVaultTable/EntityVaultTable.tsx index c7fad01e0c..4d169611ca 100644 --- a/plugins/vault/src/components/EntityVaultTable/EntityVaultTable.tsx +++ b/plugins/vault/src/components/EntityVaultTable/EntityVaultTable.tsx @@ -49,7 +49,7 @@ export const EntityVaultTable = ({ entity }: { entity: Entity }) => { const { value, loading, error } = useAsync(async (): Promise< VaultSecret[] > => { - return vaultApi.listSecrets(secretPath, secretEngine); + return vaultApi.listSecrets(secretPath, { secretEngine }); }, []); const columns: TableColumn[] = [