Skip to content

Commit

Permalink
Merge pull request #7 from atc-net/feature/maintenance
Browse files Browse the repository at this point in the history
Feature/maintenance
  • Loading branch information
davidkallesen authored Nov 5, 2023
2 parents f4d5c49 + 99cf2d7 commit e7bcdf6
Show file tree
Hide file tree
Showing 56 changed files with 2,124 additions and 630 deletions.
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@
<ItemGroup Label="Code Analyzers">
<PackageReference Include="AsyncFixer" Version="1.6.0" PrivateAssets="All" />
<PackageReference Include="Asyncify" Version="0.9.7" PrivateAssets="All" />
<PackageReference Include="Meziantou.Analyzer" Version="2.0.92" PrivateAssets="All" />
<PackageReference Include="Meziantou.Analyzer" Version="2.0.103" PrivateAssets="All" />
<PackageReference Include="SecurityCodeScan.VS2019" Version="5.6.7" PrivateAssets="All" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.507" PrivateAssets="All" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.11.0.78383" PrivateAssets="All" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.12.0.78982" PrivateAssets="All" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Atc" Version="2.0.360" />
<PackageReference Include="Azure.Identity" Version="1.10.1" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.18.0" />
<PackageReference Include="Atc" Version="2.0.386" />
<PackageReference Include="Azure.Identity" Version="1.10.3" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.19.0-beta.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
global using System.Diagnostics.CodeAnalysis;
global using System.Runtime.CompilerServices;
global using System.Security.Cryptography.X509Certificates;
global using System.ServiceProcess;
global using System.Xml.Linq;

global using Atc.Helpers;
global using Atc.Installer.Integration.Helpers;

global using Microsoft.Web.Administration;
global using Microsoft.Win32;
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ Task<bool> CreateApplicationPool(
ushort timeoutInSeconds = 60,
CancellationToken cancellationToken = default);

Task<bool> DeleteApplicationPool(
string applicationPoolName,
ushort timeoutInSeconds = 60,
CancellationToken cancellationToken = default);

Task<bool> CreateWebsite(
string websiteName,
string applicationPoolName,
Expand All @@ -66,6 +71,11 @@ Task<bool> CreateWebsite(
ushort timeoutInSeconds = 60,
CancellationToken cancellationToken = default);

Task<bool> DeleteWebsite(
string websiteName,
ushort timeoutInSeconds = 60,
CancellationToken cancellationToken = default);

Task<bool> StopApplicationPool(
string applicationPoolName,
ushort timeoutInSeconds = 60,
Expand Down Expand Up @@ -97,4 +107,16 @@ Task<bool> StartWebsiteAndApplicationPool(
string applicationPoolName,
ushort timeoutInSeconds = 60,
CancellationToken cancellationToken = default);

IList<X509Certificate2> GetX509Certificates();

X509Certificate2? GetWebsiteX509Certificate(
string websiteName);

bool AssignX509CertificateToWebsite(
string websiteName,
X509Certificate2 certificate);

bool UnAssignX509CertificateOnWebsite(
string websiteName);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// ReSharper disable StringLiteralTypo
// ReSharper disable LoopCanBeConvertedToQuery
namespace Atc.Installer.Integration.InternetInformationServer;

/// <summary>
Expand Down Expand Up @@ -168,7 +169,7 @@ public bool IsComponentInstalledUrlRewriteModule2()
}

public string? ResolvedVirtualRootFolder(
string folder)
string? folder)
=> folder is not null &&
folder.StartsWith(@".\", StringComparison.Ordinal) &&
IsInstalled &&
Expand All @@ -184,7 +185,7 @@ public bool IsComponentInstalledUrlRewriteModule2()
arguments: "unlock config -section:\"system.webServer/modules\"")
.ConfigureAwait(false);

if (isSuccessful && output is not null)
if (isSuccessful && !string.IsNullOrEmpty(output))
{
return (IsSucceeded: false, ErrorMessage: null);
}
Expand Down Expand Up @@ -222,7 +223,7 @@ await FileHelper
public ComponentRunningState GetApplicationPoolState(
string applicationPoolName)
{
ArgumentNullException.ThrowIfNull(applicationPoolName);
ArgumentException.ThrowIfNullOrEmpty(applicationPoolName);

try
{
Expand Down Expand Up @@ -252,7 +253,7 @@ public ComponentRunningState GetApplicationPoolState(
public ComponentRunningState GetWebsiteState(
string websiteName)
{
ArgumentNullException.ThrowIfNull(websiteName);
ArgumentException.ThrowIfNullOrEmpty(websiteName);

try
{
Expand Down Expand Up @@ -285,7 +286,7 @@ public Task<bool> CreateApplicationPool(
ushort timeoutInSeconds = 60,
CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(applicationPoolName);
ArgumentException.ThrowIfNullOrEmpty(applicationPoolName);

var applicationPoolState = GetApplicationPoolState(applicationPoolName);
if (applicationPoolState != ComponentRunningState.NotAvailable)
Expand All @@ -311,6 +312,38 @@ public Task<bool> CreateApplicationPool(
}
}

public Task<bool> DeleteApplicationPool(
string applicationPoolName,
ushort timeoutInSeconds = 60,
CancellationToken cancellationToken = default)
{
ArgumentException.ThrowIfNullOrEmpty(applicationPoolName);

var applicationPoolState = GetApplicationPoolState(applicationPoolName);
if (applicationPoolState == ComponentRunningState.NotAvailable)
{
return Task.FromResult(false);
}

try
{
using var serverManager = new ServerManager();
var applicationPool = serverManager.ApplicationPools[applicationPoolName];
if (applicationPool is null)
{
return Task.FromResult(false);
}

applicationPool.Delete();
serverManager.CommitChanges();
return Task.FromResult(true);
}
catch
{
return Task.FromResult(false);
}
}

public Task<bool> CreateWebsite(
string websiteName,
string applicationPoolName,
Expand Down Expand Up @@ -345,8 +378,8 @@ public async Task<bool> CreateWebsite(
ushort timeoutInSeconds = 60,
CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(websiteName);
ArgumentNullException.ThrowIfNull(applicationPoolName);
ArgumentException.ThrowIfNullOrEmpty(websiteName);
ArgumentException.ThrowIfNullOrEmpty(applicationPoolName);
ArgumentNullException.ThrowIfNull(physicalPath);

var websiteState = GetWebsiteState(websiteName);
Expand Down Expand Up @@ -416,6 +449,38 @@ public async Task<bool> CreateWebsite(
}
}

public Task<bool> DeleteWebsite(
string websiteName,
ushort timeoutInSeconds = 60,
CancellationToken cancellationToken = default)
{
ArgumentException.ThrowIfNullOrEmpty(websiteName);

var websiteState = GetWebsiteState(websiteName);
if (websiteState == ComponentRunningState.NotAvailable)
{
return Task.FromResult(false);
}

try
{
using var serverManager = new ServerManager();
var site = serverManager.Sites[websiteName];
if (site is null)
{
return Task.FromResult(false);
}

site.Delete();
serverManager.CommitChanges();
return Task.FromResult(true);
}
catch
{
return Task.FromResult(false);
}
}

public async Task<bool> StopApplicationPool(
string applicationPoolName,
ushort timeoutInSeconds = 60,
Expand Down Expand Up @@ -604,6 +669,118 @@ public async Task<bool> StartWebsiteAndApplicationPool(
=> await StartApplicationPool(applicationPoolName, timeoutInSeconds, cancellationToken).ConfigureAwait(false) &&
await StartWebsite(websiteName, timeoutInSeconds, cancellationToken).ConfigureAwait(false);

public IList<X509Certificate2> GetX509Certificates()
=> CryptographyHelper.GetX509Certificates();

public X509Certificate2? GetWebsiteX509Certificate(
string websiteName)
{
ArgumentException.ThrowIfNullOrEmpty(websiteName);

try
{
using var serverManager = new ServerManager();
var site = serverManager.Sites[websiteName];
if (site is null)
{
return null;
}

foreach (var siteBinding in site.Bindings)
{
if (siteBinding.SslFlags == SslFlags.None ||
!"https".Equals(siteBinding.Protocol, StringComparison.OrdinalIgnoreCase))
{
continue;
}

return CryptographyHelper.FindX509Certificate(
siteBinding.CertificateHash.ToHex(),
Enum<StoreName>.Parse(siteBinding.CertificateStoreName));
}

return null;
}
catch
{
return null;
}
}

public bool AssignX509CertificateToWebsite(
string websiteName,
X509Certificate2 certificate)
{
ArgumentException.ThrowIfNullOrEmpty(websiteName);
ArgumentNullException.ThrowIfNull(certificate);

try
{
using var serverManager = new ServerManager();
var site = serverManager.Sites[websiteName];
if (site is null)
{
return false;
}

foreach (var siteBinding in site.Bindings)
{
if (!"https".Equals(siteBinding.Protocol, StringComparison.OrdinalIgnoreCase))
{
continue;
}

siteBinding.CertificateHash = certificate.GetCertHash();
siteBinding.CertificateStoreName = nameof(StoreName.My);
siteBinding.SslFlags = SslFlags.Sni;
serverManager.CommitChanges();
return true;
}

return false;
}
catch
{
return false;
}
}

public bool UnAssignX509CertificateOnWebsite(
string websiteName)
{
ArgumentException.ThrowIfNullOrEmpty(websiteName);

try
{
using var serverManager = new ServerManager();
var site = serverManager.Sites[websiteName];
if (site is null)
{
return false;
}

foreach (var siteBinding in site.Bindings)
{
if (!"https".Equals(siteBinding.Protocol, StringComparison.OrdinalIgnoreCase))
{
continue;
}

siteBinding.CertificateHash = null;
siteBinding.CertificateStoreName = null;
siteBinding.SslFlags = SslFlags.None;
serverManager.CommitChanges();
return true;
}

return false;
}
catch
{
return false;
}
}

private string? GetResourceDefaultNodeJsUrlWebConfig()
=> GetResourceTextFile("default-nodejs-urlrewrite-webconfig");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Npgsql" Version="8.0.0-preview.4" />
<PackageReference Include="Npgsql" Version="8.0.0-rc.2" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Atc" Version="2.0.360" />
<PackageReference Include="System.ServiceProcess.ServiceController" Version="8.0.0-rc.1.23419.4" />
<PackageReference Include="Atc" Version="2.0.386" />
<PackageReference Include="System.ServiceProcess.ServiceController" Version="8.0.0-rc.2.23479.6" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Atc" Version="2.0.360" />
<PackageReference Include="Atc" Version="2.0.386" />
</ItemGroup>

</Project>
Loading

0 comments on commit e7bcdf6

Please sign in to comment.