-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Autogenerate and print elastic pwd on startup #77291
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
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
7f7b77c
Autogenerate and print elastic pwd on startup
jkakavas 667baff
rename method
jkakavas 1789a8e
cleanup
jkakavas 3b47ec2
Fix packaging test
jkakavas 12867e4
spotting less and less as time goes by
jkakavas 79a6c21
feedback
jkakavas 2b618db
Handle Kibana password too
jkakavas 19ccb55
add package tests
jkakavas 7a328b1
Reduce character set of autogenerated passwords
jkakavas 6473ece
Merge remote-tracking branch 'origin/master' into print-elastic-password
jkakavas 8f40211
Create keystore before ES runs for the first time
jkakavas a9c1b9e
address feedback
jkakavas c653951
wire async actions a little better
jkakavas fdfe14b
Merge remote-tracking branch 'origin/master' into print-elastic-password
jkakavas a63144b
Fix username on output and adjust test stdout capturing
jkakavas 7f08b9d
disable autoconfiguration in tests, when not needed/used
jkakavas f46f815
Make test class archive specific ( Docker specific incoming )
jkakavas 1c2376c
mute some more
jkakavas 3277e1a
not ready yet
jkakavas ccaac5a
disable auto-configuration for test clusters that don't need it
jkakavas 6d0c9e4
more QA tests that don't need user autoconfiguration
jkakavas db255ab
_Actually_ mute on windows
jkakavas 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
102 changes: 102 additions & 0 deletions
102
...s/src/test/java/org/elasticsearch/packaging/test/ArchiveGenerateInitialPasswordTests.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,102 @@ | ||
| /* | ||
| * 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.packaging.test; | ||
|
|
||
| import org.apache.http.client.fluent.Request; | ||
| import org.elasticsearch.packaging.util.Distribution; | ||
| import org.elasticsearch.packaging.util.ServerUtils; | ||
| import org.elasticsearch.packaging.util.Shell; | ||
| import org.junit.BeforeClass; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| import static org.elasticsearch.packaging.util.Archives.installArchive; | ||
| import static org.elasticsearch.packaging.util.Archives.verifyArchiveInstallation; | ||
| import static org.hamcrest.CoreMatchers.containsString; | ||
| import static org.hamcrest.Matchers.equalTo; | ||
| import static org.hamcrest.Matchers.is; | ||
| import static org.junit.Assume.assumeTrue; | ||
|
|
||
| public class ArchiveGenerateInitialPasswordTests extends PackagingTestCase { | ||
|
|
||
| private static final Pattern PASSWORD_REGEX = Pattern.compile("Password for the (\\w+) user is: (.+)$", Pattern.MULTILINE); | ||
|
|
||
| @BeforeClass | ||
| public static void filterDistros() { | ||
| assumeTrue("archives only", distribution.isArchive()); | ||
| } | ||
|
|
||
| public void test10Install() throws Exception { | ||
| installation = installArchive(sh, distribution()); | ||
| // Enable security for these tests only where it is necessary, until we can enable it for all | ||
| ServerUtils.enableSecurityFeatures(installation); | ||
| verifyArchiveInstallation(installation, distribution()); | ||
| } | ||
|
|
||
| public void test20NoAutoGenerationWhenBootstrapPassword() throws Exception { | ||
| /* Windows issue awaits fix: https://github.com/elastic/elasticsearch/issues/49340 */ | ||
| assumeTrue("expect command isn't on Windows", distribution.platform != Distribution.Platform.WINDOWS); | ||
| installation.executables().keystoreTool.run("create"); | ||
| installation.executables().keystoreTool.run("add --stdin bootstrap.password", "some-password-here"); | ||
| Shell.Result result = runElasticsearchStartCommand(null, false, true); | ||
| Map<String, String> usersAndPasswords = parseUsersAndPasswords(result.stdout); | ||
| assertThat(usersAndPasswords.isEmpty(), is(true)); | ||
| String response = ServerUtils.makeRequest(Request.Get("http://localhost:9200"), "elastic", "some-password-here", null); | ||
| assertThat(response, containsString("You Know, for Search")); | ||
| } | ||
|
|
||
| public void test30NoAutoGenerationWhenAutoConfigurationDisabled() throws Exception { | ||
| /* Windows issue awaits fix: https://github.com/elastic/elasticsearch/issues/49340 */ | ||
| assumeTrue("expect command isn't on Windows", distribution.platform != Distribution.Platform.WINDOWS); | ||
| stopElasticsearch(); | ||
| installation.executables().keystoreTool.run("remove bootstrap.password"); | ||
| ServerUtils.disableSecurityAutoConfiguration(installation); | ||
| Shell.Result result = runElasticsearchStartCommand(null, false, true); | ||
| Map<String, String> usersAndPasswords = parseUsersAndPasswords(result.stdout); | ||
| assertThat(usersAndPasswords.isEmpty(), is(true)); | ||
| } | ||
|
|
||
| public void test40VerifyAutogeneratedCredentials() throws Exception { | ||
| /* Windows issue awaits fix: https://github.com/elastic/elasticsearch/issues/49340 */ | ||
| assumeTrue("expect command isn't on Windows", distribution.platform != Distribution.Platform.WINDOWS); | ||
| stopElasticsearch(); | ||
| ServerUtils.enableSecurityAutoConfiguration(installation); | ||
| Shell.Result result = runElasticsearchStartCommand(null, false, true); | ||
| Map<String, String> usersAndPasswords = parseUsersAndPasswords(result.stdout); | ||
| assertThat(usersAndPasswords.size(), equalTo(2)); | ||
| assertThat(usersAndPasswords.containsKey("elastic"), is(true)); | ||
| assertThat(usersAndPasswords.containsKey("kibana_system"), is(true)); | ||
| for (Map.Entry<String, String> userpass : usersAndPasswords.entrySet()) { | ||
| String response = ServerUtils.makeRequest(Request.Get("http://localhost:9200"), userpass.getKey(), userpass.getValue(), null); | ||
| assertThat(response, containsString("You Know, for Search")); | ||
| } | ||
| } | ||
|
|
||
| public void test50PasswordAutogenerationOnlyOnce() throws Exception { | ||
| /* Windows issue awaits fix: https://github.com/elastic/elasticsearch/issues/49340 */ | ||
| assumeTrue("expect command isn't on Windows", distribution.platform != Distribution.Platform.WINDOWS); | ||
| stopElasticsearch(); | ||
| Shell.Result result = runElasticsearchStartCommand(null, false, true); | ||
| Map<String, String> usersAndPasswords = parseUsersAndPasswords(result.stdout); | ||
| assertThat(usersAndPasswords.isEmpty(), is(true)); | ||
| } | ||
|
|
||
| private Map<String, String> parseUsersAndPasswords(String output) { | ||
| Matcher matcher = PASSWORD_REGEX.matcher(output); | ||
| assertNotNull(matcher); | ||
| Map<String, String> usersAndPasswords = new HashMap<>(); | ||
| while (matcher.find()) { | ||
| usersAndPasswords.put(matcher.group(1), matcher.group(2)); | ||
| } | ||
| return usersAndPasswords; | ||
| } | ||
| } |
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
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
120 changes: 120 additions & 0 deletions
120
...in/java/org/elasticsearch/xpack/security/GenerateInitialBuiltinUsersPasswordListener.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,120 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import org.apache.log4j.LogManager; | ||
| import org.apache.log4j.Logger; | ||
| import org.elasticsearch.action.ActionListener; | ||
| import org.elasticsearch.action.DocWriteRequest; | ||
| import org.elasticsearch.action.support.WriteRequest; | ||
| import org.elasticsearch.common.settings.SecureString; | ||
| import org.elasticsearch.index.engine.VersionConflictEngineException; | ||
| import org.elasticsearch.xpack.core.security.user.ElasticUser; | ||
| import org.elasticsearch.xpack.core.security.user.KibanaSystemUser; | ||
| import org.elasticsearch.xpack.security.authc.esnative.NativeUsersStore; | ||
| import org.elasticsearch.xpack.security.support.SecurityIndexManager; | ||
|
|
||
| import java.util.function.BiConsumer; | ||
|
|
||
| import static org.elasticsearch.xpack.security.tool.CommandUtils.generatePassword; | ||
|
|
||
| public class GenerateInitialBuiltinUsersPasswordListener implements BiConsumer<SecurityIndexManager.State, SecurityIndexManager.State> { | ||
|
|
||
| private static final Logger LOGGER = LogManager.getLogger(GenerateInitialBuiltinUsersPasswordListener.class); | ||
| private NativeUsersStore nativeUsersStore; | ||
| private SecurityIndexManager securityIndexManager; | ||
|
|
||
| public GenerateInitialBuiltinUsersPasswordListener(NativeUsersStore nativeUsersStore, SecurityIndexManager securityIndexManager) { | ||
| this.nativeUsersStore = nativeUsersStore; | ||
| this.securityIndexManager = securityIndexManager; | ||
| } | ||
|
|
||
| @Override | ||
| public void accept(SecurityIndexManager.State previousState, SecurityIndexManager.State currentState) { | ||
| if (previousState.equals(SecurityIndexManager.State.UNRECOVERED_STATE) | ||
| && currentState.equals(SecurityIndexManager.State.UNRECOVERED_STATE) == false | ||
| && securityIndexManager.indexExists() == false) { | ||
|
|
||
| final SecureString elasticPassword = new SecureString(generatePassword(20)); | ||
| final SecureString kibanaSystemPassword = new SecureString(generatePassword(20)); | ||
| nativeUsersStore | ||
| .updateReservedUser( | ||
| ElasticUser.NAME, | ||
| elasticPassword.getChars(), | ||
| DocWriteRequest.OpType.CREATE, | ||
| WriteRequest.RefreshPolicy.IMMEDIATE, | ||
| ActionListener.wrap(result -> { | ||
| nativeUsersStore | ||
| .updateReservedUser( | ||
| KibanaSystemUser.NAME, | ||
| kibanaSystemPassword.getChars(), | ||
| DocWriteRequest.OpType.CREATE, | ||
| WriteRequest.RefreshPolicy.IMMEDIATE, | ||
| ActionListener.wrap( | ||
| r -> { | ||
| outputOnSuccess(elasticPassword, kibanaSystemPassword); | ||
| }, this::outputOnError | ||
| ) | ||
| ); | ||
| }, this::outputOnError)); | ||
| securityIndexManager.removeStateListener(this); | ||
| } | ||
| } | ||
|
|
||
| private void outputOnSuccess(SecureString elasticPassword, SecureString kibanaSystemPassword) { | ||
| LOGGER.info(""); | ||
| LOGGER.info("-----------------------------------------------------------------"); | ||
| LOGGER.info(""); | ||
| LOGGER.info(""); | ||
| LOGGER.info(""); | ||
| LOGGER.info("Password for the elastic user is: " + elasticPassword); | ||
| LOGGER.info(""); | ||
| LOGGER.info(""); | ||
| LOGGER.info(""); | ||
| LOGGER.info("Password for the kibana_system user is: " + kibanaSystemPassword); | ||
| LOGGER.info(""); | ||
| LOGGER.info(""); | ||
| LOGGER.info("Please note these down as they will not be shown again."); | ||
| LOGGER.info(""); | ||
| LOGGER.info("You can use 'bin/elasticsearch-reset-elastic-password' at any time"); | ||
| LOGGER.info("in order to reset the password for the elastic user."); | ||
| LOGGER.info(""); | ||
| LOGGER.info(""); | ||
| LOGGER.info("You can use 'bin/elasticsearch-reset-kibana-system-password' at any time"); | ||
| LOGGER.info("in order to reset the password for the kibana_system user."); | ||
| LOGGER.info(""); | ||
| LOGGER.info(""); | ||
| LOGGER.info(""); | ||
| LOGGER.info("-----------------------------------------------------------------"); | ||
| LOGGER.info(""); | ||
| } | ||
|
|
||
| private void outputOnError(Exception e) { | ||
| if (e instanceof VersionConflictEngineException == false) { | ||
| LOGGER.info(""); | ||
| LOGGER.info("-----------------------------------------------------------------"); | ||
| LOGGER.info(""); | ||
| LOGGER.info(""); | ||
| LOGGER.info(""); | ||
| LOGGER.info("Failed to set the password for the elastic and kibana-system users "); | ||
| LOGGER.info("automatically"); | ||
| LOGGER.info(""); | ||
| LOGGER.info("You can use 'bin/elasticsearch-reset-elastic-password'"); | ||
| LOGGER.info("in order to set the password for the elastic user."); | ||
| LOGGER.info(""); | ||
| LOGGER.info(""); | ||
| LOGGER.info("You can use 'bin/elasticsearch-reset-kibana-system-password'"); | ||
| LOGGER.info("in order to set the password for the kibana_system user."); | ||
| LOGGER.info(""); | ||
| LOGGER.info(""); | ||
| LOGGER.info(""); | ||
| LOGGER.info("-----------------------------------------------------------------"); | ||
| LOGGER.info(""); | ||
| } | ||
| } | ||
| } | ||
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.