private void setEncryptedValue(String encryptedValue) { setEncryptedValue(new EncryptedConfigurationValue(encryptedValue)); }
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 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))); } }
@Test public void shouldGetEmptyValueWhenSecurePropertyValueIsNullOrEmpty() throws Exception { assertThat(new ConfigurationProperty(new ConfigurationKey("key"), null, new EncryptedConfigurationValue(""), cipher).getValue(), is("")); assertThat(new ConfigurationProperty(new ConfigurationKey("key"), null, new EncryptedConfigurationValue(null), cipher).getValue(), is("")); verify(cipher, never()).decrypt(anyString()); }
@Test public void shouldGetValueForSecureProperty() throws Exception { when(cipher.decrypt("encrypted-value")).thenReturn("decrypted-value"); ConfigurationProperty configurationProperty = new ConfigurationProperty(new ConfigurationKey("key"), null, new EncryptedConfigurationValue("encrypted-value"), cipher); assertThat(configurationProperty.getValue(), is("decrypted-value")); }
@Test public void addConfigurations_shouldAddConfigurationsWithEncryptedValue() throws Exception { ConfigurationProperty property = new ConfigurationProperty(new ConfigurationKey("username"), new EncryptedConfigurationValue("some_name")); SecurityAuthConfig authConfig = new SecurityAuthConfig("id", "plugin_id"); authConfig.addConfigurations(Arrays.asList(property)); assertThat(authConfig.size(), is(1)); assertThat(authConfig, contains(new ConfigurationProperty(new ConfigurationKey("username"), new EncryptedConfigurationValue("some_name")))); }
@Test public void addConfigurations_shouldAddConfigurationsWithEncryptedValue() throws Exception { ConfigurationProperty property = new ConfigurationProperty(new ConfigurationKey("username"), new EncryptedConfigurationValue("some_name")); ElasticProfile profile = new ElasticProfile("id", "plugin_id"); profile.addConfigurations(Arrays.asList(property)); assertThat(profile.size(), is(1)); assertThat(profile, contains(new ConfigurationProperty(new ConfigurationKey("username"), new EncryptedConfigurationValue("some_name")))); }
@Test public void shouldSetEmptyEncryptedValueWhenValueIsEmptyAndSecure() throws Exception { GoCipher goCipher = mock(GoCipher.class); ConfigurationProperty property = new ConfigurationProperty(new ConfigurationKey("secureKey"), new ConfigurationValue(""), new EncryptedConfigurationValue("old"), goCipher); property.handleSecureValueConfiguration(true); assertThat(property.getEncryptedValue(), is("")); verify(cipher, never()).decrypt(anyString()); }
@Test public void shouldPassValidationIfBothNameAndEncryptedValueAreProvidedForSecureProperty() throws CryptoException { String encrypted = "encrypted"; String decrypted = "decrypted"; when(cipher.decrypt(encrypted)).thenReturn(decrypted); ConfigurationProperty property = new ConfigurationProperty(new ConfigurationKey("name"), null, new EncryptedConfigurationValue(encrypted), cipher); property.validate(ConfigSaveValidationContext.forChain(property)); assertThat(property.errors().isEmpty(), is(true)); }
@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 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 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 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")); }
@Test public void shouldFailValidationIfAPropertyDoesNotHaveValue() { ConfigurationProperty property = new ConfigurationProperty(new ConfigurationKey("secureKey"), null, new EncryptedConfigurationValue("invalid-encrypted-value"), new GoCipher()); property.validate(ConfigSaveValidationContext.forChain(property)); assertThat(property.errors().isEmpty(), is(false)); assertThat(property.errors().getAllOn(ConfigurationProperty.ENCRYPTED_VALUE).contains( "Encrypted value for property with key 'secureKey' is invalid. This usually happens when the cipher text is modified to have an invalid value."), is(true)); }
@Test public void shouldGetMaskedStringIfConfigurationPropertyIsSecure() { assertThat(new ConfigurationProperty(new ConfigurationKey("key"), new EncryptedConfigurationValue("value")).getDisplayValue(), is("****")); assertThat(new ConfigurationProperty(new ConfigurationKey("key"), new ConfigurationValue("value")).getDisplayValue(), is("value")); } }
@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 shouldClearConfigurationsWhichAreEmptyAndNoErrors() throws Exception { SCM scm = new SCM(); scm.getConfiguration().add(new ConfigurationProperty(new ConfigurationKey("name-one"), new ConfigurationValue())); scm.getConfiguration().add(new ConfigurationProperty(new ConfigurationKey("name-two"), new EncryptedConfigurationValue())); scm.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"); scm.getConfiguration().add(configurationProperty); scm.clearEmptyConfigurations(); assertThat(scm.getConfiguration().size(), is(1)); assertThat(scm.getConfiguration().get(0).getConfigurationKey().getName(), is("name-four")); }
@Test public void shouldClearConfigurationsWhichAreEmptyAndNoErrors() throws Exception { PackageDefinition packageDefinition = new PackageDefinition(); packageDefinition.getConfiguration().add(new ConfigurationProperty(new ConfigurationKey("name-one"), new ConfigurationValue())); packageDefinition.getConfiguration().add(new ConfigurationProperty(new ConfigurationKey("name-two"), new EncryptedConfigurationValue())); packageDefinition.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"); packageDefinition.getConfiguration().add(configurationProperty); packageDefinition.clearEmptyConfigurations(); assertThat(packageDefinition.getConfiguration().size(), is(1)); assertThat(packageDefinition.getConfiguration().get(0).getConfigurationKey().getName(), is("name-four")); }
@Test public void shouldNotGetValuesOfSecureKeysInConfigForDisplay() { ConfigurationProperty property1 = new ConfigurationProperty(new ConfigurationKey("key1"), new ConfigurationValue("value1"), null, null); ConfigurationProperty property2 = new ConfigurationProperty(new ConfigurationKey("key2"), new ConfigurationValue("value2"), null, null); ConfigurationProperty property3 = new ConfigurationProperty(new ConfigurationKey("secure"), null, new EncryptedConfigurationValue("secured-value"), null); Configuration config = new Configuration(property1, property2, property3); assertThat(config.forDisplay(asList(property1, property2, property3)), is("[key1=value1, key2=value2]")); }
@Test public void shouldCheckIfSecureValueFieldHasNoErrors() { EncryptedConfigurationValue encryptedValue = new EncryptedConfigurationValue("encrypted-value"); assertThat(new ConfigurationProperty(new ConfigurationKey("key"), null, encryptedValue, cipher).doesNotHaveErrorsAgainstConfigurationValue(), is(true)); encryptedValue.addError("value", "some-error"); assertThat(new ConfigurationProperty(new ConfigurationKey("key"), null, encryptedValue, cipher).doesNotHaveErrorsAgainstConfigurationValue(), is(false)); }