-
Notifications
You must be signed in to change notification settings - Fork 3
Resolve key vault references #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
590ecd7
Resolve key vault references
Eskibear aa9a464
address part of comments
Eskibear f82e612
support key vault reference with specific version
Eskibear 77edc29
address comments: impl keyvault support as an adapter
Eskibear 6871925
address comments: rename to AzureKeyVaultKeyValueAdapter
Eskibear File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
| import { ConfigurationSetting } from "@azure/app-configuration"; | ||
|
|
||
| export interface IKeyValueAdapter { | ||
| /** | ||
| * Determine whether the adapter applies to a configuration setting. | ||
| * Note: A setting is expected to be processed by at most one adapter. | ||
| */ | ||
| canProcess(setting: ConfigurationSetting): boolean; | ||
|
|
||
| /** | ||
| * This method process the original configuration setting, and returns processed key and value in an array. | ||
| */ | ||
| processKeyValue(setting: ConfigurationSetting): Promise<[string, unknown]>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
|
|
||
| import { TokenCredential } from "@azure/identity"; | ||
| import { SecretClient } from "@azure/keyvault-secrets"; | ||
|
|
||
| export interface AzureAppConfigurationKeyVaultOptions { | ||
| secretClients?: SecretClient[]; | ||
| credential?: TokenCredential; | ||
| secretResolver?: (keyVaultReference: URL) => string | Promise<string>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
|
|
||
| import { ConfigurationSetting, isSecretReference, parseSecretReference } from "@azure/app-configuration"; | ||
| import { IKeyValueAdapter } from "../IKeyValueAdapter"; | ||
| import { AzureAppConfigurationKeyVaultOptions } from "./AzureAppConfigurationKeyVaultOptions"; | ||
| import { SecretClient, parseKeyVaultSecretIdentifier } from "@azure/keyvault-secrets"; | ||
|
|
||
| export class AzureKeyVaultKeyValueAdapter implements IKeyValueAdapter { | ||
| /** | ||
| * Map vault hostname to corresponding secret client. | ||
| */ | ||
| private secretClients: Map<string, SecretClient>; | ||
|
|
||
| constructor( | ||
| private keyVaultOptions: AzureAppConfigurationKeyVaultOptions | undefined | ||
| ) { } | ||
|
|
||
| public canProcess(setting: ConfigurationSetting): boolean { | ||
| return isSecretReference(setting); | ||
| } | ||
|
|
||
| public async processKeyValue(setting: ConfigurationSetting): Promise<[string, unknown]> { | ||
| // TODO: cache results to save requests. | ||
| if (!this.keyVaultOptions) { | ||
| throw new Error("Configure keyVaultOptions to resolve Key Vault Reference(s)."); | ||
| } | ||
|
|
||
| // precedence: secret clients > credential > secret resolver | ||
| const { name: secretName, vaultUrl, sourceId, version } = parseKeyVaultSecretIdentifier( | ||
| parseSecretReference(setting).value.secretId | ||
| ); | ||
|
|
||
| const client = this.getSecretClient(new URL(vaultUrl)); | ||
| if (client) { | ||
| // TODO: what if error occurs when reading a key vault value? Now it breaks the whole load. | ||
| const secret = await client.getSecret(secretName, { version }); | ||
| return [setting.key, secret.value]; | ||
| } | ||
|
|
||
| if (this.keyVaultOptions.secretResolver) { | ||
| return [setting.key, await this.keyVaultOptions.secretResolver(new URL(sourceId))]; | ||
| } | ||
|
|
||
| throw new Error("No key vault credential or secret resolver callback configured, and no matching secret client could be found."); | ||
| } | ||
|
|
||
| private getSecretClient(vaultUrl: URL): SecretClient | undefined { | ||
| if (this.secretClients === undefined) { | ||
| this.secretClients = new Map(); | ||
| for (const c of this.keyVaultOptions?.secretClients ?? []) { | ||
| this.secretClients.set(getHost(c.vaultUrl), c); | ||
| } | ||
| } | ||
|
|
||
| let client: SecretClient | undefined; | ||
| client = this.secretClients.get(vaultUrl.host); | ||
| if (client !== undefined) { | ||
| return client; | ||
| } | ||
|
|
||
| if (this.keyVaultOptions?.credential) { | ||
| client = new SecretClient(vaultUrl.toString(), this.keyVaultOptions.credential); | ||
| this.secretClients.set(vaultUrl.host, client); | ||
| return client; | ||
| } | ||
|
|
||
| return undefined; | ||
| } | ||
| } | ||
|
|
||
| function getHost(url: string) { | ||
| return new URL(url).host; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
|
|
||
| const chai = require("chai"); | ||
| const chaiAsPromised = require("chai-as-promised"); | ||
| chai.use(chaiAsPromised); | ||
| const expect = chai.expect; | ||
| const { load } = require("../dist/index"); | ||
| const { sinon, | ||
| createMockedConnectionString, | ||
| createMockedTokenCredential, | ||
| mockAppConfigurationClientListConfigurationSettings, mockSecretClientGetSecret, restoreMocks, createMockedKeyVaultReference } = require("./utils/testHelper"); | ||
| const { SecretClient } = require("@azure/keyvault-secrets"); | ||
|
|
||
| const mockedData = [ | ||
| // key, secretUri, value | ||
| ["TestKey", "https://fake-vault-name.vault.azure.net/secrets/fakeSecretName", "SecretValue"], | ||
| ["TestKeyFixedVersion", "https://fake-vault-name.vault.azure.net/secrets/fakeSecretName/741a0fc52610449baffd6e1c55b9d459", "OldSecretValue"], | ||
| ["TestKey2", "https://fake-vault-name2.vault.azure.net/secrets/fakeSecretName2", "SecretValue2"] | ||
| ]; | ||
|
|
||
| function mockAppConfigurationClient() { | ||
| const kvs = mockedData.map(([key, vaultUri, _value]) => createMockedKeyVaultReference(key, vaultUri)); | ||
| mockAppConfigurationClientListConfigurationSettings(kvs); | ||
| } | ||
|
|
||
| function mockNewlyCreatedKeyVaultSecretClients() { | ||
| mockSecretClientGetSecret(mockedData.map(([_key, secretUri, value]) => [secretUri, value])); | ||
| } | ||
| describe("key vault reference", function () { | ||
| beforeEach(() => { | ||
| mockAppConfigurationClient(); | ||
| mockNewlyCreatedKeyVaultSecretClients(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| restoreMocks(); | ||
| }); | ||
|
|
||
| it("require key vault options to resolve reference", async () => { | ||
| expect(load(createMockedConnectionString())).eventually.rejected; | ||
| }); | ||
|
|
||
| it("should resolve key vault reference with credential", async () => { | ||
| const settings = await load(createMockedConnectionString(), { | ||
| keyVaultOptions: { | ||
| credential: createMockedTokenCredential() | ||
| } | ||
| }); | ||
| expect(settings).not.undefined; | ||
| expect(settings.get("TestKey")).eq("SecretValue"); | ||
| expect(settings.get("TestKeyFixedVersion")).eq("OldSecretValue"); | ||
| }); | ||
|
|
||
| it("should resolve key vault reference with secret resolver", async () => { | ||
| const settings = await load(createMockedConnectionString(), { | ||
| keyVaultOptions: { | ||
| secretResolver: (kvrUrl) => { | ||
| return "SecretResolver::" + kvrUrl.toString(); | ||
| } | ||
| } | ||
| }); | ||
| expect(settings).not.undefined; | ||
| expect(settings.get("TestKey")).eq("SecretResolver::https://fake-vault-name.vault.azure.net/secrets/fakeSecretName"); | ||
| }); | ||
|
|
||
| it("should resolve key vault reference with corresponding secret clients", async () => { | ||
| sinon.restore(); | ||
| mockAppConfigurationClient(); | ||
|
|
||
| // mock specific behavior per secret client | ||
| const client1 = new SecretClient("https://fake-vault-name.vault.azure.net", createMockedTokenCredential()); | ||
| sinon.stub(client1, "getSecret").returns({ value: "SecretValueViaClient1" }); | ||
| const client2 = new SecretClient("https://fake-vault-name2.vault.azure.net", createMockedTokenCredential()); | ||
| sinon.stub(client2, "getSecret").returns({ value: "SecretValueViaClient2" }); | ||
| const settings = await load(createMockedConnectionString(), { | ||
| keyVaultOptions: { | ||
| secretClients: [ | ||
| client1, | ||
| client2, | ||
| ] | ||
| } | ||
| }); | ||
| expect(settings).not.undefined; | ||
| expect(settings.get("TestKey")).eq("SecretValueViaClient1"); | ||
| expect(settings.get("TestKey2")).eq("SecretValueViaClient2"); | ||
| }); | ||
|
|
||
| it("should throw error when secret clients not provided for all key vault references", async () => { | ||
| const loadKeyVaultPromise = load(createMockedConnectionString(), { | ||
| keyVaultOptions: { | ||
| secretClients: [ | ||
| new SecretClient("https://fake-vault-name.vault.azure.net", createMockedTokenCredential()), | ||
| ] | ||
| } | ||
| }); | ||
| expect(loadKeyVaultPromise).eventually.rejected; | ||
| }); | ||
|
|
||
| it("should fallback to use default credential when corresponding secret client not provided", async () => { | ||
| const settings = await load(createMockedConnectionString(), { | ||
| keyVaultOptions: { | ||
| secretClients: [ | ||
| new SecretClient("https://fake-vault-name.vault.azure.net", createMockedTokenCredential()), | ||
| ], | ||
| credential: createMockedTokenCredential() | ||
| } | ||
| }); | ||
| expect(settings).not.undefined; | ||
| expect(settings.get("TestKey")).eq("SecretValue"); | ||
| expect(settings.get("TestKey2")).eq("SecretValue2"); | ||
| }); | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.