-
Notifications
You must be signed in to change notification settings - Fork 25.3k
Support entitlements in internal cluster tests #130710
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
24 commits
Select commit
Hold shift + click to select a range
9597586
Support entitlements in internal cluster tests
mosche ac4503a
[CI] Auto commit changes from spotless
elasticsearchmachine 4c7f662
fix
mosche 305c576
revert to previous approach enabling entitlements for tests
mosche 45ea516
Merge branch 'main' into entitlements/integtestSupport
mosche debfcd9
Use separate temp folder for nodes to properly enforce file entitlements
mosche 9258e17
Merge branch 'main' into entitlements/integtestSupport
mosche 9575646
Merge branch 'main' into entitlements/integtestSupport
mosche 8989582
skip configuring node dirs if no policyManager
mosche 3a086de
Trivially allow test utility classes if annotated with @WithoutEntitl…
mosche 12aab4e
Move ReloadingDatabasesWhilePerformingGeoLookupsIT from internalClust…
mosche ea9650a
@WithoutEntitlements // CLI tools don't run with entitlements enforced
mosche ce4d075
Merge branch 'main' into entitlements/integtestSupport
mosche d301ede
[CI] Auto commit changes from spotless
elasticsearchmachine e8be979
fix forbidden
mosche 4b919cf
Merge branch 'main' into entitlements/integtestSupport
mosche bbfb065
move notEntitled into policManager to possibly ignore in tests
mosche affbb7f
fix compile
mosche c94bf75
Revert "fix compile"
mosche fcd1dd0
Revert "move notEntitled into policManager to possibly ignore in tests"
mosche c400748
Revert "[CI] Auto commit changes from spotless"
mosche 998c62c
Revert "Trivially allow test utility classes if annotated with @Witho…
mosche c9608ef
disable entitlement checks for SecuritySingleNodeTestCase and Securit…
mosche f18747a
Merge branch 'main' into entitlements/integtestSupport
mosche 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,13 +12,15 @@ | |
import org.elasticsearch.bootstrap.TestBuildInfo; | ||
import org.elasticsearch.bootstrap.TestBuildInfoParser; | ||
import org.elasticsearch.bootstrap.TestScopeResolver; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.core.Booleans; | ||
import org.elasticsearch.core.Nullable; | ||
import org.elasticsearch.core.PathUtils; | ||
import org.elasticsearch.core.Strings; | ||
import org.elasticsearch.core.SuppressForbidden; | ||
import org.elasticsearch.entitlement.initialization.EntitlementInitialization; | ||
import org.elasticsearch.entitlement.runtime.policy.PathLookup; | ||
import org.elasticsearch.entitlement.runtime.policy.PathLookup.BaseDir; | ||
import org.elasticsearch.entitlement.runtime.policy.Policy; | ||
import org.elasticsearch.entitlement.runtime.policy.PolicyParser; | ||
import org.elasticsearch.entitlement.runtime.policy.TestPathLookup; | ||
|
@@ -32,39 +34,106 @@ | |
import java.net.URI; | ||
import java.net.URL; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.TreeSet; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.BiFunction; | ||
import java.util.function.Consumer; | ||
|
||
import static java.util.stream.Collectors.toCollection; | ||
import static java.util.stream.Collectors.toSet; | ||
import static org.elasticsearch.entitlement.runtime.policy.PathLookup.BaseDir.CONFIG; | ||
import static org.elasticsearch.entitlement.runtime.policy.PathLookup.BaseDir.TEMP; | ||
import static org.elasticsearch.env.Environment.PATH_DATA_SETTING; | ||
import static org.elasticsearch.env.Environment.PATH_HOME_SETTING; | ||
import static org.elasticsearch.env.Environment.PATH_REPO_SETTING; | ||
|
||
public class TestEntitlementBootstrap { | ||
|
||
private static final Logger logger = LogManager.getLogger(TestEntitlementBootstrap.class); | ||
|
||
private static Map<BaseDir, Collection<Path>> baseDirPaths = new ConcurrentHashMap<>(); | ||
private static TestPolicyManager policyManager; | ||
|
||
/** | ||
* Activates entitlement checking in tests. | ||
*/ | ||
public static void bootstrap(@Nullable Path tempDir, @Nullable Path configDir) throws IOException { | ||
public static void bootstrap(@Nullable Path tempDir) throws IOException { | ||
if (isEnabledForTest() == false) { | ||
return; | ||
} | ||
TestPathLookup pathLookup = new TestPathLookup(Map.of(TEMP, zeroOrOne(tempDir), CONFIG, zeroOrOne(configDir))); | ||
var previousTempDir = baseDirPaths.put(TEMP, zeroOrOne(tempDir)); | ||
assert previousTempDir == null : "Test entitlement bootstrap called multiple times"; | ||
TestPathLookup pathLookup = new TestPathLookup(baseDirPaths); | ||
policyManager = createPolicyManager(pathLookup); | ||
EntitlementInitialization.initializeArgs = new EntitlementInitialization.InitializeArgs(pathLookup, Set.of(), policyManager); | ||
logger.debug("Loading entitlement agent"); | ||
EntitlementBootstrap.loadAgent(EntitlementBootstrap.findAgentJar(), EntitlementInitialization.class.getName()); | ||
} | ||
|
||
public static void registerNodeBaseDirs(Settings settings, Path configPath) { | ||
if (policyManager == null) { | ||
return; | ||
} | ||
Path homeDir = absolutePath(PATH_HOME_SETTING.get(settings)); | ||
Path configDir = configPath != null ? configPath : homeDir.resolve("config"); | ||
Collection<Path> dataDirs = dataDirs(settings, homeDir); | ||
Collection<Path> repoDirs = repoDirs(settings); | ||
logger.debug("Registering node dirs: config [{}], dataDirs [{}], repoDirs [{}]", configDir, dataDirs, repoDirs); | ||
baseDirPaths.compute(BaseDir.CONFIG, baseDirModifier(paths -> paths.add(configDir))); | ||
baseDirPaths.compute(BaseDir.DATA, baseDirModifier(paths -> paths.addAll(dataDirs))); | ||
baseDirPaths.compute(BaseDir.SHARED_REPO, baseDirModifier(paths -> paths.addAll(repoDirs))); | ||
policyManager.reset(); | ||
} | ||
|
||
public static void unregisterNodeBaseDirs(Settings settings, Path configPath) { | ||
if (policyManager == null) { | ||
return; | ||
} | ||
Path homeDir = absolutePath(PATH_HOME_SETTING.get(settings)); | ||
Path configDir = configPath != null ? configPath : homeDir.resolve("config"); | ||
Collection<Path> dataDirs = dataDirs(settings, homeDir); | ||
Collection<Path> repoDirs = repoDirs(settings); | ||
logger.debug("Unregistering node dirs: config [{}], dataDirs [{}], repoDirs [{}]", configDir, dataDirs, repoDirs); | ||
baseDirPaths.compute(BaseDir.CONFIG, baseDirModifier(paths -> paths.remove(configDir))); | ||
baseDirPaths.compute(BaseDir.DATA, baseDirModifier(paths -> paths.removeAll(dataDirs))); | ||
baseDirPaths.compute(BaseDir.SHARED_REPO, baseDirModifier(paths -> paths.removeAll(repoDirs))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Huh, this |
||
policyManager.reset(); | ||
} | ||
|
||
private static Collection<Path> dataDirs(Settings settings, Path homeDir) { | ||
List<String> dataDirs = PATH_DATA_SETTING.get(settings); | ||
return dataDirs.isEmpty() | ||
? List.of(homeDir.resolve("data")) | ||
: dataDirs.stream().map(TestEntitlementBootstrap::absolutePath).toList(); | ||
} | ||
|
||
private static Collection<Path> repoDirs(Settings settings) { | ||
return PATH_REPO_SETTING.get(settings).stream().map(TestEntitlementBootstrap::absolutePath).toList(); | ||
} | ||
|
||
private static BiFunction<BaseDir, Collection<Path>, Collection<Path>> baseDirModifier(Consumer<Collection<Path>> consumer) { | ||
return (BaseDir baseDir, Collection<Path> paths) -> { | ||
if (paths == null) { | ||
paths = new HashSet<>(); | ||
} | ||
consumer.accept(paths); | ||
return paths; | ||
}; | ||
} | ||
|
||
@SuppressForbidden(reason = "must be resolved using the default file system, rather then the mocked test file system") | ||
private static Path absolutePath(String path) { | ||
return Paths.get(path).toAbsolutePath().normalize(); | ||
} | ||
|
||
private static <T> List<T> zeroOrOne(T item) { | ||
if (item == null) { | ||
return List.of(); | ||
|
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
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.
🎉