Beispiel #1
0
        public async Task <IEnumerable <ClusterGroup> > GetClusters()
        {
            List <ClusterGroup> groups = new List <ClusterGroup>();

            foreach (AzureAccount account in await this.appSettings
                     .GetCloudAccounts <AzureAccount>(CloudAccountType.Azure))
            {
                ClusterGroup group = new ClusterGroup(
                    account.TenantId,
                    CloudAccountType.Azure,
                    account.Name);

                try
                {
                    IAzure azure = AzureAccountManager.CreateAuthenticatedClient(
                        account.EnvironmentId,
                        account.TenantId,
                        account.ClientId,
                        account.ClientSecret);

                    // TODO Handle paging ??
                    IPagedCollection <IKubernetesCluster> clusters = await azure.KubernetesClusters.ListAsync();

                    group.AddRange(clusters.Select(c => new Cluster(c.Id, c.Name, account.TenantId, CloudAccountType.Azure)));
                }
                catch (AdalServiceException e) when(e.ServiceErrorCodes != null &&
                                                    (e.ServiceErrorCodes.Contains(AzureAccountManager.AdalInvalidClientIdServiceErrorCode) ||
                                                     e.ServiceErrorCodes.Contains(AzureAccountManager.AdalInvalidClientSecretServiceErrorCode) ||
                                                     e.ServiceErrorCodes.Contains(AzureAccountManager.AdalTenantDoesntExistServiceErrorCode)))
                {
                    // Authentication issue
                    group.ErrorMessage = AppResources.AzureAccountManager_GetClusters_AuthenticationErrorMessage;
                }
                catch (AdalServiceException e) when(e.StatusCode == AzureAccountManager.AdalRequestTimeoutStatusCode)
                {
                    // No internet
                    throw new NoNetworkException(e.Message, e);
                }
                catch (HttpRequestException e) when(e.InnerException is WebException web &&
                                                    web.Status == WebExceptionStatus.NameResolutionFailure)
                {
                    // No internet
                    throw new NoNetworkException(e.Message, e);
                }

                groups.Add(group);
            }

            return(groups);
        }
Beispiel #2
0
        public async Task <byte[]> GetSelectedClusterKubeConfigContent()
        {
            Cluster selectedCluster             = this.appSettings.SelectedCluster;
            IEnumerable <AzureAccount> accounts = await this.appSettings.GetCloudAccounts <AzureAccount>(CloudAccountType.Azure);

            AzureAccount account = accounts.First(a => a.Id == selectedCluster.AccountId);

            try
            {
                IAzure azure = AzureAccountManager.CreateAuthenticatedClient(
                    account.EnvironmentId,
                    account.TenantId,
                    account.ClientId,
                    account.ClientSecret);

                IKubernetesCluster kubernetesCluster = await azure.KubernetesClusters.GetByIdAsync(selectedCluster.Id);

                if (kubernetesCluster == null)
                {
                    // Cluster not found - mostly likely deleted.
                    throw new ClusterNotFoundException($"Cluster with Id: {selectedCluster.Id}");
                }

                return(kubernetesCluster.UserKubeConfigContent);
            }
            catch (AdalServiceException e) when(e.ServiceErrorCodes != null &&
                                                (e.ServiceErrorCodes.Contains(AzureAccountManager.AdalInvalidClientIdServiceErrorCode) ||
                                                 e.ServiceErrorCodes.Contains(AzureAccountManager.AdalInvalidClientSecretServiceErrorCode) ||
                                                 e.ServiceErrorCodes.Contains(AzureAccountManager.AdalTenantDoesntExistServiceErrorCode)))
            {
                // Something is wrong with the Account's credentials.
                throw new AccountInvalidException(e.Message, e);
            }
            catch (AdalServiceException e) when(e.StatusCode == AzureAccountManager.AdalRequestTimeoutStatusCode)
            {
                // No internet
                throw new NoNetworkException(e.Message, e);
            }
            catch (HttpRequestException e) when(e.InnerException is WebException web &&
                                                web.Status == WebExceptionStatus.NameResolutionFailure)
            {
                // No internet
                throw new NoNetworkException(e.Message, e);
            }
        }
Beispiel #3
0
        public async Task <(bool isValid, string message)> TrySaveCredentials(
            CloudEnvironment cloudEnvironment,
            string tenantId,
            string clientId,
            string clientSecret,
            bool isEditing)
        {
            IEnumerable <AzureAccount> accounts = await this.appSettings
                                                  .GetCloudAccounts <AzureAccount>(CloudAccountType.Azure);

            if (!isEditing &&
                accounts.Any(a => a.TenantId == tenantId))
            {
                // Ensuring not adding duplicate accounts, based upon TenantId.
                return(false, AppResources.AzureAccountManager_TryAddCredentials_DuplicateTenantId);
            }

            try
            {
                IAzure azure = AzureAccountManager.CreateAuthenticatedClient(cloudEnvironment.Id, tenantId, clientId, clientSecret);

                string subscriptionName = azure.GetCurrentSubscription().DisplayName;

                await this.appSettings.AddOrUpdateCloudAccount(new AzureAccount(
                                                                   subscriptionName,
                                                                   cloudEnvironment.Id,
                                                                   tenantId,
                                                                   clientId,
                                                                   clientSecret));
            }
            catch (AdalServiceException e) when(e.ServiceErrorCodes != null &&
                                                e.ServiceErrorCodes.Contains(AzureAccountManager.AdalInvalidClientIdServiceErrorCode))
            {
                // AADSTS70001 - Invalid client ID.
                return(false, AppResources.AzureAccountManager_TryAddCredentials_InvalidClientId);
            }
            catch (AdalServiceException e) when(e.ServiceErrorCodes != null &&
                                                e.ServiceErrorCodes.Contains(AzureAccountManager.AdalInvalidClientSecretServiceErrorCode))
            {
                // AADSTS70002 - Invalid client secret.
                return(false, AppResources.AzureAccountManager_TryAddCredentials_InvalidClientSecret);
            }
            catch (AdalServiceException e) when(e.ServiceErrorCodes != null &&
                                                e.ServiceErrorCodes.Contains(AzureAccountManager.AdalTenantDoesntExistServiceErrorCode))
            {
                // AADSTS90002 - Tenant doesn't exist.
                return(false, AppResources.AzureAccountManager_TryAddCredentials_InvalidTenantId);
            }
            catch (AdalServiceException e) when(e.StatusCode == AzureAccountManager.AdalRequestTimeoutStatusCode)
            {
                // No internet
                return(false, AppResources.AzureAccountManager_TryAddCredentials_NoInternet);
            }
            catch (HttpRequestException e) when(e.InnerException is WebException web &&
                                                web.Status == WebExceptionStatus.NameResolutionFailure)
            {
                // No internet
                return(false, AppResources.AzureAccountManager_TryAddCredentials_NoInternet);
            }

            return(true, string.Empty);
        }