Skip to content

Commit 14ec96d

Browse files
committed
Update Azure to use connection string.
1 parent 8017ded commit 14ec96d

4 files changed

Lines changed: 47 additions & 66 deletions

File tree

README.md

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Configuration Builders
22

3-
Configuration Builders are a new feature of the full .Net Framework, introduced in .Net 4.7.1. You can read about the concept in [this blog post](http://www.msdn.com).
3+
Configuration Builders are a new feature of the full .Net Framework, introduced in .Net 4.7.1. You can read about the concept in [this blog post](http://jeffreyfritz.com/2017/11/modern-configuration-for-asp-net-4-7-1-with-configurationbuilders/).
44
With this project, Microsoft is providing a basic set of Configuration Builders that should make it easy for developers to get started with the new feature. They
55
are also intended to address some of the basic needs of applications as they move into a container and cloud focused environment.
66

@@ -87,17 +87,15 @@ There are three additional configuration attributes for this config builder:
8787
[mode|prefix|stripPrefix]
8888
(vaultName="MyVaultName" |
8989
uri="https://MyVaultName.vault.azure.net")
90-
[clientId="12345678-9012-3456-7890-123456789012"
91-
clientSecret="8eNKl240FSfhgY909unhg23DKNj3b2cOO8bVvd+wdCc="]
90+
[connectionString="connection string"]
9291
type="Microsoft.Configuration.ConfigurationBuilders.AzureKeyVaultConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Azure" />
9392
```
94-
If your secrets are kept in Azure Key Vault, then this config builder is for you. There are four additional attributes for this config builder. The `vaultName` is
93+
If your secrets are kept in Azure Key Vault, then this config builder is for you. There are three additional attributes for this config builder. The `vaultName` is
9594
required. The other attributes allow you some manual control about which vault to connect to, but are only necessary if the application is not running in an
96-
environment that works well with `Microsoft.Azure.Services.AppAuthentication`. Otherwise, the Azure Services Authentication library is used to automatically pick
97-
up connection information from the execution environment.
95+
environment that works magically with `Microsoft.Azure.Services.AppAuthentication`. The Azure Services Authentication library is used to automatically pick
96+
up connection information from the execution environment if possible, but you can override that feature by providing a connection string instead.
9897
* `vaultName` - This is a required attribute. It specifies the name of the vault in your Azure subscription from which to read key/value pairs.
99-
* `clientId` - This is the Azure Active Directory App Id. A string representation of a GUID.
100-
* `clientSecret` - This is the Azure Active Directory App Key. A string.
98+
* `connectionString` - A connection string usable by [AzureServiceTokenProvider](https://docs.microsoft.com/en-us/azure/key-vault/service-to-service-authentication#connection-string-support)
10199
* `uri` - Connect to other Key Vault providers with this attribute. If not specified, Azure is the assumed Vault provider. If the uri _is_specified, then `vaultName` is no longer a required parameter.
102100

103101
### SimpleJsonConfigBuilder
@@ -163,7 +161,6 @@ public class CustomConfigBuilder : KeyValueConfigBuilder
163161
```
164162

165163
## Blog Posts
166-
[Insert](sldfj)
167-
[Blog Posts](sldkfjs)
168-
[And References](sldkfj)
169-
[Here](lskdjf)
164+
[.Net Framework 4.7.1 ASP.NET and Configuration features](https://blogs.msdn.microsoft.com/dotnet/2017/09/13/net-framework-4-7-1-asp-net-and-configuration-features/)
165+
[Modern Configuration for ASP.NET 4.7.1 with ConfigurationBuilders](http://jeffreyfritz.com/2017/11/modern-configuration-for-asp-net-4-7-1-with-configurationbuilders/)
166+
[Service-to-service authentication to Azure Key Vault using .NET](https://docs.microsoft.com/en-us/azure/key-vault/service-to-service-authentication#connection-string-support)

src/Azure/Azure.csproj

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
<SignAssembly>true</SignAssembly>
1616
<DelaySign>true</DelaySign>
1717
<AssemblyOriginatorKeyFile>..\35MSSharedLib1024.snk</AssemblyOriginatorKeyFile>
18+
<NuGetPackageImportStamp>
19+
</NuGetPackageImportStamp>
1820
</PropertyGroup>
1921
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
2022
<DebugSymbols>true</DebugSymbols>
@@ -35,28 +37,28 @@
3537
</PropertyGroup>
3638
<ItemGroup>
3739
<Reference Include="Microsoft.Azure.KeyVault, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
38-
<HintPath>..\..\packages\Microsoft.Azure.KeyVault.2.3.2\lib\net452\Microsoft.Azure.KeyVault.dll</HintPath>
40+
<HintPath>..\..\packages\Microsoft.Azure.KeyVault.2.4.0-preview\lib\net452\Microsoft.Azure.KeyVault.dll</HintPath>
3941
</Reference>
4042
<Reference Include="Microsoft.Azure.KeyVault.WebKey, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
41-
<HintPath>..\..\packages\Microsoft.Azure.KeyVault.WebKey.2.0.7\lib\net452\Microsoft.Azure.KeyVault.WebKey.dll</HintPath>
43+
<HintPath>..\..\packages\Microsoft.Azure.KeyVault.WebKey.2.1.0-preview\lib\net452\Microsoft.Azure.KeyVault.WebKey.dll</HintPath>
4244
</Reference>
4345
<Reference Include="Microsoft.Azure.Services.AppAuthentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
44-
<HintPath>..\..\packages\Microsoft.Azure.Services.AppAuthentication.1.0.0-preview\lib\net452\Microsoft.Azure.Services.AppAuthentication.dll</HintPath>
46+
<HintPath>..\..\packages\Microsoft.Azure.Services.AppAuthentication.1.1.0-preview\lib\net452\Microsoft.Azure.Services.AppAuthentication.dll</HintPath>
4547
</Reference>
46-
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory, Version=3.14.2.11, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
47-
<HintPath>..\..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.3.14.2\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll</HintPath>
48+
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory, Version=3.19.2.6005, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
49+
<HintPath>..\..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.3.19.2\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll</HintPath>
4850
</Reference>
49-
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory.Platform, Version=3.14.2.11, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
50-
<HintPath>..\..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.3.14.2\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.Platform.dll</HintPath>
51+
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory.Platform, Version=3.19.2.6005, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
52+
<HintPath>..\..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.3.19.2\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.Platform.dll</HintPath>
5153
</Reference>
5254
<Reference Include="Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
53-
<HintPath>..\..\packages\Microsoft.Rest.ClientRuntime.2.3.8\lib\net452\Microsoft.Rest.ClientRuntime.dll</HintPath>
55+
<HintPath>..\..\packages\Microsoft.Rest.ClientRuntime.2.3.10\lib\net452\Microsoft.Rest.ClientRuntime.dll</HintPath>
5456
</Reference>
5557
<Reference Include="Microsoft.Rest.ClientRuntime.Azure, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
56-
<HintPath>..\..\packages\Microsoft.Rest.ClientRuntime.Azure.3.3.7\lib\net452\Microsoft.Rest.ClientRuntime.Azure.dll</HintPath>
58+
<HintPath>..\..\packages\Microsoft.Rest.ClientRuntime.Azure.3.3.10\lib\net452\Microsoft.Rest.ClientRuntime.Azure.dll</HintPath>
5759
</Reference>
58-
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
59-
<HintPath>..\..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
60+
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
61+
<HintPath>..\..\packages\Newtonsoft.Json.11.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
6062
</Reference>
6163
<Reference Include="System" />
6264
<Reference Include="System.Configuration" />
@@ -66,15 +68,23 @@
6668
<Compile Include="AzureKeyVaultConfigBuilder.cs" />
6769
<Compile Include="Properties\AssemblyInfo.cs" />
6870
</ItemGroup>
69-
<ItemGroup>
70-
<None Include="packages.config" />
71-
</ItemGroup>
7271
<ItemGroup>
7372
<ProjectReference Include="..\Base\Base.csproj">
7473
<Project>{f382fbf8-146d-4968-a199-90d37f9ef9a7}</Project>
7574
<Name>Base</Name>
7675
</ProjectReference>
7776
</ItemGroup>
77+
<ItemGroup>
78+
<None Include="app.config" />
79+
<None Include="packages.config" />
80+
</ItemGroup>
7881
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
7982
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory),MicrosoftConfigurationBuilders.sln))\tools\cleanup.targets" />
83+
<Import Project="..\..\packages\Microsoft.Azure.Services.AppAuthentication.1.1.0-preview\build\Microsoft.Azure.Services.AppAuthentication.targets" Condition="Exists('..\..\packages\Microsoft.Azure.Services.AppAuthentication.1.1.0-preview\build\Microsoft.Azure.Services.AppAuthentication.targets')" />
84+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
85+
<PropertyGroup>
86+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
87+
</PropertyGroup>
88+
<Error Condition="!Exists('..\..\packages\Microsoft.Azure.Services.AppAuthentication.1.1.0-preview\build\Microsoft.Azure.Services.AppAuthentication.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.Azure.Services.AppAuthentication.1.1.0-preview\build\Microsoft.Azure.Services.AppAuthentication.targets'))" />
89+
</Target>
8090
</Project>

src/Azure/AzureKeyVaultConfigBuilder.cs

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@ namespace Microsoft.Configuration.ConfigurationBuilders
1414
public class AzureKeyVaultConfigBuilder : KeyValueConfigBuilder
1515
{
1616
public const string vaultNameTag = "vaultName";
17-
public const string clientIdTag = "clientId";
18-
public const string clientSecretTag = "clientSecret";
17+
public const string connectionStringTag = "connectionString";
1918
public const string uriTag = "uri";
2019

2120
private string _vaultName;
22-
private string _clientId;
23-
private string _clientSecret;
21+
private string _connectionString;
2422
private string _uri;
2523

2624
private KeyVaultClient _kvClient;
@@ -42,24 +40,12 @@ public override void Initialize(string name, NameValueCollection config)
4240
}
4341
_uri = _uri.TrimEnd(new char[] { '/' });
4442

45-
string tmp = config?[clientIdTag];
46-
_clientId = (String.IsNullOrWhiteSpace(tmp)) ? null : tmp;
47-
tmp = config?[clientSecretTag];
48-
_clientSecret = (String.IsNullOrWhiteSpace(tmp)) ? null : tmp;
43+
_connectionString = config?[connectionStringTag];
44+
_connectionString = String.IsNullOrWhiteSpace(_connectionString) ? null : _connectionString;
4945

50-
51-
// If Client ID and Secret are provided, connect to KeyVault that way.
52-
if (!String.IsNullOrWhiteSpace(_clientId) && !String.IsNullOrWhiteSpace(_clientSecret))
53-
{
54-
_kvClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(GetTokenFromClientSecret));
55-
}
56-
57-
// Otherwise, fall back on the magic of Microsoft.Azure.Services.AppAuthentication.
58-
else
59-
{
60-
AzureServiceTokenProvider tokenProvider = new AzureServiceTokenProvider();
61-
_kvClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(tokenProvider.KeyVaultTokenCallback));
62-
}
46+
// Connect to KeyValut
47+
AzureServiceTokenProvider tokenProvider = new AzureServiceTokenProvider(_connectionString);
48+
_kvClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(tokenProvider.KeyVaultTokenCallback));
6349

6450
_allKeys = GetAllKeys();
6551
}
@@ -100,18 +86,6 @@ private async Task<string> GetValueAsync(string key)
10086
return null;
10187
}
10288

103-
private async Task<string> GetTokenFromClientSecret(string authority, string resource, string scope)
104-
{
105-
AuthenticationContext authContext = new AuthenticationContext(authority);
106-
ClientCredential clientCred = new ClientCredential(_clientId, _clientSecret);
107-
AuthenticationResult result = await authContext.AcquireTokenAsync(resource, clientCred);
108-
109-
if (result == null)
110-
throw new InvalidOperationException("Failed to obtain token from client secret.");
111-
112-
return result.AccessToken;
113-
}
114-
11589
private List<string> GetAllKeys()
11690
{
11791
var allSecrets = Task.Run(async () => { return await _kvClient.GetSecretsAsync(_uri); }).Result;

src/Azure/packages.config

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="Microsoft.Azure.KeyVault" version="2.3.2" targetFramework="net47" />
4-
<package id="Microsoft.Azure.KeyVault.WebKey" version="2.0.7" targetFramework="net47" />
5-
<package id="Microsoft.Azure.Services.AppAuthentication" version="1.0.0-preview" targetFramework="net47" />
6-
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.14.2" targetFramework="net47" />
7-
<package id="Microsoft.Rest.ClientRuntime" version="2.3.8" targetFramework="net47" />
8-
<package id="Microsoft.Rest.ClientRuntime.Azure" version="3.3.7" targetFramework="net47" />
9-
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net47" />
3+
<package id="Microsoft.Azure.KeyVault" version="2.4.0-preview" targetFramework="net471" />
4+
<package id="Microsoft.Azure.KeyVault.WebKey" version="2.1.0-preview" targetFramework="net471" />
5+
<package id="Microsoft.Azure.Services.AppAuthentication" version="1.1.0-preview" targetFramework="net471" />
6+
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.19.2" targetFramework="net471" />
7+
<package id="Microsoft.Rest.ClientRuntime" version="2.3.10" targetFramework="net471" />
8+
<package id="Microsoft.Rest.ClientRuntime.Azure" version="3.3.10" targetFramework="net471" />
9+
<package id="Newtonsoft.Json" version="11.0.1" targetFramework="net471" />
1010
</packages>

0 commit comments

Comments
 (0)