@Test public void shouldMigrateEncryptedEnvironmentVariablesWithNewlineAndSpaces_XslMigrationFrom88To90() throws Exception { resetCipher.setupDESCipherFile(); String plainText = "user-password!"; // "user-password!" encrypted using the above key String encryptedValue = "mvcX9yrQsM4iPgm1tDxN1A=="; String encryptedValueWithWhitespaceAndNewline = new StringBuilder(encryptedValue).insert(2, "\r\n" + " ").toString(); String content = configWithPipeline( "<pipeline name='some_pipeline'>" + "<environmentvariables>\n" + " <variable name=\"var_name\" secure=\"true\"><encryptedValue>" + encryptedValueWithWhitespaceAndNewline + "</encryptedValue></variable>\n" + " </environmentvariables>" + " <materials>" + " <svn url='svnurl'/>" + " </materials>" + " <stage name='some_stage'>" + " <jobs>" + " <job name='some_job'>" + " </job>" + " </jobs>" + " </stage>" + "</pipeline>", 88); CruiseConfig config = ConfigMigrator.loadWithMigration(content).config; assertThat(config.allPipelines().get(0).getVariables().get(0).getValue(), is(plainText)); assertThat(config.allPipelines().get(0).getVariables().get(0).getEncryptedValue(), startsWith("AES:")); }
@Test public void shouldMigrateEmptyEnvironmentVariable() throws Exception { String content = configWithPipeline( "<pipeline name='some_pipeline'>" + "<environmentvariables>\n" + " <variable name=\"var_name\" />\n" + " </environmentvariables>" + " <materials>" + " <svn url='svnurl'/>" + " </materials>" + " <stage name='some_stage'>" + " <jobs>" + " <job name='some_job'>" + " </job>" + " </jobs>" + " </stage>" + "</pipeline>", 48); CruiseConfig config = ConfigMigrator.loadWithMigration(content).config; PipelineConfig pipelineConfig = config.pipelineConfigByName(new CaseInsensitiveString("some_pipeline")); EnvironmentVariablesConfig variables = pipelineConfig.getVariables(); assertThat(variables.size(), is(1)); EnvironmentVariableConfig environmentVariableConfig = variables.get(0); assertThat(environmentVariableConfig.getName(), is("var_name")); assertThat(environmentVariableConfig.getValue().isEmpty(), is(true)); }
@Test public void shouldAllowAnEnvironmentVariableToBeMarkedAsSecure_WithEncryptedValueInItsOwnTag() throws Exception { String value = "abc"; String encryptedValue = new GoCipher().encrypt(value); String content = configWithPipeline(format( "<pipeline name='some_pipeline'>" + "<environmentvariables>\n" + " <variable name=\"var_name\" secure=\"true\"><encryptedValue>%s</encryptedValue></variable>\n" + " </environmentvariables>" + " <materials>" + " <svn url='svnurl'/>" + " </materials>" + " <stage name='some_stage'>" + " <jobs>" + " <job name='some_job'>" + " </job>" + " </jobs>" + " </stage>" + "</pipeline>", encryptedValue), CONFIG_SCHEMA_VERSION); CruiseConfig config = ConfigMigrator.loadWithMigration(content).config; PipelineConfig pipelineConfig = config.pipelineConfigByName(new CaseInsensitiveString("some_pipeline")); EnvironmentVariablesConfig variables = pipelineConfig.getVariables(); assertThat(variables.size(), is(1)); EnvironmentVariableConfig environmentVariableConfig = variables.get(0); assertThat(environmentVariableConfig.getEncryptedValue(), is(encryptedValue)); assertThat(environmentVariableConfig.isSecure(), is(true)); }
@Test public void shouldMigrateDESEncryptedEnvironmentVariables_XslMigrationFrom108To109() throws Exception { resetCipher.setupDESCipherFile(); String clearText = "user-password!"; // "user-password!" encrypted using the above key String desEncryptedPassword = "mvcX9yrQsM4iPgm1tDxN1A=="; String content = configWithPipeline( "" + "<pipeline name='some_pipeline'>" + " <environmentvariables>" + " <variable name='var_name' secure='true'>" + " <encryptedValue>" + desEncryptedPassword + "</encryptedValue>" + " </variable>" + " </environmentvariables>" + " <materials>" + " <svn url='svnurl'/>" + " </materials>" + " <stage name='some_stage'>" + " <jobs>" + " <job name='some_job'>" + " </job>" + " </jobs>" + " </stage>" + "</pipeline>", 108); CruiseConfig config = ConfigMigrator.loadWithMigration(content).config; assertThat(config.allPipelines().get(0).getVariables().get(0).getValue(), is(clearText)); String encryptedValue = config.allPipelines().get(0).getVariables().get(0).getEncryptedValue(); assertThat(encryptedValue, startsWith("AES:")); assertThat(new AESEncrypter(new AESCipherProvider(systemEnvironment)).decrypt(encryptedValue), is("user-password!")); }
@Test public void shouldAllowAnEnvironmentVariableToBeMarkedAsSecure_WithValueInItsOwnTag() throws Exception { String cipherText = new GoCipher().encrypt("plainText"); String content = configWithPipeline( "<pipeline name='some_pipeline'>" + "<environmentvariables>\n" + " <variable name=\"var_name\" secure=\"true\"><encryptedValue>" + cipherText + "</encryptedValue></variable>\n" + " </environmentvariables>" + " <materials>" + " <svn url='svnurl'/>" + " </materials>" + " <stage name='some_stage'>" + " <jobs>" + " <job name='some_job'>" + " </job>" + " </jobs>" + " </stage>" + "</pipeline>", CONFIG_SCHEMA_VERSION); CruiseConfig config = ConfigMigrator.loadWithMigration(content).config; PipelineConfig pipelineConfig = config.pipelineConfigByName(new CaseInsensitiveString("some_pipeline")); EnvironmentVariablesConfig variables = pipelineConfig.getVariables(); assertThat(variables.size(), is(1)); EnvironmentVariableConfig environmentVariableConfig = variables.get(0); assertThat(environmentVariableConfig.getEncryptedValue(), is(cipherText)); assertThat(environmentVariableConfig.isSecure(), is(true)); }
@Test public void shouldCopyErrorsForFieldsOnPipelineConfig(){ PipelineConfig pipelineConfig = PipelineConfigMother.pipelineConfig("pipeline", MaterialConfigsMother.defaultMaterialConfigs(), new JobConfigs(JobConfigMother.createJobConfigWithJobNameAndEmptyResources())); pipelineConfig.setVariables(new EnvironmentVariablesConfig(asList(new EnvironmentVariableConfig("name", "value")))); PipelineConfig pipelineWithErrors = new Cloner().deepClone(pipelineConfig); pipelineWithErrors.getVariables().get(0).addError("name", "error on environment variable"); pipelineWithErrors.first().addError("name", "error on stage"); pipelineWithErrors.first().getJobs().first().addError("name", "error on job"); BasicCruiseConfig.copyErrors(pipelineWithErrors, pipelineConfig); assertThat(pipelineConfig.getVariables().get(0).errors().on("name"), is("error on environment variable")); assertThat(pipelineConfig.first().errors().on("name"), is("error on stage")); assertThat(pipelineConfig.first().getJobs().first().errors().on("name"), is("error on job")); }
@Test public void shouldValidateTree() { PipelineConfig pipeline = new PipelineConfig(); pipeline.setName("pipeline"); pipeline.addEnvironmentVariable("", ""); pipeline.addParam(new ParamConfig("", "")); pipeline.setMaterialConfigs(new MaterialConfigs(MaterialConfigsMother.gitMaterialConfig(), MaterialConfigsMother.svnMaterialConfig())); StageConfig stage1 = getStageConfig("stage1", "s1j1"); StageConfig stage2 = getStageConfig("stage2", "s2j1"); pipeline.getStages().add(stage1); pipeline.getStages().add(stage2); BasicCruiseConfig cruiseConfig = new BasicCruiseConfig(new BasicPipelineConfigs("group", new Authorization(), pipeline)); boolean isValid = pipeline.validateTree(PipelineConfigSaveValidationContext.forChain(true, cruiseConfig.getGroups().first().getGroup(), cruiseConfig, pipeline)); assertThat(isValid, is(false)); assertThat(pipeline.getVariables().get(0).errors().firstError(), is("Environment Variable cannot have an empty name for pipeline 'pipeline'.")); assertThat(pipeline.getParams().get(0).errors().firstError(), is("Parameter cannot have an empty name for pipeline 'pipeline'.")); assertThat(pipeline.materialConfigs().errors().isEmpty(), is(true)); assertThat(pipeline.materialConfigs().get(0).errors().isEmpty(), is(true)); assertThat(pipeline.materialConfigs().get(1).errors().isEmpty(), is(true)); assertThat(pipeline.errors().getAll().isEmpty(), is(true)); }