Skip to content

Commit 6aa265d

Browse files
andselCopilot
andauthored
Moved Ruby Password setting to Java implementation (#18183)
Translates Password setting class into plain Java. Co-authored-by: Copilot <[email protected]>
1 parent cd71a4b commit 6aa265d

File tree

5 files changed

+133
-24
lines changed

5 files changed

+133
-24
lines changed

logstash-core/lib/logstash/environment.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def self.as_java_range(r)
7575
Setting::StringSetting.new("api.environment", "production"),
7676
Setting::StringSetting.new("api.auth.type", "none", true, %w(none basic)),
7777
Setting::StringSetting.new("api.auth.basic.username", nil, false).nullable,
78-
Setting::Password.new("api.auth.basic.password", nil, false).nullable,
78+
Setting::PasswordSetting.new("api.auth.basic.password", nil, false).nullable,
7979
Setting::StringSetting.new("api.auth.basic.password_policy.mode", "WARN", true, %w[WARN ERROR]),
8080
Setting::NumericSetting.new("api.auth.basic.password_policy.length.minimum", 8),
8181
Setting::StringSetting.new("api.auth.basic.password_policy.include.upper", "REQUIRED", true, %w[REQUIRED OPTIONAL]),
@@ -84,7 +84,7 @@ def self.as_java_range(r)
8484
Setting::StringSetting.new("api.auth.basic.password_policy.include.symbol", "OPTIONAL", true, %w[REQUIRED OPTIONAL]),
8585
Setting::BooleanSetting.new("api.ssl.enabled", false),
8686
Setting::ExistingFilePath.new("api.ssl.keystore.path", nil, false).nullable,
87-
Setting::Password.new("api.ssl.keystore.password", nil, false).nullable,
87+
Setting::PasswordSetting.new("api.ssl.keystore.password", nil, false).nullable,
8888
Setting::StringArray.new("api.ssl.supported_protocols", nil, true, %w[TLSv1 TLSv1.1 TLSv1.2 TLSv1.3]),
8989
Setting::StringSetting.new("queue.type", "memory", true, ["persisted", "memory"]),
9090
Setting::BooleanSetting.new("queue.drain", false),

logstash-core/lib/logstash/settings.rb

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -437,27 +437,9 @@ def validate(value)
437437

438438
java_import org.logstash.settings.NullableStringSetting
439439

440-
class Password < Coercible
441-
def initialize(name, default = nil, strict = true)
442-
super(name, LogStash::Util::Password, default, strict)
443-
end
444-
445-
def coerce(value)
446-
return value if value.kind_of?(LogStash::Util::Password)
447-
448-
if value && !value.kind_of?(::String)
449-
raise(ArgumentError, "Setting `#{name}` could not coerce non-string value to password")
450-
end
451-
452-
LogStash::Util::Password.new(value)
453-
end
454-
455-
def validate(value)
456-
super(value)
457-
end
458-
end
440+
java_import org.logstash.settings.PasswordSetting
459441

460-
class ValidatedPassword < Setting::Password
442+
class ValidatedPassword < Setting::PasswordSetting
461443
def initialize(name, value, password_policies)
462444
@password_policies = password_policies
463445
super(name, value, true)

logstash-core/spec/logstash/settings/password_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
require "spec_helper"
1919
require "logstash/settings"
2020

21-
describe LogStash::Setting::Password do
21+
describe LogStash::Setting::PasswordSetting do
2222
let(:setting_name) { "secure" }
2323
subject(:password_setting) { described_class.new(setting_name, nil, true) }
2424

@@ -55,7 +55,7 @@
5555
context 'with an invalid non-string value' do
5656
let(:setting_value) { 867_5309 }
5757
it 'rejects the invalid value' do
58-
expect { password_setting.set(setting_value) }.to raise_error(ArgumentError, "Setting `#{setting_name}` could not coerce non-string value to password")
58+
expect { password_setting.set(setting_value) }.to raise_error(IllegalArgumentException, "Setting `#{setting_name}` could not coerce non-string value to password")
5959
expect(password_setting).to_not be_set
6060
end
6161
end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.logstash.settings;
21+
22+
import co.elastic.logstash.api.Password;
23+
24+
25+
public class PasswordSetting extends Coercible<Object> {
26+
27+
public PasswordSetting(String name, Object defaultValue) {
28+
this(name, defaultValue, true);
29+
}
30+
31+
public PasswordSetting(String name, Object defaultValue, boolean strict) {
32+
super(name, defaultValue, strict, noValidator());
33+
}
34+
35+
@Override
36+
public Password coerce(Object obj) {
37+
if (obj instanceof Password) {
38+
return (Password) obj;
39+
}
40+
if (obj != null && !(obj instanceof String)) {
41+
throw new IllegalArgumentException("Setting `" + getName() + "` could not coerce non-string value to password");
42+
}
43+
return new Password((String) obj);
44+
}
45+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.logstash.settings;
21+
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
25+
import static org.hamcrest.CoreMatchers.instanceOf;
26+
import static org.hamcrest.CoreMatchers.is;
27+
import static org.hamcrest.MatcherAssert.assertThat;
28+
import static org.junit.Assert.*;
29+
30+
public class PasswordSettingTest {
31+
32+
private final String SETTING_NAME = "setting_name";
33+
private PasswordSetting sut;
34+
35+
@Before
36+
public void setUp() {
37+
sut = new PasswordSetting(SETTING_NAME, null, true);
38+
}
39+
40+
@Test
41+
public void givenUnsetPasswordSetting_thenIsConsideredAsValid() {
42+
assertNotThrown(() -> sut.validateValue());
43+
assertThat(sut.value(), is(instanceOf(co.elastic.logstash.api.Password.class)));
44+
assertNull(((co.elastic.logstash.api.Password) sut.value()).getValue());
45+
}
46+
47+
@Test
48+
public void givenUnsetPasswordSetting_whenIsSetIsInvoked_thenReturnFalse() {
49+
assertFalse(sut.isSet());
50+
}
51+
52+
@Test
53+
public void givenSetPasswordSetting_thenIsValid() {
54+
sut.set("s3cUr3p4$$w0rd");
55+
56+
assertNotThrown(() -> sut.validateValue());
57+
assertThat(sut.value(), is(instanceOf(co.elastic.logstash.api.Password.class)));
58+
assertEquals("s3cUr3p4$$w0rd", ((co.elastic.logstash.api.Password) sut.value()).getValue());
59+
}
60+
61+
@Test
62+
public void givenSetPasswordSetting_whenIsSetIsInvoked_thenReturnTrue() {
63+
sut.set("s3cUr3p4$$w0rd");
64+
65+
assertTrue(sut.isSet());
66+
}
67+
68+
@Test
69+
public void givenSetPasswordSettingWithInvalidNonStringValue_thenRejectsTheInvalidValue() {
70+
Exception e = assertThrows(IllegalArgumentException.class, () -> sut.set(867_5309));
71+
assertThat(e.getMessage(), is("Setting `" + SETTING_NAME + "` could not coerce non-string value to password"));
72+
}
73+
74+
private void assertNotThrown(Runnable test) {
75+
try {
76+
test.run();
77+
} catch (Exception e) {
78+
fail("Exception should not be thrown");
79+
}
80+
}
81+
82+
}

0 commit comments

Comments
 (0)