Skip to content

Commit

Permalink
KeyVaultConfigService: fixed nextLinks + removed maxresults. (julienb…
Browse files Browse the repository at this point in the history
  • Loading branch information
julienblin authored Sep 7, 2018
1 parent fa88856 commit b9892eb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- [BREAKING] Removed maxresults in Azure KeyVaultConfigService options as it is not very useful (blocked at 25 anyway)
- Allow Azure KeyVaultConfigService to retrieve more than 25 results (Fix #73)

## [0.50.1] - 2018-09-05
### Changed
Expand Down
52 changes: 28 additions & 24 deletions packages/uno-serverless-azure/src/services/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { KeyVaultClient } from "azure-keyvault";
import { SecretBundle, SecretListResult } from "azure-keyvault/lib/models";
import { SecretBundle } from "azure-keyvault/lib/models";
import { loginWithAppServiceMSI, loginWithServicePrincipalSecret } from "ms-rest-azure";
import { checkHealth, CheckHealth, ConfigService, configurationError, duration } from "uno-serverless";

Expand All @@ -16,7 +16,6 @@ export interface KeyVaultConfigServiceOptionsWithCredentials {
export interface KeyVaultConfigServiceOptionsCommon {
prefix?: string | Promise<string>;
keyVaultUrl: string | Promise<string>;
maxResults?: number;
ttl?: number | string;
}

Expand Down Expand Up @@ -73,34 +72,39 @@ export class KeyVaultConfigService implements ConfigService, CheckHealth {
}

private async getParameters(): Promise<Record<string, string>> {
const client = await this.getClient() as any;
const client = await this.getClient();
let keyVaultUrl = await this.options.keyVaultUrl;
if (keyVaultUrl.endsWith("/")) {
keyVaultUrl = keyVaultUrl.slice(0, -1);
}
const results = await client.getSecrets(
keyVaultUrl,
{ maxresults: this.options.maxResults }) as SecretListResult;

const secretBundles = await Promise.all(
results
.filter((x) => !!x.id)
.map((item) => {
return client.getSecret(keyVaultUrl, this.secretName(keyVaultUrl, item.id), "") as Promise<SecretBundle>;
}));

const prefix = await this.options.prefix;

const parameterMap = {};
secretBundles.forEach((bundle) => {
const secretName = this.secretName(keyVaultUrl, bundle.id, true);
if (prefix) {
if (secretName.startsWith(prefix)) {
parameterMap[secretName.replace(prefix, "")] = bundle.value;
let nextLink: string | undefined;
do {
const results = nextLink
? await client.getSecretsNext(nextLink)
: await client.getSecrets(keyVaultUrl);

nextLink = results.nextLink;
const secretBundles = await Promise.all(
results
.filter((x) => !!x.id)
.map((item) => {
return client.getSecret(keyVaultUrl, this.secretName(keyVaultUrl, item.id), "") as Promise<SecretBundle>;
}));

const prefix = await this.options.prefix;
secretBundles.forEach((bundle) => {
const secretName = this.secretName(keyVaultUrl, bundle.id, true);
if (prefix) {
if (secretName.startsWith(prefix)) {
parameterMap[secretName.replace(prefix, "")] = bundle.value;
}
} else {
parameterMap[secretName] = bundle.value;
}
} else {
parameterMap[secretName] = bundle.value;
}
});
});
} while (nextLink);

return parameterMap;
}
Expand Down

0 comments on commit b9892eb

Please sign in to comment.