Skip to content
This repository was archived by the owner on May 2, 2023. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e1d0e67

Browse files
committedJun 14, 2022
Ensure HealthCheck respects configured proxy settings
Fixes OctopusDeploy/Issues#7544 Updates the HealthCheck to use the new version of the Azure SDKs. It's in beta, so not touching the actual deployment code at this stage.
1 parent 2fa692c commit e1d0e67

File tree

4 files changed

+55
-10
lines changed

4 files changed

+55
-10
lines changed
 

‎source/Calamari/Azure/AzureClient.cs

+22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using System.Net;
22
using System.Net.Http;
3+
using Azure.Core.Pipeline;
4+
using Azure.Identity;
5+
using Azure.ResourceManager;
36
using Microsoft.Azure.Management.Fluent;
47
using Microsoft.Azure.Management.ResourceManager.Fluent;
58

@@ -23,5 +26,24 @@ public static IAzure CreateAzureClient(this ServicePrincipalAccount servicePrinc
2326
.Authenticate(credentials)
2427
.WithSubscription(servicePrincipal.SubscriptionNumber);
2528
}
29+
30+
/// <summary>
31+
/// Creates an ArmClient for the new Azure SDK, which replaces the older fluent libraries.
32+
/// We should migrate to this SDK once it stabilises.
33+
/// </summary>
34+
/// <param name="servicePrincipal">Service Principal Account to use when connecting to Azure</param>
35+
/// <returns></returns>
36+
public static ArmClient CreateArmClient(this ServicePrincipalAccount servicePrincipal)
37+
{
38+
var environment = new AzureKnownEnvironment(servicePrincipal.AzureEnvironment).AsAzureArmEnvironment();
39+
40+
var httpClientTransport = new HttpClientTransport(new HttpClientHandler { Proxy = WebRequest.DefaultWebProxy });
41+
42+
var tokenCredentialOptions = new TokenCredentialOptions { Transport = httpClientTransport };
43+
var credential = new ClientSecretCredential(servicePrincipal.TenantId, servicePrincipal.ClientId, servicePrincipal.Password, tokenCredentialOptions);
44+
45+
var armClientOptions = new ArmClientOptions() { Transport = httpClientTransport, Environment = environment };
46+
return new ArmClient(credential, defaultSubscriptionId: servicePrincipal.SubscriptionNumber, armClientOptions);
47+
}
2648
}
2749
}

‎source/Calamari/Azure/AzureKnownEnvironment.cs

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using Azure.ResourceManager;
23
using Microsoft.Azure.Management.ResourceManager.Fluent;
34

45
namespace Calamari.Azure
@@ -15,11 +16,11 @@ public AzureKnownEnvironment(string environment)
1516
if (string.IsNullOrEmpty(environment) || environment == "AzureCloud") // This environment name is defined in Sashimi.Azure.Accounts.AzureEnvironmentsListAction
1617
Value = Global.Value; // We interpret it as the normal Azure environment for historical reasons)
1718

18-
azureEnvironment = AzureEnvironment.FromName(Value) ??
19+
azureSdkEnvironment = AzureEnvironment.FromName(Value) ??
1920
throw new InvalidOperationException($"Unknown environment name {Value}");
2021
}
2122

22-
private readonly AzureEnvironment azureEnvironment;
23+
private readonly AzureEnvironment azureSdkEnvironment;
2324
public string Value { get; }
2425

2526
public static readonly AzureKnownEnvironment Global = new AzureKnownEnvironment("AzureGlobalCloud");
@@ -29,7 +30,18 @@ public AzureKnownEnvironment(string environment)
2930

3031
public AzureEnvironment AsAzureSDKEnvironment()
3132
{
32-
return azureEnvironment;
33+
return azureSdkEnvironment;
3334
}
35+
36+
public ArmEnvironment AsAzureArmEnvironment() => ToArmEnvironment(Value);
37+
38+
private static ArmEnvironment ToArmEnvironment(string name) => name switch
39+
{
40+
"AzureGlobalCloud" => ArmEnvironment.AzurePublicCloud,
41+
"AzureChinaCloud" => ArmEnvironment.AzureChina,
42+
"AzureGermanCloud" => ArmEnvironment.AzureGermany,
43+
"AzureUSGovernment" => ArmEnvironment.AzureGovernment,
44+
_ => throw new InvalidOperationException($"ARM Environment {name} is not a known Azure Environment name.")
45+
};
3446
}
3547
}

‎source/Calamari/Calamari.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
<TargetFramework>net5.0</TargetFramework>
1717
</PropertyGroup>
1818
<ItemGroup>
19+
<PackageReference Include="Azure.Identity" Version="1.2.3" />
20+
<PackageReference Include="Azure.ResourceManager.AppService" Version="1.0.0-beta.2" />
1921
<PackageReference Include="Calamari.Common" Version="21.1.0" />
2022
<PackageReference Include="Microsoft.Azure.Management.AppService.Fluent" Version="1.37.1" />
2123
<PackageReference Include="Microsoft.Azure.Management.Fluent" Version="1.37.1" />

‎source/Calamari/HealthCheckCommand.cs

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Threading.Tasks;
4+
using Azure;
5+
using Azure.ResourceManager.AppService;
6+
using Azure.ResourceManager.Resources;
47
using Calamari.Azure;
58
using Calamari.Common.Commands;
69
using Calamari.Common.Plumbing.Pipeline;
7-
using Microsoft.Azure.Management.ResourceManager.Fluent;
810

911
namespace Calamari.AzureAppService
1012
{
@@ -34,14 +36,21 @@ public Task Execute(RunningDeployment context)
3436
return ConfirmWebAppExists(account, resourceGroupName, webAppName);
3537
}
3638

37-
async Task ConfirmWebAppExists(ServicePrincipalAccount servicePrincipal, string resourceGroupName, string siteAndSlotName)
39+
private async Task ConfirmWebAppExists(ServicePrincipalAccount servicePrincipal, string resourceGroupName, string siteAndSlotName)
3840
{
39-
var azureClient = servicePrincipal.CreateAzureClient();
40-
var webApp = await azureClient.WebApps.GetByResourceGroupAsync(resourceGroupName, siteAndSlotName);
41-
if (webApp == null)
41+
var client = servicePrincipal.CreateArmClient();
42+
var subscription = await client.GetDefaultSubscriptionAsync();
43+
var resourceGroups = subscription.GetResourceGroups();
44+
45+
try
46+
{
47+
ResourceGroupResource resourceGroup = await resourceGroups.GetAsync(resourceGroupName);
48+
_ = await resourceGroup.GetWebSiteAsync(siteAndSlotName);
49+
}
50+
catch (RequestFailedException rfe) when (rfe.Status == 404)
4251
{
43-
throw new Exception($"Could not find site {siteAndSlotName} in resource group {resourceGroupName}, using Service Principal with subscription {servicePrincipal.SubscriptionNumber}");
52+
throw new Exception($"Could not find site {siteAndSlotName} in resource group {resourceGroupName}, using Service Principal with subscription {servicePrincipal.SubscriptionNumber}", rfe);
4453
}
4554
}
4655
}
47-
}
56+
}

0 commit comments

Comments
 (0)
This repository has been archived.