|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.security.enrollment.tool; |
| 9 | + |
| 10 | +import joptsimple.OptionSet; |
| 11 | +import joptsimple.OptionSpec; |
| 12 | + |
| 13 | +import org.elasticsearch.cli.ExitCodes; |
| 14 | +import org.elasticsearch.cli.KeyStoreAwareCommand; |
| 15 | +import org.elasticsearch.cli.Terminal; |
| 16 | +import org.elasticsearch.cli.UserException; |
| 17 | +import org.elasticsearch.common.Strings; |
| 18 | +import org.elasticsearch.common.settings.KeyStoreWrapper; |
| 19 | +import org.elasticsearch.common.settings.SecureString; |
| 20 | +import org.elasticsearch.common.settings.Settings; |
| 21 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 22 | +import org.elasticsearch.common.xcontent.json.JsonXContent; |
| 23 | +import org.elasticsearch.core.CheckedFunction; |
| 24 | +import org.elasticsearch.env.Environment; |
| 25 | +import org.elasticsearch.xpack.core.security.user.ElasticUser; |
| 26 | +import org.elasticsearch.xpack.security.authc.esnative.ReservedRealm; |
| 27 | +import org.elasticsearch.xpack.security.enrollment.EnrollmentToken; |
| 28 | +import org.elasticsearch.xpack.security.enrollment.EnrollmentTokenGenerator; |
| 29 | +import org.elasticsearch.xpack.security.tool.CommandLineHttpClient; |
| 30 | +import org.elasticsearch.xpack.security.tool.HttpResponse; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | +import java.net.HttpURLConnection; |
| 34 | +import java.net.MalformedURLException; |
| 35 | +import java.net.URISyntaxException; |
| 36 | +import java.net.URL; |
| 37 | +import java.security.SecureRandom; |
| 38 | +import java.util.function.Function; |
| 39 | + |
| 40 | +import static org.elasticsearch.xpack.security.tool.CommandLineHttpClient.createURL; |
| 41 | + |
| 42 | +public class BootstrapPasswordAndEnrollmentTokenForInitialNode extends KeyStoreAwareCommand { |
| 43 | + private final CheckedFunction<Environment, EnrollmentTokenGenerator, Exception> createEnrollmentTokenFunction; |
| 44 | + private final Function<Environment, CommandLineHttpClient> clientFunction; |
| 45 | + private final CheckedFunction<Environment, KeyStoreWrapper, Exception> keyStoreFunction; |
| 46 | + private final OptionSpec<Void> includeNodeEnrollmentToken; |
| 47 | + private final SecureRandom secureRandom = new SecureRandom(); |
| 48 | + |
| 49 | + BootstrapPasswordAndEnrollmentTokenForInitialNode() { |
| 50 | + this( |
| 51 | + environment -> new CommandLineHttpClient(environment), |
| 52 | + environment -> KeyStoreWrapper.load(environment.configFile()), |
| 53 | + environment -> new EnrollmentTokenGenerator(environment) |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + BootstrapPasswordAndEnrollmentTokenForInitialNode(Function<Environment, CommandLineHttpClient> clientFunction, |
| 58 | + CheckedFunction<Environment, KeyStoreWrapper, Exception> keyStoreFunction, |
| 59 | + CheckedFunction<Environment, EnrollmentTokenGenerator, Exception> |
| 60 | + createEnrollmentTokenFunction){ |
| 61 | + super("Set elastic password and generate enrollment token for initial node"); |
| 62 | + this.clientFunction = clientFunction; |
| 63 | + this.keyStoreFunction = keyStoreFunction; |
| 64 | + this.createEnrollmentTokenFunction = createEnrollmentTokenFunction; |
| 65 | + includeNodeEnrollmentToken = parser.accepts("include-node-enrollment-token", "determine that we have to generate " + |
| 66 | + "a node enrollment token"); |
| 67 | + } |
| 68 | + |
| 69 | + public static void main(String[] args) throws Exception { |
| 70 | + exit(new BootstrapPasswordAndEnrollmentTokenForInitialNode().main(args, Terminal.DEFAULT)); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + protected void execute(Terminal terminal, OptionSet options, Environment env) throws Exception { |
| 75 | + final SecureString keystorePassword; |
| 76 | + try { |
| 77 | + keystorePassword = new SecureString(terminal.readSecret("")); |
| 78 | + } catch (Exception e) { |
| 79 | + throw new UserException(ExitCodes.USAGE, null); |
| 80 | + } |
| 81 | + |
| 82 | + final Environment secureEnvironment = readSecureSettings(env, keystorePassword); |
| 83 | + final CommandLineHttpClient client = clientFunction.apply(secureEnvironment); |
| 84 | + final EnrollmentTokenGenerator enrollmentTokenGenerator = createEnrollmentTokenFunction.apply(secureEnvironment); |
| 85 | + final SecureString bootstrapPassword = ReservedRealm.BOOTSTRAP_ELASTIC_PASSWORD.get(secureEnvironment.settings()); |
| 86 | + try { |
| 87 | + String output; |
| 88 | + client.checkClusterHealthWithRetriesWaitingForCluster(ElasticUser.NAME, bootstrapPassword, 15); |
| 89 | + final EnrollmentToken kibanaToken = enrollmentTokenGenerator.createKibanaEnrollmentToken(ElasticUser.NAME, bootstrapPassword); |
| 90 | + output = "Kibana enrollment token: " + kibanaToken.getEncoded() + System.lineSeparator(); |
| 91 | + output += "CA fingerprint: " + kibanaToken.getFingerprint() + System.lineSeparator(); |
| 92 | + if (options.has(includeNodeEnrollmentToken)) { |
| 93 | + final EnrollmentToken nodeToken = enrollmentTokenGenerator.createNodeEnrollmentToken(ElasticUser.NAME, bootstrapPassword); |
| 94 | + output += "Node enrollment token: " + nodeToken.getEncoded() + System.lineSeparator(); |
| 95 | + } |
| 96 | + if (ReservedRealm.BOOTSTRAP_ELASTIC_PASSWORD.exists(secureEnvironment.settings()) == false) { |
| 97 | + output += "elastic user password: " + setElasticUserPassword(client, bootstrapPassword); |
| 98 | + } |
| 99 | + terminal.println(output); |
| 100 | + } catch (Exception e) { |
| 101 | + throw new UserException(ExitCodes.UNAVAILABLE, null); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + protected SecureString setElasticUserPassword(CommandLineHttpClient client, SecureString bootstrapPassword) throws Exception { |
| 106 | + final URL passwordSetUrl = setElasticUserPasswordUrl(client); |
| 107 | + final HttpResponse response; |
| 108 | + SecureString password = new SecureString(generatePassword(20)); |
| 109 | + try { |
| 110 | + response = client.execute("POST", passwordSetUrl, ElasticUser.NAME, bootstrapPassword, |
| 111 | + () -> { |
| 112 | + XContentBuilder xContentBuilder = JsonXContent.contentBuilder(); |
| 113 | + xContentBuilder.startObject().field("password", password.toString()).endObject(); |
| 114 | + return Strings.toString(xContentBuilder); |
| 115 | + }, CommandLineHttpClient::responseBuilder); |
| 116 | + if (response.getHttpStatus() != HttpURLConnection.HTTP_OK) { |
| 117 | + throw new UserException(ExitCodes.UNAVAILABLE, null); |
| 118 | + } |
| 119 | + } catch (IOException e) { |
| 120 | + throw new UserException(ExitCodes.IO_ERROR, null); |
| 121 | + } |
| 122 | + return password; |
| 123 | + } |
| 124 | + |
| 125 | + Environment readSecureSettings(Environment env, SecureString keystorePassword) throws Exception { |
| 126 | + final KeyStoreWrapper keyStoreWrapper = keyStoreFunction.apply(env); |
| 127 | + keyStoreWrapper.decrypt(keystorePassword.getChars()); |
| 128 | + Settings.Builder settingsBuilder = Settings.builder(); |
| 129 | + settingsBuilder.put(env.settings(), true); |
| 130 | + if (settingsBuilder.getSecureSettings() == null) { |
| 131 | + settingsBuilder.setSecureSettings(keyStoreWrapper); |
| 132 | + } |
| 133 | + final Settings settings = settingsBuilder.build(); |
| 134 | + return new Environment(settings, env.configFile()); |
| 135 | + } |
| 136 | + |
| 137 | + public static URL checkClusterHealthUrl(CommandLineHttpClient client) throws MalformedURLException, URISyntaxException { |
| 138 | + return createURL(new URL(client.getDefaultURL()), "_cluster/health", "?pretty"); |
| 139 | + } |
| 140 | + |
| 141 | + public static URL setElasticUserPasswordUrl(CommandLineHttpClient client) throws MalformedURLException, URISyntaxException { |
| 142 | + return createURL(new URL(client.getDefaultURL()), "/_security/user/" + ElasticUser.NAME + "/_password", |
| 143 | + "?pretty"); |
| 144 | + } |
| 145 | + |
| 146 | + protected char[] generatePassword(int passwordLength) { |
| 147 | + final char[] passwordChars = ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*-_=+?").toCharArray(); |
| 148 | + char[] characters = new char[passwordLength]; |
| 149 | + for (int i = 0; i < passwordLength; ++i) { |
| 150 | + characters[i] = passwordChars[secureRandom.nextInt(passwordChars.length)]; |
| 151 | + } |
| 152 | + return characters; |
| 153 | + } |
| 154 | +} |
0 commit comments