Skip to content

Refactor: ScopeResolver #126921

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 14 commits into from
Apr 21, 2025
Merged

Refactor: ScopeResolver #126921

merged 14 commits into from
Apr 21, 2025

Conversation

prdoyle
Copy link
Contributor

@prdoyle prdoyle commented Apr 16, 2025

Currently, the responsibilities are split:

  • Mapping a Class to a plugin name is done by PluginsResolver
  • Mapping a Class to a module name is done by PolicyManager

Conceptually speaking, these are part of one process of determining the scope from the Class.

Practically speaking, we're going to want to override both of these together for a testing environment, where we can't rely on modules and module layers, but instead need to go spelunking in jar files.

This PR moves the scope resolution responsibility into one component called ScopeResolver.

  • ScopeResolver is responsible for mapping a Class to a component and a module
  • PolicyManager is responsible for looking up the appropriate entitlements for a given component and module

Not included in this PR

The PolicyManager still references the Module class. More work is required to make this unnecessary, including (but not limited to) allowing moduleEntitlementsMap to be managed externally.

A note about unit tests

The unit tests, while they cover all the functionality, don't make a ton of sense anymore. As a follow-on item, I'd like to clean up the tests to reflect the new split in responsibilities.

However, given that:

  • the team is waiting for this change,
  • rejiggering the tests will take some time (a day?), and
  • the test coverage is already sufficient

... I think it makes sense to clean up the tests as a follow-on item.

@prdoyle prdoyle added >non-issue auto-backport Automatically create backport pull requests when merged v8.18.1 v8.19.0 v9.0.1 v9.1.0 :Core/Infra/Entitlements Entitlements infrastructure labels Apr 16, 2025
@prdoyle prdoyle self-assigned this Apr 16, 2025
@prdoyle prdoyle requested a review from a team as a code owner April 16, 2025 13:25
@elasticsearchmachine elasticsearchmachine added the Team:Core/Infra Meta label for core/infra team label Apr 16, 2025
@elasticsearchmachine
Copy link
Collaborator

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

Copy link
Contributor

@jdconrad jdconrad left a comment

Choose a reason for hiding this comment

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

LGTM!

@@ -87,6 +87,7 @@ class Elasticsearch {

private static final String POLICY_PATCH_PREFIX = "es.entitlements.policy.";
private static final String SERVER_POLICY_PATCH_NAME = POLICY_PATCH_PREFIX + "server";
private static final String APM_AGENT_PACKAGE_NAME = "co.elastic.apm.agent";
Copy link
Contributor

Choose a reason for hiding this comment

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

This is unfortunate.

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 could go inside ScopeResolver I guess? I think I made it a constructor arg because unit tests passed in a different value.

@prdoyle prdoyle enabled auto-merge (squash) April 17, 2025 20:37
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.

Drive by comments, but nothing blocking necessarily.

@@ -99,7 +100,7 @@ public static BootstrapArgs bootstrapArgs() {
public static void bootstrap(
Policy serverPolicyPatch,
Map<String, Policy> pluginPolicies,
Function<Class<?>, String> pluginResolver,
Function<Class<?>, PolicyManager.ScopeInfo> scopeResolver,
Copy link
Member

Choose a reason for hiding this comment

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

If we move ScopeResolver to bootstrap (or in an exported package) can we use that here instead of a Function?

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 can change it if you want. Function has the benefit that everyone knows exactly what it means.

static final String UNKNOWN_COMPONENT_NAME = "(unknown)";
static final String SERVER_COMPONENT_NAME = "(server)";
static final String APM_AGENT_COMPONENT_NAME = "(APM agent)";
public static final String UNKNOWN_COMPONENT_NAME = "(unknown)";
Copy link
Member

Choose a reason for hiding this comment

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

Instead of exposing these strings, can we have constants defined in ScopeInfo for each of these?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh like an enum? Yeah I'd much prefer that actually. I was considering it but didn't want to hold up the PR.

I was thinking of:

enum ComponentKind { UNKNOWN, SERVER, PLUGIN, AGENT }

Is that what you were picturing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we didn't have to support JDK 17, we could go all the way to algebraic datatypes:

sealed interface PolicyScope {
  String component();
  String module();

  record Unknown() implements PolicyScope { ... };
  record Server(String module) implements PolicyScope { ... };
  record ApmAgent() implements PolicyScope { ... };
  record Plugin(String component, String module) {}
}

Copy link
Member

Choose a reason for hiding this comment

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

what about Java 17 prohibits that? Still seems doable, even if with static methods?

Copy link
Contributor Author

@prdoyle prdoyle Apr 17, 2025

Choose a reason for hiding this comment

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

Consuming it is harder than the enum because you can't use switch patterns until Java 21.

Copy link
Member

Choose a reason for hiding this comment

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

Where do we consume it in a switch? Don't we just use the two strings right now? I view the helpers as making construction if the scope more robust, and allowing us to keep some eg logging strings for server internal to entitlements.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here.

Copy link
Contributor Author

@prdoyle prdoyle Apr 21, 2025

Choose a reason for hiding this comment

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

I've gone with an enum plus some factory method helpers.

@prdoyle prdoyle disabled auto-merge April 17, 2025 21:15
@prdoyle prdoyle enabled auto-merge (squash) April 21, 2025 14:39
@prdoyle prdoyle merged commit 15c2c46 into elastic:main Apr 21, 2025
15 of 17 checks passed
@elasticsearchmachine
Copy link
Collaborator

💔 Backport failed

Status Branch Result
8.18 Commit could not be cherrypicked due to conflicts
8.x Commit could not be cherrypicked due to conflicts
9.0 Commit could not be cherrypicked due to conflicts

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

@prdoyle
Copy link
Contributor Author

prdoyle commented Apr 21, 2025

Backport is conflicting with #127040 which has not yet been backported.

prdoyle added a commit to prdoyle/elasticsearch that referenced this pull request Apr 22, 2025
* Fix: use getScopeName consistently

* Rename PolicyManagerTests method

* Refacor: simplify PluginsResolver.create

* Change PluginsResolver to ScopeResolver

* Move boot layer test to ScopeResolverTests

* [CI] Auto commit changes from spotless

* Rename PolicyScope

* Add ComponentKind enum

* Package private componentName field

---------

Co-authored-by: elasticsearchmachine <[email protected]>
@prdoyle
Copy link
Contributor Author

prdoyle commented Apr 22, 2025

Backporting in #127174.

@prdoyle prdoyle deleted the scope-resolver branch April 22, 2025 18:06
elasticsearchmachine added a commit that referenced this pull request Apr 22, 2025
* Fix: use getScopeName consistently

* Rename PolicyManagerTests method

* Refacor: simplify PluginsResolver.create

* Change PluginsResolver to ScopeResolver

* Move boot layer test to ScopeResolverTests

* [CI] Auto commit changes from spotless

* Rename PolicyScope

* Add ComponentKind enum

* Package private componentName field

---------

Co-authored-by: elasticsearchmachine <[email protected]>
prdoyle added a commit to prdoyle/elasticsearch that referenced this pull request Apr 22, 2025
* Fix: use getScopeName consistently

* Rename PolicyManagerTests method

* Refacor: simplify PluginsResolver.create

* Change PluginsResolver to ScopeResolver

* Move boot layer test to ScopeResolverTests

* [CI] Auto commit changes from spotless

* Rename PolicyScope

* Add ComponentKind enum

* Package private componentName field

---------

Co-authored-by: elasticsearchmachine <[email protected]>
elasticsearchmachine added a commit that referenced this pull request Apr 22, 2025
* Fix: use getScopeName consistently

* Rename PolicyManagerTests method

* Refacor: simplify PluginsResolver.create

* Change PluginsResolver to ScopeResolver

* Move boot layer test to ScopeResolverTests

* [CI] Auto commit changes from spotless

* Rename PolicyScope

* Add ComponentKind enum

* Package private componentName field

---------

Co-authored-by: elasticsearchmachine <[email protected]>
elasticsearchmachine added a commit that referenced this pull request Apr 23, 2025
* Fix: use getScopeName consistently

* Rename PolicyManagerTests method

* Refacor: simplify PluginsResolver.create

* Change PluginsResolver to ScopeResolver

* Move boot layer test to ScopeResolverTests

* [CI] Auto commit changes from spotless

* Rename PolicyScope

* Add ComponentKind enum

* Package private componentName field

---------

Co-authored-by: elasticsearchmachine <[email protected]>
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 >non-issue Team:Core/Infra Meta label for core/infra team v8.18.1 v8.19.0 v9.0.1 v9.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants