Skip to content

Commit dc725be

Browse files
author
Rachel Breeze
committed
Added support for Azure Key Vault in Umbraco 7, based on #182
1 parent d4f1468 commit dc725be

File tree

7 files changed

+72
-14
lines changed

7 files changed

+72
-14
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,18 @@ In `Web.config` create the new application keys and post fix each key with the `
138138
<add key="AzureBlobFileSystem.UsePrivateContainer:media" value="false" />
139139
```
140140

141+
#### Configuration for Azure Key Vault
142+
For Azure Key Vault only the key values in the `Web.config` should use '-', rather than a '.' or ':' as shown below
143+
144+
```xml
145+
<add key="AzureBlobFileSystem-ConnectionString-media" value="DefaultEndpointsProtocol=https;AccountName=[myAccountName];AccountKey=[myAccountKey]" />
146+
<add key="AzureBlobFileSystem-ContainerName-media" value="media" />
147+
<add key="AzureBlobFileSystem-RootUrl-media" value="https://[myAccountName].blob.core.windows.net/" />
148+
<add key="AzureBlobFileSystem-MaxDays-media" value="365" />
149+
<add key="AzureBlobFileSystem-UseDefaultRoute-media" value="true" />
150+
<add key="AzureBlobFileSystem-UsePrivateContainer-media" value="false" />
151+
```
152+
141153
## Virtual Path Provider
142154
By default the plugin will serve files transparently from your domain or serve media directly from Azure. This is made possible by using a custom [Virtual Path Provider](https://msdn.microsoft.com/en-us/library/system.web.hosting.virtualpathprovider%28v=vs.110%29.aspx) included and automatically initialised upon application startup. This can be disable by adding the configuration setting noted above.
143155

src/UmbracoFileSystemProviders.Azure.Installer/InstallerController.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// Licensed under the Apache License, Version 2.0.
44
// </copyright>
55

6+
using Our.Umbraco.FileSystemProviders.Azure.Helpers;
7+
68
namespace Our.Umbraco.FileSystemProviders.Azure.Installer
79
{
810
using System;
@@ -447,8 +449,8 @@ private static bool ExecuteImageProcessorWebConfigTransform()
447449

448450
private static bool TestAzureCredentials(string connectionString, string containerName)
449451
{
450-
bool useEmulator = ConfigurationManager.AppSettings[Azure.Constants.Configuration.UseStorageEmulatorKey] != null
451-
&& ConfigurationManager.AppSettings[Azure.Constants.Configuration.UseStorageEmulatorKey]
452+
bool useEmulator = ConfigurationHelper.GetAppSetting(Azure.Constants.Configuration.UseStorageEmulatorKey) != null
453+
&& ConfigurationHelper.GetAppSetting(Azure.Constants.Configuration.UseStorageEmulatorKey)
452454
.Equals("true", StringComparison.InvariantCultureIgnoreCase);
453455
try
454456
{

src/UmbracoFileSystemProviders.Azure/AzureBlobFileSystem.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// Licensed under the Apache License, Version 2.0.
44
// </copyright>
55

6+
using Our.Umbraco.FileSystemProviders.Azure.Helpers;
7+
68
namespace Our.Umbraco.FileSystemProviders.Azure
79
{
810
using System;
@@ -104,34 +106,34 @@ public AzureBlobFileSystem(string containerName, string rootUrl, string connecti
104106
/// <param name="alias">The alias of the provider</param>
105107
public AzureBlobFileSystem(string alias)
106108
{
107-
string connectionString = ConfigurationManager.AppSettings[$"{ConnectionStringKey}:{alias}"];
109+
string connectionString = ConfigurationHelper.GetAppSetting($"{ConnectionStringKey}:{alias}");
108110
if (!string.IsNullOrWhiteSpace(connectionString))
109111
{
110-
string rootUrl = ConfigurationManager.AppSettings[$"{RootUrlKey}:{alias}"];
112+
string rootUrl = ConfigurationHelper.GetAppSetting($"{RootUrlKey}:{alias}");
111113
if (string.IsNullOrWhiteSpace(rootUrl))
112114
{
113115
throw new InvalidOperationException("Azure Storage Root URL is not defined in application settings. The " + RootUrlKey + " property was not defined or is empty.");
114116
}
115117

116-
string containerName = ConfigurationManager.AppSettings[$"{ContainerNameKey}:{alias}"];
118+
string containerName = ConfigurationHelper.GetAppSetting($"{ContainerNameKey}:{alias}");
117119
if (string.IsNullOrWhiteSpace(containerName))
118120
{
119121
containerName = "media";
120122
}
121123

122-
string maxDays = ConfigurationManager.AppSettings[$"{MaxDaysKey}:{alias}"];
124+
string maxDays = ConfigurationHelper.GetAppSetting($"{MaxDaysKey}:{alias}");
123125
if (string.IsNullOrWhiteSpace(maxDays))
124126
{
125127
maxDays = "365";
126128
}
127129

128-
string useDefaultRoute = ConfigurationManager.AppSettings[$"{UseDefaultRootKey}:{alias}"];
130+
string useDefaultRoute = ConfigurationHelper.GetAppSetting($"{UseDefaultRootKey}:{alias}");
129131
if (string.IsNullOrWhiteSpace(useDefaultRoute))
130132
{
131133
useDefaultRoute = "true";
132134
}
133135

134-
string accessType = ConfigurationManager.AppSettings[$"{UsePrivateContainerKey}:{alias}"];
136+
string accessType = ConfigurationHelper.GetAppSetting($"{UsePrivateContainerKey}:{alias}");
135137
if (string.IsNullOrWhiteSpace(accessType))
136138
{
137139
accessType = "true";

src/UmbracoFileSystemProviders.Azure/AzureFileSystem.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
// <summary>
77
// A singleton class for communicating with Azure Blob Storage.
88
// </summary>
9+
10+
using Our.Umbraco.FileSystemProviders.Azure.Helpers;
11+
912
namespace Our.Umbraco.FileSystemProviders.Azure
1013
{
1114
using System;
@@ -93,12 +96,12 @@ internal AzureFileSystem(string containerName, string rootUrl, string connection
9396
throw new ArgumentNullException(nameof(containerName));
9497
}
9598

96-
this.DisableVirtualPathProvider = ConfigurationManager.AppSettings[DisableVirtualPathProviderKey] != null
97-
&& ConfigurationManager.AppSettings[DisableVirtualPathProviderKey]
99+
this.DisableVirtualPathProvider = ConfigurationHelper.GetAppSetting(DisableVirtualPathProviderKey) != null
100+
&& ConfigurationHelper.GetAppSetting(DisableVirtualPathProviderKey)
98101
.Equals("true", StringComparison.InvariantCultureIgnoreCase);
99102

100-
bool useEmulator = ConfigurationManager.AppSettings[UseStorageEmulatorKey] != null
101-
&& ConfigurationManager.AppSettings[UseStorageEmulatorKey]
103+
bool useEmulator = ConfigurationHelper.GetAppSetting(UseStorageEmulatorKey) != null
104+
&& ConfigurationHelper.GetAppSetting(UseStorageEmulatorKey)
102105
.Equals("true", StringComparison.InvariantCultureIgnoreCase);
103106

104107
CloudStorageAccount cloudStorageAccount;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Configuration;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Our.Umbraco.FileSystemProviders.Azure.Helpers
9+
{
10+
public static class ConfigurationHelper
11+
{
12+
public static string GetAppSetting(string key)
13+
{
14+
var settings = ConfigurationManager.AppSettings[key];
15+
16+
if (!string.IsNullOrEmpty(settings))
17+
{
18+
return settings;
19+
}
20+
21+
return ConfigurationManager.AppSettings[key.Replace(".", "-")];
22+
}
23+
24+
public static string GetAppSetting(string key, string providerAlias)
25+
{
26+
var settings = ConfigurationManager.AppSettings[$"{key}:{providerAlias}"];
27+
28+
if (!string.IsNullOrEmpty(settings))
29+
{
30+
return settings;
31+
}
32+
33+
return ConfigurationManager.AppSettings[$"{key.Replace(".", "-")}-{providerAlias}"];
34+
}
35+
}
36+
}

src/UmbracoFileSystemProviders.Azure/UmbracoFileSystemProviders.Azure.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@
265265
<Compile Include="AzureBlobFileSystem.cs" />
266266
<Compile Include="AzureFileSystem.cs" />
267267
<Compile Include="Constants.cs" />
268+
<Compile Include="Helpers\ConfigurationHelper.cs" />
268269
<Compile Include="Helpers\IMimeTypeResolver.cs" />
269270
<Compile Include="Helpers\MimeTypeResolver.cs" />
270271
<Compile Include="Helpers\WrappedLogHelper.cs" />

src/UmbracoFileSystemProviders.Azure/VirtualPathProviderController.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// Licensed under the Apache License, Version 2.0.
44
// </copyright>
55

6+
using Our.Umbraco.FileSystemProviders.Azure.Helpers;
7+
68
namespace Our.Umbraco.FileSystemProviders.Azure
79
{
810
using System;
@@ -29,8 +31,8 @@ public class VirtualPathProviderController : ApplicationEventHandler
2931
/// <param name="applicationContext">The Umbraco <see cref="ApplicationContext"/> for the current application.</param>
3032
protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
3133
{
32-
bool disable = ConfigurationManager.AppSettings[DisableVirtualPathProviderKey] != null
33-
&& ConfigurationManager.AppSettings[DisableVirtualPathProviderKey]
34+
bool disable = ConfigurationHelper.GetAppSetting(DisableVirtualPathProviderKey) != null
35+
&& ConfigurationHelper.GetAppSetting(DisableVirtualPathProviderKey)
3436
.Equals("true", StringComparison.InvariantCultureIgnoreCase);
3537

3638
IFileSystem fileSystem = FileSystemProviderManager.Current.GetUnderlyingFileSystemProvider(Constants.DefaultMediaRoute);

0 commit comments

Comments
 (0)