Skip to content

Commit 5e1a250

Browse files
committed
Umbraco Installer - including security.config and transforms for ImageProcessor configuration
1 parent 30442b5 commit 5e1a250

File tree

4 files changed

+111
-7
lines changed

4 files changed

+111
-7
lines changed

build/UmbracoFileSystemProviders.Azure.proj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@
136136
<UmbracoInstallerFiles Include="$(InstallerProjectDir)\Configurator\**\*.*"/>
137137
<UmbracoInstallerLogo Include="$(MSBuildProjectDirectory)\assets\logo\azure-logo-32.png"/>
138138
<PackageFile Include="$(MSBuildProjectDirectory)\package.xml" />
139-
139+
<ImageProcessorSecurityConfigXDTInstallFile Include="$(MSBuildProjectDirectory)\transforms\security.config.install.xdt" />
140+
<ImageProcessorSecurityConfigDefaultFile Include="$(MSBuildProjectDirectory)\config\security.config" />
141+
140142
<!-- NuGet specific -->
141143
<SrcFiles Include="$(ProjectDir)\**\*.cs" Exclude="$(ProjectDir)\obj\**"/>
142144
<NuSpecFile Include="$(MSBuildProjectDirectory)\package.nuspec" />
@@ -149,6 +151,9 @@
149151
<Copy SourceFiles="@(WebConfigXDTFiles)" DestinationFolder="$(BuildUmbDir)\App_Plugins\UmbracoFileSystemProviders\Azure\Install" />
150152
<Copy SourceFiles="@(FileSystemProvidersConfigXDTInstallFile)" DestinationFolder="$(BuildUmbDir)\App_Plugins\UmbracoFileSystemProviders\Azure\Install" />
151153
<Copy SourceFiles="@(FileSystemProvidersConfigXDTUninstallFile)" DestinationFolder="$(BuildUmbDir)\App_Plugins\UmbracoFileSystemProviders\Azure\Install" />
154+
<Copy SourceFiles="@(ImageProcessorSecurityConfigXDTInstallFile)" DestinationFolder="$(BuildUmbDir)\App_Plugins\UmbracoFileSystemProviders\Azure\Install" />
155+
<Copy SourceFiles="@(ImageProcessorSecurityConfigDefaultFile)" DestinationFolder="$(BuildUmbDir)\App_Plugins\UmbracoFileSystemProviders\Azure\Install" />
156+
152157
<Copy SourceFiles="@(UmbracoInstallerFiles)" DestinationFiles="@(UmbracoInstallerFiles->'$(BuildUmbDir)\App_Plugins\UmbracoFileSystemProviders\Azure\Install\Configurator\%(RecursiveDir)%(Filename)%(Extension)')" />
153158
<Copy SourceFiles="@(UmbracoInstallerLogo)" DestinationFolder="$(BuildUmbDir)\App_Plugins\UmbracoFileSystemProviders\Azure\Install" />
154159
<Copy SourceFiles="@(PackageFile)" DestinationFolder="$(BuildUmbDir)" />

build/config/security.config

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<security>
3+
<services>
4+
<service name="LocalFileImageService" type="ImageProcessor.Web.Services.LocalFileImageService, ImageProcessor.Web"/>
5+
<!--Disable the LocalFileImageService and enable this one when using virtual paths. -->
6+
<!--<service name="CloudImageService" type="ImageProcessor.Web.Services.CloudImageService, ImageProcessor.Web">
7+
<settings>
8+
<setting key="MaxBytes" value="8194304"/>
9+
<setting key="Timeout" value="30000"/>
10+
<setting key="Host" value="http://yourhost.com/"/>
11+
</settings>
12+
</service>-->
13+
<service prefix="remote.axd" name="RemoteImageService" type="ImageProcessor.Web.Services.RemoteImageService, ImageProcessor.Web">
14+
<settings>
15+
<setting key="MaxBytes" value="4194304"/>
16+
<setting key="Timeout" value="3000"/>
17+
<setting key="Protocol" value="http"/>
18+
</settings>
19+
<whitelist>
20+
</whitelist>
21+
</service>
22+
</services>
23+
</security>

build/transforms/security.config.install.xdt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<security xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
33
<services>
4-
<service prefix="media/" name="CloudImageService" type="ImageProcessor.Web.Services.CloudImageService, ImageProcessor.Web" xdt:Locator="Match(prefix, name)" xdt:Transform="InsertIfMissing">
5-
<settings>
6-
<setting key="MaxBytes" value="8194304"/>
7-
<setting key="Timeout" value="30000"/>
8-
<setting key="Host" value="http://[myAccountName].blob.core.windows.net/media/"/>
4+
<service prefix="media/" name="CloudImageService" type="ImageProcessor.Web.Services.CloudImageService, ImageProcessor.Web" xdt:Locator="Match(prefix, name, type)" xdt:Transform="Remove" />
5+
<service prefix="media/" name="CloudImageService" type="ImageProcessor.Web.Services.CloudImageService, ImageProcessor.Web" xdt:Locator="Match(prefix, name, type)" xdt:Transform="InsertIfMissing">
6+
<settings xdt:Transform="InsertIfMissing">
7+
<setting key="MaxBytes" value="8194304" xdt:Locator="Match(key)" xdt:Transform="InsertIfMissing" />
8+
<setting key="Timeout" value="30000" xdt:Locator="Match(key)" xdt:Transform="InsertIfMissing"/>
9+
<setting key="Host" value="http://[myAccountName].blob.core.windows.net/media/" xdt:Locator="Match(key)" xdt:Transform="InsertIfMissing"/>
910
</settings>
1011
</service>
1112
</services>

src/UmbracoFileSystemProviders.Azure.Installer/InstallerController.cs

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ public class InstallerController : UmbracoAuthorizedApiController
3939
private static readonly string ImageProcessorWebAssemblyPath = HostingEnvironment.MapPath("~/bin/ImageProcessor.Web.dll");
4040
private static readonly Version ImageProcessorWebMinRequiredVersion = new Version("4.3.2.0");
4141

42+
private static readonly string ImageProcessorConfigPath = HostingEnvironment.MapPath("~/Config/imageprocessor/");
43+
44+
private static readonly string ImageProcessorSecurityConfigPath = HostingEnvironment.MapPath("~/Config/imageprocessor/security.config");
45+
private static readonly string ImageProcessorSecurityDefaultConfigPath = HostingEnvironment.MapPath("~/App_Plugins/UmbracoFileSystemProviders/Azure/Install/security.config");
46+
private static readonly string ImageProcessorSecurityInstallXdtPath = HostingEnvironment.MapPath("~/App_Plugins/UmbracoFileSystemProviders/Azure/Install/security.config.install.xdt");
47+
private static readonly string ImageProcessorSecurityServiceType = "ImageProcessor.Web.Services.CloudImageService, ImageProcessor.Web";
48+
private static readonly string ImageProcessorSecurityServiceName = "CloudImageService";
49+
private static readonly string ImageProcessorSecurityServicePrefix = "media/";
50+
4251
private readonly string fileSystemProvidersConfigInstallXdtPath = HostingEnvironment.MapPath("~/App_Plugins/UmbracoFileSystemProviders/Azure/Install/FileSystemProviders.config.install.xdt");
4352
private readonly string fileSystemProvidersConfigPath = HostingEnvironment.MapPath("~/Config/FileSystemProviders.config");
4453

@@ -53,7 +62,10 @@ public IEnumerable<Parameter> GetParameters()
5362
public InstallerStatus PostParameters(IEnumerable<Parameter> parameters)
5463
{
5564
var connection = parameters.SingleOrDefault(k => k.Key == "connectionString").Value;
56-
var containerName = parameters.SingleOrDefault(k => k.Key == "containerName").Value;
65+
var containerName = parameters.SingleOrDefault(k => k.Key == "containerName").Value;
66+
var rootUrl = parameters.SingleOrDefault(k => k.Key == "rootUrl").Value;
67+
68+
var host = $"{rootUrl}{containerName}/";
5769

5870
if (!TestAzureCredentials(connection, containerName))
5971
{
@@ -71,6 +83,14 @@ public InstallerStatus PostParameters(IEnumerable<Parameter> parameters)
7183
{
7284
return InstallerStatus.ImageProcessorWebCompatibility;
7385
}
86+
else
87+
{
88+
// merge in Storage url
89+
SaveBlobPathToImageProcessorSecurityXdt(ImageProcessorSecurityInstallXdtPath, host);
90+
91+
// transform ImageProcessor security.config
92+
ExecuteImageProcessorSecurityConfigTransform();
93+
}
7494

7595
return InstallerStatus.Ok;
7696
}
@@ -139,6 +159,41 @@ internal static bool SaveParametersToXdt(string xdtPath, IEnumerable<Parameter>
139159
return result;
140160
}
141161

162+
internal static bool SaveBlobPathToImageProcessorSecurityXdt(string xdtPath, string blobPath)
163+
{
164+
var result = false;
165+
166+
var document = XmlHelper.OpenAsXmlDocument(xdtPath);
167+
168+
var rawSettings = document.SelectNodes(string.Format("//services/service[@prefix = '{0}' and @name = '{1}' and @type = '{2}']/settings/setting", ImageProcessorSecurityServicePrefix, ImageProcessorSecurityServiceName, ImageProcessorSecurityServiceType));
169+
170+
if (rawSettings == null)
171+
{
172+
return false;
173+
}
174+
175+
foreach (var setting in from XmlElement setting in rawSettings let key = setting.GetAttribute("key") where key == "Host" select setting)
176+
{
177+
setting.SetAttribute("value", blobPath);
178+
}
179+
180+
try
181+
{
182+
document.Save(xdtPath);
183+
184+
// No errors so the result is true
185+
result = true;
186+
}
187+
catch (Exception e)
188+
{
189+
// Log error message
190+
var message = "Error saving XDT Settings: " + e.Message;
191+
LogHelper.Error(typeof(InstallerController), message, e);
192+
}
193+
194+
return result;
195+
}
196+
142197
internal static IEnumerable<Parameter> GetParametersFromXdt(string xdtPath, string configPath)
143198
{
144199
// For package upgrades check for configured values in existing FileSystemProviders.config and merge with the Parameters from the XDT file (there could be new ones)
@@ -203,6 +258,26 @@ private static bool ExecuteWebConfigTransform()
203258
return transformConfig.Execute("UmbracoFileSystemProviders.Azure", transFormConfigAction);
204259
}
205260

261+
private static bool ExecuteImageProcessorSecurityConfigTransform()
262+
{
263+
// Ensure that security.config exists in ~/Config/Imageprocessor/
264+
if (!File.Exists(ImageProcessorSecurityConfigPath))
265+
{
266+
if (!Directory.Exists(ImageProcessorConfigPath))
267+
{
268+
Directory.CreateDirectory(ImageProcessorConfigPath);
269+
}
270+
271+
File.Copy(ImageProcessorSecurityDefaultConfigPath, ImageProcessorSecurityConfigPath);
272+
}
273+
274+
var transFormConfigAction = helper.parseStringToXmlNode("<Action runat=\"install\" undo=\"true\" alias=\"UmbracoFileSystemProviders.Azure.TransformConfig\" file=\"~/config/imageprocessor/security.config\" xdtfile=\"~/app_plugins/UmbracoFileSystemProviders/Azure/install/security.config\">" +
275+
"</Action>").FirstChild;
276+
277+
var transformConfig = new PackageActions.TransformConfig();
278+
return transformConfig.Execute("UmbracoFileSystemProviders.Azure", transFormConfigAction);
279+
}
280+
206281
private static bool TestAzureCredentials(string connectionString, string containerName)
207282
{
208283
var useEmulator = ConfigurationManager.AppSettings[Azure.Constants.Configuration.UseStorageEmulatorKey] != null

0 commit comments

Comments
 (0)