-
Notifications
You must be signed in to change notification settings - Fork 25.2k
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
Refactor: ScopeResolver #126921
Conversation
Pinging @elastic/es-core-infra (Team:Core/Infra) |
libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/policy/PolicyManager.java
Show resolved
Hide resolved
server/src/main/java/org/elasticsearch/bootstrap/ScopeResolver.java
Outdated
Show resolved
Hide resolved
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.
LGTM!
.../entitlement/src/main/java/org/elasticsearch/entitlement/bootstrap/EntitlementBootstrap.java
Outdated
Show resolved
Hide resolved
@@ -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"; |
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.
This is unfortunate.
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.
It could go inside ScopeResolver
I guess? I think I made it a constructor arg because unit tests passed in a different value.
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.
Drive by comments, but nothing blocking necessarily.
.../entitlement/src/main/java/org/elasticsearch/entitlement/bootstrap/EntitlementBootstrap.java
Outdated
Show resolved
Hide resolved
@@ -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, |
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.
If we move ScopeResolver to bootstrap (or in an exported package) can we use that here instead of a Function?
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 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)"; |
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.
Instead of exposing these strings, can we have constants defined in ScopeInfo for each of these?
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.
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?
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.
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) {}
}
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.
what about Java 17 prohibits that? Still seems doable, even if with static methods?
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.
Consuming it is harder than the enum because you can't use switch patterns until Java 21.
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.
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.
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.
Here.
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've gone with an enum plus some factory method helpers.
💔 Backport failed
You can use sqren/backport to manually backport by running |
Backport is conflicting with #127040 which has not yet been backported. |
* 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]>
Backporting in #127174. |
* 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]>
* 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]>
* 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]>
* 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]>
Currently, the responsibilities are split:
PluginsResolver
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 modulePolicyManager
is responsible for looking up the appropriate entitlements for a given component and moduleNot included in this PR
The
PolicyManager
still references theModule
class. More work is required to make this unnecessary, including (but not limited to) allowingmoduleEntitlementsMap
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:
... I think it makes sense to clean up the tests as a follow-on item.