public Map<String, Map<String, String>> configAsMap() { Map<String, Map<String, String>> configMap = new HashMap<>(); for (ConfigurationProperty property : configuration) { Map<String, String> mapValue = new HashMap<>(); mapValue.put(VALUE_KEY, property.getValue()); if (!property.errors().isEmpty()) { mapValue.put(ERRORS_KEY, StringUtils.join(property.errors().getAll(), ", ")); } configMap.put(property.getConfigKeyName(), mapValue); } return configMap; }
public Map<String, Map<String, String>> getConfigAsMap() { Map<String, Map<String, String>> configMap = new HashMap<>(); for (ConfigurationProperty property : configuration) { Map<String, String> mapValue = new HashMap<>(); mapValue.put(VALUE_KEY, property.getValue()); if (!property.errors().isEmpty()) { mapValue.put(ERRORS_KEY, StringUtils.join(property.errors().getAll(), ", ")); } configMap.put(property.getConfigKeyName(), mapValue); } return configMap; }
public List<String> getErrorFor(String settingsKey) { for (ConfigurationProperty configurationProperty : settingsMap) { if (configurationProperty.getConfigKeyName().equals(settingsKey)) { return configurationProperty.errors().getAllOn(settingsKey); } } return errors().getAllOn(settingsKey); }
@Test public void shouldValidateConfigPropertyNameUniqueness() { ConfigurationProperty prop1 = ConfigurationPropertyMother.create("USERNAME"); ConfigurationProperty prop2 = ConfigurationPropertyMother.create("USERNAME"); ArtifactStore store = new ArtifactStore("s3.plugin", "cd.go.s3.plugin", prop1, prop2); store.validate(null); assertThat(store.errors().size(), is(0)); assertThat(prop1.errors().size(), is(1)); assertThat(prop2.errors().size(), is(1)); assertThat(prop1.errors().on(ConfigurationProperty.CONFIGURATION_KEY), is("Duplicate key 'USERNAME' found for Artifact store 's3.plugin'")); assertThat(prop2.errors().on(ConfigurationProperty.CONFIGURATION_KEY), is("Duplicate key 'USERNAME' found for Artifact store 's3.plugin'")); }
@Test public void shouldValidateConfigPropertyNameUniqueness() throws Exception { ConfigurationProperty prop1 = ConfigurationPropertyMother.create("USERNAME"); ConfigurationProperty prop2 = ConfigurationPropertyMother.create("USERNAME"); ElasticProfile profile = new ElasticProfile("docker.unit-test", "cd.go.elastic-agent.docker", prop1, prop2); profile.validate(null); assertThat(profile.errors().size(), is(0)); assertThat(prop1.errors().size(), is(1)); assertThat(prop2.errors().size(), is(1)); assertThat(prop1.errors().on(ConfigurationProperty.CONFIGURATION_KEY), is("Duplicate key 'USERNAME' found for Elastic agent profile 'docker.unit-test'")); assertThat(prop2.errors().on(ConfigurationProperty.CONFIGURATION_KEY), is("Duplicate key 'USERNAME' found for Elastic agent profile 'docker.unit-test'")); }
@Test public void shouldValidateKeyUniqueness(){ ConfigurationProperty property = new ConfigurationProperty(new ConfigurationKey("key"), new ConfigurationValue()); HashMap<String, ConfigurationProperty> map = new HashMap<>(); ConfigurationProperty original = new ConfigurationProperty(new ConfigurationKey("key"), new ConfigurationValue()); map.put("key", original); property.validateKeyUniqueness(map, "Repo"); assertThat(property.errors().isEmpty(), is(false)); assertThat(property.errors().getAllOn(ConfigurationProperty.CONFIGURATION_KEY).contains("Duplicate key 'key' found for Repo"), is(true)); assertThat(original.errors().isEmpty(), is(false)); assertThat(original.errors().getAllOn(ConfigurationProperty.CONFIGURATION_KEY).contains("Duplicate key 'key' found for Repo"), is(true)); }
@Test public void shouldValidateUniqueKeysInConfiguration() { ConfigurationProperty one = create("one", false, "value1"); ConfigurationProperty duplicate1 = create("ONE", false, "value2"); ConfigurationProperty duplicate2 = create("ONE", false, "value3"); ConfigurationProperty two = create("two", false, null); SCM scm = new SCM(); scm.setConfiguration(new Configuration(one, duplicate1, duplicate2, two)); scm.setName("git"); scm.validate(null); assertThat(one.errors().isEmpty(), is(false)); assertThat(one.errors().getAllOn(ConfigurationProperty.CONFIGURATION_KEY).contains("Duplicate key 'ONE' found for SCM 'git'"), is(true)); assertThat(duplicate1.errors().isEmpty(), is(false)); assertThat(one.errors().getAllOn(ConfigurationProperty.CONFIGURATION_KEY).contains("Duplicate key 'ONE' found for SCM 'git'"), is(true)); assertThat(duplicate2.errors().isEmpty(), is(false)); assertThat(one.errors().getAllOn(ConfigurationProperty.CONFIGURATION_KEY).contains("Duplicate key 'ONE' found for SCM 'git'"), is(true)); assertThat(two.errors().isEmpty(), is(true)); }
@Test public void validate_shouldValidateArtifactPropertiesConfigurationKeyUniqueness() { final ValidationContext validationContext = mock(ValidationContext.class); final PluggableArtifactConfig artifactConfig = new PluggableArtifactConfig("Artifact-ID", "Store-ID", create("Foo", false, "Bar"), create("Foo", true, "Bar")); final ArtifactStores artifactStores = mock(ArtifactStores.class); assertFalse(artifactConfig.hasErrors()); when(validationContext.artifactStores()).thenReturn(artifactStores); when(artifactStores.find("Store-ID")).thenReturn(new ArtifactStore("Store-ID", "pluginId")); artifactConfig.validate(validationContext); Configuration configuration = artifactConfig.getConfiguration(); assertThat(configuration.get(0).errors().getAllOn("configurationKey"), is(Arrays.asList("Duplicate key 'Foo' found for Pluggable Artifact"))); assertThat(configuration.get(1).errors().getAllOn("configurationKey"), is(Arrays.asList("Duplicate key 'Foo' found for Pluggable Artifact"))); }
@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 shouldValidateUniqueKeysAreAddedToConfiguration(){ ConfigurationProperty one = new ConfigurationProperty(new ConfigurationKey("one"), new ConfigurationValue("value1")); ConfigurationProperty duplicate1 = new ConfigurationProperty(new ConfigurationKey("ONE"), new ConfigurationValue("value2")); ConfigurationProperty duplicate2 = new ConfigurationProperty(new ConfigurationKey("ONE"), new ConfigurationValue("value3")); ConfigurationProperty two = new ConfigurationProperty(new ConfigurationKey("two"), new ConfigurationValue()); Configuration configuration = new Configuration(one, duplicate1, duplicate2, two); configuration.validateUniqueness("Entity"); assertThat(one.errors().isEmpty(), is(false)); assertThat(one.errors().getAllOn(ConfigurationProperty.CONFIGURATION_KEY).contains("Duplicate key 'ONE' found for Entity"), is(true)); assertThat(duplicate1.errors().isEmpty(), is(false)); assertThat(one.errors().getAllOn(ConfigurationProperty.CONFIGURATION_KEY).contains("Duplicate key 'ONE' found for Entity"), is(true)); assertThat(duplicate2.errors().isEmpty(), is(false)); assertThat(one.errors().getAllOn(ConfigurationProperty.CONFIGURATION_KEY).contains("Duplicate key 'ONE' found for Entity"), is(true)); assertThat(two.errors().isEmpty(), is(true)); }
@Test public void shouldCreateWithErrorsInPresenceOfEncryptedTextInputForUnSecuredProperty() { Property key = new Property("key"); key.with(Property.SECURE, false); ConfigurationProperty property = new ConfigurationPropertyBuilder().create("key", null, "enc_value", false); assertThat(property.errors().get("encryptedValue").get(0), is("encrypted_value cannot be specified to a unsecured property.")); assertThat(property.getEncryptedValue(), is("enc_value")); }
@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 shouldValidateUniqueKeysInConfiguration() { ConfigurationProperty one = new ConfigurationProperty(new ConfigurationKey("one"), new ConfigurationValue("value1")); ConfigurationProperty duplicate1 = new ConfigurationProperty(new ConfigurationKey("ONE"), new ConfigurationValue("value2")); ConfigurationProperty duplicate2 = new ConfigurationProperty(new ConfigurationKey("ONE"), new ConfigurationValue("value3")); ConfigurationProperty two = new ConfigurationProperty(new ConfigurationKey("two"), new ConfigurationValue()); PackageRepository repository = new PackageRepository(); repository.setConfiguration(new Configuration(one, duplicate1, duplicate2, two)); repository.setName("yum"); repository.validate(null); assertThat(one.errors().isEmpty(), is(false)); assertThat(one.errors().getAllOn(ConfigurationProperty.CONFIGURATION_KEY).contains("Duplicate key 'ONE' found for Repository 'yum'"), is(true)); assertThat(duplicate1.errors().isEmpty(), is(false)); assertThat(one.errors().getAllOn(ConfigurationProperty.CONFIGURATION_KEY).contains("Duplicate key 'ONE' found for Repository 'yum'"), is(true)); assertThat(duplicate2.errors().isEmpty(), is(false)); assertThat(one.errors().getAllOn(ConfigurationProperty.CONFIGURATION_KEY).contains("Duplicate key 'ONE' found for Repository 'yum'"), is(true)); assertThat(two.errors().isEmpty(), is(true)); }
@Test public void shouldValidateUniqueKeysInConfiguration() { ConfigurationProperty one = new ConfigurationProperty(new ConfigurationKey("one"), new ConfigurationValue("value1")); ConfigurationProperty duplicate1 = new ConfigurationProperty(new ConfigurationKey("ONE"), new ConfigurationValue("value2")); ConfigurationProperty duplicate2 = new ConfigurationProperty(new ConfigurationKey("ONE"), new ConfigurationValue("value3")); ConfigurationProperty two = new ConfigurationProperty(new ConfigurationKey("two"), new ConfigurationValue()); PackageDefinition packageDefinition = new PackageDefinition(); packageDefinition.setConfiguration(new Configuration(one, duplicate1, duplicate2, two)); packageDefinition.setName("go-server"); packageDefinition.validate(null); assertThat(one.errors().isEmpty(), is(false)); assertThat(one.errors().getAllOn(ConfigurationProperty.CONFIGURATION_KEY).contains("Duplicate key 'ONE' found for Package 'go-server'"), is(true)); assertThat(duplicate1.errors().isEmpty(), is(false)); assertThat(one.errors().getAllOn(ConfigurationProperty.CONFIGURATION_KEY).contains("Duplicate key 'ONE' found for Package 'go-server'"), is(true)); assertThat(duplicate2.errors().isEmpty(), is(false)); assertThat(one.errors().getAllOn(ConfigurationProperty.CONFIGURATION_KEY).contains("Duplicate key 'ONE' found for Package 'go-server'"), is(true)); assertThat(two.errors().isEmpty(), is(true)); }
@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 shouldPassValidationIfBothNameAndValueAreProvided() { GoCipher cipher = mock(GoCipher.class); ConfigurationProperty property = new ConfigurationProperty(new ConfigurationKey("name"), new ConfigurationValue("value"), null, cipher); property.validate(ConfigSaveValidationContext.forChain(property)); assertThat(property.errors().isEmpty(), is(true)); }
@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 validate_shouldValidateFetchPluggableArtifactConfigurationUniqueness() { FetchPluggableArtifactTask task = new FetchPluggableArtifactTask(new CaseInsensitiveString("dummy"), new CaseInsensitiveString("stage"), new CaseInsensitiveString("job"), "s3", create("Foo", false, "Bar"), create("Foo", false, "Bar")); task.validate(ConfigSaveValidationContext.forChain(config, new TemplatesConfig(), downstream.getStage(new CaseInsensitiveString("stage")))); assertTrue(task.getConfiguration().hasErrors()); assertThat(task.getConfiguration().get(0).errors().on("configurationKey"), is("Duplicate key 'Foo' found for Fetch pluggable artifact")); assertThat(task.getConfiguration().get(1).errors().on("configurationKey"), is("Duplicate key 'Foo' found for Fetch pluggable artifact")); }