-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide certificate file login method #14738
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,7 @@ public class ConnectAzureRmAccountCommand : AzureContextModificationCmdlet, IMod | |
public const string UserWithCredentialParameterSet = "UserWithCredential"; | ||
public const string ServicePrincipalParameterSet = "ServicePrincipalWithSubscriptionId"; | ||
public const string ServicePrincipalCertificateParameterSet= "ServicePrincipalCertificateWithSubscriptionId"; | ||
public const string ServicePrincipalCertificateFileParameterSet = "ServicePrincipalCertificateFileWithSubscriptionId"; | ||
public const string AccessTokenParameterSet = "AccessTokenWithSubscriptionId"; | ||
public const string ManagedServiceParameterSet = "ManagedServiceLogin"; | ||
public const string MSIEndpointVariable = "MSI_ENDPOINT"; | ||
|
@@ -79,12 +80,16 @@ public class ConnectAzureRmAccountCommand : AzureContextModificationCmdlet, IMod | |
|
||
[Parameter(ParameterSetName = ServicePrincipalCertificateParameterSet, | ||
Mandatory = true, HelpMessage = "SPN")] | ||
[Parameter(ParameterSetName = ServicePrincipalCertificateFileParameterSet, | ||
Mandatory = true, HelpMessage = "SPN")] | ||
public string ApplicationId { get; set; } | ||
|
||
[Parameter(ParameterSetName = ServicePrincipalParameterSet, | ||
Mandatory = true)] | ||
[Parameter(ParameterSetName = ServicePrincipalCertificateParameterSet, | ||
Mandatory = false)] | ||
[Parameter(ParameterSetName = ServicePrincipalCertificateFileParameterSet, | ||
Mandatory = false)] | ||
public SwitchParameter ServicePrincipal { get; set; } | ||
|
||
[Parameter(ParameterSetName = UserParameterSet, | ||
|
@@ -97,6 +102,8 @@ public class ConnectAzureRmAccountCommand : AzureContextModificationCmdlet, IMod | |
Mandatory = false, HelpMessage = "Tenant name or ID")] | ||
[Parameter(ParameterSetName = ServicePrincipalCertificateParameterSet, | ||
Mandatory = true, HelpMessage = "Tenant name or ID")] | ||
[Parameter(ParameterSetName = ServicePrincipalCertificateFileParameterSet, | ||
Mandatory = true, HelpMessage = "Tenant name or ID")] | ||
[Parameter(ParameterSetName = ManagedServiceParameterSet, | ||
Mandatory = false, HelpMessage = "Optional tenant name or ID")] | ||
[Alias("Domain", "TenantId")] | ||
|
@@ -138,6 +145,8 @@ public class ConnectAzureRmAccountCommand : AzureContextModificationCmdlet, IMod | |
Mandatory = false, HelpMessage = "Subscription Name or ID", ValueFromPipeline = true)] | ||
[Parameter(ParameterSetName = ServicePrincipalCertificateParameterSet, | ||
Mandatory = false, HelpMessage = "Subscription Name or ID", ValueFromPipeline = true)] | ||
[Parameter(ParameterSetName = ServicePrincipalCertificateFileParameterSet, | ||
Mandatory = false, HelpMessage = "Subscription Name or ID", ValueFromPipeline = true)] | ||
[Parameter(ParameterSetName = AccessTokenParameterSet, | ||
Mandatory = false, HelpMessage = "Subscription Name or ID", ValueFromPipeline = true)] | ||
[Parameter(ParameterSetName = ManagedServiceParameterSet, | ||
|
@@ -187,6 +196,7 @@ public class ConnectAzureRmAccountCommand : AzureContextModificationCmdlet, IMod | |
[Parameter(ParameterSetName = UserWithCredentialParameterSet, Mandatory = false, HelpMessage = "Max subscription number to populate contexts after login. Default is " + DefaultMaxContextPopulationString + ". To populate all subscriptions to contexts, set to -1.")] | ||
[Parameter(ParameterSetName = ServicePrincipalParameterSet, Mandatory = false, HelpMessage = "Max subscription number to populate contexts after login. Default is " + DefaultMaxContextPopulationString + ". To populate all subscriptions to contexts, set to -1.")] | ||
[Parameter(ParameterSetName = ServicePrincipalCertificateParameterSet, Mandatory = false, HelpMessage = "Max subscription number to populate contexts after login. Default is " + DefaultMaxContextPopulationString + ". To populate all subscriptions to contexts, set to -1.")] | ||
[Parameter(ParameterSetName = ServicePrincipalCertificateFileParameterSet, Mandatory = false, HelpMessage = "Max subscription number to populate contexts after login. Default is " + DefaultMaxContextPopulationString + ". To populate all subscriptions to contexts, set to -1.")] | ||
[Parameter(ParameterSetName = AccessTokenParameterSet, Mandatory = false, HelpMessage = "Max subscription number to populate contexts after login. Default is " + DefaultMaxContextPopulationString + ". To populate all subscriptions to contexts, set to -1.")] | ||
[Parameter(ParameterSetName = ManagedServiceParameterSet, Mandatory = false, HelpMessage = "Max subscription number to populate contexts after login. Default is " + DefaultMaxContextPopulationString + ". To populate all subscriptions to contexts, set to -1.")] | ||
[PSDefaultValue(Help = DefaultMaxContextPopulationString, Value = DefaultMaxContextPopulation)] | ||
|
@@ -201,9 +211,17 @@ public class ConnectAzureRmAccountCommand : AzureContextModificationCmdlet, IMod | |
[Parameter(Mandatory = false, HelpMessage = "Overwrite the existing context with the same name, if any.")] | ||
public SwitchParameter Force { get; set; } | ||
|
||
[Parameter(ParameterSetName = ServicePrincipalCertificateParameterSet, Mandatory = false, HelpMessage = "Specifies if the x5c claim (public key of the certificate) should be sent to the STS to achieve easy certificate rollover in Azure AD.")] | ||
[Parameter(ParameterSetName = ServicePrincipalCertificateParameterSet, HelpMessage = "Specifies if the x5c claim (public key of the certificate) should be sent to the STS to achieve easy certificate rollover in Azure AD.")] | ||
[Parameter(ParameterSetName = ServicePrincipalCertificateFileParameterSet, HelpMessage = "Specifies if the x5c claim (public key of the certificate) should be sent to the STS to achieve easy certificate rollover in Azure AD.")] | ||
public SwitchParameter SendCertificateChain { get; set; } | ||
|
||
|
||
[Parameter(ParameterSetName = ServicePrincipalCertificateFileParameterSet, Mandatory = true, HelpMessage = "The path of certficate file in pkcs#12 format.")] | ||
public String CertificatePath { get; set; } | ||
|
||
[Parameter(ParameterSetName = ServicePrincipalCertificateFileParameterSet, HelpMessage = "The password required to access the pkcs#12 certificate file.")] | ||
public SecureString CertificatePassword { get; set; } | ||
|
||
protected override IAzureContext DefaultContext | ||
{ | ||
get | ||
|
@@ -305,6 +323,7 @@ public override void ExecuteCmdlet() | |
azureAccount.SetProperty(AzureAccount.Property.KeyVaultAccessToken, KeyVaultAccessToken); | ||
break; | ||
case ServicePrincipalCertificateParameterSet: | ||
case ServicePrincipalCertificateFileParameterSet: | ||
case ServicePrincipalParameterSet: | ||
azureAccount.Type = AzureAccount.AccountType.ServicePrincipal; | ||
break; | ||
|
@@ -345,7 +364,23 @@ public override void ExecuteCmdlet() | |
azureAccount.SetThumbprint(CertificateThumbprint); | ||
} | ||
|
||
if (ParameterSetName == ServicePrincipalCertificateParameterSet && SendCertificateChain) | ||
if( !string.IsNullOrWhiteSpace(CertificatePath)) | ||
{ | ||
var resolvedPath = this.SessionState.Path.GetResolvedPSPathFromPSPath(CertificatePath).MaxOrDefault(p => p.Path, string.Empty); | ||
if (string.IsNullOrEmpty(resolvedPath)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we verify whether file exist here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If file doesn't exist, GetResolvedPSPathFromPSPath will throw an exception with the error message. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
var parametersLog = $"- Invalid certificate path :'{CertificatePath}'."; | ||
throw new InvalidOperationException(parametersLog); | ||
} | ||
azureAccount.SetProperty(AzureAccount.Property.CertificatePath, resolvedPath); | ||
if (CertificatePassword != null) | ||
{ | ||
azureAccount.SetProperty(AzureAccount.Property.CertificatePassword, CertificatePassword.ConvertToString()); | ||
} | ||
} | ||
|
||
if ((ParameterSetName == ServicePrincipalCertificateParameterSet || ParameterSetName == ServicePrincipalCertificateFileParameterSet) | ||
&& SendCertificateChain) | ||
{ | ||
azureAccount.SetProperty(AzureAccount.Property.SendCertificateChain, SendCertificateChain.ToString()); | ||
bool supressWarningOrError = false; | ||
|
@@ -368,7 +403,7 @@ public override void ExecuteCmdlet() | |
azureAccount.SetProperty(AzureAccount.Property.Tenants, Tenant); | ||
} | ||
|
||
if (azureAccount.Type == AzureAccount.AccountType.ServicePrincipal && string.IsNullOrEmpty(CertificateThumbprint)) | ||
if (azureAccount.Type == AzureAccount.AccountType.ServicePrincipal && string.IsNullOrEmpty(CertificateThumbprint) && password != null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggest to use |
||
{ | ||
azureAccount.SetProperty(AzureAccount.Property.ServicePrincipalSecret, password.ConvertToString()); | ||
if (GetContextModificationScope() == ContextModificationScope.CurrentUser) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MaxOrDefault or FirstOrDefault?