public String getConfigKeyName() { return configurationKey != null ? configurationKey.getName() : null; }
public ElasticProfile elasticProfile() { Gson gson = new Gson(); Map map = gson.fromJson(metadata, LinkedHashMap.class); String pluginId = (String) map.get("pluginId"); String id = (String) map.get("id"); Map<String, String> properties = (Map<String, String>) map.get("properties"); Collection<ConfigurationProperty> configProperties = properties.entrySet().stream().map(entry -> new ConfigurationProperty(new ConfigurationKey(entry.getKey()), new ConfigurationValue(entry.getValue()))).collect(Collectors.toList()); return new ElasticProfile(id, pluginId, configProperties); }
@Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } ConfigurationProperty that = (ConfigurationProperty) o; if (configurationKey != null ? !configurationKey.equals(that.configurationKey) : that.configurationKey != null) { return false; } if (configurationValue != null ? !configurationValue.equals(that.configurationValue) : that.configurationValue != null) { return false; } if (!cipher.passwordEquals(encryptedValue, that.encryptedValue)) { return false; } return true; }
public void setConfigAttributes(Object attributes, SecureKeyInfoProvider secureKeyInfoProvider) { Map attributesMap = (Map) attributes; if (attributesMap.containsKey(CONFIGURATION_KEY)) { configurationKey = new ConfigurationKey(); configurationKey.setConfigAttributes(attributesMap.get(CONFIGURATION_KEY)); } if (attributesMap.containsKey(CONFIGURATION_VALUE)) { configurationValue = new ConfigurationValue(""); configurationValue.setConfigAttributes(attributesMap.get(CONFIGURATION_VALUE)); } if (attributesMap.containsKey(ENCRYPTED_VALUE)) { encryptedValue = new EncryptedConfigurationValue(); encryptedValue.setConfigAttributes(attributesMap.get(ENCRYPTED_VALUE)); } if (secureKeyInfoProvider == null) { return; } if (secureKeyInfoProvider.isSecure(configurationKey.getName())) { if (!attributesMap.containsKey(IS_CHANGED)) { setConfigurationValue(null); } else { handleSecureValueConfiguration(true); } } }
@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())); }
@Override public int hashCode() { int result = configurationKey != null ? configurationKey.hashCode() : 0; result = 31 * result + (configurationValue != null ? configurationValue.hashCode() : 0); result = 31 * result + cipher.passwordHashcode(encryptedValue); return result; }
@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")); }
public void addNewConfigurationWithValue(String key, String value, boolean isSecure) { if (isSecure) { add(new ConfigurationProperty(new ConfigurationKey(key), new EncryptedConfigurationValue(value))); } else { add(new ConfigurationProperty(new ConfigurationKey(key), new ConfigurationValue(value))); } }
public void validateKeyUniqueness(HashMap<String, ConfigurationProperty> map, String entity) { String name = configurationKey.getName().toLowerCase(); ConfigurationProperty propertyWithSameKey = map.get(name); if (propertyWithSameKey == null) { map.put(name, this); } else { String errorMessage = format("Duplicate key '%s' found for %s", configurationKey.getName(), entity); propertyWithSameKey.addError(ConfigurationProperty.CONFIGURATION_KEY, errorMessage); addError(ConfigurationProperty.CONFIGURATION_KEY, errorMessage); } }
@Test public void shouldClearConfigurationsWhichAreEmptyAndNoErrors() throws Exception { Configuration configuration = new Configuration(); configuration.add(new ConfigurationProperty(new ConfigurationKey("name-one"), new ConfigurationValue())); configuration.add(new ConfigurationProperty(new ConfigurationKey("name-two"), new EncryptedConfigurationValue())); configuration.add(new ConfigurationProperty(new ConfigurationKey("name-three"), null, new EncryptedConfigurationValue(), null)); ConfigurationProperty configurationProperty = new ConfigurationProperty(new ConfigurationKey("name-four"), null, new EncryptedConfigurationValue(), null); configurationProperty.addErrorAgainstConfigurationValue("error"); configuration.add(configurationProperty); configuration.clearEmptyConfigurations(); assertThat(configuration.size(), is(1)); assertThat(configuration.get(0).getConfigurationKey().getName(), is("name-four")); }
public static ConfigurationProperty createKeyOnly(String key) { ConfigurationValue configurationValue = null; return new ConfigurationProperty(new ConfigurationKey(key), configurationValue); } }
public List<String> listOfConfigKeys() { ArrayList<String> list = new ArrayList<>(); for (ConfigurationProperty configurationProperty : this) { list.add(configurationProperty.getConfigurationKey().getName()); } return list; }
@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())); }
public void addNewConfiguration(String key, boolean isSecure) { if (isSecure) { add(new ConfigurationProperty(new ConfigurationKey(key), new EncryptedConfigurationValue())); } else { add(new ConfigurationProperty(new ConfigurationKey(key), new ConfigurationValue())); } }
public String forFingerprint() { return format("%s=%s", configurationKey.getName(), getValue()); }
@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 shouldCheckForEqualityOfKey() { ConfigurationKey configurationKey = new ConfigurationKey(ConfigurationKey.NAME); assertThat(configurationKey, is(new ConfigurationKey(ConfigurationKey.NAME))); } }
public ConfigurationProperty getProperty(final String key) { return stream().filter(item -> item.getConfigurationKey().getName().equals(key)).findFirst().orElse(null); }
@Test public void shouldClearConfigurationsWhichAreEmptyAndNoErrors() throws Exception { PackageRepository packageRepository = new PackageRepository(); packageRepository.getConfiguration().add(new ConfigurationProperty(new ConfigurationKey("name-one"), new ConfigurationValue())); packageRepository.getConfiguration().add(new ConfigurationProperty(new ConfigurationKey("name-two"), new EncryptedConfigurationValue())); packageRepository.getConfiguration().add(new ConfigurationProperty(new ConfigurationKey("name-three"), null, new EncryptedConfigurationValue(), null)); ConfigurationProperty configurationProperty = new ConfigurationProperty(new ConfigurationKey("name-four"), null, new EncryptedConfigurationValue(), null); configurationProperty.addErrorAgainstConfigurationValue("error"); packageRepository.getConfiguration().add(configurationProperty); packageRepository.clearEmptyConfigurations(); assertThat(packageRepository.getConfiguration().size(), is(1)); assertThat(packageRepository.getConfiguration().get(0).getConfigurationKey().getName(), is("name-four")); }
@Test public void shouldGetNullValueForPropertyWhenValueIsNull() { ConfigurationProperty configurationProperty = new ConfigurationProperty(new ConfigurationKey("key"), null, null, cipher); assertThat(configurationProperty.getValue(), is(nullValue())); }