-
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
Changes from 3 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 |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ | |
| import org.elasticsearch.common.network.NetworkService; | ||
| import org.elasticsearch.common.settings.ClusterSettings; | ||
| import org.elasticsearch.common.settings.IndexScopedSettings; | ||
| import org.elasticsearch.common.settings.SecureString; | ||
| import org.elasticsearch.common.settings.Setting; | ||
| import org.elasticsearch.common.settings.Setting.Property; | ||
| import org.elasticsearch.common.settings.Settings; | ||
|
|
@@ -49,6 +50,7 @@ | |
| import org.elasticsearch.env.NodeEnvironment; | ||
| import org.elasticsearch.http.HttpServerTransport; | ||
| import org.elasticsearch.index.IndexModule; | ||
| import org.elasticsearch.index.engine.VersionConflictEngineException; | ||
| import org.elasticsearch.indices.ExecutorNames; | ||
| import org.elasticsearch.indices.SystemIndexDescriptor; | ||
| import org.elasticsearch.indices.breaker.CircuitBreakerService; | ||
|
|
@@ -159,6 +161,7 @@ | |
| import org.elasticsearch.xpack.core.security.index.RestrictedIndicesNames; | ||
| import org.elasticsearch.xpack.core.security.support.Automatons; | ||
| import org.elasticsearch.xpack.core.security.user.AnonymousUser; | ||
| import org.elasticsearch.xpack.core.security.user.ElasticUser; | ||
| import org.elasticsearch.xpack.core.ssl.SSLConfigurationSettings; | ||
| import org.elasticsearch.xpack.core.ssl.SSLService; | ||
| import org.elasticsearch.xpack.core.ssl.TLSLicenseBootstrapCheck; | ||
|
|
@@ -339,10 +342,12 @@ | |
| import static org.elasticsearch.xpack.core.XPackSettings.HTTP_SSL_ENABLED; | ||
| import static org.elasticsearch.xpack.core.security.index.RestrictedIndicesNames.SECURITY_MAIN_ALIAS; | ||
| import static org.elasticsearch.xpack.core.security.index.RestrictedIndicesNames.SECURITY_TOKENS_ALIAS; | ||
| import static org.elasticsearch.xpack.security.authc.esnative.ReservedRealm.BOOTSTRAP_ELASTIC_PASSWORD; | ||
| import static org.elasticsearch.xpack.security.operator.OperatorPrivileges.OPERATOR_PRIVILEGES_ENABLED; | ||
| import static org.elasticsearch.xpack.security.support.SecurityIndexManager.INTERNAL_MAIN_INDEX_FORMAT; | ||
| import static org.elasticsearch.xpack.security.support.SecurityIndexManager.INTERNAL_TOKENS_INDEX_FORMAT; | ||
| import static org.elasticsearch.xpack.security.support.SecurityIndexManager.SECURITY_VERSION_STRING; | ||
| import static org.elasticsearch.xpack.security.tool.CommandUtils.generatePassword; | ||
|
|
||
| public class Security extends Plugin implements SystemIndexPlugin, IngestPlugin, NetworkPlugin, ClusterPlugin, | ||
| DiscoveryPlugin, MapperPlugin, ExtensiblePlugin, SearchPlugin { | ||
|
|
@@ -390,6 +395,7 @@ public class Security extends Plugin implements SystemIndexPlugin, IngestPlugin, | |
| private final List<SecurityExtension> securityExtensions = new ArrayList<>(); | ||
| private final SetOnce<Transport> transportReference = new SetOnce<>(); | ||
| private final SetOnce<ScriptService> scriptServiceReference = new SetOnce<>(); | ||
| private final SetOnce<NativeUsersStore> nativeUsersStoreReference = new SetOnce<>(); | ||
|
|
||
| public Security(Settings settings, final Path configPath) { | ||
| this(settings, configPath, Collections.emptyList()); | ||
|
|
@@ -453,7 +459,6 @@ Collection<Object> createComponents(Client client, ThreadPool threadPool, Cluste | |
| } | ||
|
|
||
| scriptServiceReference.set(scriptService); | ||
|
|
||
| // We need to construct the checks here while the secure settings are still available. | ||
| // If we wait until #getBoostrapChecks the secure settings will have been cleared/closed. | ||
| final List<BootstrapCheck> checks = new ArrayList<>(); | ||
|
|
@@ -492,9 +497,12 @@ Collection<Object> createComponents(Client client, ThreadPool threadPool, Cluste | |
| ); | ||
| this.tokenService.set(tokenService); | ||
| components.add(tokenService); | ||
|
|
||
| if (BOOTSTRAP_ELASTIC_PASSWORD.exists(settings) == false) { | ||
| securityIndex.get().addStateListener(this::generateElasticPassword); | ||
| } | ||
jkakavas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // realms construction | ||
| final NativeUsersStore nativeUsersStore = new NativeUsersStore(settings, client, securityIndex.get()); | ||
| nativeUsersStoreReference.set(nativeUsersStore); | ||
| final NativeRoleMappingStore nativeRoleMappingStore = new NativeRoleMappingStore(settings, client, securityIndex.get(), | ||
| scriptService); | ||
| final AnonymousUser anonymousUser = new AnonymousUser(settings); | ||
|
|
@@ -628,6 +636,61 @@ auditTrailService, failureHandler, threadPool, anonymousUser, getAuthorizationEn | |
| return components; | ||
| } | ||
|
|
||
| private void generateElasticPassword(SecurityIndexManager.State previousState, SecurityIndexManager.State currentState) { | ||
jkakavas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (previousState.equals(SecurityIndexManager.State.UNRECOVERED_STATE) | ||
| && currentState.equals(SecurityIndexManager.State.UNRECOVERED_STATE) == false | ||
| && securityIndex.get().indexExists() == false) { | ||
|
|
||
| final SecureString elasticPassword = new SecureString(generatePassword(20)); | ||
| nativeUsersStoreReference.get() | ||
| .createReservedUser( | ||
| ElasticUser.NAME, | ||
| elasticPassword.getChars(), | ||
| ActionListener.wrap( | ||
| r -> { | ||
|
||
| logger.info(""); | ||
| logger.info("-----------------------------------------------------------------"); | ||
| logger.info(""); | ||
| logger.info(""); | ||
| logger.info(""); | ||
| logger.info("Password for the elastic user is: "); | ||
| logger.info(elasticPassword); | ||
| logger.info(""); | ||
| logger.info(""); | ||
| logger.info("Please note this down as it 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(""); | ||
| logger.info("-----------------------------------------------------------------"); | ||
| logger.info(""); | ||
jkakavas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }, | ||
| 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 user 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(""); | ||
| logger.info("-----------------------------------------------------------------"); | ||
| logger.info(""); | ||
| } | ||
| logger.warn(e); | ||
| } | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| private AuthorizationEngine getAuthorizationEngine() { | ||
| AuthorizationEngine authorizationEngine = null; | ||
| String extensionName = null; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.