Ejemplo n.º 1
0
        private List <AzureTenant> ListAccountTenants(
            IAzureAccount account,
            IAzureEnvironment environment,
            SecureString password,
            string promptBehavior,
            Action <string> promptAction)
        {
            IList <AzureTenant> result = new List <AzureTenant>();
            var commonTenant           = account.GetCommonTenant();

            try
            {
                var commonTenantToken = AcquireAccessToken(
                    account,
                    environment,
                    commonTenant,
                    password,
                    promptBehavior,
                    promptAction);

                result = SubscriptionAndTenantClient?.ListAccountTenants(commonTenantToken, environment);
            }
            catch
            {
                WriteWarningMessage(string.Format(ProfileMessages.UnableToAqcuireToken, commonTenant));
                if (account.IsPropertySet(AzureAccount.Property.Tenants))
                {
                    result =
                        account.GetPropertyAsArray(AzureAccount.Property.Tenants)
                        .Select(ti =>
                    {
                        var tenant = new AzureTenant();

                        Guid guid;
                        if (Guid.TryParse(ti, out guid))
                        {
                            tenant.Id        = ti;
                            tenant.Directory = AccessTokenExtensions.GetDomain(account.Id);
                        }
                        else
                        {
                            tenant.Directory = ti;
                        }

                        return(tenant);
                    }).ToList();
                }
                if (!result.Any())
                {
                    throw;
                }
            }

            return(result.ToList());
        }
Ejemplo n.º 2
0
        private IAzureAccount MergeAccountProperties(IAzureAccount account1, IAzureAccount account2)
        {
            if (account1 == null || account2 == null)
            {
                throw new ArgumentNullException("account1");
            }
            if (!string.Equals(account1.Id, account2.Id, StringComparison.InvariantCultureIgnoreCase))
            {
                throw new ArgumentException("Account Ids do not match.");
            }
            if (account1.Type != account2.Type)
            {
                throw new ArgumentException("Account1 types do not match.");
            }
            AzureAccount mergeAccount = new AzureAccount
            {
                Id   = account1.Id,
                Type = account1.Type
            };

            foreach (var property in account1.ExtendedProperties.Keys.Union(account2.ExtendedProperties.Keys))
            {
                mergeAccount.SetProperty(property, account1.IsPropertySet(property) ?
                                         account1.GetProperty(property) : account2.GetProperty(property));
            }


            // Merge Tenants
            var tenants = account1.GetPropertyAsArray(AzureAccount.Property.Tenants)
                          .Union(account2.GetPropertyAsArray(AzureAccount.Property.Tenants), StringComparer.CurrentCultureIgnoreCase);

            mergeAccount.SetProperty(AzureAccount.Property.Tenants, tenants.ToArray());

            // Merge Subscriptions
            var subscriptions = account1.GetPropertyAsArray(AzureAccount.Property.Subscriptions)
                                .Union(account2.GetPropertyAsArray(AzureAccount.Property.Subscriptions), StringComparer.CurrentCultureIgnoreCase);

            mergeAccount.SetProperty(AzureAccount.Property.Subscriptions, subscriptions.ToArray());

            return(mergeAccount);
        }
Ejemplo n.º 3
0
        private IEnumerable <IAzureSubscription> ListSubscriptionsFromServer(IAzureAccount account, IAzureEnvironment environment, SecureString password, string promptBehavior)
        {
            string[] tenants = null;
            try
            {
                if (!account.IsPropertySet(AzureAccount.Property.Tenants))
                {
                    tenants = LoadAccountTenants(account, environment, password, promptBehavior);
                }
                else
                {
                    var storedTenants = account.GetPropertyAsArray(AzureAccount.Property.Tenants);
                    if (account.Type == AzureAccount.AccountType.User && storedTenants.Count() == 1)
                    {
                        TracingAdapter.Information(Resources.AuthenticatingForSingleTenant, account.Id, storedTenants[0]);
                        AzureSession.Instance.AuthenticationFactory.Authenticate(account, environment, storedTenants[0], password,
                                                                                 promptBehavior);
                    }
                }
            }
            catch (AadAuthenticationException aadEx)
            {
                WriteOrThrowAadExceptionMessage(aadEx);
                return(new AzureSubscription[0]);
            }

            try
            {
                tenants = tenants ?? account.GetTenants();
                List <IAzureSubscription> rdfeSubscriptions = ListServiceManagementSubscriptions(account, environment,
                                                                                                 password, ShowDialog.Never, tenants).ToList();

                // Set user ID
                foreach (var subscription in rdfeSubscriptions)
                {
                    account.SetOrAppendProperty(AzureAccount.Property.Subscriptions, subscription.Id.ToString());
                }

                if (rdfeSubscriptions.Any())
                {
                    return(rdfeSubscriptions);
                }
                else
                {
                    return(new AzureSubscription[0]);
                }
            }
            catch (AadAuthenticationException aadEx)
            {
                WriteOrThrowAadExceptionMessage(aadEx);
                return(new AzureSubscription[0]);
            }
        }
        /// <summary>
        /// Determine if the given account has access to the given subscription
        /// </summary>
        /// <param name="account">The account to look in</param>
        /// <param name="subscriptionId">The subscription to check for</param>
        /// <returns>True if the account has access to the subscription, otherwise false</returns>
        public static bool HasSubscription(this IAzureAccount account, Guid subscriptionId)
        {
            bool exists        = false;
            var  subscriptions = account.GetPropertyAsArray(AzureAccount.Property.Subscriptions);

            if (subscriptions != null && subscriptions.Length > 0)
            {
                exists = subscriptions.Contains(subscriptionId.ToString(), StringComparer.OrdinalIgnoreCase);
            }

            return(exists);
        }
Ejemplo n.º 5
0
        private List <AzureTenant> ListAccountTenants(
            IAzureAccount account,
            IAzureEnvironment environment,
            SecureString password,
            string promptBehavior,
            Action <string> promptAction)
        {
            List <AzureTenant> result = new List <AzureTenant>();
            var commonTenant          = GetCommonTenant(account);

            try
            {
                var commonTenantToken = AcquireAccessToken(
                    account,
                    environment,
                    commonTenant,
                    password,
                    promptBehavior,
                    promptAction);

                SubscriptionClient subscriptionClient = null;
                try
                {
                    subscriptionClient = AzureSession.Instance.ClientFactory.CreateCustomArmClient <SubscriptionClient>(
                        environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager),
                        new TokenCredentials(commonTenantToken.AccessToken) as ServiceClientCredentials,
                        AzureSession.Instance.ClientFactory.GetCustomHandlers());
                    //TODO: Fix subscription client to not require subscriptionId
                    result = account.MergeTenants(subscriptionClient.Tenants.List(), commonTenantToken);
                }
                finally
                {
                    // In test mode, we are reusing the client since disposing of it will
                    // fail some tests (due to HttpClient being null)
                    if (subscriptionClient != null && !TestMockSupport.RunningMocked)
                    {
                        subscriptionClient.Dispose();
                    }
                }
            }
            catch
            {
                WriteWarningMessage(string.Format(ProfileMessages.UnableToAqcuireToken, commonTenant));
                if (account.IsPropertySet(AzureAccount.Property.Tenants))
                {
                    result =
                        account.GetPropertyAsArray(AzureAccount.Property.Tenants)
                        .Select(ti =>
                    {
                        var tenant = new AzureTenant();

                        Guid guid;
                        if (Guid.TryParse(ti, out guid))
                        {
                            tenant.Id        = ti;
                            tenant.Directory = AccessTokenExtensions.GetDomain(account.Id);
                        }
                        else
                        {
                            tenant.Directory = ti;
                        }

                        return(tenant);
                    }).ToList();
                }
                if (!result.Any())
                {
                    throw;
                }
            }

            return(result);
        }
 /// <summary>
 /// Get the tenants this account has access to
 /// </summary>
 /// <param name="account">The accoutn to check</param>
 /// <returns>The set of tenants the accoutn has access to</returns>
 public static string[] GetTenants(this IAzureAccount account)
 {
     return(account.GetPropertyAsArray(AzureAccount.Property.Tenants));
 }
 /// <summary>
 /// Get the set of subscriptiosn associated with the account
 /// </summary>
 /// <param name="account">The account to check</param>
 /// <returns>The set of subscriptions associated with the account</returns>
 public static string[] GetSubscriptions(this IAzureAccount account)
 {
     return(account.GetPropertyAsArray(AzureAccount.Property.Subscriptions));
 }