Skip to content

Commit 153f853

Browse files
committed
Added credentials utility to use github-set environment variables for service principal for pulumi deployment
1 parent 910ab0b commit 153f853

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

infra/pulumi-infra-deploy/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
using ProgrammerAl.Site.IaC.Config.GlobalConfigs;
1010
using ProgrammerAl.Site.IaC.StackBuilders.StorageApi;
1111
using ProgrammerAl.Site.IaC.StackBuilders.RouteFilterWorker;
12+
using ProgrammerAl.Site.IaC.Utilities;
1213

1314
return await Pulumi.Deployment.RunAsync(async () =>
1415
{
16+
CredentialsUtilities.SetCredentialEnvironmentVariables();
1517
var clientConfig = await Pulumi.AzureNative.Authorization.GetClientConfig.InvokeAsync();
1618

1719
var config = new Config();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Pulumi;
2+
using Pulumi.AzureNative.KeyVault;
3+
using Pulumi.AzureNative.Storage;
4+
5+
using System;
6+
7+
namespace ProgrammerAl.Site.IaC.Utilities;
8+
9+
public static class CredentialsUtilities
10+
{
11+
/// <summary>
12+
/// We use the `DefaultCredential` class for authenticating against Azure
13+
/// The `EnvironmentCredential` uses the AZURE_* environment variables to know what Service Principal to use
14+
/// But when using Pulumi in GitHub we have to set the same variables under the `ARM_*` names
15+
/// So create the AZURE_* variables from the ARM_* variables if needed
16+
/// </summary>
17+
public static void SetCredentialEnvironmentVariables()
18+
{
19+
var clientId = Environment.GetEnvironmentVariable("ARM_CLIENT_ID");
20+
var clientSecret = Environment.GetEnvironmentVariable("ARM_CLIENT_SECRET");
21+
var tenantId = Environment.GetEnvironmentVariable("ARM_TENANT_ID");
22+
23+
if (!string.IsNullOrWhiteSpace(clientId)
24+
&& !string.IsNullOrWhiteSpace(clientSecret)
25+
&& !string.IsNullOrWhiteSpace(tenantId))
26+
{
27+
Log.Info($"ARM_* environment variables set. Creating AZURE_* environment variables for use with the `EnvironmentCredential` class");
28+
29+
Environment.SetEnvironmentVariable("AZURE_CLIENT_ID", clientId);
30+
Environment.SetEnvironmentVariable("AZURE_CLIENT_SECRET", clientSecret);
31+
Environment.SetEnvironmentVariable("AZURE_TENANT_ID", tenantId);
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)