|
| 1 | +// ---------------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// Copyright Microsoft Corporation |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +// ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +using Hyak.Common; |
| 16 | +using Microsoft.Azure.Common.Authentication; |
| 17 | +using Microsoft.Azure.Common.Authentication.Factories; |
| 18 | +using Microsoft.Azure.Common.Authentication.Models; |
| 19 | +using Microsoft.Azure.Subscriptions; |
| 20 | +using System; |
| 21 | +using System.Collections.Generic; |
| 22 | +using System.Linq; |
| 23 | +using System.Management.Automation; |
| 24 | +using System.Security; |
| 25 | + |
| 26 | +namespace Microsoft.Azure.Commands.ResourceManager.Common |
| 27 | +{ |
| 28 | + public class RMProfileClient |
| 29 | + { |
| 30 | + private AzureRMProfile _profile; |
| 31 | + public Action<string> WarningLog; |
| 32 | + |
| 33 | + public RMProfileClient(AzureRMProfile profile) |
| 34 | + { |
| 35 | + _profile = profile; |
| 36 | + } |
| 37 | + |
| 38 | + public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment, string tenantId, string subscriptionId, SecureString password) |
| 39 | + { |
| 40 | + AzureSubscription newSubscription = null; |
| 41 | + AzureTenant newTenant = new AzureTenant(); |
| 42 | + |
| 43 | + // (tenant and subscription are present) OR |
| 44 | + // (tenant is present and subscription is not provided) |
| 45 | + if (!string.IsNullOrEmpty(tenantId)) |
| 46 | + { |
| 47 | + newTenant.Id = new Guid(tenantId); |
| 48 | + ShowDialog promptBehavior = password == null ? ShowDialog.Always : ShowDialog.Never; |
| 49 | + TryGetTenantSubscription(account, environment, tenantId, subscriptionId, password, promptBehavior, out newSubscription); |
| 50 | + } |
| 51 | + // (tenant is not provided and subscription is present) OR |
| 52 | + // (tenant is not provided and subscription is not provided) |
| 53 | + else |
| 54 | + { |
| 55 | + foreach(var tenant in ListAccountTenants(account, environment, password)) |
| 56 | + { |
| 57 | + if (TryGetTenantSubscription(account, environment, tenant, subscriptionId, password, ShowDialog.Auto, out newSubscription)) |
| 58 | + { |
| 59 | + newTenant.Id = new Guid(tenant); |
| 60 | + break; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + if (newSubscription == null) |
| 67 | + { |
| 68 | + throw new PSInvalidOperationException("Subscription was not found."); |
| 69 | + } |
| 70 | + |
| 71 | + _profile.DefaultContext = new AzureContext(newSubscription, account, environment, newTenant); |
| 72 | + |
| 73 | + return _profile; |
| 74 | + } |
| 75 | + |
| 76 | + private bool TryGetTenantSubscription( |
| 77 | + AzureAccount account, |
| 78 | + AzureEnvironment environment, |
| 79 | + string tenantId, |
| 80 | + string subscriptionId, |
| 81 | + SecureString password, |
| 82 | + ShowDialog promptBehavior, |
| 83 | + out AzureSubscription subscription) |
| 84 | + { |
| 85 | + var accessToken = AzureSession.AuthenticationFactory.Authenticate( |
| 86 | + account, |
| 87 | + environment, |
| 88 | + tenantId, |
| 89 | + password, |
| 90 | + promptBehavior); |
| 91 | + using (var subscriptionClient = AzureSession.ClientFactory.CreateCustomClient<SubscriptionClient>( |
| 92 | + new TokenCloudCredentials(accessToken.AccessToken), |
| 93 | + environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager))) |
| 94 | + { |
| 95 | + Subscriptions.Models.Subscription subscriptionFromServer = null; |
| 96 | + |
| 97 | + try |
| 98 | + { |
| 99 | + if (subscriptionId != null) |
| 100 | + { |
| 101 | + subscriptionFromServer = subscriptionClient.Subscriptions.Get(subscriptionId).Subscription; |
| 102 | + } |
| 103 | + else |
| 104 | + { |
| 105 | + var subscriptions = subscriptionClient.Subscriptions.List().Subscriptions; |
| 106 | + if (subscriptions != null) |
| 107 | + { |
| 108 | + if (subscriptions.Count > 1) |
| 109 | + { |
| 110 | + WriteWarningMessage(string.Format( |
| 111 | + "Tenant '{0}' contains more than one subscription. First one will be selected for further use.", |
| 112 | + tenantId)); |
| 113 | + } |
| 114 | + subscriptionFromServer = subscriptions.First(); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + catch (CloudException ex) |
| 119 | + { |
| 120 | + WriteWarningMessage(ex.Message); |
| 121 | + } |
| 122 | + |
| 123 | + if (subscriptionFromServer != null) |
| 124 | + { |
| 125 | + subscription = new AzureSubscription |
| 126 | + { |
| 127 | + Id = new Guid(subscriptionFromServer.SubscriptionId), |
| 128 | + Account = accessToken.UserId, |
| 129 | + Environment = environment.Name, |
| 130 | + Name = subscriptionFromServer.DisplayName, |
| 131 | + Properties = new Dictionary<AzureSubscription.Property, string> { { AzureSubscription.Property.Tenants, accessToken.TenantId } } |
| 132 | + }; |
| 133 | + return true; |
| 134 | + } |
| 135 | + |
| 136 | + subscription = null; |
| 137 | + return false; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private string[] ListAccountTenants(AzureAccount account, AzureEnvironment environment, SecureString password) |
| 142 | + { |
| 143 | + ShowDialog promptBehavior = password == null ? ShowDialog.Always : ShowDialog.Never; |
| 144 | + |
| 145 | + var commonTenantToken = AzureSession.AuthenticationFactory.Authenticate(account, environment, |
| 146 | + AuthenticationFactory.CommonAdTenant, password, promptBehavior); |
| 147 | + |
| 148 | + using (var subscriptionClient = AzureSession.ClientFactory.CreateCustomClient<SubscriptionClient>( |
| 149 | + new TokenCloudCredentials(commonTenantToken.AccessToken), |
| 150 | + environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager))) |
| 151 | + { |
| 152 | + return subscriptionClient.Tenants.List().TenantIds.Select(ti => ti.TenantId).ToArray(); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private void WriteWarningMessage(string message) |
| 157 | + { |
| 158 | + if (WarningLog != null) |
| 159 | + { |
| 160 | + WarningLog(message); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments