-
Notifications
You must be signed in to change notification settings - Fork 25.8k
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
Changes from 10 commits
7f7b77c
667baff
1789a8e
3b47ec2
12867e4
79a6c21
2b618db
19ccb55
7a328b1
6473ece
8f40211
a9c1b9e
c653951
fdfe14b
a63144b
7f08b9d
f46f815
1c2376c
3277e1a
ccaac5a
6d0c9e4
db255ab
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,93 @@ | ||
| /* | ||
| * 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.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 GenerateInitialPasswordTests extends PackagingTestCase { | ||
|
|
||
| private static final Pattern PASSWORD_REGEX = Pattern.compile("Password for the (\\w+) user is: (.+)$"); | ||
|
|
||
| @BeforeClass | ||
| public static void filterDistros() { | ||
| assumeTrue("archives and docker only", distribution.isArchive() || distribution.isDocker()); | ||
| } | ||
|
|
||
| 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 { | ||
| 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 { | ||
| 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 { | ||
| 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 { | ||
| 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> userpases = new HashMap<>(); | ||
| while (matcher.find()) { | ||
| userpases.put(matcher.group(1), matcher.group(2)); | ||
| } | ||
| return userpases; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| /* | ||
| * 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.GroupedActionListener; | ||
| 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)); | ||
| final GroupedActionListener<Void> changePasswordListener = new GroupedActionListener<>( | ||
|
||
| ActionListener.wrap(() -> { | ||
| nativeUsersStore | ||
| .updateReservedUser( | ||
| KibanaSystemUser.NAME, | ||
| kibanaSystemPassword.getChars(), | ||
| DocWriteRequest.OpType.CREATE, | ||
| WriteRequest.RefreshPolicy.IMMEDIATE, | ||
| ActionListener.wrap( | ||
| r -> { | ||
| 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 user is: " + kibanaSystemPassword); | ||
| LOGGER.info(""); | ||
| LOGGER.info(""); | ||
| LOGGER.info("Please note these down as the will not be shown again."); | ||
jkakavas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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(""); | ||
| }, | ||
| 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(""); | ||
| } | ||
| LOGGER.warn(e); | ||
| } | ||
| ) | ||
| ); | ||
| }),1); | ||
| nativeUsersStore | ||
| .updateReservedUser( | ||
| ElasticUser.NAME, | ||
| elasticPassword.getChars(), | ||
| DocWriteRequest.OpType.CREATE, | ||
| WriteRequest.RefreshPolicy.IMMEDIATE, | ||
| changePasswordListener); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.