public void clearEmptyConfigurations() { List<ConfigurationProperty> propertiesToRemove = new ArrayList<>(); for (ConfigurationProperty configurationProperty : this) { ConfigurationValue configurationValue = configurationProperty.getConfigurationValue(); EncryptedConfigurationValue encryptedValue = configurationProperty.getEncryptedConfigurationValue(); if (StringUtils.isBlank(configurationProperty.getValue()) && (configurationValue == null || configurationValue.errors().isEmpty()) && (encryptedValue == null || encryptedValue.errors().isEmpty())) { propertiesToRemove.add(configurationProperty); } } this.removeAll(propertiesToRemove); }
public boolean doesNotHaveErrorsAgainstConfigurationValue() { if (isSecure()) { List<String> errorsOnValue = getEncryptedConfigurationValue().errors().getAllOn("value"); return errorsOnValue == null || errorsOnValue.isEmpty(); } else { List<String> errorsOnValue = getConfigurationValue().errors().getAllOn("value"); return errorsOnValue == null || errorsOnValue.isEmpty(); } }
public void addErrorAgainstConfigurationValue(String message) { if (isSecure()) { getEncryptedConfigurationValue().errors().add("value", message); } else { getConfigurationValue().errors().add("value", message); } }
@Test public void shouldSetConfigAttributesWhenMetadataIsNotPassedInMap() { ConfigurationProperty configurationProperty = new ConfigurationProperty(); HashMap attributes = new HashMap(); HashMap keyMap = new HashMap(); keyMap.put("name", "fooKey"); attributes.put(ConfigurationProperty.CONFIGURATION_KEY, keyMap); HashMap valueMap = new HashMap(); valueMap.put("value", "fooValue"); attributes.put(ConfigurationProperty.CONFIGURATION_VALUE, valueMap); configurationProperty.setConfigAttributes(attributes,null); assertThat(configurationProperty.getConfigurationKey().getName(), is("fooKey")); assertThat(configurationProperty.getConfigurationValue().getValue(), is("fooValue")); assertThat(configurationProperty.getEncryptedConfigurationValue(), is(nullValue())); }
@Test public void shouldInitializeConfigValueToBlankWhenBothValueAndEncryptedValueIsNull() { ConfigurationProperty configurationProperty = new ConfigurationProperty(new ConfigurationKey("key"), (ConfigurationValue) null); configurationProperty.initialize(); assertThat(configurationProperty.getConfigurationKey().getName(), is("key")); assertThat(configurationProperty.getConfigurationValue(), is(notNullValue())); assertThat(configurationProperty.getConfigurationValue().getValue(), is("")); assertThat(configurationProperty.getEncryptedConfigurationValue(), is(nullValue())); Method initializeMethod = ReflectionUtils.findMethod(ConfigurationProperty.class, "initialize"); assertThat(initializeMethod.getAnnotation(PostConstruct.class), is(notNullValue())); }
@Test public void shouldNotClearEncryptedValueWhenWhenNewValueNotProvided() { GoCipher goCipher = mock(GoCipher.class); ConfigurationProperty property = new ConfigurationProperty(new ConfigurationKey("secureKey"), null, new EncryptedConfigurationValue("secureValue"), goCipher); property.handleSecureValueConfiguration(true); assertThat(property.isSecure(), is(true)); assertThat(property.getConfigurationKey().getName(), is("secureKey")); assertThat(property.getConfigurationValue(), is(nullValue())); assertThat(property.getEncryptedConfigurationValue(), is(notNullValue())); assertThat(property.getEncryptedValue(), is("secureValue")); }
@Test public void shouldNotUpdateSecurePropertyWhenPluginIsMissing() { GoCipher goCipher = new GoCipher(); ConfigurationProperty secureProperty = new ConfigurationProperty(new ConfigurationKey("key1"), null, new EncryptedConfigurationValue("value"), goCipher); ConfigurationProperty nonSecureProperty = new ConfigurationProperty(new ConfigurationKey("key2"), new ConfigurationValue("value1"), null, goCipher); SCM scm = SCMMother.create("scm-id", "scm-name", "plugin-id", "version", new Configuration(secureProperty, nonSecureProperty)); scm.applyPluginMetadata(); assertThat(secureProperty.getEncryptedConfigurationValue(), is(notNullValue())); assertThat(secureProperty.getConfigurationValue(), is(nullValue())); assertThat(nonSecureProperty.getConfigurationValue(), is(notNullValue())); assertThat(nonSecureProperty.getEncryptedConfigurationValue(), is(nullValue())); }
@Test public void shouldNotEncryptWhenWhenConstructedAsNotSecure() { GoCipher goCipher = mock(GoCipher.class); ConfigurationProperty property = new ConfigurationProperty(new ConfigurationKey("secureKey"), new ConfigurationValue("secureValue"), null, goCipher); property.handleSecureValueConfiguration(false); assertThat(property.isSecure(), is(false)); assertThat(property.getConfigurationKey().getName(), is("secureKey")); assertThat(property.getConfigurationValue().getValue(), is("secureValue")); assertThat(property.getEncryptedConfigurationValue(), is(nullValue())); }
@Test public void shouldNotUpdateSecurePropertyWhenPluginIsMissing() { GoCipher goCipher = new GoCipher(); ConfigurationProperty secureProperty = new ConfigurationProperty(new ConfigurationKey("key1"), null, new EncryptedConfigurationValue("value"), goCipher); ConfigurationProperty nonSecureProperty = new ConfigurationProperty(new ConfigurationKey("key2"), new ConfigurationValue("value2"), null, goCipher); PackageDefinition packageDefinition = new PackageDefinition("go", "name", new Configuration(secureProperty, nonSecureProperty)); ConfigurationProperty nonSecureRepoProperty = new ConfigurationProperty(new ConfigurationKey("key1"), new ConfigurationValue("value1"), null, goCipher); ConfigurationProperty secureRepoProperty = new ConfigurationProperty(new ConfigurationKey("key2"), null, new EncryptedConfigurationValue("value"), goCipher); PackageRepository packageRepository = createPackageRepository("plugin-id", "version", "id", "name", new Configuration(secureRepoProperty, nonSecureRepoProperty), new Packages(packageDefinition)); packageRepository.applyPackagePluginMetadata(); assertThat(secureProperty.getEncryptedConfigurationValue(), is(notNullValue())); assertThat(secureProperty.getConfigurationValue(), is(nullValue())); assertThat(nonSecureProperty.getConfigurationValue(), is(notNullValue())); assertThat(nonSecureProperty.getEncryptedConfigurationValue(), is(nullValue())); assertThat(secureRepoProperty.getEncryptedConfigurationValue(), is(notNullValue())); assertThat(secureRepoProperty.getConfigurationValue(), is(nullValue())); assertThat(nonSecureRepoProperty.getConfigurationValue(), is(notNullValue())); assertThat(nonSecureRepoProperty.getEncryptedConfigurationValue(), is(nullValue())); }
@Test public void shouldCreateWithValueForAUnsecuredProperty() { Property key = new Property("key"); key.with(Property.SECURE, false); ConfigurationProperty property = new ConfigurationPropertyBuilder().create("key", "value", null, false); assertThat(property.getConfigurationValue().getValue(), is("value")); assertNull(property.getEncryptedConfigurationValue()); } }
@Test public void shouldSetConfigAttributesAsAvailable() throws Exception { SCMConfigurations scmConfigurations = new SCMConfigurations(); scmConfigurations.add(new SCMConfiguration("url")); scmConfigurations.add(new SCMConfiguration("username")); scmConfigurations.add(new SCMConfiguration("password").with(SECURE, true)); SCMMetadataStore.getInstance().addMetadataFor("plugin-id", scmConfigurations, null); Map<String, String> attributeMap = DataStructureUtils.m(SCM.SCM_ID, "scm-id", SCM.NAME, "scm-name", SCM.AUTO_UPDATE, "false", "url", "http://localhost", "username", "user", "password", "pass"); SCM scm = new SCM(null, new PluginConfiguration("plugin-id", "1"), new Configuration()); scm.setConfigAttributes(attributeMap); assertThat(scm.getId(), is("scm-id")); assertThat(scm.getName(), is("scm-name")); assertThat(scm.isAutoUpdate(), is(false)); assertThat(scm.getPluginConfiguration().getId(), is("plugin-id")); assertThat(scm.getConfigAsMap().get("url").get(SCM.VALUE_KEY), is("http://localhost")); assertThat(scm.getConfigAsMap().get("username").get(SCM.VALUE_KEY), is("user")); assertThat(scm.getConfigAsMap().get("password").get(SCM.VALUE_KEY), is("pass")); assertThat(scm.getConfiguration().getProperty("password").getConfigurationValue(), is(nullValue())); assertThat(scm.getConfiguration().getProperty("password").getEncryptedConfigurationValue(), is(not(nullValue()))); }
@Test public void shouldCreatePropertyInAbsenceOfPlainAndEncryptedTextInputForSecureProperty() throws Exception { Property key = new Property("key"); key.with(Property.SECURE, true); ConfigurationProperty property = new ConfigurationPropertyBuilder().create("key", null, null, true); assertThat(property.errors().size(), is(0)); assertThat(property.getConfigKeyName(), is("key")); assertNull(property.getEncryptedConfigurationValue()); assertNull(property.getConfigurationValue()); }
@Test public void shouldHandleEncryptionOfConfigProperties() throws CryptoException { GoCipher goCipher = new GoCipher(); ArrayList<PluginConfiguration> pluginConfigurations = new ArrayList<>(); pluginConfigurations.add(new PluginConfiguration("key1", new Metadata(true, true))); pluginConfigurations.add(new PluginConfiguration("key2", new Metadata(true, false))); when(artifactPluginInfo.getArtifactConfigSettings()).thenReturn(new PluggableInstanceSettings(pluginConfigurations)); ConfigurationProperty secureProperty = new ConfigurationProperty(new ConfigurationKey("key1"), new ConfigurationValue("value1"), null, goCipher); ConfigurationProperty nonSecureProperty = new ConfigurationProperty(new ConfigurationKey("key2"), new ConfigurationValue("value2"), null, goCipher); PluggableArtifactConfig pluggableArtifactConfig = new PluggableArtifactConfig("id", "store-id", secureProperty, nonSecureProperty); BasicCruiseConfig cruiseConfig = GoConfigMother.defaultCruiseConfig(); cruiseConfig.getArtifactStores().add(new ArtifactStore("store-id", "cd.go.s3")); pluggableArtifactConfig.encryptSecureProperties(cruiseConfig, pluggableArtifactConfig); assertThat(secureProperty.isSecure(), is(true)); assertThat(secureProperty.getEncryptedConfigurationValue(), is(notNullValue())); assertThat(secureProperty.getEncryptedValue(), is(goCipher.encrypt("value1"))); assertThat(nonSecureProperty.isSecure(), is(false)); assertThat(nonSecureProperty.getValue(), is("value2")); }
@Test public void shouldHandleEncryptionOfConfigPropertiesIfStoreIdIsAValidParam() throws Exception { GoCipher goCipher = new GoCipher(); ArrayList<PluginConfiguration> pluginConfigurations = new ArrayList<>(); pluginConfigurations.add(new PluginConfiguration("key1", new Metadata(true, true))); pluginConfigurations.add(new PluginConfiguration("key2", new Metadata(true, false))); when(artifactPluginInfo.getArtifactConfigSettings()).thenReturn(new PluggableInstanceSettings(pluginConfigurations)); ConfigurationProperty secureProperty = new ConfigurationProperty(new ConfigurationKey("key1"), new ConfigurationValue("value1"), null, goCipher); ConfigurationProperty nonSecureProperty = new ConfigurationProperty(new ConfigurationKey("key2"), new ConfigurationValue("value2"), null, goCipher); PluggableArtifactConfig pluggableArtifactConfig = new PluggableArtifactConfig("id", "#{storeId}", secureProperty, nonSecureProperty); PluggableArtifactConfig preprocessedPluggableArtifactConfig = new PluggableArtifactConfig("id", "store-id", secureProperty, nonSecureProperty); BasicCruiseConfig cruiseConfig = GoConfigMother.defaultCruiseConfig(); cruiseConfig.getArtifactStores().add(new ArtifactStore("store-id", "cd.go.s3")); pluggableArtifactConfig.encryptSecureProperties(cruiseConfig, preprocessedPluggableArtifactConfig); assertThat(secureProperty.isSecure(), is(true)); assertThat(secureProperty.getEncryptedConfigurationValue(), is(notNullValue())); assertThat(secureProperty.getEncryptedValue(), is(goCipher.encrypt("value1"))); assertThat(nonSecureProperty.isSecure(), is(false)); assertThat(nonSecureProperty.getValue(), is("value2")); }
@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())); }
@Test public void shouldMakeConfigurationSecureBasedOnMetadata() throws Exception { GoCipher goCipher = new GoCipher(); //meta data of SCM SCMConfigurations scmConfiguration = new SCMConfigurations(); scmConfiguration.add(new SCMConfiguration("key1").with(SECURE, true)); scmConfiguration.add(new SCMConfiguration("key2").with(SECURE, false)); SCMMetadataStore.getInstance().addMetadataFor("plugin-id", scmConfiguration, null); /*secure property is set based on metadata*/ ConfigurationProperty secureProperty = new ConfigurationProperty(new ConfigurationKey("key1"), new ConfigurationValue("value1"), null, goCipher); ConfigurationProperty nonSecureProperty = new ConfigurationProperty(new ConfigurationKey("key2"), new ConfigurationValue("value2"), null, goCipher); SCM scm = SCMMother.create("scm-id", "scm-name", "plugin-id", "1.0", new Configuration(secureProperty, nonSecureProperty)); scm.applyPluginMetadata(); //assert SCM properties assertThat(secureProperty.isSecure(), is(true)); assertThat(secureProperty.getEncryptedConfigurationValue(), is(notNullValue())); assertThat(secureProperty.getEncryptedValue(), is(goCipher.encrypt("value1"))); assertThat(nonSecureProperty.isSecure(), is(false)); assertThat(nonSecureProperty.getValue(), is("value2")); }
@Test public void shouldSetConfigAttributes() throws Exception { PackageDefinition definition = new PackageDefinition(); String pluginId = "plugin"; Map config = createPackageDefinitionConfiguration("package-name", pluginId, new ConfigurationHolder("key1", "value1"), new ConfigurationHolder("key2", "value2", "encrypted-value", true, "1"), new ConfigurationHolder("key3", "test", "encrypted-value", true, "0")); PackageConfigurations metadata = new PackageConfigurations(); metadata.addConfiguration(new PackageConfiguration("key1")); metadata.addConfiguration(new PackageConfiguration("key2").with(PackageConfiguration.SECURE, true)); metadata.addConfiguration(new PackageConfiguration("key3").with(PackageConfiguration.SECURE, true)); PackageMetadataStore.getInstance().addMetadataFor(pluginId, metadata); definition.setRepository(PackageRepositoryMother.create("1")); definition.setConfigAttributes(config); String encryptedValue = new GoCipher().encrypt("value2"); assertThat(definition.getName(), is("package-name")); assertThat(definition.getConfiguration().size(), is(3)); assertThat(definition.getConfiguration().getProperty("key1").getConfigurationValue().getValue(), is("value1")); assertThat(definition.getConfiguration().getProperty("key1").getEncryptedConfigurationValue(), is(nullValue())); assertThat(definition.getConfiguration().getProperty("key2").getEncryptedValue(), is(encryptedValue)); assertThat(definition.getConfiguration().getProperty("key2").getConfigurationValue(), is(nullValue())); assertThat(definition.getConfiguration().getProperty("key3").getEncryptedValue(), is("encrypted-value")); assertThat(definition.getConfiguration().getProperty("key3").getConfigurationValue(), is(nullValue())); }
@Test public void shouldWriteRepositoryConfigurationWithPackages() throws Exception { CruiseConfig configToSave = new BasicCruiseConfig(); PackageRepository packageRepository = new PackageRepository(); packageRepository.setId("id"); packageRepository.setName("name"); packageRepository.setPluginConfiguration(new PluginConfiguration("plugin-id", "version")); packageRepository.setConfiguration(new Configuration(getConfigurationProperty("url", false, "http://go"), getConfigurationProperty("secure", true, "secure"))); PackageDefinition packageDefinition = new PackageDefinition("id", "name", new Configuration(getConfigurationProperty("name", false, "go-agent"))); packageDefinition.setRepository(packageRepository); packageRepository.getPackages().add(packageDefinition); configToSave.getPackageRepositories().add(packageRepository); xmlWriter.write(configToSave, output, false); GoConfigHolder goConfigHolder = xmlLoader.loadConfigHolder(output.toString()); PackageRepositories packageRepositories = goConfigHolder.config.getPackageRepositories(); assertThat(packageRepositories, is(configToSave.getPackageRepositories())); assertThat(packageRepositories.get(0).getConfiguration().first().getConfigurationValue().getValue(), is("http://go")); assertThat(packageRepositories.get(0).getConfiguration().first().getEncryptedConfigurationValue(), is(nullValue())); assertThat(packageRepositories.get(0).getConfiguration().last().getEncryptedValue(), is(new GoCipher().encrypt("secure"))); assertThat(packageRepositories.get(0).getConfiguration().last().getConfigurationValue(), is(nullValue())); assertThat(packageRepositories.get(0).getPackages().get(0), is(packageDefinition)); assertThat(packageRepositories.get(0).getPackages().get(0).getConfiguration().first().getConfigurationValue().getValue(), is("go-agent")); assertThat(packageRepositories.get(0).getPackages().get(0).getConfiguration().first().getEncryptedConfigurationValue(), is(nullValue())); }
assertThat(secureProperty.getEncryptedConfigurationValue(), is(notNullValue())); assertThat(secureProperty.getEncryptedValue(), is(goCipher.encrypt("value1"))); assertThat(secureRepoProperty.getEncryptedConfigurationValue(), is(notNullValue())); assertThat(secureRepoProperty.getEncryptedValue(), is(goCipher.encrypt("value1")));