use of org.graylog2.plugin.configuration.fields.ConfigurationField in project graylog2-server by Graylog2.
the class ConfigurationRequest method filter.
/**
* Creates a new {@link org.graylog2.plugin.configuration.Configuration configuration object} containing only the
* fields specified in this request object.
* @param config original Configuration
* @return filtered Configuration, not null but might be empty
*/
@Nonnull
public Configuration filter(Configuration config) {
final Map<String, Object> values = Maps.newHashMap();
for (final ConfigurationField field : fields.values()) {
final String name = field.getName();
final String type = field.getFieldType();
switch(type) {
case BooleanField.FIELD_TYPE:
if (config.booleanIsSet(name)) {
values.put(name, config.getBoolean(name));
}
break;
case NumberField.FIELD_TYPE:
if (config.intIsSet(name)) {
values.put(name, config.getInt(name));
}
break;
case ListField.FIELD_TYPE:
if (config.listIsSet(name)) {
values.put(name, config.getList(name));
}
break;
case TextField.FIELD_TYPE:
case DropdownField.FIELD_TYPE:
if (config.stringIsSet(name)) {
values.put(name, config.getString(name));
}
break;
default:
throw new IllegalStateException("Unknown field type " + type + " for configuration field \"" + name + "\". This is a bug.");
}
}
return new Configuration(values);
}
use of org.graylog2.plugin.configuration.fields.ConfigurationField in project graylog2-server by Graylog2.
the class AbstractInputsResource method maskPasswordsInConfiguration.
protected Map<String, Object> maskPasswordsInConfiguration(Map<String, Object> configuration, ConfigurationRequest configurationRequest) {
if (configuration == null || configurationRequest == null) {
return configuration;
}
return configuration.entrySet().stream().collect(HashMap::new, (map, entry) -> {
final ConfigurationField field = configurationRequest.getField(entry.getKey());
if (field instanceof TextField) {
final TextField textField = (TextField) field;
if (textField.getAttributes().contains(TextField.Attribute.IS_PASSWORD.toString().toLowerCase(Locale.ENGLISH)) && !Strings.isNullOrEmpty((String) entry.getValue())) {
map.put(entry.getKey(), "<password set>");
return;
}
}
map.put(entry.getKey(), entry.getValue());
}, HashMap::putAll);
}
use of org.graylog2.plugin.configuration.fields.ConfigurationField in project graylog2-server by Graylog2.
the class ConfigurationRequestTest method putAllRetainsOrder.
@Test
public void putAllRetainsOrder() throws Exception {
final ImmutableMap<String, ConfigurationField> fields = ImmutableMap.<String, ConfigurationField>of("field1", new TextField("field1", "humanName", "defaultValue", "description"), "field2", new TextField("field2", "humanName", "defaultValue", "description"), "field3", new TextField("field3", "humanName", "defaultValue", "description"));
configurationRequest.putAll(fields);
assertThat(configurationRequest.getFields().keySet()).containsSequence("field1", "field2", "field3");
}
use of org.graylog2.plugin.configuration.fields.ConfigurationField in project graylog2-server by Graylog2.
the class InputsResourceMaskingPasswordsTest method testMaskingOfPasswordFields.
@Test
public void testMaskingOfPasswordFields() {
final ConfigurationField fooInput = mock(ConfigurationField.class);
final TextField passwordInput = mock(TextField.class);
when(fooInput.getName()).thenReturn("foo");
when(passwordInput.getName()).thenReturn("password");
when(passwordInput.getAttributes()).thenReturn(ImmutableList.of(TextField.Attribute.IS_PASSWORD.toString().toLowerCase(Locale.ENGLISH)));
final ConfigurationRequest configurationRequest = ConfigurationRequest.createWithFields(fooInput, passwordInput);
final Map<String, Object> configuration = ImmutableMap.of("foo", 42, "password", "verysecret");
final Map<String, Object> resultingAttributes = this.inputsResource.maskPasswordsInConfiguration(configuration, configurationRequest);
assertThat(resultingAttributes).hasSize(2);
assertThat(resultingAttributes).containsEntry("password", "<password set>");
assertThat(resultingAttributes).containsEntry("foo", 42);
}
use of org.graylog2.plugin.configuration.fields.ConfigurationField in project graylog2-server by Graylog2.
the class InputsResourceMaskingPasswordsTest method testRetrievalOfInputWithPasswordFieldIfUserIsNotAllowedToEditInput.
@Test
public void testRetrievalOfInputWithPasswordFieldIfUserIsNotAllowedToEditInput() throws NotFoundException {
final String inputId = "myinput";
final String inputType = "dummyinput";
final Input input = getInput(inputId, inputType);
when(inputService.find(inputId)).thenReturn(input);
final ConfigurationField fooInput = mock(ConfigurationField.class);
when(fooInput.getName()).thenReturn("foo");
final TextField passwordInput = getPasswordField("password");
final ConfigurationRequest configurationRequest = ConfigurationRequest.createWithFields(fooInput, passwordInput);
final InputDescription inputDescription = getInputDescription(configurationRequest);
this.availableInputs.put(inputType, inputDescription);
when(currentSubject.isPermitted(RestPermissions.INPUTS_READ + ":" + inputId)).thenReturn(true);
when(currentSubject.isPermitted(RestPermissions.INPUTS_EDIT + ":" + inputId)).thenReturn(false);
final Map<String, Object> configuration = ImmutableMap.of("foo", 42, "password", "verysecret");
when(input.getConfiguration()).thenReturn(configuration);
final InputSummary summary = this.inputsResource.get(inputId);
assertThat(summary.attributes()).hasSize(2);
assertThat(summary.attributes()).containsEntry("password", "<password set>");
assertThat(summary.attributes()).containsEntry("foo", 42);
}
Aggregations