public async Task Setup()
        {
            clientId       = ExternalVariables.Get(ExternalVariable.AzureSubscriptionClientId);
            clientSecret   = ExternalVariables.Get(ExternalVariable.AzureSubscriptionPassword);
            tenantId       = ExternalVariables.Get(ExternalVariable.AzureSubscriptionTenantId);
            subscriptionId = ExternalVariables.Get(ExternalVariable.AzureSubscriptionId);

            var resourceGroupName = SdkContext.RandomResourceName(nameof(AzureResourceGroupActionHandlerFixture), 60);

            var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(clientId,
                                                                                      clientSecret,
                                                                                      tenantId,
                                                                                      AzureEnvironment.AzureGlobalCloud);

            azure = Microsoft.Azure.Management.Fluent.Azure
                    .Configure()
                    .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
                    .Authenticate(credentials)
                    .WithSubscription(subscriptionId);

            resourceGroup = await azure.ResourceGroups
                            .Define(resourceGroupName)
                            .WithRegion(Region.USWest)
                            .CreateAsync();
        }
Exemplo n.º 2
0
 public void Setup()
 {
     clientId       = ExternalVariables.Get(ExternalVariable.AzureSubscriptionClientId);
     clientSecret   = ExternalVariables.Get(ExternalVariable.AzureSubscriptionPassword);
     tenantId       = ExternalVariables.Get(ExternalVariable.AzureSubscriptionTenantId);
     subscriptionId = ExternalVariables.Get(ExternalVariable.AzureSubscriptionId);
 }
        public void DeleteStack(string stackName)
        {
            var variablesFile = Path.GetTempFileName();
            var variables     = new CalamariVariables();

            variables.Set("Octopus.Action.AwsAccount.Variable", "AWSAccount");
            variables.Set("AWSAccount.AccessKey", ExternalVariables.Get(ExternalVariable.AwsAcessKey));
            variables.Set("AWSAccount.SecretKey", ExternalVariables.Get(ExternalVariable.AwsSecretKey));
            variables.Set("Octopus.Action.Aws.Region", region);
            variables.Set(AwsSpecialVariables.CloudFormation.StackName, stackName);
            variables.Save(variablesFile);

            using (new TemporaryFile(variablesFile))
            {
                var log     = new InMemoryLog();
                var command = new DeleteCloudFormationCommand(
                    log,
                    variables
                    );
                var result = command.Execute(new[]
                {
                    "--variables", $"{variablesFile}",
                    "--waitForCompletion", "true"
                });

                result.Should().Be(0);
            }
        }
Exemplo n.º 4
0
        public async Task Setup()
        {
            resourceGroupName = Guid.NewGuid().ToString();

            clientId       = ExternalVariables.Get(ExternalVariable.AzureSubscriptionClientId);
            clientSecret   = ExternalVariables.Get(ExternalVariable.AzureSubscriptionPassword);
            tenantId       = ExternalVariables.Get(ExternalVariable.AzureSubscriptionTenantId);
            subscriptionId = ExternalVariables.Get(ExternalVariable.AzureSubscriptionId);

            var resourceGroupLocation = Environment.GetEnvironmentVariable("AZURE_NEW_RESOURCE_REGION") ?? "eastus";

            authToken = await GetAuthToken(tenantId, clientId, clientSecret);

            var resourcesClient = new ResourcesManagementClient(subscriptionId,
                                                                new ClientSecretCredential(tenantId, clientId, clientSecret));

            resourceGroupClient = resourcesClient.ResourceGroups;

            var resourceGroup = new ResourceGroup(resourceGroupLocation);

            resourceGroup = await resourceGroupClient.CreateOrUpdateAsync(resourceGroupName, resourceGroup);

            webMgmtClient = new WebSiteManagementClient(new TokenCredentials(authToken))
            {
                SubscriptionId = subscriptionId,
                HttpClient     = { BaseAddress = new Uri(DefaultVariables.ResourceManagementEndpoint) },
            };

            var svcPlan = await webMgmtClient.AppServicePlans.BeginCreateOrUpdateAsync(resourceGroup.Name,
                                                                                       resourceGroup.Name, new AppServicePlan(resourceGroup.Location)
            {
                Kind     = "linux",
                Reserved = true,
                Sku      = new SkuDescription
                {
                    Name = "S1",
                    Tier = "Standard"
                }
            });

            site = await webMgmtClient.WebApps.BeginCreateOrUpdateAsync(resourceGroup.Name, resourceGroup.Name,
                                                                        new Site(resourceGroup.Location)
            {
                ServerFarmId = svcPlan.Id,
                SiteConfig   = new SiteConfig
                {
                    LinuxFxVersion = @"DOCKER|mcr.microsoft.com/azuredocs/aci-helloworld",
                    AppSettings    = new List <NameValuePair>
                    {
                        new NameValuePair("DOCKER_REGISTRY_SERVER_URL", "https://index.docker.io"),
                        new NameValuePair("WEBSITES_ENABLE_APP_SERVICE_STORAGE", "false")
                    },
                    AlwaysOn = true
                }
            });

            webappName = site.Name;

            await AssertSetupSuccessAsync();
        }
        public void WebApp_Is_Not_Found()
        {
            var randomName     = SdkContext.RandomResourceName(nameof(AzureWebAppHealthCheckActionHandlerFixtures), 60);
            var clientId       = ExternalVariables.Get(ExternalVariable.AzureSubscriptionClientId);
            var clientSecret   = ExternalVariables.Get(ExternalVariable.AzureSubscriptionPassword);
            var tenantId       = ExternalVariables.Get(ExternalVariable.AzureSubscriptionTenantId);
            var subscriptionId = ExternalVariables.Get(ExternalVariable.AzureSubscriptionId);

            ActionHandlerTestBuilder.CreateAsync <AzureWebAppHealthCheckActionHandler, Program>()
            .WithArrange(context =>
            {
                context.Variables.Add(AccountVariables.SubscriptionId,
                                      subscriptionId);
                context.Variables.Add(AccountVariables.TenantId,
                                      tenantId);
                context.Variables.Add(AccountVariables.ClientId,
                                      clientId);
                context.Variables.Add(AccountVariables.Password,
                                      clientSecret);
                context.Variables.Add(SpecialVariables.Action.Azure.ResourceGroupName, randomName);
                context.Variables.Add(SpecialVariables.Action.Azure.WebAppName, randomName);
                context.Variables.Add(SpecialVariables.AccountType, AccountTypes.AzureServicePrincipalAccountType.ToString());
            })
            .WithAssert(result => result.WasSuccessful.Should().BeFalse())
            .Execute(false);
        }
Exemplo n.º 6
0
 protected override Dictionary <string, string> GetEnvironmentVars()
 {
     return(new Dictionary <string, string>
     {
         { "GOOGLE_CLOUD_KEYFILE_JSON", ExternalVariables.Get(ExternalVariable.GoogleCloudJsonKeyfile) }
     });
 }
        public async Task Setup()
        {
            azureConfigPath = TemporaryDirectory.Create();
            Environment.SetEnvironmentVariable("AZURE_CONFIG_DIR", azureConfigPath.DirectoryPath);

            clientId       = ExternalVariables.Get(ExternalVariable.AzureSubscriptionClientId);
            clientSecret   = ExternalVariables.Get(ExternalVariable.AzureSubscriptionPassword);
            tenantId       = ExternalVariables.Get(ExternalVariable.AzureSubscriptionTenantId);
            subscriptionId = ExternalVariables.Get(ExternalVariable.AzureSubscriptionId);
            var resourceGroupName = SdkContext.RandomResourceName(nameof(DeployAzureWebCommandFixture), 60);

            var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(clientId, clientSecret, tenantId,
                                                                                      AzureEnvironment.AzureGlobalCloud);

            azure = Azure
                    .Configure()
                    .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
                    .Authenticate(credentials)
                    .WithSubscription(subscriptionId);

            resourceGroup = await azure.ResourceGroups
                            .Define(resourceGroupName)
                            .WithRegion(Region.USWest)
                            .CreateAsync();

            appServicePlan = await azure.AppServices.AppServicePlans
                             .Define(SdkContext.RandomResourceName(nameof(DeployAzureWebCommandFixture), 60))
                             .WithRegion(resourceGroup.Region)
                             .WithExistingResourceGroup(resourceGroup)
                             .WithPricingTier(PricingTier.StandardS1)
                             .WithOperatingSystem(OperatingSystem.Windows)
                             .CreateAsync();
        }
Exemplo n.º 8
0
        public void CheckExternalVariableRuleTest()
        {
            // Initialize yara context
            using (YaraContext ctx = new YaraContext())
            {
                using (var compiler = new Compiler())
                {
                    //must declare this or the compiler will complain it doesn't exist when a rule references it
                    compiler.DeclareExternalStringVariable("filename");

                    //declare rule with an external variable available
                    compiler.AddRuleString("rule foo: bar {strings: $a = \"lmn\" condition: $a and filename matches /\\.txt$/is}");
                    CompiledRules compiledRules = compiler.Compile();

                    Assert.True(compiledRules.RuleCount == 1);

                    Encoding encoding = Encoding.ASCII;
                    byte[]   buffer   = encoding.GetBytes("abcdefgjiklmnoprstuvwxyz");

                    // Initialize a customscanner we can add variables to
                    var scanner = new CustomScanner(compiledRules);

                    ExternalVariables externalVariables = new ExternalVariables();
                    externalVariables.StringVariables.Add("filename", "Alphabet.txt");

                    List <ScanResult> compiledScanResults = scanner.ScanMemory(ref buffer, externalVariables);

                    Assert.True(compiledScanResults.Count == 1);
                    Assert.Equal("foo", compiledScanResults[0].MatchingRule.Identifier);

                    //release before falling out of the yara context
                    scanner.Release();
                }
            }
        }
        public async Task CanDownloadPackage()
        {
            string filename  = "Acme.Core.1.0.0.0-bugfix.zip";
            string packageId = $"{bucketName}/Acme.Core";
            var    version   = VersionFactory.CreateVersion("1.0.0.0-bugfix", VersionFormat.Semver);

            File.Copy(GetFixtureResource("Samples", filename), Path.Combine(rootDir, filename));

            await Validate(async client => await client.PutObjectAsync(new PutObjectRequest
            {
                BucketName  = bucketName,
                Key         = filename,
                InputStream = File.OpenRead(Path.Combine(rootDir, filename))
            },
                                                                       CancellationToken.None));

            var downloader = GetDownloader();
            var package    = downloader.DownloadPackage(packageId,
                                                        version,
                                                        "s3-feed",
                                                        new Uri("http://please-ignore.com"),
                                                        ExternalVariables.Get(ExternalVariable.AwsAcessKey),
                                                        ExternalVariables.Get(ExternalVariable.AwsSecretKey),
                                                        true,
                                                        3,
                                                        TimeSpan.FromSeconds(3));

            package.PackageId.Should().Be(packageId);
            package.Size.Should().Be(new FileInfo(Path.Combine(rootDir, filename)).Length);
        }
Exemplo n.º 10
0
        protected string UploadEntireCompressedPackage(string packageFilePath, string packageId, string packageVersion, List <S3PackageOptions> propertiesList, VariableDictionary customVariables = null)
        {
            var bucketKeyPrefix = $"calamaritest/{Guid.NewGuid():N}/";
            var variables       = new CalamariVariables();

            propertiesList.ForEach(properties =>
            {
                properties.BucketKeyPrefix = bucketKeyPrefix;
                variables.Set(AwsSpecialVariables.S3.PackageOptions, JsonConvert.SerializeObject(properties, GetEnrichedSerializerSettings()));
                variables.Set(PackageVariables.PackageId, packageId);
                variables.Set(PackageVariables.PackageVersion, packageVersion);
            });

            var variablesFile = Path.GetTempFileName();

            variables.Set("Octopus.Action.AwsAccount.Variable", "AWSAccount");
            variables.Set("AWSAccount.AccessKey", ExternalVariables.Get(ExternalVariable.AwsAcessKey));
            variables.Set("AWSAccount.SecretKey", ExternalVariables.Get(ExternalVariable.AwsSecretKey));
            variables.Set("Octopus.Action.Aws.Region", region);

            if (customVariables != null)
            {
                variables.Merge(customVariables);
            }

            variables.Save(variablesFile);

            using (new TemporaryFile(variablesFile))
            {
                var log        = new InMemoryLog();
                var fileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem();

                var command = new UploadAwsS3Command(
                    log,
                    variables,
                    fileSystem,
                    new SubstituteInFiles(log, fileSystem, new FileSubstituter(log, fileSystem), variables),
                    new ExtractPackage(new CombinedPackageExtractor(log, variables, new CommandLineRunner(log, variables)), fileSystem, variables, log),
                    new StructuredConfigVariablesService(new PrioritisedList <IFileFormatVariableReplacer>
                {
                    new JsonFormatVariableReplacer(fileSystem, log),
                    new XmlFormatVariableReplacer(fileSystem, log),
                    new YamlFormatVariableReplacer(fileSystem, log),
                    new PropertiesFormatVariableReplacer(fileSystem, log),
                }, variables, fileSystem, log)
                    );

                var result = command.Execute(new[] {
                    "--package", $"{packageFilePath}",
                    "--variables", $"{variablesFile}",
                    "--bucket", bucketName,
                    "--targetMode", S3TargetMode.EntirePackage.ToString()
                });

                result.Should().Be(0);
            }

            return(bucketKeyPrefix);
        }
 protected override Dictionary <string, string> GetEnvironmentVars()
 {
     return(new Dictionary <string, string>
     {
         { "AWS_ACCESS_KEY_ID", ExternalVariables.Get(ExternalVariable.AwsCloudFormationAndS3AccessKey) },
         { "AWS_SECRET_ACCESS_KEY", ExternalVariables.Get(ExternalVariable.AwsCloudFormationAndS3SecretKey) },
         { "AWS_DEFAULT_REGION", region },
         { "TF_VAR_tests_source_dir", testFolder }
     });
 }
Exemplo n.º 12
0
        static async Task <(IAzure azure, IResourceGroup resourceGroup, IWebApp webApp)> SetUpAzureWebApp()
        {
            var resourceGroupName = SdkContext.RandomResourceName(nameof(AzureWebAppHealthCheckActionHandlerFixtures), 60);
            var clientId          = ExternalVariables.Get(ExternalVariable.AzureSubscriptionClientId);
            var clientSecret      = ExternalVariables.Get(ExternalVariable.AzureSubscriptionPassword);
            var tenantId          = ExternalVariables.Get(ExternalVariable.AzureSubscriptionTenantId);
            var subscriptionId    = ExternalVariables.Get(ExternalVariable.AzureSubscriptionId);

            var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(clientId,
                                                                                      clientSecret,
                                                                                      tenantId,
                                                                                      AzureEnvironment.AzureGlobalCloud);

            var azure = Microsoft.Azure.Management.Fluent.Azure
                        .Configure()
                        .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
                        .Authenticate(credentials)
                        .WithSubscription(subscriptionId);

            IResourceGroup resourceGroup = null;
            IWebApp        webApp        = null;

            try
            {
                resourceGroup = await azure.ResourceGroups
                                .Define(resourceGroupName)
                                .WithRegion(Region.USWest)
                                .CreateAsync();

                var appServicePlan = await azure.AppServices.AppServicePlans
                                     .Define(SdkContext.RandomResourceName(nameof(AzureWebAppHealthCheckActionHandlerFixtures), 60))
                                     .WithRegion(resourceGroup.Region)
                                     .WithExistingResourceGroup(resourceGroup)
                                     .WithPricingTier(PricingTier.BasicB1)
                                     .WithOperatingSystem(OperatingSystem.Windows)
                                     .CreateAsync();

                var webAppName = SdkContext.RandomResourceName(nameof(AzureWebAppHealthCheckActionHandlerFixtures), 60);
                webApp = await azure.WebApps
                         .Define(webAppName)
                         .WithExistingWindowsPlan(appServicePlan)
                         .WithExistingResourceGroup(resourceGroup)
                         .WithRuntimeStack(WebAppRuntimeStack.NETCore)
                         .CreateAsync();

                return(azure, resourceGroup, webApp);
            }
            catch
            {
                // When errors encountered in set up, we return the resources that have been set up so far anyway
                // We will clean the resources from the caller
                return(azure, resourceGroup, webApp);
            }
        }
        async Task ValidateCloudFormation(Func <AmazonCloudFormationClient, Task> execute)
        {
            var credentials = new BasicAWSCredentials(ExternalVariables.Get(ExternalVariable.AwsAcessKey), ExternalVariables.Get(ExternalVariable.AwsSecretKey));
            var config      = new AmazonCloudFormationConfig {
                AllowAutoRedirect = true, RegionEndpoint = RegionEndpoint.GetBySystemName(region)
            };

            using (var client = new AmazonCloudFormationClient(credentials, config))
            {
                await execute(client);
            }
        }
Exemplo n.º 14
0
        private void btGo_Click(object sender, EventArgs e)
        {
            this.Cursor  = Cursors.WaitCursor;
            btGo.Enabled = false;

            try
            {
                WriteConfiguration();

                if (String.IsNullOrEmpty(Server.Address) || String.IsNullOrEmpty(Server.Password) ||
                    String.IsNullOrEmpty(Server.Port) || String.IsNullOrEmpty(Server.Username))
                {
                    Dialog.Error(Resource.InvalidServerConfiguration);
                }
                else if (String.IsNullOrEmpty(DefinitionFilename))
                {
                    Dialog.Error(Resource.EmptyDefinitionFilename);
                }
                else if (String.IsNullOrEmpty(Target.Address) || String.IsNullOrEmpty(Target.Username) ||
                         String.IsNullOrEmpty(Target.Password) || (txtSSHPort != null && String.IsNullOrEmpty(Target.SSHPort)))
                {
                    Dialog.Error(Resource.InvalidTargetConfiguration);
                }
                else if (String.IsNullOrEmpty(DestinationFolder))
                {
                    Dialog.Error(Resource.EmptyDestinationFolderName);
                }
                else
                {
                    var hasExternalVariable = ((ExternalVariables != null) && (ExternalVariables.Count() > 0));
                    if (hasExternalVariable)
                    {
                        var externalVariableWindow = new ExternalVariableWindow(this);
                        if (externalVariableWindow.ShowDialog() != DialogResult.OK)
                        {
                            return;
                        }
                    }

                    SaveExternalVariableXml();

                    var collectionWindow = new CollectionWindow(this.Server, this.Target, this.DefinitionFilename, this.DestinationFolder, this.ExternalVariablesXml);
                    collectionWindow.ShowDialog();
                }
            }
            finally
            {
                btGo.Enabled = true;
                this.Cursor  = Cursors.Default;
            }
        }
Exemplo n.º 15
0
 protected override Dictionary <string, string> GetEnvironmentVars()
 {
     azureSubscriptionId = ExternalVariables.Get(ExternalVariable.AzureSubscriptionId);
     return(new Dictionary <string, string>()
     {
         { "ARM_SUBSCRIPTION_ID", azureSubscriptionId },
         { "ARM_CLIENT_ID", ExternalVariables.Get(ExternalVariable.AzureSubscriptionClientId) },
         { "ARM_CLIENT_SECRET", ExternalVariables.Get(ExternalVariable.AzureSubscriptionPassword) },
         { "ARM_TENANT_ID", ExternalVariables.Get(ExternalVariable.AzureSubscriptionTenantId) },
         { "TF_VAR_aks_client_id", ExternalVariables.Get(ExternalVariable.AzureSubscriptionClientId) },
         { "TF_VAR_aks_client_secret", ExternalVariables.Get(ExternalVariable.AzureSubscriptionPassword) },
         { "TF_VAR_test_namespace", TestNamespace },
     });
 }
Exemplo n.º 16
0
        public void AuthorisingWithAzureServicePrincipal()
        {
            variables.Set(Deployment.SpecialVariables.Account.AccountType, "AzureServicePrincipal");
            variables.Set("Octopus.Action.Kubernetes.AksClusterResourceGroup", azurermResourceGroup);
            variables.Set(SpecialVariables.AksClusterName, aksClusterName);
            variables.Set("Octopus.Action.Kubernetes.AksAdminLogin", Boolean.FalseString);
            variables.Set("Octopus.Action.Azure.SubscriptionId", ExternalVariables.Get(ExternalVariable.AzureSubscriptionId));
            variables.Set("Octopus.Action.Azure.TenantId", ExternalVariables.Get(ExternalVariable.AzureSubscriptionTenantId));
            variables.Set("Octopus.Action.Azure.Password", ExternalVariables.Get(ExternalVariable.AzureSubscriptionPassword));
            variables.Set("Octopus.Action.Azure.ClientId", ExternalVariables.Get(ExternalVariable.AzureSubscriptionClientId));
            var wrapper = CreateWrapper();

            TestScriptAndVerifyCluster(wrapper, "Test-Script");
        }
Exemplo n.º 17
0
        static void SetUpVariables(CommandTestBuilderContext context, string resourceGroupName, string webAppName)
        {
            var clientId       = ExternalVariables.Get(ExternalVariable.AzureSubscriptionClientId);
            var clientSecret   = ExternalVariables.Get(ExternalVariable.AzureSubscriptionPassword);
            var tenantId       = ExternalVariables.Get(ExternalVariable.AzureSubscriptionTenantId);
            var subscriptionId = ExternalVariables.Get(ExternalVariable.AzureSubscriptionId);

            context.Variables.Add(AccountVariables.SubscriptionId, subscriptionId);
            context.Variables.Add(AccountVariables.TenantId, tenantId);
            context.Variables.Add(AccountVariables.ClientId, clientId);
            context.Variables.Add(AccountVariables.Password, clientSecret);
            context.Variables.Add(SpecialVariables.Action.Azure.ResourceGroupName, resourceGroupName);
            context.Variables.Add(SpecialVariables.Action.Azure.WebAppName, webAppName);
            context.Variables.Add("Octopus.Account.AccountType", "AzureServicePrincipal");
        }
        public void ExecutionWithGoogleCloudAccount_WhenNeitherZoneOrRegionAreProvided()
        {
            variables.Set(Deployment.SpecialVariables.Account.AccountType, "GoogleCloudAccount");
            variables.Set(SpecialVariables.GkeClusterName, "gke-cluster-name");
            var account = "gke_account";

            variables.Set("Octopus.Action.GoogleCloudAccount.Variable", account);
            var jsonKey = ExternalVariables.Get(ExternalVariable.GoogleCloudJsonKeyfile);

            variables.Set($"{account}.JsonKey", Convert.ToBase64String(Encoding.UTF8.GetBytes(jsonKey)));
            variables.Set("Octopus.Action.GoogleCloud.Project", "gke-project");
            var wrapper = CreateWrapper();

            Assert.Throws <ArgumentException>(() => TestScriptInReadOnlyMode(wrapper));
        }
Exemplo n.º 19
0
        public void AuthorisingWithGoogleCloudAccount()
        {
            variables.Set(Deployment.SpecialVariables.Account.AccountType, "GoogleCloudAccount");
            variables.Set(SpecialVariables.GkeClusterName, gkeClusterName);
            var account = "gke_account";

            variables.Set("Octopus.Action.GoogleCloudAccount.Variable", account);
            var jsonKey = ExternalVariables.Get(ExternalVariable.GoogleCloudJsonKeyfile);

            variables.Set($"{account}.JsonKey", Convert.ToBase64String(Encoding.UTF8.GetBytes(jsonKey)));
            variables.Set("Octopus.Action.GoogleCloud.Project", gkeProject);
            variables.Set("Octopus.Action.GoogleCloud.Zone", gkeLocation);
            var wrapper = CreateWrapper();

            TestScriptAndVerifyCluster(wrapper, "Test-Script");
        }
Exemplo n.º 20
0
        private CalamariVariableDictionary BuildVariables(CalamariVariableDictionary variables)
        {
            variables.Set("Octopus.Action.AwsAccount.Variable", "AwsAccount");
            variables.Set("Octopus.Action.Aws.Region", "us-east-1");
            variables.Set("AwsAccount.AccessKey", ExternalVariables.Get(ExternalVariable.AwsAcessKey));
            variables.Set("AwsAccount.SecretKey", ExternalVariables.Get(ExternalVariable.AwsSecretKey));
            variables.Set("Octopus.Action.Aws.AssumeRole", "False");
            variables.Set("Octopus.Action.Aws.AssumedRoleArn", "");
            variables.Set("Octopus.Action.Aws.AssumedRoleSession", "");
            variables.Set("Octopus.Account.AccountType", "AzureServicePrincipal");
            variables.Set("Octopus.Action.Azure.TenantId", "2a881dca-3230-4e01-abcb-a1fd235c0981");
            variables.Set("Octopus.Action.Azure.ClientId", ExternalVariables.Get(ExternalVariable.AzureSubscriptionClientId));
            variables.Set("Octopus.Action.Azure.Password", ExternalVariables.Get(ExternalVariable.AzureSubscriptionPassword));
            variables.Set("Octopus.Action.Azure.SubscriptionId", "cf21dc34-73dc-4d7d-bd86-041884e0bc75");

            return(variables);
        }
Exemplo n.º 21
0
        public async Task AzureIntegration()
        {
            var random  = Guid.NewGuid().ToString("N").Substring(0, 6);
            var appName = $"cfe2e-{random}";

            void PopulateVariables(VariableDictionary _)
            {
                _.Set(SpecialVariables.Action.Azure.SubscriptionId, ExternalVariables.Get(ExternalVariable.AzureSubscriptionId));
                _.Set(SpecialVariables.Action.Azure.TenantId, ExternalVariables.Get(ExternalVariable.AzureSubscriptionTenantId));
                _.Set(SpecialVariables.Action.Azure.ClientId, ExternalVariables.Get(ExternalVariable.AzureSubscriptionClientId));
                _.Set(SpecialVariables.Action.Azure.Password, ExternalVariables.Get(ExternalVariable.AzureSubscriptionPassword));
                _.Set("app_name", appName);
                _.Set("random", random);
                _.Set(TerraformSpecialVariables.Action.Terraform.VarFiles, "example.tfvars");
                _.Set(TerraformSpecialVariables.Action.Terraform.AzureManagedAccount, Boolean.TrueString);
            }

            using (var outputs = ExecuteAndReturnLogOutput(PopulateVariables, "Azure", typeof(PlanCommand),
                                                           typeof(ApplyCommand), typeof(DestroyCommand)).GetEnumerator())
            {
                outputs.MoveNext();
                outputs.Current.Should()
                .Contain("Octopus.Action[\"\"].Output.TerraformPlanOutput");

                outputs.MoveNext();
                outputs.Current.Should()
                .Contain($"Saving variable 'Octopus.Action[\"\"].Output.TerraformValueOutputs[\"url\"]' with the value only of '{appName}.azurewebsites.net'");

                using (var client = new HttpClient())
                {
                    using (var responseMessage =
                               await client.GetAsync($"https://{appName}.azurewebsites.net").ConfigureAwait(false))
                    {
                        Assert.AreEqual(HttpStatusCode.Forbidden, responseMessage.StatusCode);
                    }
                }

                outputs.MoveNext();
                outputs.Current.Should()
                .Contain("destroy -force -no-color");
            }
        }
Exemplo n.º 22
0
        public async Task Setup()
        {
            var resourceManagementEndpointBaseUri =
                Environment.GetEnvironmentVariable(AccountVariables.ResourceManagementEndPoint) ??
                DefaultVariables.ResourceManagementEndpoint;
            var activeDirectoryEndpointBaseUri =
                Environment.GetEnvironmentVariable(AccountVariables.ActiveDirectoryEndPoint) ??
                DefaultVariables.ActiveDirectoryEndpoint;

            resourceGroupName = Guid.NewGuid().ToString();

            clientId       = ExternalVariables.Get(ExternalVariable.AzureSubscriptionClientId);
            clientSecret   = ExternalVariables.Get(ExternalVariable.AzureSubscriptionPassword);
            tenantId       = ExternalVariables.Get(ExternalVariable.AzureSubscriptionTenantId);
            subscriptionId = ExternalVariables.Get(ExternalVariable.AzureSubscriptionId);
            var resourceGroupLocation = Environment.GetEnvironmentVariable("AZURE_NEW_RESOURCE_REGION") ?? "eastus";

            authToken = await Auth.GetAuthTokenAsync(activeDirectoryEndpointBaseUri, resourceManagementEndpointBaseUri,
                                                     tenantId, clientId, clientSecret);

            var resourcesClient = new ResourcesManagementClient(subscriptionId,
                                                                new ClientSecretCredential(tenantId, clientId, clientSecret));

            resourceGroupClient = resourcesClient.ResourceGroups;

            resourceGroup = new ResourceGroup(resourceGroupLocation);
            resourceGroup = await resourceGroupClient.CreateOrUpdateAsync(resourceGroupName, resourceGroup);

            webMgmtClient = new WebSiteManagementClient(new TokenCredentials(authToken))
            {
                SubscriptionId = subscriptionId,
                HttpClient     = { BaseAddress = new Uri(DefaultVariables.ResourceManagementEndpoint), Timeout = TimeSpan.FromMinutes(5) },
            };

            svcPlan = await webMgmtClient.AppServicePlans.CreateOrUpdateAsync(resourceGroup.Name,
                                                                              resourceGroup.Name,
                                                                              new AppServicePlan(resourceGroup.Location) { Sku = new SkuDescription("S1", "Standard") }
                                                                              );
        }
        public void DeployTemplate(string resourceName, string templateFilePath, IVariables variables)
        {
            var variablesFile = Path.GetTempFileName();

            variables.Set("Octopus.Action.AwsAccount.Variable", "AWSAccount");
            variables.Set("AWSAccount.AccessKey", ExternalVariables.Get(ExternalVariable.AwsAcessKey));
            variables.Set("AWSAccount.SecretKey", ExternalVariables.Get(ExternalVariable.AwsSecretKey));
            variables.Set("Octopus.Action.Aws.Region", region);
            variables.Save(variablesFile);

            using (var templateFile = new TemporaryFile(templateFilePath))
                using (new TemporaryFile(variablesFile))
                {
                    var log        = new InMemoryLog();
                    var fileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem();
                    var command    = new DeployCloudFormationCommand(
                        log,
                        variables,
                        fileSystem,
                        new ExtractPackage(new CombinedPackageExtractor(log, variables, new CommandLineRunner(log, variables)), fileSystem, variables, log),
                        new StructuredConfigVariablesService(new PrioritisedList <IFileFormatVariableReplacer>
                    {
                        new JsonFormatVariableReplacer(fileSystem, log),
                        new XmlFormatVariableReplacer(fileSystem, log),
                        new YamlFormatVariableReplacer(fileSystem, log),
                        new PropertiesFormatVariableReplacer(fileSystem, log),
                    }, variables, fileSystem, log)
                        );
                    var result = command.Execute(new[]
                    {
                        "--template", $"{templateFile.FilePath}",
                        "--variables", $"{variablesFile}",
                        "--stackName", resourceName,
                        "--waitForCompletion", "true"
                    });

                    result.Should().Be(0);
                }
        }
Exemplo n.º 24
0
        public async Task AWSIntegration()
        {
            var bucketName = $"cfe2e-{Guid.NewGuid().ToString("N").Substring(0, 6)}";

            void PopulateVariables(VariableDictionary _)
            {
                _.Set(TerraformSpecialVariables.Action.Terraform.FileSubstitution, "test.txt");
                _.Set("Octopus.Action.Amazon.AccessKey", ExternalVariables.Get(ExternalVariable.AwsCloudFormationAndS3AccessKey));
                _.Set("Octopus.Action.Amazon.SecretKey", ExternalVariables.Get(ExternalVariable.AwsCloudFormationAndS3SecretKey));
                _.Set("Octopus.Action.Aws.Region", "ap-southeast-1");
                _.Set("Hello", "Hello World from AWS");
                _.Set("bucket_name", bucketName);
                _.Set(TerraformSpecialVariables.Action.Terraform.VarFiles, "example.tfvars");
                _.Set(TerraformSpecialVariables.Action.Terraform.AWSManagedAccount, "AWS");
            }

            using (var outputs = ExecuteAndReturnLogOutput(PopulateVariables, "AWS", typeof(PlanCommand), typeof(ApplyCommand), typeof(DestroyCommand)).GetEnumerator())
            {
                outputs.MoveNext();
                outputs.Current.Should()
                .Contain("Octopus.Action[\"\"].Output.TerraformPlanOutput");

                outputs.MoveNext();
                outputs.Current.Should()
                .Contain($"Saving variable 'Octopus.Action[\"\"].Output.TerraformValueOutputs[\"url\"]' with the value only of 'https://{bucketName}.s3.amazonaws.com/test.txt'");

                string fileData;
                using (var client = new HttpClient())
                {
                    fileData = await client.GetStringAsync($"https://{bucketName}.s3.amazonaws.com/test.txt").ConfigureAwait(false);
                }

                fileData.Should().Be("Hello World from AWS");

                outputs.MoveNext();
                outputs.Current.Should()
                .Contain("destroy -force -no-color");
            }
        }
Exemplo n.º 25
0
        public async Task Setup()
        {
            var resourceManagementEndpointBaseUri =
                Environment.GetEnvironmentVariable(AccountVariables.ResourceManagementEndPoint) ??
                DefaultVariables.ResourceManagementEndpoint;
            var activeDirectoryEndpointBaseUri =
                Environment.GetEnvironmentVariable(AccountVariables.ActiveDirectoryEndPoint) ??
                DefaultVariables.ActiveDirectoryEndpoint;

            resourceGroupName = Guid.NewGuid().ToString();

            clientId              = ExternalVariables.Get(ExternalVariable.AzureSubscriptionClientId);
            clientSecret          = ExternalVariables.Get(ExternalVariable.AzureSubscriptionPassword);
            tenantId              = ExternalVariables.Get(ExternalVariable.AzureSubscriptionTenantId);
            subscriptionId        = ExternalVariables.Get(ExternalVariable.AzureSubscriptionId);
            resourceGroupLocation = Environment.GetEnvironmentVariable("AZURE_NEW_RESOURCE_REGION") ?? "eastus";

            authToken = await Auth.GetAuthTokenAsync(activeDirectoryEndpointBaseUri, resourceManagementEndpointBaseUri,
                                                     tenantId, clientId, clientSecret);

            var resourcesClient = new ResourcesManagementClient(subscriptionId,
                                                                new ClientSecretCredential(tenantId, clientId, clientSecret));

            resourceGroupClient = resourcesClient.ResourceGroups;

            var resourceGroup = new ResourceGroup(resourceGroupLocation);

            resourceGroup = await resourceGroupClient.CreateOrUpdateAsync(resourceGroupName, resourceGroup);

            webMgmtClient = new WebSiteManagementClient(new TokenCredentials(authToken))
            {
                SubscriptionId = subscriptionId,
                HttpClient     = { BaseAddress = new Uri(DefaultVariables.ResourceManagementEndpoint) },
            };

            await ConfigureTestResources(resourceGroup);
        }
        public void ConfiguredProxy_IsUsedForHealthCheck()
        {
            // Arrange
            var randomName     = SdkContext.RandomResourceName(nameof(AzureWebAppHealthCheckActionHandlerFixtures), 60);
            var clientId       = ExternalVariables.Get(ExternalVariable.AzureSubscriptionClientId);
            var clientSecret   = ExternalVariables.Get(ExternalVariable.AzureSubscriptionPassword);
            var tenantId       = ExternalVariables.Get(ExternalVariable.AzureSubscriptionTenantId);
            var subscriptionId = ExternalVariables.Get(ExternalVariable.AzureSubscriptionId);

            // Act
            var result = ActionHandlerTestBuilder.CreateAsync <AzureWebAppHealthCheckActionHandler, Program>()
                         .WithArrange(context =>
            {
                context.Variables.Add(AccountVariables.SubscriptionId, subscriptionId);
                context.Variables.Add(AccountVariables.TenantId, tenantId);
                context.Variables.Add(AccountVariables.ClientId, clientId);
                context.Variables.Add(AccountVariables.Password, clientSecret);
                context.Variables.Add(SpecialVariables.Action.Azure.ResourceGroupName, randomName);
                context.Variables.Add(SpecialVariables.Action.Azure.WebAppName, randomName);
                context.Variables.Add(SpecialVariables.AccountType, AccountTypes.AzureServicePrincipalAccountType.ToString());
            })
                         .Execute(assertWasSuccess: false);

            // Assert
            result.Outcome.Should().Be(ExecutionOutcome.Unsuccessful);

            // This also operates differently locally vs on CI, so combine both StdErr and Calamari Log to get
            // the full picture
            var windowsNetFxDnsError  = "The remote name could not be resolved: 'non-existent-proxy.local'";
            var ubuntuDnsError        = "Resource temporarily unavailable (non-existent-proxy.local:3128)";
            var generalLinuxDnsError  = "Name or service not known (non-existent-proxy.local:3128)";
            var windowsDotNetDnsError = "No such host is known. (non-existent-proxy.local:3128)";

            var calamariOutput = result.FullLog + errorStream;

            calamariOutput.Should().ContainAny(windowsDotNetDnsError, ubuntuDnsError, generalLinuxDnsError, windowsNetFxDnsError);
        }
Exemplo n.º 27
0
        public async Task Setup()
        {
            _resourceGroupName = Guid.NewGuid().ToString();
            _tempDirs          = new List <DirectoryInfo>();

            _clientId       = ExternalVariables.Get(ExternalVariable.AzureSubscriptionClientId);
            _clientSecret   = ExternalVariables.Get(ExternalVariable.AzureSubscriptionPassword);
            _tenantId       = ExternalVariables.Get(ExternalVariable.AzureSubscriptionTenantId);
            _subscriptionId = ExternalVariables.Get(ExternalVariable.AzureSubscriptionId);
            _greeting       = "Sashimi";
            var resourceGroupLocation = Environment.GetEnvironmentVariable("AZURE_NEW_RESOURCE_REGION") ?? "eastus";

            var token = await GetAuthToken(_tenantId, _clientId, _clientSecret);

            var resourcesClient = new ResourcesManagementClient(_subscriptionId,
                                                                new ClientSecretCredential(_tenantId, _clientId, _clientSecret));

            _resourceGroupClient = resourcesClient.ResourceGroups;

            var resourceGroup = new ResourceGroup(resourceGroupLocation);

            resourceGroup = await _resourceGroupClient.CreateOrUpdateAsync(_resourceGroupName, resourceGroup);

            _webMgmtClient = new WebSiteManagementClient(new TokenCredentials(token))
            {
                SubscriptionId = _subscriptionId
            };

            var svcPlan = await _webMgmtClient.AppServicePlans.BeginCreateOrUpdateAsync(resourceGroup.Name,
                                                                                        resourceGroup.Name, new AppServicePlan(resourceGroup.Location));

            var webapp = await _webMgmtClient.WebApps.BeginCreateOrUpdateAsync(resourceGroup.Name, resourceGroup.Name,
                                                                               new Site(resourceGroup.Location) { ServerFarmId = svcPlan.Id });

            _webappName = webapp.Name;
        }
Exemplo n.º 28
0
        protected string Upload(string packageName, List <S3TargetPropertiesBase> propertiesList, VariableDictionary customVariables = null, S3TargetMode s3TargetMode = S3TargetMode.FileSelections)
        {
            const string packageVersion  = "1.0.0";
            var          bucketKeyPrefix = $"calamaritest/{Guid.NewGuid():N}/";
            var          variables       = new CalamariVariables();

            propertiesList.ForEach(properties =>
            {
                switch (properties)
                {
                case S3MultiFileSelectionProperties multiFileSelectionProperties:
                    multiFileSelectionProperties.BucketKeyPrefix = bucketKeyPrefix;
                    variables.Set(AwsSpecialVariables.S3.FileSelections, JsonConvert.SerializeObject(propertiesList, GetEnrichedSerializerSettings()));
                    break;

                case S3SingleFileSelectionProperties singleFileSelectionProperties:
                    singleFileSelectionProperties.BucketKeyPrefix = bucketKeyPrefix;
                    variables.Set(AwsSpecialVariables.S3.FileSelections, JsonConvert.SerializeObject(propertiesList, GetEnrichedSerializerSettings()));
                    break;

                case S3PackageOptions packageOptions:
                    packageOptions.BucketKeyPrefix = bucketKeyPrefix;
                    variables.Set(AwsSpecialVariables.S3.PackageOptions, JsonConvert.SerializeObject(packageOptions, GetEnrichedSerializerSettings()));
                    variables.Set(PackageVariables.PackageId, packageName);
                    variables.Set(PackageVariables.PackageVersion, packageVersion);
                    break;
                }
            });

            var variablesFile = Path.GetTempFileName();

            variables.Set("Octopus.Action.AwsAccount.Variable", "AWSAccount");
            variables.Set("AWSAccount.AccessKey", ExternalVariables.Get(ExternalVariable.AwsAcessKey));
            variables.Set("AWSAccount.SecretKey", ExternalVariables.Get(ExternalVariable.AwsSecretKey));
            variables.Set("Octopus.Action.Aws.Region", region);

            if (customVariables != null)
            {
                variables.Merge(customVariables);
            }

            variables.Save(variablesFile);

            var packageDirectory = TestEnvironment.GetTestPath("AWS", "S3", packageName);

            using (var package = new TemporaryFile(PackageBuilder.BuildSimpleZip(packageName, packageVersion, packageDirectory)))
                using (new TemporaryFile(variablesFile))
                {
                    var log        = new InMemoryLog();
                    var fileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem();

                    var command = new UploadAwsS3Command(
                        log,
                        variables,
                        fileSystem,
                        new SubstituteInFiles(log, fileSystem, new FileSubstituter(log, fileSystem), variables),
                        new ExtractPackage(new CombinedPackageExtractor(log, variables, new CommandLineRunner(log, variables)), fileSystem, variables, log),
                        new StructuredConfigVariablesService(new PrioritisedList <IFileFormatVariableReplacer>
                    {
                        new JsonFormatVariableReplacer(fileSystem, log),
                        new XmlFormatVariableReplacer(fileSystem, log),
                        new YamlFormatVariableReplacer(fileSystem, log),
                        new PropertiesFormatVariableReplacer(fileSystem, log),
                    }, variables, fileSystem, log)
                        );

                    var result = command.Execute(new[] {
                        "--package", $"{package.FilePath}",
                        "--variables", $"{variablesFile}",
                        "--bucket", bucketName,
                        "--targetMode", s3TargetMode.ToString()
                    });

                    result.Should().Be(0);
                }

            return(bucketKeyPrefix);
        }
        public async Task Setup()
        {
            var resourceManagementEndpointBaseUri =
                Environment.GetEnvironmentVariable(AccountVariables.ResourceManagementEndPoint) ??
                DefaultVariables.ResourceManagementEndpoint;
            var activeDirectoryEndpointBaseUri =
                Environment.GetEnvironmentVariable(AccountVariables.ActiveDirectoryEndPoint) ??
                DefaultVariables.ActiveDirectoryEndpoint;

            resourceGroupName = Guid.NewGuid().ToString();

            clientId       = ExternalVariables.Get(ExternalVariable.AzureSubscriptionClientId);
            clientSecret   = ExternalVariables.Get(ExternalVariable.AzureSubscriptionPassword);
            tenantId       = ExternalVariables.Get(ExternalVariable.AzureSubscriptionTenantId);
            subscriptionId = ExternalVariables.Get(ExternalVariable.AzureSubscriptionId);

            var resourceGroupLocation = Environment.GetEnvironmentVariable("AZURE_NEW_RESOURCE_REGION") ?? "eastus";

            authToken = await Auth.GetAuthTokenAsync(activeDirectoryEndpointBaseUri,
                                                     resourceManagementEndpointBaseUri, tenantId, clientId, clientSecret);

            var resourcesClient = new ResourcesManagementClient(subscriptionId,
                                                                new ClientSecretCredential(tenantId, clientId, clientSecret));

            resourceGroupClient = resourcesClient.ResourceGroups;

            var resourceGroup = new ResourceGroup(resourceGroupLocation);

            resourceGroup = await resourceGroupClient.CreateOrUpdateAsync(resourceGroupName, resourceGroup);

            webMgmtClient = new WebSiteManagementClient(new TokenCredentials(authToken))
            {
                SubscriptionId = subscriptionId,
                HttpClient     = { BaseAddress = new Uri(DefaultVariables.ResourceManagementEndpoint) },
            };

            var svcPlan = await webMgmtClient.AppServicePlans.BeginCreateOrUpdateAsync(resourceGroup.Name, resourceGroup.Name,
                                                                                       new AppServicePlan(resourceGroup.Location));

            site = await webMgmtClient.WebApps.BeginCreateOrUpdateAsync(resourceGroup.Name, resourceGroup.Name,
                                                                        new Site(resourceGroup.Location) { ServerFarmId = svcPlan.Id });

            existingSettings = new StringDictionary
            {
                Properties = new Dictionary <string, string> {
                    { "ExistingSetting", "Foo" }, { "ReplaceSetting", "Foo" }
                }
            };

            existingConnectionStrings = new ConnectionStringDictionary
            {
                Properties = new Dictionary <string, ConnStringValueTypePair>
                {
                    {
                        "ExistingConnectionString",
                        new ConnStringValueTypePair("ConnectionStringValue", ConnectionStringType.SQLAzure)
                    },
                    {
                        "ReplaceConnectionString",
                        new ConnStringValueTypePair("originalConnectionStringValue", ConnectionStringType.Custom)
                    }
                }
            };

            await webMgmtClient.WebApps.UpdateConnectionStringsAsync(resourceGroupName, site.Name, existingConnectionStrings);

            webappName = site.Name;
        }
Exemplo n.º 30
0
        int Inner(BinaryTarget targ)
        {
            try
            {
                lock (_scanLock)
                {
                    if (LoadedRuleCount() == 0)
                    {
                        if (rgatState.NetworkBridge.Connected && rgatState.NetworkBridge.GUIMode is false)
                        {
                            JObject statusObj = new JObject {
                                { "TargetSHA1", targ.GetSHA1Hash() },
                                { "Type", "YARA" },
                                { "Loaded", 0 },
                                { "State", eYaraScanProgress.eNotStarted.ToString() }
                            };
                            rgatState.NetworkBridge.SendAsyncData("SigStatus", statusObj);
                        }
                        return(0);
                    }

                    byte[] fileContentsBuf;
                    try
                    {
                        if (targ.PEFileObj is not null)
                        {
                            fileContentsBuf = targ.PEFileObj.RawFile.ToArray();
                        }
                        else
                        {
                            fileContentsBuf = File.ReadAllBytes(targ.FilePath);
                        }
                    }
                    catch (Exception e)
                    {
                        Logging.RecordException($"Failed to get file bytes to YARA scan: {e.Message}", e);
                        return(0);
                    }

                    if (rgatState.NetworkBridge.Connected && rgatState.NetworkBridge.GUIMode is false)
                    {
                        JObject statusObj = new JObject {
                            { "TargetSHA1", targ.GetSHA1Hash() },
                            { "Type", "YARA" },
                            { "Loaded", LoadedRuleCount() },
                            { "State", eYaraScanProgress.eRunning.ToString() }
                        };
                        rgatState.NetworkBridge.SendAsyncData("SigStatus", statusObj);
                    }
                    targetScanProgress[targ] = eYaraScanProgress.eRunning;

                    // Initialize the scanner
                    var scanner = new dnYara.CustomScanner(loadedRules);

                    ExternalVariables externalVariables = new ExternalVariables();
                    externalVariables.StringVariables.Add("filename", targ.FileName);
                    try
                    {
                        //bugs here tend to happen in unmanaged code, so this try-catch is unlikely to help
                        List <ScanResult> scanResults = scanner.ScanMemory(ref fileContentsBuf, externalVariables);
                        foreach (ScanResult sighit in scanResults)
                        {
                            targ.AddYaraSignatureHit(sighit);
                        }
                    }
                    catch (Exception e)
                    {
                        Logging.RecordException($"Yara ScanMemory exception: {e.Message}", e);
                    }
                    finally
                    {
                        scanner.Release();
                    }

                    if (rgatState.NetworkBridge.Connected && rgatState.NetworkBridge.GUIMode is false)
                    {
                        JObject statusObj = new JObject {
                            { "TargetSHA1", targ.GetSHA1Hash() },
                            { "Type", "YARA" },
                            { "Loaded", LoadedRuleCount() },
                            { "State", eYaraScanProgress.eComplete.ToString() }
                        };
                        rgatState.NetworkBridge.SendAsyncData("SigStatus", statusObj);
                    }
                }
            }
            catch (dnYara.Exceptions.YaraException e)
            {
                Logging.RecordException("YARA Scan failed with exeption: " + e.Message, e);
                targetScanProgress[targ] = eYaraScanProgress.eFailed;
            }
            catch (Exception e)
            {
                Logging.RecordException("YARA Scan failed with exeption: " + e.Message, e);
                targetScanProgress[targ] = eYaraScanProgress.eFailed;
            }


            targetScanProgress[targ] = eYaraScanProgress.eComplete;
            return(0);
        }