Skip to content

Commit 7273d77

Browse files
Bring back connectionString for Azure App Configuration. (#106)
1 parent af83839 commit 7273d77

3 files changed

Lines changed: 39 additions & 26 deletions

File tree

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,20 +210,19 @@ and currently exposes the format of the file which, as mentioned above, should b
210210
```xml
211211
<add name="AzureAppConfig"
212212
[mode|@prefix|@stripPrefix|tokenPattern|@escapeExpandedValues|@optional=false]
213-
@endpoint="https://your-appconfig-store.azconfig.io"
213+
(@endpoint="https://your-appconfig-store.azconfig.io" | @connectionString="Endpoint=https://your-appconfig-store.azconfig.io;Id=XXXXXXXXXX;Secret=XXXXXXXXXX")
214214
[@keyFilter="string"]
215215
[@labelFilter="label"]
216216
[@acceptDateTime="DateTimeOffset"]
217217
[@useAzureKeyVault="bool"]
218218
type="Microsoft.Configuration.ConfigurationBuilders.AzureAppConfigurationBuilder, Microsoft.Configuration.ConfigurationBuilders.AzureAppConfig" />
219219
```
220220
[AppConfiguration](https://docs.microsoft.com/en-us/azure/azure-app-configuration/overview) is a new offering from Azure, currently in preview. If you
221-
wish to use this new service for managing your configuration, then use this AzureAppConfigurationBuilder. `endpoint` is
222-
required, but all other attributes are optional.
223-
Previous iterations of this config builder allowed for a `connectionString` to connect to the
224-
App Configuration service. This method is no longer allowed, and this config builder now exclusively uses [DefaultAzureCredential](https://docs.microsoft.com/en-us/dotnet/api/azure.identity.defaultazurecredential)
225-
from the Azure.Identity package to handle credentials for connecting to the service.
221+
wish to use this new service for managing your configuration, then use this AzureAppConfigurationBuilder. Either `endpoint` or `connectionString` are
222+
required, but all other attributes are optional. If both `endpoint` and `connectionString` are used, then preference is given to the connection string.
226223
* `endpoint` - This specifies the AppConfiguration store to connect to.
224+
* `connectionString` - This specifies the AppConfiguration store to connect to, along with the Id and Secret necessary to access the service. Be careful
225+
not to expose any secrets in your code, repos, or App Configuration stores if you use this method for connecting.
227226
* `keyFilter` - Use this to select a set of configuration values matching a certain key pattern.
228227
* `labelFilter` - Only retrieve configuration values that match a certain label.
229228
* `acceptDateTime` - Instead of versioning ala Azure Key Vault, AppConfiguration uses timestamps. Use this attribute to go back in time

src/AzureAppConfig/AzureAppConfigurationBuilder.cs

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@ public class AzureAppConfigurationBuilder : KeyValueConfigBuilder
2828

2929
#pragma warning disable CS1591 // No xml comments for tag literals.
3030
public const string endpointTag = "endpoint";
31-
public const string connectionStringTag = "connectionString"; // obsolete
31+
public const string connectionStringTag = "connectionString";
3232
public const string keyFilterTag = "keyFilter";
3333
public const string labelFilterTag = "labelFilter";
3434
public const string dateTimeFilterTag = "acceptDateTime";
3535
public const string useKeyVaultTag = "useAzureKeyVault";
3636
#pragma warning restore CS1591 // No xml comments for tag literals.
3737

3838
private Uri _endpoint;
39+
private string _connectionString;
3940
private string _keyFilter;
4041
private string _labelFilter;
4142
private DateTimeOffset _dateTimeFilter;
@@ -82,34 +83,46 @@ protected override void LazyInitialize(string name, NameValueCollection config)
8283
if (_useKeyVault)
8384
_kvClientCache = new ConcurrentDictionary<Uri, SecretClient>(EqualityComparer<Uri>.Default);
8485

85-
// Config Store Endpoint
86-
string uri = UpdateConfigSettingWithAppSettings(endpointTag);
87-
if (!String.IsNullOrWhiteSpace(uri))
86+
87+
// Always allow 'connectionString' to override black magic. But we expect this to be null most of the time.
88+
_connectionString = UpdateConfigSettingWithAppSettings(connectionStringTag);
89+
if (String.IsNullOrWhiteSpace(_connectionString))
8890
{
91+
_connectionString = null;
92+
93+
// Use Endpoint instead
94+
string uri = UpdateConfigSettingWithAppSettings(endpointTag);
95+
if (!String.IsNullOrWhiteSpace(uri))
96+
{
97+
try
98+
{
99+
_endpoint = new Uri(uri);
100+
_client = new ConfigurationClient(_endpoint, new DefaultAzureCredential());
101+
}
102+
catch (Exception ex)
103+
{
104+
if (!Optional)
105+
throw new ArgumentException($"Exception encountered while creating connection to Azure App Configuration store.", ex);
106+
}
107+
}
108+
else
109+
{
110+
throw new ArgumentException($"An endpoint URI or connection string must be provided for connecting to Azure App Configuration service via the '{endpointTag}' or '{connectionStringTag}' attribute.");
111+
}
112+
}
113+
else
114+
{
115+
// If we get here, then we should try to connect with a connection string.
89116
try
90117
{
91-
_endpoint = new Uri(uri);
92-
_client = new ConfigurationClient(_endpoint, new DefaultAzureCredential());
118+
_client = new ConfigurationClient(_connectionString);
93119
}
94120
catch (Exception ex)
95121
{
96122
if (!Optional)
97123
throw new ArgumentException($"Exception encountered while creating connection to Azure App Configuration store.", ex);
98124
}
99125
}
100-
else
101-
{
102-
throw new ArgumentException($"An endpoint URI must be provided for connecting to Azure App Configuration service via the '{endpointTag}' attribute.");
103-
}
104-
105-
if (config[connectionStringTag] != null)
106-
{
107-
// A connection string was given. Connection strings are no longer supported. Azure.Identity is the preferred way to
108-
// authenticate, and that library has various mechanisms other than a plain text connection string in config to obtain
109-
// the necessary client credentials for connecting to Azure.
110-
// Be noisy about this even if optional, as it is a fundamental misconfiguration going forward.
111-
throw new ArgumentException("AzureAppConfigurationBuilder no longer supports connection strings.", connectionStringTag);
112-
}
113126

114127
// At this point we've got all our ducks in a row and are ready to go. And we know that
115128
// we will be used, because this is the 'lazy' initializer. But let's handle one oddball case

src/packages/ConfigurationBuilders.AzureAppConfiguration.nupkg/tools/Net471/Install.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ $keyVaultConfigBuilder = [BuilderDescription]@{
1212
Version="$assemblyVersion$";
1313
DefaultName="AzureAppConfig";
1414
AllowedParameters=@( $keyValueCommonParameters +
15-
[ParameterDescription]@{ Name="endpoint"; IsRequired=$true; DefaultValue="[Config_Store_Endpoint_Url]" },
15+
[ParameterDescription]@{ Name="endpoint"; IsRequired=$false; DefaultValue="[Config_Store_Endpoint_Url]" },
16+
[ParameterDescription]@{ Name="connectionString"; IsRequired=$false },
1617
[ParameterDescription]@{ Name="keyFilter"; IsRequired=$false },
1718
[ParameterDescription]@{ Name="labelFilter"; IsRequired=$false },
1819
[ParameterDescription]@{ Name="acceptDateTime"; IsRequired=$false };

0 commit comments

Comments
 (0)