Search in sources :

Example 1 with ConfigurationField

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);
}
Also used : ConfigurationField(org.graylog2.plugin.configuration.fields.ConfigurationField) Nonnull(javax.annotation.Nonnull)

Example 2 with ConfigurationField

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);
}
Also used : HashMap(java.util.HashMap) ConfigurationField(org.graylog2.plugin.configuration.fields.ConfigurationField) TextField(org.graylog2.plugin.configuration.fields.TextField)

Example 3 with ConfigurationField

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");
}
Also used : ConfigurationField(org.graylog2.plugin.configuration.fields.ConfigurationField) TextField(org.graylog2.plugin.configuration.fields.TextField) Test(org.junit.Test)

Example 4 with ConfigurationField

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);
}
Also used : ConfigurationRequest(org.graylog2.plugin.configuration.ConfigurationRequest) ConfigurationField(org.graylog2.plugin.configuration.fields.ConfigurationField) TextField(org.graylog2.plugin.configuration.fields.TextField) Test(org.junit.Test)

Example 5 with ConfigurationField

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);
}
Also used : InputSummary(org.graylog2.rest.models.system.inputs.responses.InputSummary) Input(org.graylog2.inputs.Input) ConfigurationRequest(org.graylog2.plugin.configuration.ConfigurationRequest) ConfigurationField(org.graylog2.plugin.configuration.fields.ConfigurationField) TextField(org.graylog2.plugin.configuration.fields.TextField) InputDescription(org.graylog2.shared.inputs.InputDescription) Test(org.junit.Test)

Aggregations

ConfigurationField (org.graylog2.plugin.configuration.fields.ConfigurationField)12 Test (org.junit.Test)8 ConfigurationRequest (org.graylog2.plugin.configuration.ConfigurationRequest)7 TextField (org.graylog2.plugin.configuration.fields.TextField)7 Input (org.graylog2.inputs.Input)4 InputDescription (org.graylog2.shared.inputs.InputDescription)4 InputSummary (org.graylog2.rest.models.system.inputs.responses.InputSummary)2 InputsList (org.graylog2.rest.models.system.inputs.responses.InputsList)2 JsonValue (com.fasterxml.jackson.annotation.JsonValue)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Nonnull (javax.annotation.Nonnull)1