-
Notifications
You must be signed in to change notification settings - Fork 284
Update variable replacement during deserialization to use replacement settings class and add AKV replacement logic. #2882
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
base: main
Are you sure you want to change the base?
Changes from all commits
2fc665d
50e54aa
455ff18
e0574bf
033eeb0
c7ad08f
049b23e
cfdc2b5
5eb9f0e
bbcc159
f6f92bd
0dd7ad6
802eb53
4e71c25
ac4356e
c3dabe3
6e77297
3254af5
b28cd6b
5e79339
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
| using Azure.DataApiBuilder.Config.ObjectModel; | ||
|
|
||
| namespace Azure.DataApiBuilder.Config.Converters; | ||
|
|
||
| /// <summary> | ||
| /// Converter factory for AzureKeyVaultOptions that can optionally perform variable replacement. | ||
| /// </summary> | ||
| internal class AzureKeyVaultOptionsConverterFactory : JsonConverterFactory | ||
| { | ||
| // Determines whether to replace environment variable with its | ||
| // value or not while deserializing. | ||
| private readonly DeserializationVariableReplacementSettings? _replacementSettings; | ||
|
|
||
| /// <param name="replaceEnvVar">Whether to replace environment variable with its | ||
| /// value or not while deserializing.</param> | ||
| internal AzureKeyVaultOptionsConverterFactory(DeserializationVariableReplacementSettings? replacementSettings = null) | ||
| { | ||
| _replacementSettings = replacementSettings; | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public override bool CanConvert(Type typeToConvert) | ||
| { | ||
| return typeToConvert.IsAssignableTo(typeof(AzureKeyVaultOptions)); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options) | ||
| { | ||
| return new AzureKeyVaultOptionsConverter(_replacementSettings); | ||
| } | ||
|
|
||
| private class AzureKeyVaultOptionsConverter : JsonConverter<AzureKeyVaultOptions> | ||
| { | ||
| // Determines whether to replace environment variable with its | ||
| // value or not while deserializing. | ||
| private readonly DeserializationVariableReplacementSettings? _replacementSettings; | ||
|
|
||
| /// <param name="replaceEnvVar">Whether to replace environment variable with its | ||
| /// value or not while deserializing.</param> | ||
| public AzureKeyVaultOptionsConverter(DeserializationVariableReplacementSettings? replacementSettings) | ||
| { | ||
| _replacementSettings = replacementSettings; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Reads AzureKeyVaultOptions with optional variable replacement. | ||
| /// </summary> | ||
| public override AzureKeyVaultOptions? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
| { | ||
| if (reader.TokenType is JsonTokenType.Null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| if (reader.TokenType is JsonTokenType.StartObject) | ||
| { | ||
| string? endpoint = null; | ||
| AKVRetryPolicyOptions? retryPolicy = null; | ||
|
|
||
| while (reader.Read()) | ||
| { | ||
| if (reader.TokenType is JsonTokenType.EndObject) | ||
| { | ||
| return new AzureKeyVaultOptions | ||
| { | ||
| Endpoint = endpoint, | ||
| RetryPolicy = retryPolicy | ||
| }; | ||
| } | ||
|
|
||
| string? property = reader.GetString(); | ||
| reader.Read(); | ||
|
|
||
| switch (property) | ||
| { | ||
| case "endpoint": | ||
| if (reader.TokenType is JsonTokenType.String) | ||
| { | ||
| endpoint = reader.DeserializeString(_replacementSettings); | ||
| } | ||
|
|
||
| break; | ||
|
|
||
| case "retry-policy": | ||
| if (reader.TokenType is JsonTokenType.StartObject) | ||
| { | ||
| // Pass the replaceEnvVar setting to the retry policy converter | ||
| retryPolicy = JsonSerializer.Deserialize<AKVRetryPolicyOptions>(ref reader, options); | ||
| } | ||
|
|
||
| break; | ||
|
|
||
| default: | ||
| throw new JsonException($"Unexpected property {property}"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| throw new JsonException("Invalid AzureKeyVaultOptions format"); | ||
| } | ||
|
|
||
| public override void Write(Utf8JsonWriter writer, AzureKeyVaultOptions value, JsonSerializerOptions options) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What changed in this file? |
||
| { | ||
| JsonSerializer.Serialize(writer, value, options); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we always want to write the options back into the config? I would expect to only write values if the user wants to do it in the first place. |
||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.