-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Auto-configure the elastic user password #78306
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
albertzaharovits
merged 20 commits into
elastic:master
from
albertzaharovits:set-auto-conf-password
Oct 5, 2021
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
10f3474
Main
albertzaharovits 6076aab
Nit rename & javadoc
albertzaharovits 674202d
ReservedRealm security index param remove fallout
albertzaharovits 98e3ed9
Merge branch 'master' into set-auto-conf-password
albertzaharovits ccd1dbe
WIP
albertzaharovits c2092d8
ReservedRealm tests
albertzaharovits 0b89640
Merge branch 'master' into set-auto-conf-password
albertzaharovits e8b390c
Exception serialization test
albertzaharovits 93abfb2
Almost
albertzaharovits ed8646f
ReservedRealm tests
albertzaharovits 53443bd
NativeUsersStoreTests
albertzaharovits 5645b7d
isElasticUserAutoConfigured
albertzaharovits 14e1f36
Negative integ tests
albertzaharovits a7e4c49
Merge branch 'master' into set-auto-conf-password
albertzaharovits 4128d61
Merge fallout
albertzaharovits 59c2542
Checkstyle
albertzaharovits 299c710
Nit
albertzaharovits 0ecbead
Merge branch 'master' into set-auto-conf-password
albertzaharovits cf0f4b3
Review Ioannis
albertzaharovits b249f9c
Integ test for authn success after promotion failure
albertzaharovits 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
35 changes: 35 additions & 0 deletions
35
server/src/main/java/org/elasticsearch/ElasticsearchAuthenticationProcessingError.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,35 @@ | ||
| /* | ||
| * 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 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 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| package org.elasticsearch; | ||
|
|
||
| import org.elasticsearch.common.io.stream.StreamInput; | ||
| import org.elasticsearch.rest.RestStatus; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * Used to indicate that the authentication process encountered a server-side error (5xx) that prevented the credentials verification. | ||
| * The presented client credentials might or might not be valid. | ||
| * This differs from an authentication failure error in subtle ways. This should be preferred when the issue hindering credentials | ||
| * verification is transient, such as network congestion or overloaded instances, but not in cases of misconfiguration. | ||
| * However this distinction is further blurred because in certain configurations and for certain credential types, the same | ||
| * credential can be validated in multiple ways, only some of which might experience transient problems. | ||
| * When in doubt, rely on the implicit behavior of 401 authentication failure. | ||
| */ | ||
| public class ElasticsearchAuthenticationProcessingError extends ElasticsearchSecurityException { | ||
|
|
||
| public ElasticsearchAuthenticationProcessingError(String msg, RestStatus status, Throwable cause, Object... args) { | ||
| super(msg, status, cause, args); | ||
| assert status == RestStatus.INTERNAL_SERVER_ERROR || status == RestStatus.SERVICE_UNAVAILABLE; | ||
| } | ||
|
|
||
| public ElasticsearchAuthenticationProcessingError(StreamInput in) throws IOException { | ||
| super(in); | ||
| } | ||
| } |
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
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
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
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
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
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 |
|---|---|---|
|
|
@@ -58,12 +58,6 @@ public abstract class SecuritySingleNodeTestCase extends ESSingleNodeTestCase { | |
| private static SecuritySettingsSource SECURITY_DEFAULT_SETTINGS = null; | ||
| private static CustomSecuritySettingsSource customSecuritySettingsSource = null; | ||
| private static RestClient restClient = null; | ||
| private static SecureString BOOTSTRAP_PASSWORD = null; | ||
|
|
||
| @BeforeClass | ||
| public static void generateBootstrapPassword() { | ||
| BOOTSTRAP_PASSWORD = TEST_PASSWORD_SECURE_STRING.clone(); | ||
| } | ||
|
|
||
| @BeforeClass | ||
| public static void initDefaultSettings() { | ||
|
|
@@ -82,10 +76,6 @@ public static void initDefaultSettings() { | |
| public static void destroyDefaultSettings() { | ||
| SECURITY_DEFAULT_SETTINGS = null; | ||
| customSecuritySettingsSource = null; | ||
| if (BOOTSTRAP_PASSWORD != null) { | ||
| BOOTSTRAP_PASSWORD.close(); | ||
| BOOTSTRAP_PASSWORD = null; | ||
| } | ||
| tearDownRestClient(); | ||
| } | ||
|
|
||
|
|
@@ -180,10 +170,17 @@ protected Settings nodeSettings() { | |
| if (builder.getSecureSettings() == null) { | ||
| builder.setSecureSettings(new MockSecureSettings()); | ||
| } | ||
| ((MockSecureSettings) builder.getSecureSettings()).setString("bootstrap.password", BOOTSTRAP_PASSWORD.toString()); | ||
| SecureString boostrapPassword = getBootstrapPassword(); | ||
| if (boostrapPassword != null) { | ||
| ((MockSecureSettings) builder.getSecureSettings()).setString("bootstrap.password", boostrapPassword.toString()); | ||
| } | ||
| return builder.build(); | ||
| } | ||
|
|
||
| protected SecureString getBootstrapPassword() { | ||
| return TEST_PASSWORD_SECURE_STRING; | ||
| } | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need this in order to override to not set the bootstrap password. |
||
| @Override | ||
| protected Collection<Class<? extends Plugin>> getPlugins() { | ||
| return customSecuritySettingsSource.nodePlugins(); | ||
|
|
||
171 changes: 171 additions & 0 deletions
171
...elasticsearch/xpack/security/authc/esnative/ReservedRealmElasticAutoconfigIntegTests.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,171 @@ | ||
| /* | ||
| * 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; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.security.authc.esnative; | ||
|
|
||
| import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest; | ||
| import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; | ||
| import org.elasticsearch.action.admin.indices.get.GetIndexRequest; | ||
| import org.elasticsearch.action.admin.indices.get.GetIndexResponse; | ||
| import org.elasticsearch.action.support.IndicesOptions; | ||
| import org.elasticsearch.client.Request; | ||
| import org.elasticsearch.client.RequestOptions; | ||
| import org.elasticsearch.client.ResponseException; | ||
| import org.elasticsearch.cluster.metadata.Metadata; | ||
| import org.elasticsearch.common.Strings; | ||
| import org.elasticsearch.common.settings.MockSecureSettings; | ||
| import org.elasticsearch.common.settings.SecureString; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.rest.RestStatus; | ||
| import org.elasticsearch.test.SecuritySingleNodeTestCase; | ||
| import org.elasticsearch.xpack.core.security.action.user.PutUserAction; | ||
| import org.elasticsearch.xpack.core.security.action.user.PutUserRequest; | ||
| import org.elasticsearch.xpack.core.security.authc.support.Hasher; | ||
| import org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken; | ||
| import org.elasticsearch.xpack.core.security.index.RestrictedIndicesNames; | ||
| import org.junit.BeforeClass; | ||
|
|
||
| import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; | ||
| import static org.elasticsearch.xpack.core.security.index.RestrictedIndicesNames.SECURITY_MAIN_ALIAS; | ||
| import static org.hamcrest.Matchers.is; | ||
|
|
||
| public class ReservedRealmElasticAutoconfigIntegTests extends SecuritySingleNodeTestCase { | ||
|
|
||
| private static Hasher hasher; | ||
|
|
||
| @BeforeClass | ||
| public static void setHasher() { | ||
| hasher = getFastStoredHashAlgoForTests(); | ||
| } | ||
|
|
||
| @Override | ||
| public Settings nodeSettings() { | ||
| Settings.Builder settingsBuilder = Settings.builder() | ||
| .put(super.nodeSettings()) | ||
| .put("xpack.security.authc.password_hashing.algorithm", hasher.name()); | ||
| ((MockSecureSettings) settingsBuilder.getSecureSettings()).setString("autoconfiguration.password_hash", | ||
| new String(hasher.hash(new SecureString("auto_password_that_is_longer_than_14_chars_because_of_FIPS".toCharArray())))); | ||
| return settingsBuilder.build(); | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean addMockHttpTransport() { | ||
| return false; // enable HTTP | ||
| } | ||
|
|
||
| @Override | ||
| protected SecureString getBootstrapPassword() { | ||
| return null; // no bootstrap password for this test | ||
| } | ||
|
|
||
| public void testAutoconfigFailedPasswordPromotion() { | ||
| try { | ||
| // prevents the .security index from being created automatically (after elastic user authentication) | ||
| ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); | ||
| updateSettingsRequest.transientSettings(Settings.builder().put(Metadata.SETTING_READ_ONLY_ALLOW_DELETE_SETTING.getKey(), | ||
| true)); | ||
| assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); | ||
|
|
||
| // delete the security index, if it exist | ||
| GetIndexRequest getIndexRequest = new GetIndexRequest(); | ||
| getIndexRequest.indices(SECURITY_MAIN_ALIAS); | ||
| getIndexRequest.indicesOptions(IndicesOptions.lenientExpandOpen()); | ||
| GetIndexResponse getIndexResponse = client().admin().indices().getIndex(getIndexRequest).actionGet(); | ||
| if (getIndexResponse.getIndices().length > 0) { | ||
| assertThat(getIndexResponse.getIndices().length, is(1)); | ||
| assertThat(getIndexResponse.getIndices()[0], is(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7)); | ||
| DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(getIndexResponse.getIndices()); | ||
| assertAcked(client().admin().indices().delete(deleteIndexRequest).actionGet()); | ||
| } | ||
|
|
||
| // elastic user gets 503 for the good password | ||
| Request restRequest = randomFrom(new Request("GET", "/_security/_authenticate"), new Request("GET", "_cluster/health"), | ||
| new Request("GET", "_nodes")); | ||
| RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder(); | ||
| options.addHeader(UsernamePasswordToken.BASIC_AUTH_HEADER, UsernamePasswordToken.basicAuthHeaderValue("elastic", | ||
| new SecureString("auto_password_that_is_longer_than_14_chars_because_of_FIPS".toCharArray()))); | ||
| restRequest.setOptions(options); | ||
| ResponseException exception = expectThrows(ResponseException.class, () -> getRestClient().performRequest(restRequest)); | ||
| assertThat(exception.getResponse().getStatusLine().getStatusCode(), is(RestStatus.SERVICE_UNAVAILABLE.getStatus())); | ||
|
|
||
| // but gets a 401 for the wrong password | ||
| Request restRequest2 = randomFrom(new Request("GET", "/_security/_authenticate"), new Request("GET", "_cluster/health"), | ||
| new Request("GET", "_nodes")); | ||
| options = RequestOptions.DEFAULT.toBuilder(); | ||
| options.addHeader(UsernamePasswordToken.BASIC_AUTH_HEADER, UsernamePasswordToken.basicAuthHeaderValue("elastic", | ||
| new SecureString("wrong password_that_is_longer_than_14_chars_because_of_FIPS".toCharArray()))); | ||
| restRequest2.setOptions(options); | ||
| exception = expectThrows(ResponseException.class, () -> getRestClient().performRequest(restRequest2)); | ||
| assertThat(exception.getResponse().getStatusLine().getStatusCode(), is(RestStatus.UNAUTHORIZED.getStatus())); | ||
| } finally { | ||
| ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); | ||
| updateSettingsRequest.transientSettings(Settings.builder().put(Metadata.SETTING_READ_ONLY_ALLOW_DELETE_SETTING.getKey(), | ||
| (String) null)); | ||
| assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); | ||
| } | ||
| } | ||
|
|
||
| public void testAutoconfigSucceedsAfterPromotionFailure() throws Exception { | ||
| try { | ||
| // create any non-elastic user, which triggers .security index creation | ||
| final PutUserRequest putUserRequest = new PutUserRequest(); | ||
| final String username = randomAlphaOfLength(8); | ||
| putUserRequest.username(username); | ||
| final SecureString password = new SecureString("super-strong-password!".toCharArray()); | ||
| putUserRequest.passwordHash(Hasher.PBKDF2.hash(password)); | ||
| putUserRequest.roles(Strings.EMPTY_ARRAY); | ||
| client().execute(PutUserAction.INSTANCE, putUserRequest).get(); | ||
|
|
||
| // but then make the cluster read-only | ||
| ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); | ||
| updateSettingsRequest.transientSettings(Settings.builder().put(Metadata.SETTING_READ_ONLY_ALLOW_DELETE_SETTING.getKey(), | ||
| true)); | ||
| assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); | ||
|
|
||
| // elastic user now gets 503 for the good password | ||
| Request restRequest = randomFrom(new Request("GET", "/_security/_authenticate"), new Request("GET", "_cluster/health"), | ||
| new Request("GET", "_nodes")); | ||
| RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder(); | ||
| options.addHeader(UsernamePasswordToken.BASIC_AUTH_HEADER, UsernamePasswordToken.basicAuthHeaderValue("elastic", | ||
| new SecureString("auto_password_that_is_longer_than_14_chars_because_of_FIPS".toCharArray()))); | ||
| restRequest.setOptions(options); | ||
| ResponseException exception = expectThrows(ResponseException.class, () -> getRestClient().performRequest(restRequest)); | ||
| assertThat(exception.getResponse().getStatusLine().getStatusCode(), is(RestStatus.SERVICE_UNAVAILABLE.getStatus())); | ||
|
|
||
| // clear cluster-wide write block | ||
| updateSettingsRequest = new ClusterUpdateSettingsRequest(); | ||
| updateSettingsRequest.transientSettings(Settings.builder().put(Metadata.SETTING_READ_ONLY_ALLOW_DELETE_SETTING.getKey(), | ||
| (String) null)); | ||
| assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); | ||
|
|
||
| if (randomBoolean()) { | ||
| Request restRequest2 = randomFrom(new Request("GET", "/_security/_authenticate"), new Request("GET", "_cluster/health"), | ||
| new Request("GET", "_nodes")); | ||
| options = RequestOptions.DEFAULT.toBuilder(); | ||
| options.addHeader(UsernamePasswordToken.BASIC_AUTH_HEADER, UsernamePasswordToken.basicAuthHeaderValue("elastic", | ||
| new SecureString("wrong password_that_is_longer_than_14_chars_because_of_FIPS".toCharArray()))); | ||
| restRequest2.setOptions(options); | ||
| exception = expectThrows(ResponseException.class, () -> getRestClient().performRequest(restRequest2)); | ||
| assertThat(exception.getResponse().getStatusLine().getStatusCode(), is(RestStatus.UNAUTHORIZED.getStatus())); | ||
| } | ||
|
|
||
| // now the auto config password can be promoted, and authn succeeds | ||
| Request restRequest3 = randomFrom(new Request("GET", "/_security/_authenticate"), new Request("GET", "_cluster/health"), | ||
| new Request("GET", "_nodes")); | ||
| options = RequestOptions.DEFAULT.toBuilder(); | ||
| options.addHeader(UsernamePasswordToken.BASIC_AUTH_HEADER, UsernamePasswordToken.basicAuthHeaderValue("elastic", | ||
| new SecureString("auto_password_that_is_longer_than_14_chars_because_of_FIPS".toCharArray()))); | ||
| restRequest3.setOptions(options); | ||
| assertThat(getRestClient().performRequest(restRequest3).getStatusLine().getStatusCode(), is(RestStatus.OK.getStatus())); | ||
| } finally { | ||
| ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); | ||
| updateSettingsRequest.transientSettings(Settings.builder().put(Metadata.SETTING_READ_ONLY_ALLOW_DELETE_SETTING.getKey(), | ||
| (String) null)); | ||
| assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); | ||
| } | ||
| } | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only raising exceptions with a specific type (
ElasticsearchAuthenticationProcessingError) AND while invoking the request processing error handler (and not any other handler), results in 500 or 503; anything else goes to 401 .