Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.Version;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.bulk.BackoffPolicy;
import org.elasticsearch.action.support.GroupedActionListener;
import org.elasticsearch.bootstrap.BootstrapInfo;
import org.elasticsearch.bootstrap.ConsoleLoader;
import org.elasticsearch.client.internal.Client;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.util.concurrent.AbstractRunnable;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.env.Environment;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.xpack.core.ssl.SSLService;
import org.elasticsearch.xpack.security.authc.esnative.NativeUsersStore;
import org.elasticsearch.xpack.security.enrollment.InternalEnrollmentTokenGenerator;
import org.elasticsearch.xpack.security.support.SecurityIndexManager;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import static org.elasticsearch.xpack.core.XPackSettings.ENROLLMENT_ENABLED;
import static org.elasticsearch.xpack.security.authc.esnative.ReservedRealm.AUTOCONFIG_ELASTIC_PASSWORD_HASH;
import static org.elasticsearch.xpack.security.authc.esnative.ReservedRealm.BOOTSTRAP_ELASTIC_PASSWORD;
import static org.elasticsearch.xpack.security.tool.CommandUtils.generatePassword;
public class InitialNodeSecurityAutoConfiguration {
private static final Logger LOGGER = LogManager.getLogger(InitialNodeSecurityAutoConfiguration.class);
private static final BackoffPolicy BACKOFF_POLICY = BackoffPolicy.exponentialBackoff();
private InitialNodeSecurityAutoConfiguration() {
throw new IllegalStateException("Class should not be instantiated");
}
/**
* Generates and displays a password for the elastic superuser, an enrollment token for kibana and an enrollment token for es
* nodes, the first time a node starts as the first node in a cluster, when a terminal is attached.
*/
public static void maybeGenerateEnrollmentTokensAndElasticCredentialsOnNodeStartup(
NativeUsersStore nativeUsersStore,
SecurityIndexManager securityIndexManager,
SSLService sslService,
Client client,
Environment environment,
OnNodeStartedListener onNodeStartedListener,
ThreadPool threadPool
) {
// Assume the following auto-configuration must NOT run if enrollment is disabled when the node starts,
// so no credentials or HTTPS CA fingerprint will be displayed in this case (in addition to no enrollment
// tokens being generated).
// This is not ideal because the {@code ENROLLMENT_ENABLED} setting is now interpreted as
// "did the pre-startup configuration completed", in order to generate/display information assuming
// and relying on that configuration being done.
// TODO maybe we can improve the "did pre-start-up config run" check
if (false == ENROLLMENT_ENABLED.get(environment.settings())) {
return;
}
final InternalEnrollmentTokenGenerator enrollmentTokenGenerator = new InternalEnrollmentTokenGenerator(
environment,
sslService,
client
);
final ConsoleLoader.Console console = getConsole();
if (console == null) {
LOGGER.info(
"Auto-configuration will not generate a password for the elastic built-in superuser, as we cannot "
+ " determine if there is a terminal attached to the elasticsearch process. You can use the"
+ " `bin/elasticsearch-reset-password` tool to set the password for the elastic user."
);
return;
}
// if enrollment is enabled, we assume (and document this assumption) that the node is auto-configured in a specific way
// wrt to TLS and cluster formation
securityIndexManager.onStateRecovered(securityIndexState -> {
if (false == securityIndexState.indexExists()) {
// a starting node with {@code ENROLLMENT_ENABLED} set to true, and with no .security index,
// must be the initial node of a cluster (starting for the first time and forming a cluster by itself)
// Not always true, but in the cases where it's not (which involve deleting the .security index which
// is now a system index), it's not a catastrophic position to be in either, because it only entails
// that new tokens and possibly credentials are generated anew
// TODO maybe we can improve the check that this is indeed the initial node
// a lot of stuff runs when a node just started, and the autoconfiguration is not time-critical
// and nothing else depends on it; be a good sport and wait a couple
onNodeStartedListener.run(() -> threadPool.schedule(new AbstractRunnable() {
@Override
public void onFailure(Exception e) {
LOGGER.error("Unexpected exception when auto configuring the initial node for Security", e);
}
@Override
protected void doRun() {
// the HTTP address is guaranteed to be bound only after the node started
String fingerprint;
try {
fingerprint = enrollmentTokenGenerator.getHttpsCaFingerprint();
LOGGER.info(
"HTTPS has been configured with automatically generated certificates, "
+ "and the CA's hex-encoded SHA-256 fingerprint is ["
+ fingerprint
+ "]"
);
} catch (Exception e) {
fingerprint = null;
LOGGER.error("Failed to compute the HTTPS CA fingerprint, probably the certs are not auto-generated", e);
}
final String httpsCaFingerprint = fingerprint;
GroupedActionListener