-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Service Accounts - New CLI tool for managing file tokens #70454
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
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f33ac0e
Add new cli tool for creating service account file tokens
ywangd 987eb83
print created token to screen
ywangd 3fcab92
bearer string instead of raw secret
ywangd 43857dc
complete tests
ywangd a151156
forbbiden apis
ywangd faca377
tweak
ywangd 16822ff
Update x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/s…
ywangd 31b491e
address feedback
ywangd f99668a
checkstyle
ywangd 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
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
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
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
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
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
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-service-tokens
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.authc.service.FileTokensTool \ | ||
ES_ADDITIONAL_SOURCES="x-pack-env;x-pack-security-env" \ | ||
"`dirname "$0"`"/elasticsearch-cli \ | ||
"$@" |
20 changes: 20 additions & 0 deletions
20
x-pack/plugin/security/src/main/bin/elasticsearch-service-tokens.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,20 @@ | ||
@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.authc.service.FileTokensTool | ||
set ES_ADDITIONAL_SOURCES=x-pack-env;x-pack-security-env | ||
call "%~dp0elasticsearch-cli.bat" ^ | ||
%%* ^ | ||
|| goto exit | ||
|
||
endlocal | ||
endlocal | ||
:exit | ||
exit /b %ERRORLEVEL% |
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
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
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
136 changes: 136 additions & 0 deletions
136
...in/java/org/elasticsearch/xpack/security/authc/service/FileServiceAccountsTokenStore.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,136 @@ | ||
/* | ||
* 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.authc.service; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.elasticsearch.ElasticsearchException; | ||
import org.elasticsearch.common.Nullable; | ||
import org.elasticsearch.common.util.Maps; | ||
import org.elasticsearch.env.Environment; | ||
import org.elasticsearch.watcher.FileWatcher; | ||
import org.elasticsearch.watcher.ResourceWatcherService; | ||
import org.elasticsearch.xpack.core.XPackPlugin; | ||
import org.elasticsearch.xpack.core.security.authc.support.Hasher; | ||
import org.elasticsearch.xpack.core.security.support.NoOpLogger; | ||
import org.elasticsearch.xpack.security.support.FileLineParser; | ||
import org.elasticsearch.xpack.security.support.FileReloadListener; | ||
import org.elasticsearch.xpack.security.support.SecurityFiles; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.HashMap; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
|
||
public class FileServiceAccountsTokenStore implements ServiceAccountsTokenStore { | ||
|
||
private static final Logger logger = LogManager.getLogger(FileServiceAccountsTokenStore.class); | ||
|
||
private final Path file; | ||
private final CopyOnWriteArrayList<Runnable> listeners; | ||
private volatile Map<String, char[]> tokenHashes; | ||
|
||
public FileServiceAccountsTokenStore(Environment env, ResourceWatcherService resourceWatcherService) { | ||
file = resolveFile(env); | ||
FileWatcher watcher = new FileWatcher(file.getParent()); | ||
watcher.addListener(new FileReloadListener(file, this::tryReload)); | ||
try { | ||
resourceWatcherService.add(watcher, ResourceWatcherService.Frequency.HIGH); | ||
} catch (IOException e) { | ||
throw new ElasticsearchException("failed to start watching service_tokens file [{}]", e, file.toAbsolutePath()); | ||
} | ||
try { | ||
tokenHashes = parseFile(file, logger); | ||
} catch (IOException e) { | ||
throw new IllegalStateException("Failed to load service_tokens file [" + file + "]", e); | ||
} | ||
listeners = new CopyOnWriteArrayList<>(); | ||
} | ||
|
||
@Override | ||
public boolean authenticate(ServiceAccountToken token) { | ||
return false; | ||
} | ||
|
||
public void addListener(Runnable listener) { | ||
listeners.add(listener); | ||
} | ||
|
||
private void notifyRefresh() { | ||
listeners.forEach(Runnable::run); | ||
} | ||
|
||
private void tryReload() { | ||
final Map<String, char[]> previousTokenHashes = tokenHashes; | ||
tokenHashes = parseFileLenient(file, logger); | ||
if (false == Maps.deepEquals(tokenHashes, previousTokenHashes)) { | ||
logger.info("service tokens file [{}] changed. updating ...", file.toAbsolutePath()); | ||
notifyRefresh(); | ||
} | ||
} | ||
|
||
// package private for testing | ||
Map<String, char[]> getTokenHashes() { | ||
return tokenHashes; | ||
} | ||
|
||
static Path resolveFile(Environment env) { | ||
return XPackPlugin.resolveConfigFile(env, "service_tokens"); | ||
} | ||
|
||
static Map<String, char[]> parseFileLenient(Path path, @Nullable Logger logger) { | ||
try { | ||
return parseFile(path, logger); | ||
} catch (Exception e) { | ||
logger.error("failed to parse service tokens file [{}]. skipping/removing all tokens...", | ||
path.toAbsolutePath()); | ||
return Map.of(); | ||
} | ||
} | ||
|
||
static Map<String, char[]> parseFile(Path path, @Nullable Logger logger) throws IOException { | ||
final Logger thisLogger = logger == null ? NoOpLogger.INSTANCE : logger; | ||
thisLogger.trace("reading service_tokens file [{}]...", path.toAbsolutePath()); | ||
if (Files.exists(path) == false) { | ||
thisLogger.trace("file [{}] does not exist", path.toAbsolutePath()); | ||
return Map.of(); | ||
} | ||
final Map<String, char[]> parsedTokenHashes = new HashMap<>(); | ||
FileLineParser.parse(path, (lineNumber, line) -> { | ||
line = line.trim(); | ||
final int colon = line.indexOf(':'); | ||
if (colon == -1) { | ||
thisLogger.warn("invalid format at line #{} of service_tokens file [{}] - missing ':' character - ", lineNumber, path); | ||
throw new IllegalStateException("Missing ':' character at line #" + lineNumber); | ||
} | ||
final String key = line.substring(0, colon); | ||
// TODO: validate against known service accounts? | ||
char[] hash = new char[line.length() - (colon + 1)]; | ||
line.getChars(colon + 1, line.length(), hash, 0); | ||
if (Hasher.resolveFromHash(hash) == Hasher.NOOP) { | ||
thisLogger.warn("skipping plaintext service account token for key [{}]", key); | ||
} else { | ||
thisLogger.trace("parsed tokens for key [{}]", key); | ||
final char[] previousHash = parsedTokenHashes.put(key, hash); | ||
if (previousHash != null) { | ||
thisLogger.warn("found duplicated key [{}], earlier entries are overridden", key); | ||
} | ||
} | ||
}); | ||
thisLogger.debug("parsed [{}] tokens from file [{}]", parsedTokenHashes.size(), path.toAbsolutePath()); | ||
return Map.copyOf(parsedTokenHashes); | ||
} | ||
|
||
static void writeFile(Path path, Map<String, char[]> tokenHashes) { | ||
SecurityFiles.writeFileAtomically( | ||
path, tokenHashes, e -> String.format(Locale.ROOT, "%s:%s", e.getKey(), new String(e.getValue()))); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm surprised that we need so much boilerplate in order to add another hashing algorithm. Do we need an issue to refactor them down into some amount of shared code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it can be part of #66840. The implication is 8.0 only, but I think it is fine.