-
Notifications
You must be signed in to change notification settings - Fork 25.8k
Add a tool for creating enrollment tokens #74890
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
+993
−8
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
925c13b
Add a tool for creating enrollment tokens
jkakavas 7c2d68a
Merge remote-tracking branch 'origin/master' into create-tokens-cli
jkakavas 81cfa9e
formatting
jkakavas e64daab
address feedback
jkakavas 24228ba
reduce visibility
jkakavas 57c6a07
Allow users to override the health check against the cluster and stil…
jkakavas ec4dd43
Merge remote-tracking branch 'origin/master' into create-tokens-cli
jkakavas 51e8e8c
Merge remote-tracking branch 'origin/master' into create-tokens-cli
jkakavas 7b41749
address feedback
jkakavas a8be2c4
Merge remote-tracking branch 'origin/master' into create-tokens-cli
jkakavas f501712
don't explicitly check for multiple file realms, node does that already
jkakavas 44ccacb
Rename file and add link to the top-level commands page
b5e7dfe
Apply doc suggestions from code review
jkakavas b373f85
Merge remote-tracking branch 'origin/master' into create-tokens-cli
jkakavas 6e7dd05
address feedback
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
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,59 @@ | ||
| [roles="xpack"] | ||
| [[create-enrollment-token]] | ||
|
|
||
| == elasticsearch-create-enrollment-token | ||
|
|
||
| The `elasticsearch-create-enrollment-token` command creates enrollment tokens for | ||
| {es} nodes and {kib} instances. | ||
|
|
||
| [discrete] | ||
| === Synopsis | ||
|
|
||
| [source,shell] | ||
| ---- | ||
| bin/elasticsearch-create-enrollment-token | ||
| [-f, --force] [-h, --help] [-E <KeyValuePair>] [-s, --scope] | ||
| ---- | ||
|
|
||
| [discrete] | ||
| === Description | ||
|
|
||
| Use this command to create enrollment tokens, which you can use to enroll new | ||
| {es} nodes to an existing cluster or configure {kib} instances to communicate | ||
| with an existing {es} cluster that has security features enabled. | ||
| The command generates (and subsequently removes) a temporary user in the | ||
| <<file-realm,file realm>> to run the request that creates enrollment tokens. | ||
| IMPORTANT: You cannot use this tool if the file realm is disabled in your | ||
| `elasticsearch.yml` file. | ||
|
|
||
| This command uses an HTTP connection to connect to the cluster and run the user | ||
| management requests. The command automatically attempts to establish the connection | ||
| over HTTPS by using the `xpack.security.http.ssl` settings in | ||
| the `elasticsearch.yml` file. If you do not use the default configuration directory, | ||
| ensure that the `ES_PATH_CONF` environment variable returns the | ||
| correct path before you run the `elasticsearch-create-enrollment-token` command. You can | ||
| override settings in your `elasticsearch.yml` file by using the `-E` command | ||
| option. For more information about debugging connection failures, see | ||
| <<trb-security-setup>>. | ||
|
|
||
| [discrete] | ||
| [[create-enrollment-token-parameters]] | ||
| === Parameters | ||
|
|
||
| `-E <KeyValuePair>`:: Configures a standard {es} or {xpack} setting. | ||
|
|
||
| `-f, --force`:: Forces the command to run against an unhealthy cluster. | ||
|
|
||
| `-h, --help`:: Returns all of the command parameters. | ||
|
|
||
| `-s, --scope`:: Specifies the scope of the generated token. Supported values are `node` and `kibana`. | ||
|
|
||
| [discrete] | ||
| === Examples | ||
|
|
||
| The following command creates an enrollment token for enrolling an {es} node into a cluster: | ||
|
|
||
| [source,shell] | ||
| ---- | ||
| bin/elasticsearch-create-enrollment-token -s node | ||
| ---- |
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
11 changes: 11 additions & 0 deletions
11
x-pack/plugin/security/src/main/bin/elasticsearch-create-enrollment-token
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,11 @@ | ||
| #!/bin/bash | ||
|
|
||
| # 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. | ||
|
|
||
| ES_MAIN_CLASS=org.elasticsearch.xpack.security.enrollment.tool.CreateEnrollmentTokenTool \ | ||
| ES_ADDITIONAL_SOURCES="x-pack-env;x-pack-security-env" \ | ||
| "`dirname "$0"`"/elasticsearch-cli \ | ||
| "$@" |
21 changes: 21 additions & 0 deletions
21
x-pack/plugin/security/src/main/bin/elasticsearch-create-enrollment-token.bat
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,21 @@ | ||
| @echo off | ||
|
|
||
| rem Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| rem or more contributor license agreements. Licensed under the Elastic License | ||
| rem 2.0; you may not use this file except in compliance with the Elastic License | ||
| rem 2.0. | ||
|
|
||
| setlocal enabledelayedexpansion | ||
| setlocal enableextensions | ||
|
|
||
| set ES_MAIN_CLASS=org.elasticsearch.xpack.security.enrollment.tool.CreateEnrollmentTokenTool | ||
| set ES_ADDITIONAL_SOURCES=x-pack-env;x-pack-security-env | ||
| set ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/security-cli | ||
| call "%~dp0elasticsearch-cli.bat" ^ | ||
| %%* ^ | ||
| || goto exit | ||
|
|
||
| endlocal | ||
| endlocal | ||
| :exit | ||
| exit /b %ERRORLEVEL% |
89 changes: 89 additions & 0 deletions
89
...main/java/org/elasticsearch/xpack/security/enrollment/tool/CreateEnrollmentTokenTool.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,89 @@ | ||
| /* | ||
| * 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.enrollment.tool; | ||
|
|
||
| import joptsimple.OptionSet; | ||
| import joptsimple.OptionSpec; | ||
|
|
||
| import org.elasticsearch.cli.ExitCodes; | ||
| import org.elasticsearch.cli.Terminal; | ||
| import org.elasticsearch.cli.UserException; | ||
| import org.elasticsearch.common.settings.KeyStoreWrapper; | ||
| import org.elasticsearch.common.settings.SecureString; | ||
| import org.elasticsearch.core.CheckedFunction; | ||
| import org.elasticsearch.env.Environment; | ||
| import org.elasticsearch.xpack.core.XPackSettings; | ||
| import org.elasticsearch.xpack.security.enrollment.CreateEnrollmentToken; | ||
| import org.elasticsearch.xpack.security.tool.BaseRunAsSuperuserCommand; | ||
| import org.elasticsearch.xpack.security.tool.CommandLineHttpClient; | ||
|
|
||
| import java.util.List; | ||
| import java.util.function.Function; | ||
|
|
||
| public class CreateEnrollmentTokenTool extends BaseRunAsSuperuserCommand { | ||
|
|
||
| private final OptionSpec<String> scope; | ||
| private final CheckedFunction<Environment, CreateEnrollmentToken, Exception> createEnrollmentTokenFunction; | ||
| static final List<String> ALLOWED_SCOPES = List.of("node", "kibana"); | ||
|
|
||
| CreateEnrollmentTokenTool() { | ||
| this( | ||
| environment -> new CommandLineHttpClient(environment), | ||
| environment -> KeyStoreWrapper.load(environment.configFile()), | ||
| environment -> new CreateEnrollmentToken(environment) | ||
| ); | ||
| } | ||
|
|
||
| CreateEnrollmentTokenTool( | ||
| Function<Environment, CommandLineHttpClient> clientFunction, | ||
| CheckedFunction<Environment, KeyStoreWrapper, Exception> keyStoreFunction, | ||
| CheckedFunction<Environment, CreateEnrollmentToken, Exception> createEnrollmentTokenFunction | ||
| ) { | ||
| super(clientFunction, keyStoreFunction, "Creates enrollment tokens for elasticsearch nodes and kibana instances"); | ||
| this.createEnrollmentTokenFunction = createEnrollmentTokenFunction; | ||
| scope = parser.acceptsAll(List.of("scope", "s"), "The scope of this enrollment token, can be either \"node\" or \"kibana\"") | ||
| .withRequiredArg() | ||
| .required(); | ||
| } | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| exit(new CreateEnrollmentTokenTool().main(args, Terminal.DEFAULT)); | ||
| } | ||
|
|
||
| @Override | ||
| protected void validate(Terminal terminal, OptionSet options, Environment env) throws Exception { | ||
| if (XPackSettings.ENROLLMENT_ENABLED.get(env.settings()) == false) { | ||
| throw new UserException( | ||
| ExitCodes.CONFIG, | ||
| "[xpack.security.enrollment.enabled] must be set to `true` to create an enrollment token" | ||
| ); | ||
| } | ||
| final String tokenScope = scope.value(options); | ||
| if (ALLOWED_SCOPES.contains(tokenScope) == false) { | ||
| terminal.errorPrintln("The scope of this enrollment token, can only be one of " + ALLOWED_SCOPES); | ||
jkakavas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw new UserException(ExitCodes.USAGE, "Invalid scope"); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected void executeCommand(Terminal terminal, OptionSet options, Environment env, String username, SecureString password) | ||
| throws Exception { | ||
| final String tokenScope = scope.value(options); | ||
| try { | ||
| CreateEnrollmentToken createEnrollmentTokenService = createEnrollmentTokenFunction.apply(env); | ||
| if (tokenScope.equals("node")) { | ||
| terminal.println(createEnrollmentTokenService.createNodeEnrollmentToken(username, password)); | ||
| } else { | ||
| terminal.println(createEnrollmentTokenService.createKibanaEnrollmentToken(username, password)); | ||
| } | ||
| } catch (Exception e) { | ||
| terminal.errorPrintln("Unable to create enrollment token for scope [" + tokenScope + "]"); | ||
| throw new UserException(ExitCodes.CANT_CREATE, e.getMessage(), e.getCause()); | ||
| } | ||
| } | ||
| } | ||
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.