-
Notifications
You must be signed in to change notification settings - Fork 4
Key vault reference support #6
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
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
15cf96b
key vault reference support
linglingye001 40cf314
update
linglingye001 b4ace40
update
linglingye001 d783da6
update secret resolver
linglingye001 b2c435a
update keyvaultRef with url.URL type in SecretResolver interface
linglingye001 30bdd11
add test for concurrent key vault reference resolution
linglingye001 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
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
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,142 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package azureappconfiguration | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/url" | ||
| "strings" | ||
| "sync" | ||
|
|
||
| "github.com/Azure/azure-sdk-for-go/sdk/azcore" | ||
| "github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azsecrets" | ||
| ) | ||
|
|
||
| // keyVaultReferenceResolver resolves Key Vault references to their actual secret values | ||
| type keyVaultReferenceResolver struct { | ||
| clients sync.Map // map[string]secretClient | ||
| secretResolver SecretResolver | ||
| credential azcore.TokenCredential | ||
| } | ||
|
|
||
| // secretMetadata contains parsed information about a Key Vault secret reference | ||
| type secretMetadata struct { | ||
| host string | ||
| name string | ||
| version string | ||
| } | ||
|
|
||
| // keyVaultReference represents the JSON structure of a Key Vault reference | ||
| type keyVaultReference struct { | ||
| URI string `json:"uri"` | ||
| } | ||
|
|
||
| type secretClient interface { | ||
| GetSecret(ctx context.Context, name string, version string, options *azsecrets.GetSecretOptions) (azsecrets.GetSecretResponse, error) | ||
| } | ||
|
|
||
| // resolveSecret resolves a Key Vault reference to its actual secret value | ||
| func (r *keyVaultReferenceResolver) resolveSecret(ctx context.Context, keyVaultReference string) (string, error) { | ||
| // vaultUri: "https://{keyVaultName}.vault.azure.net/secrets/{secretName}/{secretVersion}" | ||
| uri, err := r.extractKeyVaultURI(keyVaultReference) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to parse Key Vault reference: %w", err) | ||
| } | ||
|
|
||
| // Parse the URI to get metadata (host, secret name, version) | ||
| secretMeta, err := parse(uri) | ||
| if err != nil { | ||
| return "", fmt.Errorf("invalid Key Vault reference: %w", err) | ||
| } | ||
|
|
||
| if r.secretResolver != nil { | ||
| vaultUri, err := url.Parse(uri) | ||
| if err != nil { | ||
| return "", fmt.Errorf("invalid Key Vault reference: %w", err) | ||
| } | ||
|
|
||
| return r.secretResolver.ResolveSecret(ctx, *vaultUri) | ||
| } | ||
|
|
||
| vaultURL := fmt.Sprintf("https://%s", secretMeta.host) | ||
| client, err := r.getSecretClient(vaultURL) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to get Key Vault client: %w", err) | ||
| } | ||
|
|
||
| response, err := client.GetSecret(ctx, secretMeta.name, secretMeta.version, nil) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to retrieve secret '%s' from Key Vault: %w", secretMeta.name, err) | ||
| } | ||
|
|
||
| if response.Value == nil { | ||
| return "", nil | ||
| } | ||
|
|
||
| return *response.Value, nil | ||
| } | ||
|
|
||
| // extractKeyVaultURI tries to parse a Key Vault reference in various formats | ||
| func (r *keyVaultReferenceResolver) extractKeyVaultURI(reference string) (string, error) { | ||
| // Valid Key Vault Reference setting value to parse | ||
| // { | ||
| // "uri":"https://{keyVaultName}.vault.azure.net/secrets/{secretName}/{secretVersion}" | ||
| // } | ||
| var kvRef keyVaultReference | ||
| if err := json.Unmarshal([]byte(reference), &kvRef); err == nil && kvRef.URI != "" { | ||
| return kvRef.URI, nil | ||
| } | ||
|
|
||
| return "", fmt.Errorf("invalid Key Vault reference format: %s", reference) | ||
| } | ||
|
|
||
| // getSecretClient gets or creates a client for the specified vault URL | ||
| func (r *keyVaultReferenceResolver) getSecretClient(vaultURL string) (secretClient, error) { | ||
| if client, ok := r.clients.Load(vaultURL); ok { | ||
| return client.(secretClient), nil | ||
| } | ||
|
|
||
| client, err := azsecrets.NewClient(vaultURL, r.credential, nil) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create Key Vault client: %w", err) | ||
| } | ||
|
|
||
| // Store the client - if concurrent call already stored a client, use the existing one | ||
| storedClient, loaded := r.clients.LoadOrStore(vaultURL, client) | ||
| if loaded { | ||
| // Another goroutine already created and stored a client | ||
| return storedClient.(secretClient), nil | ||
| } | ||
|
|
||
| return client, nil | ||
| } | ||
|
|
||
| // parse extracts metadata from a Key Vault secret reference URI | ||
| func parse(reference string) (*secretMetadata, error) { | ||
| secretURL, err := url.Parse(reference) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("invalid URL format: %w", err) | ||
| } | ||
|
|
||
| trimmedPath := strings.TrimPrefix(secretURL.Path, "/") | ||
| segments := strings.Split(trimmedPath, "/") | ||
|
|
||
| if len(segments) < 2 || strings.ToLower(segments[0]) != "secrets" || segments[1] == "" { | ||
| return nil, fmt.Errorf("invalid Key Vault URL format: %s", reference) | ||
| } | ||
|
|
||
| secretName := segments[1] | ||
| var secretVersion string | ||
| if len(segments) > 2 { | ||
| secretVersion = segments[2] | ||
| } | ||
|
|
||
| return &secretMetadata{ | ||
| host: strings.ToLower(secretURL.Host), | ||
| name: secretName, | ||
| version: secretVersion, | ||
| }, nil | ||
| } | ||
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.