bool TryGetAuthenticationContextTypeAndDiscoveryContextScope(
            string contextJson,
            out string type,
            out TargetDiscoveryScope scope)
        {
            type  = null;
            scope = null;
            try
            {
                var discoveryContext = JsonConvert
                                       .DeserializeObject <TargetDiscoveryContext <AuthenticationType> >(contextJson);

                type  = discoveryContext?.Authentication?.Type;
                scope = discoveryContext?.Scope;

                if (type != null && scope != null)
                {
                    return(true);
                }

                log.Error($"Could not extract Type or Scope from {ContextVariableName}, the data is in the wrong format.");
                return(false);
            }
            catch (JsonException ex)
            {
                log.Error($"Could not extract Type or Scope from {ContextVariableName}, the data is in the wrong format: {ex.Message}");
                return(false);
            }
        }
        protected void DoDiscovery(AwsAuthenticationDetails authenticationDetails)
        {
            var scope = new TargetDiscoveryScope("TestSpace",
                                                 "Staging",
                                                 "testProject",
                                                 null,
                                                 new[] { "discovery-role" },
                                                 "WorkerPools-1");

            var targetDiscoveryContext =
                new TargetDiscoveryContext <AwsAuthenticationDetails>(scope,
                                                                      authenticationDetails);

            var result =
                ExecuteDiscoveryCommand(targetDiscoveryContext,
                                        new[] { "Calamari.Aws" }
                                        );

            result.AssertSuccess();
        }
示例#3
0
        public void DiscoverKubernetesClusterWithAzureServicePrincipalAccount()
        {
            var serviceMessageCollectorLog = new ServiceMessageCollectorLog();

            Log = serviceMessageCollectorLog;

            var scope = new TargetDiscoveryScope("TestSpace",
                                                 "Staging",
                                                 "testProject",
                                                 null,
                                                 new[] { "discovery-role" },
                                                 "WorkerPool-1");

            var account = new ServicePrincipalAccount(
                ExternalVariables.Get(ExternalVariable.AzureSubscriptionId),
                ExternalVariables.Get(ExternalVariable.AzureSubscriptionClientId),
                ExternalVariables.Get(ExternalVariable.AzureSubscriptionTenantId),
                ExternalVariables.Get(ExternalVariable.AzureSubscriptionPassword),
                null,
                null,
                null);

            var authenticationDetails =
                new AccountAuthenticationDetails <ServicePrincipalAccount>(
                    "Azure",
                    "Accounts-1",
                    account);

            var targetDiscoveryContext =
                new TargetDiscoveryContext <AccountAuthenticationDetails <ServicePrincipalAccount> >(scope,
                                                                                                     authenticationDetails);

            var result =
                ExecuteDiscoveryCommand(targetDiscoveryContext,
                                        new[] { "Calamari.Azure" }
                                        );

            result.AssertSuccess();

            var targetName             = $"aks/{azureSubscriptionId}/{azurermResourceGroup}/{aksClusterName}";
            var expectedServiceMessage = new ServiceMessage(
                KubernetesDiscoveryCommand.CreateKubernetesTargetServiceMessageName,
                new Dictionary <string, string>
            {
                { "name", targetName },
                { "clusterName", aksClusterName },
                { "clusterResourceGroup", azurermResourceGroup },
                { "skipTlsVerification", bool.TrueString },
                { "octopusDefaultWorkerPoolIdOrName", scope.WorkerPoolId },
                { "octopusAccountIdOrName", "Accounts-1" },
                { "octopusRoles", "discovery-role" },
                { "updateIfExisting", bool.TrueString },
                { "isDynamic", bool.TrueString },
                { "awsUseWorkerCredentials", bool.FalseString },
                { "awsAssumeRole", bool.FalseString },
            });

            serviceMessageCollectorLog.ServiceMessages.Should()
            .ContainSingle(s => s.Properties["name"] == targetName)
            .Which.Should()
            .BeEquivalentTo(expectedServiceMessage);
        }
        void WriteTargetCreationServiceMessage(KubernetesCluster cluster, TargetMatchResult matchResult, TargetDiscoveryScope scope)
        {
            var parameters = new Dictionary <string, string> {
                { "name", cluster.Name },
                { "clusterName", cluster.ClusterName },
                { "clusterUrl", cluster.Endpoint },
                { "clusterResourceGroup", cluster.ResourceGroupName },
                { "clusterAdminLogin", null },
                { "namespace", null },
                { "skipTlsVerification", bool.TrueString },
                { "octopusDefaultWorkerPoolIdOrName", cluster.WorkerPool ?? scope.WorkerPoolId },
                { "octopusAccountIdOrName", cluster.AccountId },
                { "octopusClientCertificateIdOrName", null },
                { "octopusServerCertificateIdOrName", null },
                { "octopusRoles", matchResult.Role },
                { "healthCheckContainerImageFeedIdOrName", null },
                { "healthCheckContainerImage", null },
                { "updateIfExisting", bool.TrueString },
                { "isDynamic", bool.TrueString },
                { "clusterProject", null },
                { "clusterRegion", null },
                { "clusterZone", null },
                { "clusterImpersonateServiceAccount", null },
                { "clusterServiceAccountEmails", null },
                { "clusterUseVmServiceAccount", null },
                { "awsUseWorkerCredentials", cluster.AwsUseWorkerCredentials.ToString() },
                { "awsAssumeRole", (cluster.AwsAssumeRole != null).ToString() },
                { "awsAssumeRoleArn", cluster.AwsAssumeRole?.Arn },
                { "awsAssumeRoleSession", cluster.AwsAssumeRole?.Session },
                { "awsAssumeRoleSessionDurationSeconds", cluster.AwsAssumeRole?.SessionDuration?.ToString() },
                { "awsAssumeRoleExternalId", cluster.AwsAssumeRole?.ExternalId }
            };

            var serviceMessage = new ServiceMessage(
                CreateKubernetesTargetServiceMessageName,
                parameters.Where(p => p.Value != null)
                .ToDictionary(p => p.Key, p => p.Value));

            log.WriteServiceMessage(serviceMessage);
        }