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, 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 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 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")); }
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 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 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 shouldGetEncryptValueWhenConstructedAsSecure() throws CryptoException { GoCipher goCipher = mock(GoCipher.class); String encryptedText = "encryptedValue"; when(goCipher.encrypt("secureValue")).thenReturn(encryptedText); ConfigurationProperty property = new ConfigurationProperty(new ConfigurationKey("secureKey"), new ConfigurationValue("secureValue"), new EncryptedConfigurationValue("old-encrypted-text"), goCipher); property.handleSecureValueConfiguration(true); assertThat(property.isSecure(), is(true)); assertThat(property.getEncryptedValue(), is(encryptedText)); assertThat(property.getConfigurationKey().getName(), is("secureKey")); assertThat(property.getConfigurationValue(), is(nullValue())); }
@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 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 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 shouldCreateConfigurationPropertyWithEncyrptedValueForSecureProperty() { Property key = new Property("key"); key.with(Property.SECURE, true); ConfigurationProperty property = new ConfigurationPropertyBuilder().create("key", null, "enc_value", true); assertThat(property.getConfigKeyName(), is("key")); assertThat(property.getEncryptedValue(), is("enc_value")); assertNull(property.getConfigurationValue()); }
@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 shouldCreateWithEncyrptedValueForOnlyPlainTextInputForSecureProperty() throws Exception { Property key = new Property("key"); key.with(Property.SECURE, true); ConfigurationProperty property = new ConfigurationPropertyBuilder().create("key", "value", null, true); assertThat(property.getConfigKeyName(), is("key")); assertThat(property.getEncryptedValue(), is(new GoCipher().encrypt("value"))); assertNull(property.getConfigurationValue()); }
@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()); }