-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Setting elastic password from stored hash #77036
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
Changes from all commits
0ffe056
621c735
1aaf1f9
9fd474c
7260cb4
92a48eb
9c543fa
2fcca5c
c28a3ad
1e09d7a
e985d4e
42cd17a
43ab0f5
d0fafd6
25a97bb
6227d1f
a4424d9
dce0ef5
91d590a
5322487
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| package org.elasticsearch.xpack.core.security.authc; | ||
|
|
||
| import org.elasticsearch.ElasticsearchSecurityException; | ||
| import org.elasticsearch.ElasticsearchStatusException; | ||
| import org.elasticsearch.common.util.concurrent.ThreadContext; | ||
| import org.elasticsearch.rest.RestRequest; | ||
| import org.elasticsearch.rest.RestStatus; | ||
|
|
@@ -20,12 +21,15 @@ | |
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import static org.elasticsearch.rest.RestStatus.INTERNAL_SERVER_ERROR; | ||
| import static org.elasticsearch.xpack.core.security.support.Exceptions.authenticationError; | ||
| import static org.elasticsearch.xpack.core.security.support.Exceptions.internalServerError; | ||
|
|
||
| /** | ||
| * The default implementation of a {@link AuthenticationFailureHandler}. This | ||
| * handler will return an exception with a RestStatus of 401 and default failure | ||
| * response headers like 'WWW-Authenticate' | ||
| * response headers like 'WWW-Authenticate' or an ElasticSecurityException with a | ||
| * RestStatus of 500 (INTERNAL_SERVER_ERROR) | ||
| */ | ||
| public class DefaultAuthenticationFailureHandler implements AuthenticationFailureHandler { | ||
| private volatile Map<String, List<String>> defaultFailureResponseHeaders; | ||
|
|
@@ -126,7 +130,7 @@ public ElasticsearchSecurityException authenticationRequired(String action, Thre | |
|
|
||
| /** | ||
| * Creates an instance of {@link ElasticsearchSecurityException} with | ||
| * {@link RestStatus#UNAUTHORIZED} status. | ||
| * {@link RestStatus#UNAUTHORIZED} or {@link RestStatus#INTERNAL_SERVER_ERROR}status. | ||
| * <p> | ||
| * Also adds default failure response headers as configured for this | ||
| * {@link DefaultAuthenticationFailureHandler} | ||
|
|
@@ -137,7 +141,10 @@ public ElasticsearchSecurityException authenticationRequired(String action, Thre | |
| * @param message error message | ||
| * @param t cause, if it is an instance of | ||
| * {@link ElasticsearchSecurityException} asserts status is | ||
| * RestStatus.UNAUTHORIZED and adds headers to it, else it will | ||
| * RestStatus.UNAUTHORIZED and adds headers to it, | ||
| * if it is an instance of {@link ElasticsearchStatusException} | ||
| * with status code 500 (INTERNAL_SERVER_ERROR) set status to | ||
| * RestStatus.INTERNAL_SERVER_ERROR, else it will | ||
| * create a new instance of {@link ElasticsearchSecurityException} | ||
| * @param args error message args | ||
| * @return instance of {@link ElasticsearchSecurityException} | ||
|
|
@@ -160,6 +167,9 @@ private ElasticsearchSecurityException createAuthenticationError(final String me | |
| } else { | ||
| containsNegotiateWithToken = false; | ||
| } | ||
| } else if (t instanceof ElasticsearchStatusException && ((ElasticsearchStatusException) t).status() == INTERNAL_SERVER_ERROR) { | ||
| ese = internalServerError(message, t, args); | ||
| containsNegotiateWithToken = false; | ||
| } else { | ||
|
Contributor
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. Seeing this code, I'm having second thoughts about the plan here.
Contributor
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.
It's the other way round, right ? We can simplify this very much by just returning 401 in all of our cases too. What we were trying to capture was a difference between:
The contract to the user is that
but maybe we're thinking this too much from the user's perspective? There is no mandate that a 401 is permanent or that the user should not retry the request etc. We were trying to use this to signal our logic.
Contributor
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.
Thanks for writing this down! On point 2) above. That something else that already set the password could be an identical process racing to set the password after a successful validation of the hash in the keystore. The racing process could be on the same node, writing the same password hash in the index, or on a different node, potentially writing a different one, or it can even be a password change by the user through the API or the cmd line tool. In this general case (without knowing who set the elastic user in the index in the meantime) returning 401 or 200 are both wrong (the only two error codes we can currently return for authn failures). Authentication has to be retried, given the new state where there is an elastic user in the .security index (which would make authn validate that instead of the keystore value). Point 3) sounds right to me, apart that maybe we should return 503. But I missed that there are a bunch of other error conditions that should probably be 503 and are now 401, eg shards unavailable for the .security index (though those are "read" ops and we're going to return 503 for "write" ones). Overall, after this conversation, I still think returning 503 if the "promised" password cannot be persisted (because the index or document cannot be created, or because they have been created in the meantime by something else) is the best course of action, because this "write" op during authentication is the only exception (which is not the approach this PR implements, FWIW). |
||
| ese = authenticationError(message, t, args); | ||
| containsNegotiateWithToken = false; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /* | ||
| * 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.ElasticsearchStatusException; | ||
| import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; | ||
| import org.elasticsearch.cluster.health.ClusterHealthStatus; | ||
| import org.elasticsearch.cluster.health.ClusterIndexHealth; | ||
| import org.elasticsearch.common.Priority; | ||
| 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.user.ElasticUser; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import static java.util.Collections.singletonMap; | ||
| import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue; | ||
| import static org.hamcrest.Matchers.equalTo; | ||
| import static org.hamcrest.Matchers.notNullValue; | ||
|
|
||
| public class PasswordHashAndBootstrapPasswordIntegTests extends SecuritySingleNodeTestCase { | ||
|
|
||
| @Override | ||
| public Settings nodeSettings() { | ||
| Settings customSettings = customSecuritySettingsSource.nodeSettings(0, Settings.EMPTY); | ||
| MockSecureSettings mockSecuritySettings = new MockSecureSettings(); | ||
| mockSecuritySettings.setString("autoconfiguration.password_hash", // password1 | ||
| "{PBKDF2_STRETCH}1000$JnmgicthPZkczB8MaQeJiV6IX43h7mSfPSzESqnYYSA=$OZKH5XFNK+M65mcKal6zgugWRcpl6wUXmSQZ6hPy+iw="); | ||
| mockSecuritySettings.setString("bootstrap.password", "password"); | ||
| Settings.Builder builder = Settings.builder().put(customSettings, true); | ||
| builder.setSecureSettings(mockSecuritySettings); | ||
| return builder.build(); | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean addMockHttpTransport() { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean transportSSLEnabled() { return false; } | ||
|
|
||
| public void testBootstrapPwdAuthenticatePwdHashNotIndexNotCreated() { | ||
| ElasticsearchStatusException e = expectThrows( ElasticsearchStatusException.class, | ||
| () -> client() | ||
| .filterWithHeader(singletonMap("Authorization", basicAuthHeaderValue(ElasticUser.NAME, | ||
| new SecureString("password1".toCharArray())))) | ||
| .admin() | ||
| .cluster() | ||
| .prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus() | ||
| .get() ); | ||
|
|
||
| assertThat(e.status(), equalTo(RestStatus.UNAUTHORIZED)); | ||
|
|
||
| ClusterHealthResponse response = client() | ||
| .filterWithHeader(singletonMap("Authorization", basicAuthHeaderValue(ElasticUser.NAME, | ||
| new SecureString("password".toCharArray())))) | ||
| .admin() | ||
| .cluster() | ||
| .prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus() | ||
| .get(); | ||
|
|
||
| assertThat(response, notNullValue()); | ||
| assertThat(response.isTimedOut(), equalTo(false)); | ||
| assertThat(response.status(), equalTo(RestStatus.OK)); | ||
| assertThat(response.getStatus(), equalTo(ClusterHealthStatus.GREEN)); | ||
| boolean securityIndexCreated = false; | ||
| for (Map.Entry<String, ClusterIndexHealth> indexEntry: response.getIndices().entrySet()) { | ||
| if (indexEntry.getKey().startsWith(".security")) { | ||
| securityIndexCreated = true; | ||
| break; | ||
| } | ||
| } | ||
| assertFalse(securityIndexCreated); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| /* | ||
| * 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.ElasticsearchStatusException; | ||
| import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; | ||
| import org.elasticsearch.action.support.PlainActionFuture; | ||
| import org.elasticsearch.client.Client; | ||
| import org.elasticsearch.cluster.health.ClusterHealthStatus; | ||
| import org.elasticsearch.cluster.health.ClusterIndexHealth; | ||
| import org.elasticsearch.common.Priority; | ||
| 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.SecuritySettingsSource; | ||
| 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.action.user.PutUserResponse; | ||
| import org.elasticsearch.xpack.core.security.user.ElasticUser; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.concurrent.ExecutionException; | ||
|
|
||
| import static java.util.Collections.singletonMap; | ||
| import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue; | ||
| import static org.hamcrest.Matchers.equalTo; | ||
| import static org.hamcrest.Matchers.notNullValue; | ||
|
|
||
| public class PasswordHashPromotionIntegTests extends SecuritySingleNodeTestCase { | ||
jkakavas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Override | ||
| public Settings nodeSettings() { | ||
| Settings customSettings = customSecuritySettingsSource.nodeSettings(0, Settings.EMPTY); | ||
| MockSecureSettings mockSecuritySettings = new MockSecureSettings(); | ||
| mockSecuritySettings.setString("autoconfiguration.password_hash", | ||
| "{PBKDF2_STRETCH}1000$JnmgicthPZkczB8MaQeJiV6IX43h7mSfPSzESqnYYSA=$OZKH5XFNK+M65mcKal6zgugWRcpl6wUXmSQZ6hPy+iw="); | ||
| Settings.Builder builder = Settings.builder().put(customSettings, false); // don't bring in bootstrap.password | ||
| builder.setSecureSettings(mockSecuritySettings); | ||
| return builder.build(); | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean addMockHttpTransport() { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean transportSSLEnabled() { return false; } | ||
|
|
||
| public void testAuthenticate() { | ||
| ClusterHealthResponse response = client() | ||
| .filterWithHeader(singletonMap("Authorization", basicAuthHeaderValue(ElasticUser.NAME, | ||
| new SecureString("password1".toCharArray())))) | ||
| .admin() | ||
| .cluster() | ||
| .prepareHealth() | ||
| .setWaitForEvents(Priority.LANGUID) | ||
| .setWaitForNodes(Integer.toString(1)) | ||
| .setWaitForGreenStatus() | ||
| .get(); | ||
|
|
||
| assertThat(response, notNullValue()); | ||
| assertThat(response.isTimedOut(), equalTo(false)); | ||
| assertThat(response.status(), equalTo(RestStatus.OK)); | ||
| assertThat(response.getStatus(), equalTo(ClusterHealthStatus.GREEN)); | ||
| assertTrue(securityIndexExists()); | ||
|
|
||
| // Now as the document exists let's try to authenticate again | ||
| response = client() | ||
| .filterWithHeader(singletonMap("Authorization", basicAuthHeaderValue(ElasticUser.NAME, | ||
| new SecureString("password1".toCharArray())))) | ||
| .admin() | ||
| .cluster() | ||
| .prepareHealth() | ||
| .get(); | ||
|
|
||
| assertThat(response, notNullValue()); | ||
| assertThat(response.status(), equalTo(RestStatus.OK)); | ||
| } | ||
|
|
||
| public void testInvalidPasswordHashNoSecurityIndex() { | ||
| ElasticsearchStatusException e = expectThrows( ElasticsearchStatusException.class, | ||
| () -> client() | ||
| .filterWithHeader(singletonMap("Authorization", basicAuthHeaderValue(ElasticUser.NAME, | ||
| new SecureString("password".toCharArray())))) | ||
| .admin() | ||
| .cluster() | ||
| .prepareHealth() | ||
| .get() ); | ||
|
|
||
| assertThat(e.status(), equalTo(RestStatus.UNAUTHORIZED)); | ||
| assertFalse(securityIndexExists()); | ||
| } | ||
|
|
||
| public void testSecurityIndexExistsButElasticuserNot() throws Exception { | ||
| // Create a user to create the Index | ||
| createUser("user", SecuritySettingsSource.TEST_PASSWORD_HASHED.toCharArray(), Strings.EMPTY_ARRAY); | ||
| assertTrue(securityIndexExists()); | ||
|
|
||
| ClusterHealthResponse response = client() | ||
| .filterWithHeader(singletonMap("Authorization", basicAuthHeaderValue(ElasticUser.NAME, | ||
| new SecureString("password1".toCharArray())))) | ||
| .admin() | ||
| .cluster() | ||
| .prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus() | ||
| .get(); | ||
| assertThat(response, notNullValue()); | ||
| assertThat(response.isTimedOut(), equalTo(false)); | ||
| assertThat(response.status(), equalTo(RestStatus.OK)); | ||
| } | ||
|
|
||
| // TODO: Add a test a test where we index the document for the elastic user manually and then we try to authenticate with the | ||
| // autoconfigured password hash and it fails | ||
|
|
||
| private void createUser(String username, char[] password, String[] roles) throws ExecutionException, InterruptedException { | ||
| final PutUserRequest putUserRequest = new PutUserRequest(); | ||
| putUserRequest.username(username); | ||
| putUserRequest.roles(roles); | ||
| putUserRequest.passwordHash(password); | ||
| PlainActionFuture<PutUserResponse> listener = new PlainActionFuture<>(); | ||
| final Client client = client().filterWithHeader( | ||
| Map.of("Authorization", basicAuthHeaderValue("test_user", new SecureString("x-pack-test-password" | ||
| .toCharArray())))); | ||
| client.execute(PutUserAction.INSTANCE, putUserRequest, listener); | ||
| final PutUserResponse putUserResponse = listener.get(); | ||
| assertTrue(putUserResponse.created()); | ||
| } | ||
|
|
||
| private boolean securityIndexExists () { | ||
| ClusterHealthResponse response = client() | ||
| .filterWithHeader(singletonMap("Authorization", basicAuthHeaderValue("test_user", | ||
| new SecureString("x-pack-test-password".toCharArray())))) | ||
| .admin() | ||
| .cluster() | ||
| .prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus() | ||
| .get(); | ||
|
|
||
| boolean securityIndexExists = false; | ||
| for (Map.Entry<String, ClusterIndexHealth> indexEntry: response.getIndices().entrySet()) { | ||
| if (indexEntry.getKey().startsWith(".security")) { | ||
| securityIndexExists = true; | ||
| break; | ||
| } | ||
| } | ||
| return securityIndexExists; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.