-
Notifications
You must be signed in to change notification settings - Fork 25.8k
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
Changes from 16 commits
10f3474
6076aab
674202d
98e3ed9
ccd1dbe
c2092d8
0b89640
e8b390c
93abfb2
ed8646f
53443bd
5645b7d
14e1f36
a7e4c49
4128d61
59c2542
299c710
0ecbead
cf0f4b3
b249f9c
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 |
|---|---|---|
| @@ -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); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| */ | ||
| package org.elasticsearch.xpack.core.security.authc; | ||
|
|
||
| import org.elasticsearch.ElasticsearchAuthenticationProcessingError; | ||
| import org.elasticsearch.ElasticsearchSecurityException; | ||
| import org.elasticsearch.common.util.concurrent.ThreadContext; | ||
| import org.elasticsearch.rest.RestRequest; | ||
|
|
@@ -100,12 +101,20 @@ public ElasticsearchSecurityException failedAuthentication(TransportMessage mess | |
|
|
||
| @Override | ||
| public ElasticsearchSecurityException exceptionProcessingRequest(RestRequest request, Exception e, ThreadContext context) { | ||
| // a couple of authn processing errors can also return 500 & 503, besides the obvious 401 | ||
| if (e instanceof ElasticsearchAuthenticationProcessingError) { | ||
| return (ElasticsearchAuthenticationProcessingError) e; | ||
| } | ||
| return createAuthenticationError("error attempting to authenticate request", e, (Object[]) null); | ||
|
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. Only raising exceptions with a specific type ( |
||
| } | ||
|
|
||
| @Override | ||
| public ElasticsearchSecurityException exceptionProcessingRequest(TransportMessage message, String action, Exception e, | ||
| ThreadContext context) { | ||
| // a couple of authn processing errors can also return 500 & 503, besides the obvious 401 | ||
| if (e instanceof ElasticsearchAuthenticationProcessingError) { | ||
| return (ElasticsearchAuthenticationProcessingError) e; | ||
| } | ||
| return createAuthenticationError("error attempting to authenticate request", e, (Object[]) null); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
|
|
||
| import io.netty.util.ThreadDeathWatcher; | ||
| import io.netty.util.concurrent.GlobalEventExecutor; | ||
|
|
||
| import org.apache.http.HttpHost; | ||
| import org.elasticsearch.action.admin.cluster.node.info.NodeInfo; | ||
| import org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse; | ||
|
|
@@ -58,12 +59,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 +77,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 +171,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(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /* | ||
| * 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.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.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".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".toCharArray()))); | ||
albertzaharovits marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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".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()); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.