Skip to content

Commit 16dc9ba

Browse files
authored
Added --azure-credential-type option that can be used to specify the credential type for Azure authentication. (#891)
Fix #867.
1 parent 86b7776 commit 16dc9ba

18 files changed

+222
-3
lines changed

src/Sign.Cli/AzureCredentialOptions.cs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ namespace Sign.Cli
1313
{
1414
internal sealed class AzureCredentialOptions
1515
{
16+
internal Option<string?> CredentialTypeOption = new Option<string?>(["--azure-credential-type", "-act"], Resources.CredentialTypeOptionDescription).FromAmong(
17+
AzureCredentialType.AzureCli,
18+
AzureCredentialType.AzurePowerShell,
19+
AzureCredentialType.ManagedIdentity,
20+
AzureCredentialType.WorkloadIdentity);
1621
internal Option<string?> ManagedIdentityClientIdOption = new(["--managed-identity-client-id", "-mici"], Resources.ManagedIdentityClientIdOptionDescription);
1722
internal Option<string?> ManagedIdentityResourceIdOption = new(["--managed-identity-resource-id", "-miri"], Resources.ManagedIdentityResourceIdOptionDescription);
1823
internal Option<bool?> ObsoleteManagedIdentityOption { get; } = new(["--azure-key-vault-managed-identity", "-kvm"], Resources.ManagedIdentityOptionDescription) { IsHidden = true };
@@ -22,6 +27,7 @@ internal sealed class AzureCredentialOptions
2227

2328
internal void AddOptionsToCommand(Command command)
2429
{
30+
command.AddOption(CredentialTypeOption);
2531
command.AddOption(ManagedIdentityClientIdOption);
2632
command.AddOption(ManagedIdentityResourceIdOption);
2733
command.AddOption(ObsoleteManagedIdentityOption);
@@ -70,10 +76,38 @@ internal DefaultAzureCredentialOptions CreateDefaultAzureCredentialOptions(Parse
7076
return new ClientSecretCredential(tenantId, clientId, secret);
7177
}
7278

73-
DefaultAzureCredentialOptions options = CreateDefaultAzureCredentialOptions(context.ParseResult);
79+
switch (context.ParseResult.GetValueForOption(CredentialTypeOption))
80+
{
81+
case AzureCredentialType.AzureCli:
82+
return new AzureCliCredential();
83+
84+
case AzureCredentialType.AzurePowerShell:
85+
return new AzurePowerShellCredential();
86+
87+
case AzureCredentialType.ManagedIdentity:
88+
string? managedIdentityClientId = context.ParseResult.GetValueForOption(ManagedIdentityClientIdOption);
89+
if (managedIdentityClientId is not null)
90+
{
91+
return new ManagedIdentityCredential(managedIdentityClientId);
92+
}
93+
94+
string? managedIdentityResourceId = context.ParseResult.GetValueForOption(ManagedIdentityResourceIdOption);
95+
if (managedIdentityResourceId is not null)
96+
{
97+
return new ManagedIdentityCredential(new ResourceIdentifier(managedIdentityResourceId));
98+
}
7499

75-
// CodeQL [SM05137] Sign CLI is not a production service.
76-
return new DefaultAzureCredential(options);
100+
return new ManagedIdentityCredential();
101+
102+
case AzureCredentialType.WorkloadIdentity:
103+
return new WorkloadIdentityCredential();
104+
105+
default:
106+
DefaultAzureCredentialOptions options = CreateDefaultAzureCredentialOptions(context.ParseResult);
107+
108+
// CodeQL [SM05137] Sign CLI is not a production service.
109+
return new DefaultAzureCredential(options);
110+
}
77111
}
78112
}
79113
}

src/Sign.Cli/AzureCredentialType.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE.txt file in the project root for more information.
4+
5+
namespace Sign.Cli
6+
{
7+
internal static class AzureCredentialType
8+
{
9+
public const string AzureCli = "azure-cli";
10+
public const string AzurePowerShell = "azure-powershell";
11+
public const string ManagedIdentity = "managed-identity";
12+
public const string WorkloadIdentity = "workload-identity";
13+
}
14+
}

src/Sign.Cli/Resources.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Sign.Cli/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,7 @@
234234
<value>Warning: The Microsoft Visual C++ 14 runtime is required but was not detected on your system. Download and install from https://aka.ms/vs/17/release/vc_redist.x64.exe</value>
235235
<comment>{Locked="https://aka.ms/vs/17/release/vc_redist.x64.exe"} is a URL.</comment>
236236
</data>
237+
<data name="CredentialTypeOptionDescription" xml:space="preserve">
238+
<value>Azure credential type that will be used. This defaults to DefaultAzureCredential.</value>
239+
</data>
237240
</root>

src/Sign.Cli/xlf/Resources.cs.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
<target state="translated">Podepisovat binární soubory a kontejnery.</target>
3838
<note />
3939
</trans-unit>
40+
<trans-unit id="CredentialTypeOptionDescription">
41+
<source>Azure credential type that will be used. This defaults to DefaultAzureCredential.</source>
42+
<target state="new">Azure credential type that will be used. This defaults to DefaultAzureCredential.</target>
43+
<note />
44+
</trans-unit>
4045
<trans-unit id="DescriptionOptionDescription">
4146
<source>Description of the signing certificate.</source>
4247
<target state="translated">Popis podpisového certifikátu.</target>

src/Sign.Cli/xlf/Resources.de.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
<target state="translated">Signieren Sie Binärdateien und Container.</target>
3838
<note />
3939
</trans-unit>
40+
<trans-unit id="CredentialTypeOptionDescription">
41+
<source>Azure credential type that will be used. This defaults to DefaultAzureCredential.</source>
42+
<target state="new">Azure credential type that will be used. This defaults to DefaultAzureCredential.</target>
43+
<note />
44+
</trans-unit>
4045
<trans-unit id="DescriptionOptionDescription">
4146
<source>Description of the signing certificate.</source>
4247
<target state="translated">Beschreibung des Signaturzertifikats.</target>

src/Sign.Cli/xlf/Resources.es.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
<target state="translated">Firmar archivos binarios y contenedores.</target>
3838
<note />
3939
</trans-unit>
40+
<trans-unit id="CredentialTypeOptionDescription">
41+
<source>Azure credential type that will be used. This defaults to DefaultAzureCredential.</source>
42+
<target state="new">Azure credential type that will be used. This defaults to DefaultAzureCredential.</target>
43+
<note />
44+
</trans-unit>
4045
<trans-unit id="DescriptionOptionDescription">
4146
<source>Description of the signing certificate.</source>
4247
<target state="translated">Descripción del certificado de firma.</target>

src/Sign.Cli/xlf/Resources.fr.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
<target state="translated">Signer les fichiers binaires et les conteneurs.</target>
3838
<note />
3939
</trans-unit>
40+
<trans-unit id="CredentialTypeOptionDescription">
41+
<source>Azure credential type that will be used. This defaults to DefaultAzureCredential.</source>
42+
<target state="new">Azure credential type that will be used. This defaults to DefaultAzureCredential.</target>
43+
<note />
44+
</trans-unit>
4045
<trans-unit id="DescriptionOptionDescription">
4146
<source>Description of the signing certificate.</source>
4247
<target state="translated">Description du certificat de signature.</target>

src/Sign.Cli/xlf/Resources.it.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
<target state="translated">Consente di firmare file binari e contenitori.</target>
3838
<note />
3939
</trans-unit>
40+
<trans-unit id="CredentialTypeOptionDescription">
41+
<source>Azure credential type that will be used. This defaults to DefaultAzureCredential.</source>
42+
<target state="new">Azure credential type that will be used. This defaults to DefaultAzureCredential.</target>
43+
<note />
44+
</trans-unit>
4045
<trans-unit id="DescriptionOptionDescription">
4146
<source>Description of the signing certificate.</source>
4247
<target state="translated">Descrizione del certificato di firma.</target>

src/Sign.Cli/xlf/Resources.ja.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
<target state="translated">バイナリとコンテナーに署名します。</target>
3838
<note />
3939
</trans-unit>
40+
<trans-unit id="CredentialTypeOptionDescription">
41+
<source>Azure credential type that will be used. This defaults to DefaultAzureCredential.</source>
42+
<target state="new">Azure credential type that will be used. This defaults to DefaultAzureCredential.</target>
43+
<note />
44+
</trans-unit>
4045
<trans-unit id="DescriptionOptionDescription">
4146
<source>Description of the signing certificate.</source>
4247
<target state="translated">署名証明書の説明。</target>

0 commit comments

Comments
 (0)