-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Add InMemoryClonedSecureSettings for keeping temporary secure settings loaded #134349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
elasticsearchmachine
merged 16 commits into
elastic:main
from
jfreden:add_secure_settings_cloner
Sep 11, 2025
Merged
Changes from 6 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
756b847
Add LoadedSecureSettings for keeping temporary secure settings loaded
jfreden ce39464
[CI] Auto commit changes from spotless
fb5878b
Add unit test
jfreden 7770cea
Update docs/changelog/134349.yaml
jfreden 6381a46
Merge branch 'main' into add_secure_settings_cloner
jfreden 817d6d7
Merge branch 'main' into add_secure_settings_cloner
jfreden e44be12
Return clone of secureSettingValue in getString
jfreden d258c9e
Update name and add javadoc
jfreden 02fef0f
Merge remote-tracking branch 'upstream/main' into add_secure_settings…
jfreden 3187974
[CI] Auto commit changes from spotless
bb1bad4
Update server/src/main/java/org/elasticsearch/common/settings/ClonedS…
jfreden fad1cdc
Update server/src/main/java/org/elasticsearch/common/settings/ClonedS…
jfreden 064ba97
Update server/src/main/java/org/elasticsearch/common/settings/ClonedS…
jfreden 044363b
Change name to InMemoryClonedSecureSettings
jfreden 3ca7b52
Merge branch 'main' into add_secure_settings_cloner
jfreden d0dad6f
[CI] Auto commit changes from spotless
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| pr: 134349 | ||
| summary: Add `LoadedSecureSettings` for keeping temporary secure settings loaded | ||
| area: Security | ||
| type: enhancement | ||
| issues: [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
server/src/main/java/org/elasticsearch/common/settings/LoadedSecureSettings.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.common.settings; | ||
|
|
||
| import org.elasticsearch.common.io.stream.StreamOutput; | ||
|
|
||
| import java.io.InputStream; | ||
| import java.security.GeneralSecurityException; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| public class LoadedSecureSettings { | ||
|
|
||
| /** | ||
| * Extracts the {@link SecureSettings}` out of the passed in {@link Settings} object. The {@code Setting} argument has to have the | ||
| * {@code SecureSettings} open/available. Normally {@code SecureSettings} are available only under specific callstacks (eg. during node | ||
| * initialization or during a `reload` call). The returned copy can be reused freely as it will never be closed (this is a bit of | ||
| * cheating, but it is necessary in this specific circumstance). Only works for secure settings of type string (not file). | ||
| * | ||
| * @param source A {@code Settings} object with its {@code SecureSettings} open/available. | ||
| * @param settingsToCopy The list of settings to copy. | ||
| * @return A copy of the {@code SecureSettings} of the passed in {@code Settings} argument. | ||
| */ | ||
| public static SecureSettings toLoadedSecureSettings(Settings source, List<Setting<?>> settingsToCopy) throws GeneralSecurityException { | ||
| final SecureSettings sourceSecureSettings = Settings.builder().put(source, true).getSecureSettings(); | ||
| final Map<String, SecureSettingValue> copiedSettings = new HashMap<>(); | ||
jfreden marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if (sourceSecureSettings != null && settingsToCopy != null) { | ||
| for (final String settingKey : sourceSecureSettings.getSettingNames()) { | ||
| for (final Setting<?> secureSetting : settingsToCopy) { | ||
| if (secureSetting.match(settingKey)) { | ||
| copiedSettings.put( | ||
| settingKey, | ||
| new SecureSettingValue( | ||
| sourceSecureSettings.getString(settingKey), | ||
| sourceSecureSettings.getSHA256Digest(settingKey) | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return new SecureSettings() { | ||
| @Override | ||
| public boolean isLoaded() { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public SecureString getString(String setting) { | ||
| return copiedSettings.get(setting).value(); | ||
jfreden marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @Override | ||
| public Set<String> getSettingNames() { | ||
| return copiedSettings.keySet(); | ||
| } | ||
|
|
||
| @Override | ||
| public InputStream getFile(String setting) { | ||
| throw new UnsupportedOperationException("A loaded SecureSetting cannot be a file"); | ||
| } | ||
|
|
||
| @Override | ||
| public byte[] getSHA256Digest(String setting) { | ||
| return copiedSettings.get(setting).sha256Digest(); | ||
mosche marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @Override | ||
| public void close() {} | ||
|
|
||
| @Override | ||
| public void writeTo(StreamOutput out) { | ||
| throw new UnsupportedOperationException("A loaded SecureSetting cannot be serialized"); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| private record SecureSettingValue(SecureString value, byte[] sha256Digest) {} | ||
| } | ||
94 changes: 94 additions & 0 deletions
94
server/src/test/java/org/elasticsearch/common/settings/LoadedSecureSettingsTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.common.settings; | ||
|
|
||
| import org.elasticsearch.test.ESTestCase; | ||
|
|
||
| import java.io.IOException; | ||
| import java.security.GeneralSecurityException; | ||
| import java.util.List; | ||
|
|
||
| import static org.hamcrest.Matchers.containsInAnyOrder; | ||
| import static org.hamcrest.Matchers.equalTo; | ||
| import static org.hamcrest.Matchers.notNullValue; | ||
|
|
||
| public class LoadedSecureSettingsTests extends ESTestCase { | ||
|
|
||
| public void testCopiesMatchingSecureSettings() throws GeneralSecurityException, IOException { | ||
| var mockSecureSettings = new MockSecureSettings(); | ||
| mockSecureSettings.setString("secure.password", "changeme"); | ||
| mockSecureSettings.setString("secure.api_key", "abcd1234"); | ||
|
|
||
| var settings = Settings.builder().put("some.other", "value").setSecureSettings(mockSecureSettings).build(); | ||
|
|
||
| var securePasswordSetting = SecureSetting.secureString("secure.password", null); | ||
| var secureApiKeySetting = SecureSetting.secureString("secure.api_key", null); | ||
|
|
||
| var loaded = LoadedSecureSettings.toLoadedSecureSettings(settings, List.of(securePasswordSetting, secureApiKeySetting)); | ||
| mockSecureSettings.close(); | ||
|
|
||
| assertTrue(loaded.isLoaded()); | ||
| assertThat(loaded.getSettingNames(), containsInAnyOrder("secure.password", "secure.api_key")); | ||
| assertThat(loaded.getString("secure.password").toString(), equalTo("changeme")); | ||
| assertThat(loaded.getString("secure.api_key").toString(), equalTo("abcd1234")); | ||
|
|
||
| assertThat(loaded.getSHA256Digest("secure.password"), notNullValue()); | ||
| assertThat(loaded.getSHA256Digest("secure.api_key"), notNullValue()); | ||
| } | ||
|
|
||
| public void testIgnoresNonMatchingSettings() throws GeneralSecurityException { | ||
| var mockSecureSettings = new MockSecureSettings(); | ||
| mockSecureSettings.setString("secure.password", "changeme"); | ||
|
|
||
| var settings = Settings.builder().setSecureSettings(mockSecureSettings).build(); | ||
| var differentSetting = SecureSetting.secureString("secure.token", null); | ||
|
|
||
| var loaded = LoadedSecureSettings.toLoadedSecureSettings(settings, List.of(differentSetting)); | ||
| assertThat(loaded.getSettingNames().isEmpty(), equalTo(true)); | ||
| } | ||
|
|
||
| public void testFileSettingThrows() throws GeneralSecurityException { | ||
| var mockSecureSettings = new MockSecureSettings(); | ||
| mockSecureSettings.setFile("secure.file", randomByteArrayOfLength(16)); | ||
| var settings = Settings.builder().setSecureSettings(mockSecureSettings).build(); | ||
|
|
||
| var loaded = LoadedSecureSettings.toLoadedSecureSettings(settings, List.of()); | ||
|
|
||
| UnsupportedOperationException ex = expectThrows(UnsupportedOperationException.class, () -> loaded.getFile("secure.file")); | ||
| assertThat(ex.getMessage(), equalTo("A loaded SecureSetting cannot be a file")); | ||
| } | ||
|
|
||
| public void testWriteToThrows() throws Exception { | ||
| var mockSecureSettings = new MockSecureSettings(); | ||
| mockSecureSettings.setString("secure.secret", "topsecret"); | ||
|
|
||
| var settings = Settings.builder().setSecureSettings(mockSecureSettings).build(); | ||
| var loaded = LoadedSecureSettings.toLoadedSecureSettings(settings, List.of()); | ||
|
|
||
| UnsupportedOperationException ex = expectThrows(UnsupportedOperationException.class, () -> loaded.writeTo(null)); | ||
| assertThat(ex.getMessage(), equalTo("A loaded SecureSetting cannot be serialized")); | ||
| } | ||
|
|
||
| public void testNullSourceOrSettingsList() throws Exception { | ||
| var empty = Settings.EMPTY; | ||
|
|
||
| var loaded = LoadedSecureSettings.toLoadedSecureSettings(empty, null); | ||
| assertThat(loaded.isLoaded(), equalTo(true)); | ||
| assertThat(loaded.getSettingNames().isEmpty(), equalTo(true)); | ||
|
|
||
| var mockSecureSettings = new MockSecureSettings(); | ||
| mockSecureSettings.setString("secure.password", "changeme"); | ||
|
|
||
| var settings = Settings.builder().setSecureSettings(mockSecureSettings).build(); | ||
|
|
||
| var loaded2 = LoadedSecureSettings.toLoadedSecureSettings(settings, null); | ||
| assertTrue(loaded2.getSettingNames().isEmpty()); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.