Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions logstash-core/lib/logstash/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def self.as_java_range(r)
Setting::StringSetting.new("api.environment", "production"),
Setting::StringSetting.new("api.auth.type", "none", true, %w(none basic)),
Setting::StringSetting.new("api.auth.basic.username", nil, false).nullable,
Setting::Password.new("api.auth.basic.password", nil, false).nullable,
Setting::PasswordSetting.new("api.auth.basic.password", nil, false).nullable,
Setting::StringSetting.new("api.auth.basic.password_policy.mode", "WARN", true, %w[WARN ERROR]),
Setting::NumericSetting.new("api.auth.basic.password_policy.length.minimum", 8),
Setting::StringSetting.new("api.auth.basic.password_policy.include.upper", "REQUIRED", true, %w[REQUIRED OPTIONAL]),
Expand All @@ -84,7 +84,7 @@ def self.as_java_range(r)
Setting::StringSetting.new("api.auth.basic.password_policy.include.symbol", "OPTIONAL", true, %w[REQUIRED OPTIONAL]),
Setting::BooleanSetting.new("api.ssl.enabled", false),
Setting::ExistingFilePath.new("api.ssl.keystore.path", nil, false).nullable,
Setting::Password.new("api.ssl.keystore.password", nil, false).nullable,
Setting::PasswordSetting.new("api.ssl.keystore.password", nil, false).nullable,
Setting::StringArray.new("api.ssl.supported_protocols", nil, true, %w[TLSv1 TLSv1.1 TLSv1.2 TLSv1.3]),
Setting::StringSetting.new("queue.type", "memory", true, ["persisted", "memory"]),
Setting::BooleanSetting.new("queue.drain", false),
Expand Down
22 changes: 2 additions & 20 deletions logstash-core/lib/logstash/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -437,27 +437,9 @@ def validate(value)

java_import org.logstash.settings.NullableStringSetting

class Password < Coercible
def initialize(name, default = nil, strict = true)
super(name, LogStash::Util::Password, default, strict)
end

def coerce(value)
return value if value.kind_of?(LogStash::Util::Password)

if value && !value.kind_of?(::String)
raise(ArgumentError, "Setting `#{name}` could not coerce non-string value to password")
end

LogStash::Util::Password.new(value)
end

def validate(value)
super(value)
end
end
java_import org.logstash.settings.PasswordSetting

class ValidatedPassword < Setting::Password
class ValidatedPassword < Setting::PasswordSetting
def initialize(name, value, password_policies)
@password_policies = password_policies
super(name, value, true)
Expand Down
4 changes: 2 additions & 2 deletions logstash-core/spec/logstash/settings/password_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
require "spec_helper"
require "logstash/settings"

describe LogStash::Setting::Password do
describe LogStash::Setting::PasswordSetting do
let(:setting_name) { "secure" }
subject(:password_setting) { described_class.new(setting_name, nil, true) }

Expand Down Expand Up @@ -55,7 +55,7 @@
context 'with an invalid non-string value' do
let(:setting_value) { 867_5309 }
it 'rejects the invalid value' do
expect { password_setting.set(setting_value) }.to raise_error(ArgumentError, "Setting `#{setting_name}` could not coerce non-string value to password")
expect { password_setting.set(setting_value) }.to raise_error(IllegalArgumentException, "Setting `#{setting_name}` could not coerce non-string value to password")
expect(password_setting).to_not be_set
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.logstash.settings;

import co.elastic.logstash.api.Password;


public class PasswordSetting extends Coercible<Object> {

public PasswordSetting(String name, Object defaultValue) {
this(name, defaultValue, true);
}

public PasswordSetting(String name, Object defaultValue, boolean strict) {
super(name, defaultValue, strict, noValidator());
}

@Override
public Password coerce(Object obj) {
if (obj instanceof Password) {
return (Password) obj;
}
if (obj != null && !(obj instanceof String)) {
throw new IllegalArgumentException("Setting `" + getName() + "` could not coerce non-string value to password");
}
return new Password((String) obj);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.logstash.settings;

import org.junit.Before;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.*;

public class PasswordSettingTest {

private final String SETTING_NAME = "setting_name";
private PasswordSetting sut;

@Before
public void setUp() {
sut = new PasswordSetting(SETTING_NAME, null, true);
}

@Test
public void givenUnsetPasswordSetting_thenIsConsideredAsValid() {
assertNotThrown(() -> sut.validateValue());
assertThat(sut.value(), is(instanceOf(co.elastic.logstash.api.Password.class)));
assertNull(((co.elastic.logstash.api.Password) sut.value()).getValue());
}

@Test
public void givenUnsetPasswordSetting_whenIsSetIsInvoked_thenReturnFalse() {
assertFalse(sut.isSet());
}

@Test
public void givenSetPasswordSetting_thenIsValid() {
sut.set("s3cUr3p4$$w0rd");

assertNotThrown(() -> sut.validateValue());
assertThat(sut.value(), is(instanceOf(co.elastic.logstash.api.Password.class)));
assertEquals("s3cUr3p4$$w0rd", ((co.elastic.logstash.api.Password) sut.value()).getValue());
}

@Test
public void givenSetPasswordSetting_whenIsSetIsInvoked_thenReturnTrue() {
sut.set("s3cUr3p4$$w0rd");

assertTrue(sut.isSet());
}

@Test
public void givenSetPasswordSettingWithInvalidNonStringValue_thenRejectsTheInvalidValue() {
Exception e = assertThrows(IllegalArgumentException.class, () -> sut.set(867_5309));
assertThat(e.getMessage(), is("Setting `" + SETTING_NAME + "` could not coerce non-string value to password"));
}

private void assertNotThrown(Runnable test) {
try {
test.run();
} catch (Exception e) {
fail("Exception should not be thrown");
}
}

}