private void populateConfiguration(Configuration configuration, com.thoughtworks.go.plugin.api.config.Configuration pluginConfiguration) { for (ConfigurationProperty configurationProperty : configuration) { pluginConfiguration.add(new SCMProperty(configurationProperty.getConfigurationKey().getName(), configurationProperty.getValue())); } }
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 applyPackagePluginMetadata(String pluginId) { for (ConfigurationProperty configurationProperty : configuration) { PackageMetadataStore packageMetadataStore = PackageMetadataStore.getInstance(); if (packageMetadataStore.getMetadata(pluginId) != null) { boolean isSecureProperty = packageMetadataStore.hasOption(pluginId, configurationProperty.getConfigurationKey().getName(), PackageConfiguration.SECURE); configurationProperty.handleSecureValueConfiguration(isSecureProperty); } } }
public Map<String, String> getConfigurationAsMap(boolean addSecureFields) { Map<String, String> configurationMap = new LinkedHashMap<>(); for (ConfigurationProperty currentConfiguration : this) { if (addSecureFields || !currentConfiguration.isSecure()) { configurationMap.put(currentConfiguration.getConfigKeyName(), currentConfiguration.getValue()); } } return configurationMap; }
public Map<String, String> getSettingsAsKeyValuePair() { Map<String, String> settingsAsKeyValuePair = new HashMap<>(); for (ConfigurationProperty configurationProperty : settingsMap) { settingsAsKeyValuePair.put(configurationProperty.getConfigKeyName(), configurationProperty.getValue()); } return settingsAsKeyValuePair; }
@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 postConstruct_shouldIgnoreEncryptionIfPluginInfoIsNotDefined() { ElasticProfile profile = new ElasticProfile("id", "plugin_id", new ConfigurationProperty(new ConfigurationKey("password"), new ConfigurationValue("pass"))); profile.encryptSecureConfigurations(); assertThat(profile.size(), is(1)); assertFalse(profile.first().isSecure()); }
@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 void addConfigurations(PluginInfo pluginInfo, List<ConfigurationProperty> configurationProperties) { settingsMap.clear(); ConfigurationPropertyBuilder builder = new ConfigurationPropertyBuilder(); for (ConfigurationProperty property : configurationProperties) { ConfigurationProperty configurationProperty = builder.create(property.getConfigKeyName(), property.getConfigValue(), property.getEncryptedValue(), pluginInfo.isSecure(property.getConfigKeyName())); settingsMap.add(configurationProperty); } }
public static List<CRConfigurationProperty> getCrConfigurations(Configuration configuration) { List<CRConfigurationProperty> config = new ArrayList<>(); for (ConfigurationProperty prop : configuration) { String configKeyName = prop.getConfigKeyName(); if (!prop.isSecure()) config.add(new CRConfigurationProperty(configKeyName, prop.getValue(), null)); else { CRConfigurationProperty crProp = new CRConfigurationProperty(configKeyName, null, prop.getEncryptedValue()); config.add(crProp); } } return config; }
@Test public void shouldGetNullValueForPropertyWhenValueIsNull() { ConfigurationProperty configurationProperty = new ConfigurationProperty(new ConfigurationKey("key"), null, null, cipher); assertThat(configurationProperty.getValue(), is(nullValue())); }
private List<CRConfigurationProperty> configurationToCRConfiguration(Configuration config) { ArrayList<CRConfigurationProperty> properties = new ArrayList(); if (config != null) { for (ConfigurationProperty p : config) { CRConfigurationProperty crProp = new CRConfigurationProperty(p.getKey().getName()); if (p.isSecure()) crProp.setEncryptedValue(p.getEncryptedValue()); else crProp.setValue(p.getValue()); properties.add(crProp); } } return properties; }
@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 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)); }
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 static ConfigurationProperty create(String key, boolean isSecure, String value) { ConfigurationProperty property = new ConfigurationProperty(new ConfigurationKey(key), new ConfigurationValue(value), null, new GoCipher()); property.handleSecureValueConfiguration(isSecure); return property; }
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, ", ")); }
@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")); }