Skip to content

Add TestEntitlementsRule and node grants for testing #132077

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 17 commits into from
Aug 5, 2025

Conversation

mosche
Copy link
Contributor

@mosche mosche commented Jul 29, 2025

Use TestEntitlementsRule and closable node grants for better isolation and management of entitlement related state during tests.

wipePendingDataDirectories in tests requires entitlement delegation to work as this uses server's FileSystemUtils.
Until ES-10920 is solved, node grants cannot be removed unless the test suite completes or explicitly removing all node
grants using revokeNodeGrants where feasible.

Relates to ES-12042

@mosche mosche requested a review from a team July 29, 2025 12:48
@mosche mosche added >refactoring auto-backport Automatically create backport pull requests when merged :Core/Infra/Entitlements Entitlements infrastructure v9.2.0 v9.1.1 v8.19.1 labels Jul 29, 2025
@elasticsearchmachine elasticsearchmachine added the Team:Core/Infra Meta label for core/infra team label Jul 29, 2025
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra (Team:Core/Infra)

Copy link
Member

@rjernst rjernst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I skimmed this and it looks pretty good. I just have one comment about naming to avoid confusion with security manager.

Copy link
Contributor

@prdoyle prdoyle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a big improvement!

/**
* Revoke all open node grants.
*/
public void revokeNodeGrants() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit surprised by this, but I see some explanation further down...

// wipePendingDataDirectories in tests requires entitlement delegation to work as this uses server's FileSystemUtils.
// until ES-10920 is solved, node grants cannot be removed until the test suite completes unless explicitly removing all node
// grants using revokeNodeGrants where feasible.
// onClose.accept(this);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...here. Am I right that when we fix ES-10920 that the revokeNodeGrants can become private?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, strictly speaking at that point we don't need this anymore. However, I'd rather keep it even then.

ESIntegTestCase can be configured to run with a TEST node scope, meaning each test case will use new nodes. revokeNodeGrants is a safety measure to ensure there's no node paths of nodes of a previous tests lingering around. The class rule only resets everything after the entire test suite.

Copy link
Member

@rjernst rjernst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great. I have one nit about the structure. I think the parallelism we had before with EntitlementBootstrap (which handles loading and managing the agent) and TestEntitlementBootstrap was good. Could we keep that, and have the test rule be simpler, just about calling the right methods on bootstrap?

}

public static void initialize(Path tempDir) throws IOException {
if (POLICY_MANAGER != null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the tempDir is only ever java.io.tmpdir right? So couldn't this be known statically and not need this initialize method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the parallelism we had before with EntitlementBootstrap (which handles loading and managing the agent) and TestEntitlementBootstrap was good. Could we keep that, and have the test rule be simpler, just about calling the right methods on bootstrap?

Not 100% sure what you mean with above, @rjernst.TestEntitlementsRule#initialize(Path tempDir) corresponds to the previous TestEntitlementBootstrap#bootstrap(@Nullable Path tempDir) which also created the policy manager and loaded the agent. I've moved this into static init of this class to prevent this from not being initialized correctly (and making it more self contained). The test rule still contains / calls the very same code for creating the policy manager / loading the agent.

    public static void bootstrap(@Nullable Path tempDir) throws IOException {
        ...
        policyManager = createPolicyManager(pathLookup);
        EntitlementInitialization.initializeArgs = new EntitlementInitialization.InitializeArgs(pathLookup, Set.of(), policyManager);
        EntitlementBootstrap.loadAgent(EntitlementBootstrap.findAgentJar(), EntitlementInitialization.class.getName());
    }

Are you suggesting to revert to above bootstrap method rather than doing that statically in TestEntitlementsRule? If so, I can absolutely do that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sort of. I'm suggesting:

  • Keep TestEntitlementBootstrap class, with that being the thing that manages bootstrapping the agent (note that the loadAgent methods et al can be package private)
  • Continue calling the bootstrap method from BootstrapForTesting, which sets up the agent
  • Have the TestRule call methods on bootstrap, the test rule is then just about calling the appropriate methods at the right time, with no inherent state itself

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, implemented here 4f5e939

Copy link
Member

@rjernst rjernst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

}

public static boolean isEnabledForTest() {
public static boolean isEnabledForTests() {
return Booleans.parseBoolean(System.getProperty("es.entitlement.enableForTests", "false"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this be enabled by default in a later change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it depends on the final shap of the gradle plugins, that's still an open task and pending on Rene's return.
for some esoteric modules (e.g. plugin-scanner )entitlements cannot be enabled and we currently rely on this defaulting to false in the test code.

@mosche mosche merged commit 76dac08 into elastic:main Aug 5, 2025
33 checks passed
mosche added a commit to mosche/elasticsearch that referenced this pull request Aug 5, 2025
…hs for testing (elastic#132077)

Use TestEntitlementsRule and closable entitled node paths for better isolation and management of entitlement related state during tests. 

However, wipePendingDataDirectories in tests requires entitlement delegation to work as this uses server's FileSystemUtils. Until ES-10920 is solved, entitled node paths cannot be removed unless the test suite completes or explicitly removing all entitled node paths in-between tests (if cluster scope is TEST).

Relates to ES-12042
@elasticsearchmachine
Copy link
Collaborator

💔 Backport failed

Status Branch Result
9.1
8.19 Commit could not be cherrypicked due to conflicts

You can use sqren/backport to manually backport by running backport --upstream elastic/elasticsearch --pr 132077

elasticsearchmachine pushed a commit that referenced this pull request Aug 5, 2025
…hs for testing (#132077) (#132431)

Use TestEntitlementsRule and closable entitled node paths for better isolation and management of entitlement related state during tests. 

However, wipePendingDataDirectories in tests requires entitlement delegation to work as this uses server's FileSystemUtils. Until ES-10920 is solved, entitled node paths cannot be removed unless the test suite completes or explicitly removing all entitled node paths in-between tests (if cluster scope is TEST).

Relates to ES-12042
szybia added a commit to szybia/elasticsearch that referenced this pull request Aug 5, 2025
…cking

* upstream/main: (26 commits)
  [Fleet] add privileges to `kibana_system` to read integrations data (elastic#132400)
  Add `TestEntitlementsRule` with support for dynamic entitled node paths for testing (elastic#132077)
  Reduce logging frequency for GCS per project clients (elastic#132429)
  Skip update/100_synthetic_source tests in yamlRestCompatTests (elastic#132296)
  Correct exception for missing nested path (elastic#132408)
  Fixing esql release tests elastic#132369 (elastic#132406)
  Adjust date docvalue formatting to return 4xx instead of 5xx (elastic#132414)
  Handle nested fields with the termvectors REST API in artificial docs (elastic#92568)
  Only collect bulk scored vectors when exceeding min competitive (elastic#132293)
  Fix release tests diskbbq update (elastic#132405)
  ESQL: Fix skipping of generative tests (elastic#132390)
  Short circuit failure handling in OIDC flow (elastic#130618)
  Small optimization in OptimizedScalarQuantizer by using mul instead of div (elastic#132397)
  Aggs: Add validation to Bucket script pipeline agg (elastic#132320)
  ESQL: Multiple parameters in ungrouped aggs (elastic#132375)
  ESQL: Explain test operators (elastic#132374)
  EQL: Deal with internally created IN in a different way for EQL (elastic#132167)
  Speed up hierarchical k-means by computing distances in bulk (elastic#132384)
  Reduce the number of fields per document (elastic#132322)
  Assert current thread in ESQL (elastic#132324)
  ...
@mosche
Copy link
Contributor Author

mosche commented Aug 11, 2025

💚 All backports created successfully

Status Branch Result
8.19

Questions ?

Please refer to the Backport tool documentation

mosche added a commit to mosche/elasticsearch that referenced this pull request Aug 11, 2025
…hs for testing (elastic#132077)

Use TestEntitlementsRule and closable entitled node paths for better isolation and management of entitlement related state during tests.

However, wipePendingDataDirectories in tests requires entitlement delegation to work as this uses server's FileSystemUtils. Until ES-10920 is solved, entitled node paths cannot be removed unless the test suite completes or explicitly removing all entitled node paths in-between tests (if cluster scope is TEST).

Relates to ES-12042

(cherry picked from commit 76dac08)
elasticsearchmachine pushed a commit that referenced this pull request Aug 11, 2025
…hs for testing (#132077) (#132636)

Use TestEntitlementsRule and closable entitled node paths for better isolation and management of entitlement related state during tests.

However, wipePendingDataDirectories in tests requires entitlement delegation to work as this uses server's FileSystemUtils. Until ES-10920 is solved, entitled node paths cannot be removed unless the test suite completes or explicitly removing all entitled node paths in-between tests (if cluster scope is TEST).

Relates to ES-12042

(cherry picked from commit 76dac08)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-backport Automatically create backport pull requests when merged :Core/Infra/Entitlements Entitlements infrastructure >refactoring Team:Core/Infra Meta label for core/infra team v8.19.1 v9.1.1 v9.2.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants