public ArrayList<SCM> getSCMs() { return cruiseConfig().getSCMs(); }
private SCMs getSCMs() { return this.cachedGoConfig.currentConfig().getSCMs(); }
@Override public void update(CruiseConfig modifiedConfig) { SCMs scms = modifiedConfig.getSCMs(); scms.add(globalScmConfig); modifiedConfig.setSCMs(scms); } }
@Override public SCM findScmById(String scmID) { return getCruiseConfig().getSCMs().find(scmID); }
private SCM findSCM(CruiseConfig modifiedConfig) { SCMs scms = modifiedConfig.getSCMs(); SCM existingSCM = scms.find(globalScmConfig.getSCMId()); if (existingSCM == null) { result.notFound(LocalizedMessage.resourceNotFound("SCM", globalScmConfig.getSCMId()), HealthStateType.notFound()); throw new NullPointerException(String.format("The pluggable scm material with id '%s' is not found.", globalScmConfig.getSCMId())); } else { return existingSCM; } }
@Override public boolean isValid(CruiseConfig preprocessedConfig) { SCMs scms = preprocessedConfig.getSCMs(); preprocessedGlobalScmConfig = scms.find(globalScmConfig.getSCMId()); preprocessedGlobalScmConfig.validate(null); if (preprocessedGlobalScmConfig.getAllErrors().isEmpty()) { scms.validate(null); boolean isValid = pluggableScmService.isValid(preprocessedGlobalScmConfig); BasicCruiseConfig.copyErrors(preprocessedGlobalScmConfig, globalScmConfig); return preprocessedGlobalScmConfig.getAllErrors().isEmpty() && scms.errors().isEmpty() && isValid; } BasicCruiseConfig.copyErrors(preprocessedGlobalScmConfig, globalScmConfig); return false; }
@Test public void shouldGenerateSCMIdWhenMissing() throws Exception { String xml = "<cruise schemaVersion='" + GoConstants.CONFIG_SCHEMA_VERSION + "'><scms>\n" + SCM_WITH_MISSING_ID + " </scms></cruise>"; GoConfigHolder configHolder = xmlLoader.loadConfigHolder(xml); assertThat(configHolder.config.getSCMs().get(0).getId(), is(notNullValue())); }
public static PluggableSCMMaterialConfig fromJSON(JsonReader jsonReader, ConfigHelperOptions options) { PluggableSCMMaterialConfig pluggableSCMMaterialConfig = new PluggableSCMMaterialConfig(); CruiseConfig cruiseConfig = options.getCruiseConfig(); if (cruiseConfig != null) { String ref = jsonReader.getString("ref"); pluggableSCMMaterialConfig.setSCMConfig(cruiseConfig.getSCMs().find(ref)); pluggableSCMMaterialConfig.setScmId(ref); } jsonReader.readStringIfPresent("destination", pluggableSCMMaterialConfig::setFolder); jsonReader.optJsonObject("filter").ifPresent(filterReader -> { pluggableSCMMaterialConfig.setFilter(FilterRepresenter.fromJSON(filterReader)); }); return pluggableSCMMaterialConfig; } }
public static PluggableSCMMaterialConfig fromJSON(JsonReader jsonReader, ConfigHelperOptions options) { PluggableSCMMaterialConfig pluggableSCMMaterialConfig = new PluggableSCMMaterialConfig(); CruiseConfig cruiseConfig = options.getCruiseConfig(); if (cruiseConfig != null) { String ref = jsonReader.getString("ref"); pluggableSCMMaterialConfig.setSCMConfig(cruiseConfig.getSCMs().find(ref)); pluggableSCMMaterialConfig.setScmId(ref); } jsonReader.readStringIfPresent("destination", pluggableSCMMaterialConfig::setFolder); jsonReader.optJsonObject("filter").ifPresent(filterReader -> { pluggableSCMMaterialConfig.setFilter(FilterRepresenter.fromJSON(filterReader)); }); return pluggableSCMMaterialConfig; } }
@Test public void shouldLoadAutoUpdateValueForSCMWhenLoadedFromConfigFile() throws Exception { String configTemplate = "<cruise schemaVersion='" + GoConstants.CONFIG_SCHEMA_VERSION + "'>" + "<scms>" + " <scm id='2ef830d7-dd66-42d6-b393-64a84646e557' name='scm-name' autoUpdate='%s' >" + " <pluginConfiguration id='yum' version='1' />" + " <configuration>" + " <property>" + " <key>SCM_URL</key>" + " <value>http://fake-scm/git/go-cd</value>" + " </property>" + " </configuration>" + " </scm>" + "</scms>" + "</cruise>"; String configContent = String.format(configTemplate, false); GoConfigHolder holder = xmlLoader.loadConfigHolder(configContent); SCM scm = holder.config.getSCMs().find("2ef830d7-dd66-42d6-b393-64a84646e557"); assertThat(scm.isAutoUpdate(), is(false)); configContent = String.format(configTemplate, true); holder = xmlLoader.loadConfigHolder(configContent); scm = holder.config.getSCMs().find("2ef830d7-dd66-42d6-b393-64a84646e557"); assertThat(scm.isAutoUpdate(), is(true)); }
@Test public void shouldWriteSCMConfigurationWhenNoSCMIdIsProvided() throws Exception { CruiseConfig configToSave = new BasicCruiseConfig(); SCM scm = new SCM(); scm.setName("name"); scm.setPluginConfiguration(new PluginConfiguration("plugin-id", "1.0")); scm.setConfiguration(new Configuration(getConfigurationProperty("url", false, "http://go"), getConfigurationProperty("secure", true, "secure"))); configToSave.getSCMs().add(scm); xmlWriter.write(configToSave, output, false); GoConfigHolder goConfigHolder = xmlLoader.loadConfigHolder(output.toString()); SCMs scms = goConfigHolder.config.getSCMs(); assertThat(scms.size(), is(configToSave.getSCMs().size())); assertThat(scms.get(0).getId(), is(notNullValue())); }
@Test public void shouldDecideIfPluggableSCMMaterialCanBeDeleted_BasedOnPluggableSCMMaterialBeingUsedByPipelines() throws Exception { SCM scmConfigOne = SCMMother.create("scm-id-1"); SCM scmConfigTwo = SCMMother.create("scm-id-2"); cruiseConfig.getSCMs().addAll(Arrays.asList(scmConfigOne, scmConfigTwo)); PipelineConfig pipeline = PipelineConfigMother.pipelineConfig("pipeline"); pipeline.addMaterialConfig(new PluggableSCMMaterialConfig(null, scmConfigOne, null, null)); cruiseConfig.addPipeline("existing_group", pipeline); assertThat(cruiseConfig.canDeletePluggableSCMMaterial(scmConfigOne), is(false)); assertThat(cruiseConfig.canDeletePluggableSCMMaterial(scmConfigTwo), is(true)); }
@Test public void shouldMigrateEncryptedPluginPropertyValueWithNewlineAndSpaces_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 = configWithPluggableScm( "<scm id=\"f7c309f5-ea4d-41c5-9c43-95d79fa9ec7b\" name=\"gocd-private\">\n" + " <pluginConfiguration id=\"github.pr\" version=\"1\" />\n" + " <configuration>\n" + " <property>\n" + " <key>plainTextKey</key>\n" + " <value>https://url/some_path</value>\n" + " </property>\n" + " <property>\n" + " <key>secureKey</key>\n" + " <encryptedValue>" + encryptedValueWithWhitespaceAndNewline + "</encryptedValue>\n" + " </property>\n" + " </configuration>\n" + " </scm>", 88); CruiseConfig config = ConfigMigrator.loadWithMigration(content).config; assertThat(config.getSCMs().get(0).getConfiguration().getProperty("secureKey").getValue(), is(plainText)); assertThat(config.getSCMs().get(0).getConfiguration().getProperty("secureKey").getEncryptedValue(), startsWith("AES:")); assertThat(config.getSCMs().get(0).getConfiguration().getProperty("plainTextKey").getValue(), is("https://url/some_path")); }
@Test public void shouldWriteSCMConfiguration() throws Exception { CruiseConfig configToSave = new BasicCruiseConfig(); SCM scm = new SCM(); scm.setId("id"); scm.setName("name"); scm.setPluginConfiguration(new PluginConfiguration("plugin-id", "1.0")); scm.setConfiguration(new Configuration(getConfigurationProperty("url", false, "http://go"), getConfigurationProperty("secure", true, "secure"))); configToSave.getSCMs().add(scm); xmlWriter.write(configToSave, output, false); GoConfigHolder goConfigHolder = xmlLoader.loadConfigHolder(output.toString()); SCMs scms = goConfigHolder.config.getSCMs(); assertThat(scms, is(configToSave.getSCMs())); assertThat(scms.get(0).getConfiguration().first().getConfigurationValue().getValue(), is("http://go")); assertThat(scms.get(0).getConfiguration().first().getEncryptedConfigurationValue(), is(nullValue())); assertThat(scms.get(0).getConfiguration().last().getEncryptedValue(), is(new GoCipher().encrypt("secure"))); assertThat(scms.get(0).getConfiguration().last().getConfigurationValue(), is(nullValue())); }
PipelineConfig pipelineConfig = goConfigHolder.config.pipelineConfigByName(new CaseInsensitiveString("new_name")); PluggableSCMMaterialConfig pluggableSCMMaterialConfig = (PluggableSCMMaterialConfig) pipelineConfig.materialConfigs().get(0); assertThat(pluggableSCMMaterialConfig.getSCMConfig(), is(goConfigHolder.config.getSCMs().get(0))); assertThat(pluggableSCMMaterialConfig.getFolder(), is(nullValue())); assertThat(pluggableSCMMaterialConfig.filter(), is(new Filter()));
@Test public void shouldMigrateDESEncryptedPluginPropertyValue_XslMigrationFrom108To109() throws Exception { resetCipher.setupDESCipherFile(); String clearText = "user-password!"; // "user-password!" encrypted using the above key String desEncryptedPassword = "mvcX9yrQsM4iPgm1tDxN1A=="; String content = configWithPluggableScm( "" + " <scm id='f7c309f5-ea4d-41c5-9c43-95d79fa9ec7b' name='gocd-private'>" + " <pluginConfiguration id='github.pr' version='1' />" + " <configuration>" + " <property>" + " <key>plainTextKey</key>" + " <value>https://url/some_path</value>" + " </property>" + " <property>" + " <key>secureKey</key>" + " <encryptedValue>" + desEncryptedPassword + "</encryptedValue>" + " </property>" + " </configuration>" + " </scm>", 108); CruiseConfig config = ConfigMigrator.loadWithMigration(content).config; assertThat(config.getSCMs().get(0).getConfiguration().getProperty("secureKey").getValue(), is(clearText)); String encryptedValue = config.getSCMs().get(0).getConfiguration().getProperty("secureKey").getEncryptedValue(); assertThat(encryptedValue, startsWith("AES:")); assertThat(new AESEncrypter(new AESCipherProvider(systemEnvironment)).decrypt(encryptedValue), is("user-password!")); assertThat(config.getSCMs().get(0).getConfiguration().getProperty("plainTextKey").getValue(), is("https://url/some_path")); }
@Test public void shouldAllowSCMTypeMaterialForPipeline() throws Exception { CruiseConfig configToSave = new BasicCruiseConfig(); SCM scm = new SCM(); String scmId = "scm-id"; scm.setId(scmId); scm.setName("name"); scm.setPluginConfiguration(new PluginConfiguration("plugin-id", "1.0")); scm.setConfiguration(new Configuration(getConfigurationProperty("url", false, "http://go"))); configToSave.getSCMs().add(scm); PluggableSCMMaterialConfig pluggableSCMMaterialConfig = new PluggableSCMMaterialConfig(scmId); pluggableSCMMaterialConfig.setSCMConfig(scm); configToSave.addPipeline("default", PipelineConfigMother.pipelineConfig("test", new MaterialConfigs(pluggableSCMMaterialConfig), new JobConfigs(new JobConfig("ls")))); xmlWriter.write(configToSave, output, false); GoConfigHolder goConfigHolder = xmlLoader.loadConfigHolder(output.toString()); PipelineConfig pipelineConfig = goConfigHolder.config.pipelineConfigByName(new CaseInsensitiveString("test")); MaterialConfig materialConfig = pipelineConfig.materialConfigs().get(0); assertThat(materialConfig instanceof PluggableSCMMaterialConfig, is(true)); assertThat(((PluggableSCMMaterialConfig) materialConfig).getScmId(), is(scmId)); assertThat(((PluggableSCMMaterialConfig) materialConfig).getSCMConfig(), is(scm)); assertThat(materialConfig.getFolder(), is(nullValue())); assertThat(materialConfig.filter(), is(new Filter())); }
PipelineConfig pipelineConfig = goConfigHolder.config.pipelineConfigByName(new CaseInsensitiveString("new_name")); PluggableSCMMaterialConfig pluggableSCMMaterialConfig = (PluggableSCMMaterialConfig) pipelineConfig.materialConfigs().get(0); assertThat(pluggableSCMMaterialConfig.getSCMConfig(), is(goConfigHolder.config.getSCMs().get(0))); assertThat(pluggableSCMMaterialConfig.getFolder(), is("dest")); assertThat(pluggableSCMMaterialConfig.filter(), is(new Filter(new IgnoredFiles("x"), new IgnoredFiles("y"))));
@Test public void shouldFailValidationIfSCMTypeMaterialForPipelineHasARefToNonExistentSCM() throws Exception { CruiseConfig configToSave = new BasicCruiseConfig(); String scmId = "does-not-exist"; PluggableSCMMaterialConfig pluggableSCMMaterialConfig = new PluggableSCMMaterialConfig(scmId); SCM scm = SCMMother.create("scm-id", "scm-name", "pluginid", "1.0", new Configuration(ConfigurationPropertyMother.create("k1", false, "v1"))); configToSave.getSCMs().add(scm); configToSave.addPipeline("default", PipelineConfigMother.pipelineConfig("test", new MaterialConfigs(pluggableSCMMaterialConfig), new JobConfigs(new JobConfig("ls")))); try { xmlWriter.write(configToSave, output, false); fail("should not allow this"); } catch (XsdValidationException exception) { assertThat(exception.getMessage(), is("Key 'scmIdReferredByMaterial' with value 'does-not-exist' not found for identity constraint of element 'cruise'.")); } }
@Test public void shouldAllowFolderAndFilterForPluggableSCMMaterialForPipeline() throws Exception { CruiseConfig configToSave = new BasicCruiseConfig(); SCM scm = new SCM(); String scmId = "scm-id"; scm.setId(scmId); scm.setName("name"); scm.setPluginConfiguration(new PluginConfiguration("plugin-id", "1.0")); scm.setConfiguration(new Configuration(getConfigurationProperty("url", false, "http://go"))); configToSave.getSCMs().add(scm); PluggableSCMMaterialConfig pluggableSCMMaterialConfig = new PluggableSCMMaterialConfig(scmId); pluggableSCMMaterialConfig.setSCMConfig(scm); pluggableSCMMaterialConfig.setFolder("dest"); pluggableSCMMaterialConfig.setFilter(new Filter(new IgnoredFiles("x"), new IgnoredFiles("y"))); configToSave.addPipeline("default", PipelineConfigMother.pipelineConfig("test", new MaterialConfigs(pluggableSCMMaterialConfig), new JobConfigs(new JobConfig("ls")))); xmlWriter.write(configToSave, output, false); GoConfigHolder goConfigHolder = xmlLoader.loadConfigHolder(output.toString()); PipelineConfig pipelineConfig = goConfigHolder.config.pipelineConfigByName(new CaseInsensitiveString("test")); MaterialConfig materialConfig = pipelineConfig.materialConfigs().get(0); assertThat(materialConfig instanceof PluggableSCMMaterialConfig, is(true)); assertThat(((PluggableSCMMaterialConfig) materialConfig).getScmId(), is(scmId)); assertThat(((PluggableSCMMaterialConfig) materialConfig).getSCMConfig(), is(scm)); assertThat(materialConfig.getFolder(), is("dest")); assertThat(materialConfig.filter(), is(new Filter(new IgnoredFiles("x"), new IgnoredFiles("y")))); }