Skip to content

Fix Profile authentication, normalize Environment cmdlets #714

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

Merged
merged 5 commits into from
Aug 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Common/Commands.Common/AzurePSCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ protected AzureContext GetCurrentContext()

protected static void InitializeTokenCaches()
{
DefaultMemoryTokenCache = new TokenCache();
DefaultMemoryTokenCache = TokenCache.DefaultShared;
if (!string.IsNullOrWhiteSpace(AzureSession.ProfileDirectory) &&
!string.IsNullOrWhiteSpace(AzureSession.TokenCacheFile))
{
Expand Down
11 changes: 10 additions & 1 deletion src/Common/Commands.Common/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions src/Common/Commands.Common/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1361,10 +1361,9 @@ use and privacy statement at <url> and (c) agree to sharing my contact inf
<data name="SwitchAzureModeDeprecated" xml:space="preserve">
<value>The Switch-AzureMode cmdlet is deprecated and will be removed in a future release.</value>
</data>
<data name="ComputeCloudExceptionOperationIdMessage" xml:space="preserve">
<value>OperationID : '{0}'</value>
</data>
<!-- DSC -->
<data name="ComputeCloudExceptionOperationIdMessage" xml:space="preserve">
<value>OperationID : '{0}'</value>
</data>
<data name="PublishVMDscExtensionGetDscResourceFailed" xml:space="preserve">
<value>Cannot get module for DscResource '{0}'. Possible solutions:
1) Specify -ModuleName for Import-DscResource in your configuration.
Expand Down Expand Up @@ -1466,4 +1465,7 @@ The file needs to be a PowerShell script (.ps1 or .psm1).</value>
<data name="AzureVMDscInvalidConfigurationDataFile" xml:space="preserve">
<value>The configuration data must be a .psd1 file</value>
</data>
<data name="CannotChangeBuiltinEnvironment" xml:space="preserve">
<value>Cannot change built-in environment {0}.</value>
</data>
</root>
1 change: 1 addition & 0 deletions src/Common/Commands.Profile/Commands.Profile.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
<Compile Include="Environment\SetAzureEnvironment.cs" />
<Compile Include="Environment\GetAzureEnvironment.cs" />
<Compile Include="Models\AzureProfileSettings.cs" />
<Compile Include="Models\PSAzureEnvironment.cs" />
<Compile Include="Models\PSAzureSubscription.cs" />
<Compile Include="Models\PSAzureSubscriptionExtended.cs" />
<Compile Include="Profile\SelectAzureProfile.cs" />
Expand Down
37 changes: 27 additions & 10 deletions src/Common/Commands.Profile/Environment/AddAzureEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.Management.Automation;
using System.Security.Permissions;
using Microsoft.Azure.Common.Authentication.Models;
using Microsoft.WindowsAzure.Commands.Profile.Models;
using Microsoft.WindowsAzure.Commands.Utilities.Profile;
using System.Collections.Generic;
using System;
Expand All @@ -26,7 +27,7 @@ namespace Microsoft.WindowsAzure.Commands.Profile
/// <summary>
/// Adds a new Microsoft Azure environment.
/// </summary>
[Cmdlet(VerbsCommon.Add, "AzureEnvironment"), OutputType(typeof(AzureEnvironment))]
[Cmdlet(VerbsCommon.Add, "AzureEnvironment"), OutputType(typeof(PSAzureEnvironment))]
public class AddAzureEnvironmentCommand : SubscriptionCmdletBase
{
[Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true)]
Expand All @@ -36,21 +37,26 @@ public class AddAzureEnvironmentCommand : SubscriptionCmdletBase
public string PublishSettingsFileUrl { get; set; }

[Parameter(Position = 2, Mandatory = false, ValueFromPipelineByPropertyName = true)]
[Alias("ServiceManagement", "ServiceManagementUrl")]
public string ServiceEndpoint { get; set; }

[Parameter(Position = 3, Mandatory = false, ValueFromPipelineByPropertyName = true)]
public string ManagementPortalUrl { get; set; }

[Parameter(Position = 4, Mandatory = false, HelpMessage = "The storage endpoint")]
[Alias("StorageEndpointSuffix")]
public string StorageEndpoint { get; set; }

[Parameter(Position = 5, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The URI for the Active Directory service for this environment")]
[Alias("AdEndpointUrl", "ActiveDirectory", "ActiveDirectoryAuthority")]
public string ActiveDirectoryEndpoint { get; set; }

[Parameter(Position = 6, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The cloud service endpoint")]
[Alias("ResourceManager", "ResourceManagerUrl")]
public string ResourceManagerEndpoint { get; set; }

[Parameter(Position = 7, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The public gallery endpoint")]
[Alias("Gallery", "GalleryUrl")]
public string GalleryEndpoint { get; set; }

[Parameter(Position = 8, Mandatory = false, ValueFromPipelineByPropertyName = true,
Expand All @@ -59,6 +65,7 @@ public class AddAzureEnvironmentCommand : SubscriptionCmdletBase

[Parameter(Position = 9, Mandatory = false, ValueFromPipelineByPropertyName = true,
HelpMessage = "The AD Graph Endpoint.")]
[Alias("Graph", "GraphUrl")]
public string GraphEndpoint { get; set; }

[Parameter(Position = 10, Mandatory = false, ValueFromPipelineByPropertyName = true,
Expand All @@ -70,8 +77,21 @@ public class AddAzureEnvironmentCommand : SubscriptionCmdletBase
public string AzureKeyVaultServiceEndpointResourceId { get; set; }

[Parameter(Position = 12, Mandatory = false, ValueFromPipelineByPropertyName = true,
HelpMessage = "Dns suffix of Traffic Manager service.")]
public string TrafficManagerDnsSuffix { get; set; }

[Parameter(Position = 13, Mandatory = false, ValueFromPipelineByPropertyName = true,
HelpMessage = "Dns suffix of Sql databases created in this environment.")]
public string SqlDatabaseDnsSuffix { get; set; }

[Parameter(Position = 14, Mandatory = false, ValueFromPipelineByPropertyName = true,
HelpMessage = "Enable ADFS authentication by disabling the authority validation")]
public SwitchParameter EnableADFSAuthentication { get; set; }
[Alias("OnPremise")]
public SwitchParameter EnableAdfsAuthentication { get; set; }

[Parameter(Position = 15, Mandatory = false, ValueFromPipelineByPropertyName = true,
HelpMessage = "The default tenant for this environment.")]
public string AdTenant { get; set; }

public AddAzureEnvironmentCommand() : base(true) { }

Expand All @@ -81,7 +101,7 @@ public override void ExecuteCmdlet()
var newEnvironment = new AzureEnvironment
{
Name = Name,
OnPremise = EnableADFSAuthentication
OnPremise = EnableAdfsAuthentication
};
newEnvironment.Endpoints[AzureEnvironment.Endpoint.PublishSettingsFileUrl] = PublishSettingsFileUrl;
newEnvironment.Endpoints[AzureEnvironment.Endpoint.ServiceManagement] = ServiceEndpoint;
Expand All @@ -94,14 +114,11 @@ public override void ExecuteCmdlet()
newEnvironment.Endpoints[AzureEnvironment.Endpoint.Graph] = GraphEndpoint;
newEnvironment.Endpoints[AzureEnvironment.Endpoint.AzureKeyVaultDnsSuffix] = AzureKeyVaultDnsSuffix;
newEnvironment.Endpoints[AzureEnvironment.Endpoint.AzureKeyVaultServiceEndpointResourceId] = AzureKeyVaultServiceEndpointResourceId;

newEnvironment.Endpoints[AzureEnvironment.Endpoint.TrafficManagerDnsSuffix] = TrafficManagerDnsSuffix;
newEnvironment.Endpoints[AzureEnvironment.Endpoint.SqlDatabaseDnsSuffix] = SqlDatabaseDnsSuffix;
newEnvironment.Endpoints[AzureEnvironment.Endpoint.AdTenant] = AdTenant;
ProfileClient.AddOrSetEnvironment(newEnvironment);
List<object> args = new List<object> { "Name", newEnvironment.Name };
foreach (AzureEnvironment.Endpoint property in Enum.GetValues(typeof(AzureEnvironment.Endpoint)))
{
args.AddRange(new object[] { property, newEnvironment.GetEndpoint(property) });
}
WriteObject(base.ConstructPSObject(null, args.ToArray()));
WriteObject((PSAzureEnvironment)newEnvironment);
}
}
}
18 changes: 5 additions & 13 deletions src/Common/Commands.Profile/Environment/GetAzureEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
// ----------------------------------------------------------------------------------

using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Security.Permissions;
using Microsoft.Azure.Common.Authentication.Models;
using Microsoft.WindowsAzure.Commands.Profile.Models;
using Microsoft.WindowsAzure.Commands.Utilities.Profile;
using System;

Expand All @@ -24,7 +26,7 @@ namespace Microsoft.WindowsAzure.Commands.Profile
/// <summary>
/// Gets the available Microsoft Azure environments.
/// </summary>
[Cmdlet(VerbsCommon.Get, "AzureEnvironment"), OutputType(typeof(List<AzureEnvironment>))]
[Cmdlet(VerbsCommon.Get, "AzureEnvironment"), OutputType(typeof(PSAzureEnvironment))]
public class GetAzureEnvironmentCommand : SubscriptionCmdletBase
{
[Parameter(Position = 0, Mandatory = false, ValueFromPipelineByPropertyName = true,
Expand All @@ -36,18 +38,8 @@ public GetAzureEnvironmentCommand() : base(false) { }
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public override void ExecuteCmdlet()
{
List<AzureEnvironment> environments = ProfileClient.ListEnvironments(Name);
List<PSObject> output = new List<PSObject>();
foreach (AzureEnvironment e in environments)
{
List<object> args = new List<object> { "Name", e.Name };
foreach (AzureEnvironment.Endpoint property in Enum.GetValues(typeof(AzureEnvironment.Endpoint)))
{
args.AddRange(new object[] { property, e.GetEndpoint(property) });
}
output.Add(base.ConstructPSObject(null, args.ToArray()));
}
WriteObject(output, true);
List<PSAzureEnvironment> environments = ProfileClient.ListEnvironments(Name).Select((e) => (PSAzureEnvironment)e).ToList();
WriteObject(environments, true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System.Security.Permissions;
using Microsoft.Azure.Common.Authentication.Models;
using Microsoft.WindowsAzure.Commands.Common.Properties;
using Microsoft.WindowsAzure.Commands.Profile.Models;
using Microsoft.WindowsAzure.Commands.Utilities.Profile;

namespace Microsoft.WindowsAzure.Commands.Profile
Expand All @@ -25,7 +26,7 @@ namespace Microsoft.WindowsAzure.Commands.Profile
/// <summary>
/// Removes a Microsoft Azure environment.
/// </summary>
[Cmdlet(VerbsCommon.Remove, "AzureEnvironment"), OutputType(typeof(AzureEnvironment))]
[Cmdlet(VerbsCommon.Remove, "AzureEnvironment"), OutputType(typeof(PSAzureEnvironment))]
public class RemoveAzureEnvironmentCommand : SubscriptionCmdletBase
{
[Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true,
Expand All @@ -50,7 +51,7 @@ public override void ExecuteCmdlet()

public void RemoveEnvironmentProcess()
{
WriteObject(ProfileClient.RemoveEnvironment(Name));
WriteObject((PSAzureEnvironment)(ProfileClient.RemoveEnvironment(Name)));
}
}
}
43 changes: 39 additions & 4 deletions src/Common/Commands.Profile/Environment/SetAzureEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.Globalization;
using System.Management.Automation;
using System.Security.Permissions;
using Microsoft.Azure.Common.Authentication.Models;
using Microsoft.WindowsAzure.Commands.Common.Properties;
using Microsoft.WindowsAzure.Commands.Profile.Models;
using Microsoft.WindowsAzure.Commands.Utilities.Profile;

namespace Microsoft.WindowsAzure.Commands.Profile
Expand All @@ -23,7 +27,7 @@ namespace Microsoft.WindowsAzure.Commands.Profile
/// <summary>
/// Sets a Microsoft Azure environment.
/// </summary>
[Cmdlet(VerbsCommon.Set, "AzureEnvironment"), OutputType(typeof(AzureEnvironment))]
[Cmdlet(VerbsCommon.Set, "AzureEnvironment"), OutputType(typeof(PSAzureEnvironment))]
public class SetAzureEnvironmentCommand : SubscriptionCmdletBase
{
[Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true)]
Expand All @@ -33,22 +37,26 @@ public class SetAzureEnvironmentCommand : SubscriptionCmdletBase
public string PublishSettingsFileUrl { get; set; }

[Parameter(Position = 2, Mandatory = false, ValueFromPipelineByPropertyName = true)]
[Alias("ServiceManagement", "ServiceManagementUrl")]
public string ServiceEndpoint { get; set; }

[Parameter(Position = 3, Mandatory = false, ValueFromPipelineByPropertyName = true)]
public string ManagementPortalUrl { get; set; }

[Parameter(Position = 4, Mandatory = false, HelpMessage = "The storage endpoint")]
[Alias("StorageEndpointSuffix")]
public string StorageEndpoint { get; set; }

[Parameter(Position = 5, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Active directory endpoint")]
[Alias("AdEndpointUrl")]
[Alias("AdEndpointUrl", "ActiveDirectory", "ActiveDirectoryAuthority")]
public string ActiveDirectoryEndpoint { get; set; }

[Parameter(Position = 6, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The cloud service endpoint")]
[Alias("ResourceManager", "ResourceManagerUrl")]
public string ResourceManagerEndpoint { get; set; }

[Parameter(Position = 7, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The public gallery endpoint")]
[Alias("Gallery", "GalleryUrl")]
public string GalleryEndpoint { get; set; }

[Parameter(Position = 8, Mandatory = false, ValueFromPipelineByPropertyName = true,
Expand All @@ -57,6 +65,7 @@ public class SetAzureEnvironmentCommand : SubscriptionCmdletBase

[Parameter(Position = 9, Mandatory = false, ValueFromPipelineByPropertyName = true,
HelpMessage = "The AD Graph Endpoint.")]
[Alias("Graph", "GraphUrl")]
public string GraphEndpoint { get; set; }

[Parameter(Position = 10, Mandatory = false, ValueFromPipelineByPropertyName = true,
Expand All @@ -67,12 +76,35 @@ public class SetAzureEnvironmentCommand : SubscriptionCmdletBase
HelpMessage = "Resource identifier of Azure Key Vault data service that is the recipient of the requested token.")]
public string AzureKeyVaultServiceEndpointResourceId { get; set; }

[Parameter(Position = 12, Mandatory = false, ValueFromPipelineByPropertyName = true,
HelpMessage = "Dns suffix of Traffic Manager service.")]
public string TrafficManagerDnsSuffix { get; set; }

[Parameter(Position = 13, Mandatory = false, ValueFromPipelineByPropertyName = true,
HelpMessage = "Dns suffix of Sql databases created in this environment.")]
public string SqlDatabaseDnsSuffix { get; set; }

[Parameter(Position = 14, Mandatory = false, ValueFromPipelineByPropertyName = true,
HelpMessage = "Determines whether to enable ADFS authentication, or to use AAD authentication instead. This value is normally true only for Azure Stack endpoints.")]
[Alias("OnPremise")]
public SwitchParameter EnableAdfsAuthentication { get; set; }

[Parameter(Position = 15, Mandatory = false, ValueFromPipelineByPropertyName = true,
HelpMessage = "The default tenant for this environment.")]
public string AdTenant { get; set; }

public SetAzureEnvironmentCommand() : base(true) { }

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public override void ExecuteCmdlet()
{
var newEnvironment = new AzureEnvironment { Name = Name };
if ((Name == "AzureCloud") || (Name == "AzureChinaCloud"))
{
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
Resources.CannotChangeBuiltinEnvironment, Name));
}

var newEnvironment = new AzureEnvironment { Name = Name, OnPremise = EnableAdfsAuthentication };
if (ProfileClient.Profile.Environments.ContainsKey(Name))
{
newEnvironment = ProfileClient.Profile.Environments[Name];
Expand All @@ -88,10 +120,13 @@ public override void ExecuteCmdlet()
SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.Graph, GraphEndpoint);
SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.AzureKeyVaultDnsSuffix, AzureKeyVaultDnsSuffix);
SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.AzureKeyVaultServiceEndpointResourceId, AzureKeyVaultServiceEndpointResourceId);
SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.TrafficManagerDnsSuffix, TrafficManagerDnsSuffix);
SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.SqlDatabaseDnsSuffix, SqlDatabaseDnsSuffix);
SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.AdTenant, AdTenant);

ProfileClient.AddOrSetEnvironment(newEnvironment);

WriteObject(newEnvironment);
WriteObject((PSAzureEnvironment)newEnvironment);
}

private void SetEndpointIfProvided(AzureEnvironment newEnvironment, AzureEnvironment.Endpoint endpoint, string property)
Expand Down
Loading