public String getConfigValue() { return configurationValue != null ? configurationValue.getValue() : null; }
public void handleSecureValueConfiguration(boolean isSecure) { if (isSecure) { if (configurationValue != null) { try { encryptedValue = new EncryptedConfigurationValue(isEmpty(configurationValue.getValue()) ? "" : cipher.encrypt(configurationValue.getValue())); } catch (CryptoException e) { throw new RuntimeException(e); } configurationValue = null; } } else { encryptedValue = null; } }
public String forDisplay(List<ConfigurationProperty> propertiesToDisplay) { ArrayList<String> list = new ArrayList<>(); for (ConfigurationProperty property : propertiesToDisplay) { if (!property.isSecure()) { list.add(format("%s=%s", property.getConfigurationKey().getName().toLowerCase(), property.getConfigurationValue().getValue())); } } return format("[%s]", StringUtils.join(list, ", ")); }
public Map<String, Map<String, String>> getConfigWithErrorsAsMap() { Map<String, Map<String, String>> configMap = new HashMap<>(); for (ConfigurationProperty property : this) { Map<String, String> mapValue = new HashMap<>(); if (property.isSecure()) { mapValue.put(VALUE_KEY, property.getEncryptedValue()); } else { mapValue.put(VALUE_KEY, property.getConfigurationValue().getValue()); } if (!property.getAllErrors().isEmpty()) { mapValue.put(ERRORS_KEY, StringUtils.join(property.getAllErrors().stream().map(ConfigErrors::getAll).collect(toList()), ", ")); } configMap.put(property.getConfigKeyName(), mapValue); } return configMap; } }
public Map<String, Map<String, Object>> getPropertyMetadataAndValuesAsMap() { Map<String, Map<String, Object>> configMap = new HashMap<>(); for (ConfigurationProperty property : this) { Map<String, Object> mapValue = new HashMap<>(); mapValue.put("isSecure", property.isSecure()); if (property.isSecure()) { mapValue.put(VALUE_KEY, property.getEncryptedValue()); } else { mapValue.put(VALUE_KEY, property.getConfigurationValue().getValue()); } mapValue.put("displayValue", property.getDisplayValue()); configMap.put(property.getConfigKeyName(), mapValue); } return configMap; }
public String getValue() { if (isSecure()) { try { if (isEmpty(encryptedValue.getValue())) { return EMPTY; } return cipher.decrypt(encryptedValue.getValue()); } catch (CryptoException e) { throw new RuntimeException(format("Could not decrypt secure configuration property value for key %s", configurationKey.getName()), e); } } return configurationValue == null ? null : configurationValue.getValue(); }
@Test public void shouldHandleLongValueAsAString() throws Exception { final ConfigurationValue configurationValue = new ConfigurationValue(5L); assertThat(configurationValue.getValue(), is("5")); }
@Test public void shouldHandleBooleanValueAsAString() throws Exception { final ConfigurationValue configurationValue = new ConfigurationValue(true); assertThat(configurationValue.getValue(), is("true")); }
@Test public void shouldHandleIntegerValueAsAString() throws Exception { final ConfigurationValue configurationValue = new ConfigurationValue(1); assertThat(configurationValue.getValue(), is("1")); }
@Test public void shouldHandleDoubleValueAsAString() throws Exception { final ConfigurationValue configurationValue = new ConfigurationValue(3.1428571429D); assertThat(configurationValue.getValue(), is("3.1428571429")); } }
@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 shouldSetConfigAttributesForNonSecureProperty() { 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); PackageConfigurations metadata = new PackageConfigurations(); metadata.addConfiguration(new PackageConfiguration("fooKey", null)); attributes.put(Configuration.METADATA, metadata); configurationProperty.setConfigAttributes(attributes,null); assertThat(configurationProperty.getConfigurationKey().getName(), is("fooKey")); assertThat(configurationProperty.getConfigurationValue().getValue(), is("fooValue")); }
@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())); }
public static void toJSON(OutputWriter writer, ConfigurationProperty configurationProperty) { writer.add("key", configurationProperty.getKey().getName()); if (!configurationProperty.isSecure() && !isBlank(configurationProperty.getConfigValue())) { writer.add("value", configurationProperty.getConfigurationValue().getValue()); } if (configurationProperty.isSecure() && !isBlank(configurationProperty.getEncryptedValue())) { writer.add("encrypted_value", configurationProperty.getEncryptedValue()); } if (configurationProperty.hasErrors()) { writer.addChild("errors", errorWriter -> new ErrorGetter(new LinkedHashMap<String, String>() {{ put("encryptedValue", "encrypted_value"); put("configurationValue", "configuration_value"); put("configurationKey", "configuration_key"); }}).toJSON(errorWriter, configurationProperty)); } }
@Test public void shouldCreateWithErrorsIfBothPlainAndEncryptedTextInputAreSpecifiedForUnSecuredProperty() { Property key = new Property("key"); key.with(Property.SECURE, false); ConfigurationProperty property = new ConfigurationPropertyBuilder().create("key", "value", "enc_value", false); assertThat(property.errors().get("configurationValue").get(0), is("You may only specify `value` or `encrypted_value`, not both!")); assertThat(property.errors().get("encryptedValue").get(0), is("You may only specify `value` or `encrypted_value`, not both!")); assertThat(property.getConfigurationValue().getValue(), is("value")); assertThat(property.getEncryptedValue(), is("enc_value")); }
@Test public void shouldCreateWithErrorsIfBothPlainAndEncryptedTextInputAreSpecifiedForSecureProperty() { Property key = new Property("key"); key.with(Property.SECURE, true); ConfigurationProperty property = new ConfigurationPropertyBuilder().create("key", "value", "enc_value", true); assertThat(property.errors().get("configurationValue").get(0), is("You may only specify `value` or `encrypted_value`, not both!")); assertThat(property.errors().get("encryptedValue").get(0), is("You may only specify `value` or `encrypted_value`, not both!")); assertThat(property.getConfigurationValue().getValue(), is("value")); assertThat(property.getEncryptedValue(), is("enc_value")); }
@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 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 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 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())); }