-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for application configuration service in 3.2 line (#1269)
* Initial support for application configuration service, fixes for CA2017 * prevent infinite loop with postprocessors
- Loading branch information
Showing
17 changed files
with
244 additions
and
31 deletions.
There are no files selected for viewing
This file contains 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
95 changes: 95 additions & 0 deletions
95
src/Configuration/src/Kubernetes.ServiceBinding/ConfigurationKeyConverter.cs
This file contains 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,95 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Microsoft.Extensions.Configuration; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace Steeltoe.Common.Configuration; | ||
|
||
internal static class ConfigurationKeyConverter | ||
{ | ||
private const string DotDelimiterString = "."; | ||
private const char DotDelimiterChar = '.'; | ||
private const char UnderscoreDelimiterChar = '_'; | ||
private const char EscapeChar = '\\'; | ||
private const string EscapeString = "\\"; | ||
|
||
private static readonly Regex ArrayRegex = new (@"\[(?<digits>\d+)\]", RegexOptions.Compiled | RegexOptions.Singleline); | ||
|
||
public static string AsDotNetConfigurationKey(string key) | ||
{ | ||
if (string.IsNullOrEmpty(key)) | ||
{ | ||
return key; | ||
} | ||
|
||
IEnumerable<string> split = UniversalHierarchySplit(key); | ||
var sb = new StringBuilder(); | ||
|
||
foreach (string keyPart in split.Select(ConvertArrayKey)) | ||
{ | ||
sb.Append(keyPart); | ||
sb.Append(ConfigurationPath.KeyDelimiter); | ||
} | ||
|
||
return sb.ToString(0, sb.Length - 1); | ||
} | ||
|
||
private static IEnumerable<string> UniversalHierarchySplit(string source) | ||
{ | ||
if (source is null) | ||
{ | ||
throw new ArgumentNullException(nameof(source)); | ||
} | ||
|
||
var result = new List<string>(); | ||
|
||
int segmentStart = 0; | ||
|
||
for (int i = 0; i < source.Length; i++) | ||
{ | ||
bool readEscapeChar = false; | ||
|
||
if (source[i] == EscapeChar) | ||
{ | ||
readEscapeChar = true; | ||
i++; | ||
} | ||
|
||
if (!readEscapeChar && source[i] == DotDelimiterChar) | ||
{ | ||
result.Add(UnEscapeString(source[segmentStart..i])); | ||
segmentStart = i + 1; | ||
} | ||
|
||
if (!readEscapeChar && source[i] == UnderscoreDelimiterChar && i < source.Length - 1 && source[i + 1] == UnderscoreDelimiterChar) | ||
{ | ||
result.Add(UnEscapeString(source[segmentStart..i])); | ||
segmentStart = i + 2; | ||
} | ||
|
||
if (i == source.Length - 1) | ||
{ | ||
result.Add(UnEscapeString(source[segmentStart..])); | ||
} | ||
} | ||
|
||
return result; | ||
|
||
static string UnEscapeString(string src) | ||
{ | ||
return src.Replace(EscapeString + DotDelimiterString, DotDelimiterString, StringComparison.Ordinal) | ||
.Replace(EscapeString + EscapeString, EscapeString, StringComparison.Ordinal); | ||
} | ||
} | ||
|
||
private static string ConvertArrayKey(string key) | ||
{ | ||
return ArrayRegex.Replace(key, ":${digits}"); | ||
} | ||
} |
This file contains 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 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
28 changes: 28 additions & 0 deletions
28
src/Configuration/test/Kubernetes.ServiceBinding.Test/ConfigurationKeyConverterTest.cs
This file contains 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,28 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using FluentAssertions; | ||
using Steeltoe.Common.Configuration; | ||
using Xunit; | ||
|
||
namespace Steeltoe.Extensions.Configuration.Kubernetes.ServiceBinding.Test; | ||
|
||
public sealed class ConfigurationKeyConverterTest | ||
{ | ||
[Theory] | ||
[InlineData("unchanged", "unchanged")] | ||
[InlineData("unchanged[index]", "unchanged[index]")] | ||
[InlineData("one.four.seven", "one:four:seven")] | ||
[InlineData("one__four__seven", "one:four:seven")] | ||
[InlineData("one__four__seven__", "one:four:seven:")] | ||
[InlineData("_one__four__and_seven_", "_one:four:and_seven_")] | ||
[InlineData("one[1]", "one:1")] | ||
[InlineData("one[12][3456]", "one:12:3456")] | ||
[InlineData("one.four.seven[0][1].twelve.thirteen[12]", "one:four:seven:0:1:twelve:thirteen:12")] | ||
[InlineData(@"one\.four\\.seven", @"one.four\:seven")] | ||
public void AsDotNetConfigurationKey_ProducesExpected(string input, string expectedOutput) | ||
{ | ||
_ = ConfigurationKeyConverter.AsDotNetConfigurationKey(input).Should().BeEquivalentTo(expectedOutput); | ||
} | ||
} |
This file contains 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 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
1 change: 1 addition & 0 deletions
1
...Configuration/test/Kubernetes.ServiceBinding.Test/resources/k8s/test-acs/key.with.periods
This file contains 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 @@ | ||
test-secret-value. |
1 change: 1 addition & 0 deletions
1
...test/Kubernetes.ServiceBinding.Test/resources/k8s/test-acs/key__with__double__underscores
This file contains 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 @@ | ||
test-secret-value0 |
1 change: 1 addition & 0 deletions
1
...est/Kubernetes.ServiceBinding.Test/resources/k8s/test-acs/key__with__double__underscores_
This file contains 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 @@ | ||
test-secret-value1 |
1 change: 1 addition & 0 deletions
1
...st/Kubernetes.ServiceBinding.Test/resources/k8s/test-acs/key__with__double__underscores__
This file contains 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 @@ | ||
test-secret-value2 |
1 change: 1 addition & 0 deletions
1
src/Configuration/test/Kubernetes.ServiceBinding.Test/resources/k8s/test-acs/provider
This file contains 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 @@ | ||
acs |
1 change: 1 addition & 0 deletions
1
src/Configuration/test/Kubernetes.ServiceBinding.Test/resources/k8s/test-acs/test-secret-key
This file contains 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 @@ | ||
test-secret-value |
1 change: 1 addition & 0 deletions
1
src/Configuration/test/Kubernetes.ServiceBinding.Test/resources/k8s/test-acs/type
This file contains 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 @@ | ||
config |
This file contains 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 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
Oops, something went wrong.