Commit graph

174 commits

Author SHA1 Message Date
Patrick Doyle
b2529edd50
Refactor: ScopeResolver (#126921) (#127174) (#127182)
* 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 <infra-root+elasticsearchmachine@elastic.co>
2025-04-23 06:25:06 +10:00
Jack Conradson
c272dda9ed
Refactor file path resolution for entitlements (#127040) (#127133) (#127159)
This change refactors the known directory resolution such as modules, 
plugins, lib, etc. into a PathLookup. This is one of the steps towards 
allowing unit tests to provide their own PathLookup for resolution so 
we can enable entitlements there.

ES-11584
2025-04-23 02:39:48 +10:00
Lorenzo Dematté
28130a63fd
[Entitlements] Improve FileAccessTree logging (#127050) (#127151)
We already had logging in FileAccessTree as result of debugging the \\pipe\ failures a while ago; this PR slightly improves the logs to provide more information.
2025-04-23 01:45:23 +10:00
Lorenzo Dematté
13ba48ee61
Add package-info.java and javadocs to document Entitlements design and implementation (#127023) (#127143)
Design and implementation of Entitlement with this level of detail needs to stay close to the code, and take advantage of javadoc features like linking and class-references to help us with refactorings and future code changes.

The bulk of the information went into the package-info file for the main library, but I split up some parts and referenced them from the main doc, where I thought it made sense (mainly: the bridge sub-project for some implementation details, PolicyManager, EntitlementInitialization and FileAccessTree); this way they still can be reached from the "overview" while being closer to where the information really belongs.

Relates to ES-11284
2025-04-22 20:52:27 +10:00
Ryan Ernst
ccd2a0f28b
Remove unnecessary network entitlements from server (#126799) (#126867)
These entitlements should not be necessary. Server does not directly
create any outbound connections, and netty is not a dependency of server
so it should not exist within the server policy.

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
2025-04-19 11:44:33 +10:00
Ryan Ernst
4b6e8d9f39
Make sure SM isn't running alongside entitlements tests (#127082) (#127083)
closes #127077
2025-04-19 11:40:51 +10:00
Lorenzo Dematté
591a08f506
[Entitlements] Validation checks on paths (#126852) (#127055)
With this PR we restrict the paths we allow access to, forbidding plugins to specify/request entitlements for reading or writing to specific protected directories.

I added this validation to EntitlementInitialization, as I wanted to fail fast and this is the earliest occurrence where we have all we need: PathLookup to resolve relative paths, policies (for plugins, server, agents) and the Paths for the specific directories we want to protect.

Relates to ES-10918
2025-04-19 00:44:49 +10:00
Patrick Doyle
7682186455
Reword Suppress a Benign Warning section (#126546) (#126721) 2025-04-12 06:05:58 +10:00
Ryan Ernst
04e979aa50
Simplify entitlement rest test discovery (#125449) (#126539)
This commit cleans up how entitlement test methods are discovered. It
also adds another robustness check to ensure an annotation doesn't exist
on a private method.
2025-04-10 01:55:06 +10:00
Lorenzo Dematté
54e5ab12f3
[Entitlements] Expand and update README (#125395) (#126265)
This PR expands the README for the entitlements package; it adds information for Elasticsearch developers on how the system is structured, how to build policies, what to do when new code "does not work" because it's bumping into a NotEntitledException, and how to apply a policy patch in case of an emergency.
2025-04-04 19:25:12 +11:00
Lorenzo Dematté
7b7301ec77
[Entitlements] Fix PolicyUtils and PolicyUtilsTests on Windows (#126185) (#126234)
This PR fixes 2 issues discovered around PolicyUtils (and PolicyUtilsTests) when running CI on Windows:
- in tests, absolute paths like always are different, this fix updates the tests to account for the difference.
- on Windows, Files.move was failing because we were parsing the Entitlement policy but not closing the stream in a timely manner. This causes plugin installation (and related CI tests) to fail 70% of the time. Fixed by closing the stream properly

Fixes #126176
2025-04-04 06:48:19 +11:00
Patrick Doyle
448d654f2e
Put the not-entitled message in the log (#126072) (#126162) (#126169) 2025-04-03 07:23:36 +11:00
Lorenzo Dematté
73d67e2249
[Entitlements] Replace Permissions with Entitlements in InstallPluginAction (#125207) (#126119)
This PR replaces the parsing and formatting of SecurityManager policies with the parsing and formatting of Entitlements policy during plugin installation.

Relates to ES-10923
2025-04-02 17:05:18 +02:00
Patrick Doyle
81098600af
Custom getCallerClass in entitlement bridge (#125139) (#126068) 2025-04-02 07:00:43 +11:00
Jack Conradson
e82f589132
Backport: Add exclusive file entitlement for settings (#125272) (#126006) (#126059) (#126067) 2025-04-02 06:58:39 +11:00
Ryan Ernst
696eabdc66
Return null from transformer when not transforming (#125961) (#125973)
The transform API for instrumentation should return null when no
transformation occurs. This commit fixes our entitlement transformer to
return null instead of the original buffer.

closes #125920
2025-04-01 05:19:47 +11:00
Patrick Doyle
a55471d6ea
Unmute entitlements unit tests (#125217) (#125530) 2025-03-25 08:01:47 +11:00
Ryan Ernst
33dc75fede
Move remaining check methods in RestEntitlementsCheckAction (#125351) (#125443)
* Move remaining check methods in RestEntitlementsCheckAction (#125351)

All future check methods should be found reflectively with the
EntitlementTest annotation.

* Fix compilation after #125442
2025-03-22 09:03:37 +11:00
Mark Vieira
4201dcc0c0
Fix entitlement test failures on Java 17 (#125344) (#125421) 2025-03-22 04:16:45 +11:00
Lorenzo Dematté
e2ae238bce
[Entitlements] Add an option to perform bytecode verification during instrumentation (#124404) (#125224)
Using ASM CheckClassAdapter was key to diagnose the issue we had with incorrect signatures for some check methods.
In this PR I polished up the code I used to pinpoint the issue, and made it available via a system property so it can be turned on if we need it (and it's always on for Entitlements IT tests too).

It is also turned on in case we get VerifyErrors during retransformClasses early in the Entitlement agent bootstrap: retransformClasses runs in the native part of the JVM, so the VerifyError it produces is not so readable (e.g. it lacks a full stack trace and a description); in case this happens, we re-apply the transformation with verification turned on to get a meaningful error before dying.
2025-03-21 09:16:19 +01:00
Patrick Doyle
57681efbfc
Use ALL-UNNAMED for the unnamed module in warnings (#125317) (#125348)
* Use ALL-UNNAMED for the unnamed module in warnings

* [CI] Auto commit changes from spotless

---------

Co-authored-by: elasticsearchmachine <infra-root+elasticsearchmachine@elastic.co>
2025-03-21 09:21:11 +11:00
Ryan Ernst
5c5a743dd8
Split out jvm and operating system actions in entitlement tests (#125122) (#125343)
This is part of continued cleanup to remove actions from
RestEntitlementCheckAction.
2025-03-21 07:42:27 +11:00
Lorenzo Dematté
98607b1425
[Entitlements] Uniform server and plugins policy patching (#125011) (#125087)
With #124904 we introduced server policy patching via system properties; now that we have the possibility to merge policies and scopes, it's worth applying the same mechanism to plugin policies too.

This PR changes the behaviour of plugin policy overrides; now they are not replacing the policy entirely, but patch it by adding the entitlements and scopes specified in the "patch policy"
2025-03-18 23:36:06 +11:00
Lorenzo Dematté
4041eb1e0c
[8.18] [Entitlelments] Server policy patching via system property (#124904) (#125007)
* [Entitlelments] Server policy patching via system property (#124904)

This PR adds a mechanism to patch the server layer policy by merging entitlements from a policy provided via a system property (merge, not replace).

* change to support 8.x language level
2025-03-17 23:42:32 +11:00
Patrick Doyle
fda5483e3e
Use child loggers broadly in PolicyManager (#124896) (#124937)
* Use child loggers broadly in PolicyManager

* Pass ModuleEntitlements to notEntitled

* Store logger name instead of object.

Some of our unit tests check for equality of `ModuleEntitlements` objects,
and they are entitled to do so (no pun intended).

* Alright, let's cache 'em

* Memoize at point of creation, not point of use

* Explanatory comments
2025-03-16 00:26:06 +11:00
Patrick Doyle
7cef9a0341
Include stack trace in Not Entitled warning (#124895) (#124915)
* Include stack trace in Not Entitled warning

* Remove redundant log message text
2025-03-15 07:28:00 +11:00
Lorenzo Dematté
48b846cbd9
add load_native_libraries entitlement to java.desktop (#124852) (#124905)
The ingest-attachment module uses Tika to parse some content; Tika in turn uses some libraries from java.desktop to perform its tasks.

In turn, the JDK loads one (or more) native libraries for its implementation as part of class initialization. This means we need to grant load_native_libraries to java.desktop so that because AWT can load libraries for itself.
2025-03-15 06:03:40 +11:00
Lorenzo Dematté
0be9e357e3
[Entitlements] Allow policy overrides via system properties (#124489) (#124860)
This PR adds the ability to override entitlement policies for Elasticsearch plugins and modules via a system property.

The system property is in the form es.entitlements.policy.<plugin name>, and accepts a versioned policy:

versions:
  - version1
  - versionN
policy:
  <a standard entitlement policy>

For example:

versions:
  - 9.1.0
policy:
  ALL-UNNAMED:
    - set_https_connection_properties
    - outbound_network
    - files:
      - relative_path: ".config/gcloud"
        relative_to: home
        mode: read

The versioned policy needs to be base64 encoded. For example, to pass the above policy to a test cluster via gradle run:

./gradlew run --debug-jvm -Dtests.jvm.argline="-Des.entitlements.policy.repository-gcs=dmVyc2lvbnM6CiAgLSA5LjEuMApwb2xpY3k6CiAgQUxMLVVOTkFNRUQ6CiAgICAtIHNldF9odHRwc19jb25uZWN0aW9uX3Byb3BlcnRpZXMKICAgIC0gb3V0Ym91bmRfbmV0d29yawogICAgLSBmaWxlczoKICAgICAgLSByZWxhdGl2ZV9wYXRoOiAiLmNvbmZpZy9nY2xvdWQiCiAgICAgICAgcmVsYXRpdmVfdG86IGhvbWUKICAgICAgICBtb2RlOiByZWFkCg=="

The versions listed in the policy are string-matched against Build.version().current(); it is possible to specify any number of versions. If the list is empty/there is no versions field, the policy is assumed to match any Elasticsearch versions.

The override policy specified for any given plugin replaces the embedded policy for that plugin. See how EntitlementsAllowedViaOverrideIT replaces an empty policy for the entitlement-test-plugin with a policy that allows load_native_libraries and access to files in the test read_dir.

Also tested manually with an override with a different version, with an override with an invalid policy and with a valid override (see command above).

Relates to ES-11009
2025-03-14 20:41:37 +11:00
Lorenzo Dematté
c0afff1b38
[Entitlements] Add support for IT tests of always allowed actions (take 2) (#124429) (#124704)
Writing tests for #123861, turns out that #124195 is not enough.
We really need new IT test cases for "always allowed" actions: in order to be sure they are allowed, we need to setup the plugin with no policy.
This PR adds test cases for that, plus the support for writing test functions that accept one Environment parameter: many test paths we test and allow/deny are relative to paths in Environment, so it's useful to have access to it (see readAccessConfigDirectory as an example)
2025-03-13 21:07:47 +11:00
Lorenzo Dematté
4ec4fe5938
Fix FileAccessTreeTests#testDuplicateExclusivePaths to work on windows (#124430) (#124700) 2025-03-13 19:42:19 +11:00
Moritz Mack
0c1158999e
Enable FIPS entitlements based on org.bouncycastle.fips.approved_only. (#124577) (#124629)
When enabling FIPS `javax.net.ssl.trustStore` is not necessarily set.
This change adds FIPS entitlements based on
`org.bouncycastle.fips.approved_only=true`, which enforces usage of FIPS
approved functionality only.

Additionally, this PR grants read access to a custom trust store if
provided via `javax.net.ssl.trustStore`, otherwise read access to the
default JDK trust store is granted.

Relates to ES-11025.
2025-03-12 21:04:05 +11:00
Lorenzo Dematté
b81b89660c
[Entitlements] Exclude java.desktop from system modules (#124563) (#124618)
* exclude java.desktop from system modules

* add IT test
2025-03-12 19:51:59 +11:00
Patrick Doyle
f547d6cc96
Reduce noise from NotEntitledException logging (#124511) (#124590)
* Refactor: findRequestingFrame

* INFO instead of WARN for NotEntitledException.

Some of these are expected, so an INFO seems more appropriate.

The stack trace tends to attract attention even when entitlements are not the
cause of a problem, so let's avoid the stack trace, but still include stack
frame info from the frame of interest.

* Use child loggers for Not Entitled logs

* Use warn, and include compoenent name

* Fix ALL_UNNAMED

* Mute entitlement warnings from repositories

* PR feedback

* Common out the Not Entitled prefix.

We're alerting on this, so let's not rely on every caller of notEntitled to remember it.
2025-03-12 08:03:37 +11:00
Moritz Mack
f790cb894b
Rethrow NoSuchFileException if encountering an invalid symlink when checking file entitlements (#124483) (#124541)
This will rethrow the `NoSuchFileException` when encountering an invalid
symbolic link when following links during file (read) entitlement
checks.

Relates to https://github.com/elastic/elasticsearch/pull/124133
(ES-11019)
2025-03-11 21:51:31 +11:00
Lorenzo Dematté
b23b5b0a21
fix file tests to work across multiple invocations (#124412) (#124503) 2025-03-11 03:56:44 +11:00
Ryan Ernst
1f2db3fde7
Add exclusive access files for security module (#123676) (#124485)
* Add exclusive access files for security module (#123676)

This commit fills out missing entitlements for the security module.
Specifically they are config files which require exclusive access.

* Use a consistent ordering in policy manager exclulsive tests

* use better assertion

* [CI] Auto commit changes from spotless

---------

Co-authored-by: elasticsearchmachine <infra-root+elasticsearchmachine@elastic.co>
2025-03-11 03:46:26 +11:00
Moritz Mack
155ba4980b
Fix entitlement checks for relative links (#124133) (#124478) 2025-03-11 00:55:10 +11:00
Lorenzo Dematté
2a82161670
[Entitlements] Add URLConnection instrumentation for jar protocol (#123861) (#124441)
Follows #123824

Relates to ES-10994
2025-03-09 21:56:47 +11:00
Lorenzo Dematté
f503dda21a
[Entitlements] Allow read access to a plugin's directory (#124111) (#124423) 2025-03-09 05:05:18 +11:00
Lorenzo Dematté
23eec7b520
[8.18] Make NotEntitledException inherit from AccessControlException for compatibility purposes (#124321) (#124417)
* Make NotEntitledException inherit from AccessControlException for compatibility purposes (#124321)



Even if the contract for JDK methods using the SecurityManager states that the exception throw is of type SecurityException, many libraries (including our own, apparently!) violates that and use the type actually thrown by SecurityManager, AccessControlException. A prime example is the GCS/CSP libraries.
In order to maintain compatibility for them, we need to inherit from the more specific AccessControlException; this is less desirable, as AccessControlException is marked as deprecated for removal alongside the other SecurityManager classes, but we discussed and found this is the best short term solution.

More work will be needed -- again, this is a short term solution.

Replaces #123984

* cleanup exceptions
2025-03-09 05:03:39 +11:00
Patrick Doyle
ae68b2ab89
[9.0] Remove duplicate paths (including exclusive) in FileAccessTree (#123776 and #124023) (#123924) (#124331)
* remove duplicate paths in FileAccessTree (#123776)




* Remove duplicate exclusive paths (#124023)

* Remove duplicate exclusive paths

* Normalize paths in tests to support Windows

* Remove withMode

---------

Co-authored-by: Jack Conradson <osjdconrad@gmail.com>
Co-authored-by: Lorenzo Dematté <lorenzo.dematte@elastic.co>
2025-03-09 03:17:13 +11:00
Lorenzo Dematté
02c8dcf298
[Entitlements] Add support for IT testing always allowed actions (#124195) (#124414) 2025-03-09 02:59:58 +11:00
Lorenzo Dematté
dc44c4cf35
[Entitlements] Use the correct format for the EntitlementInstrumented annotation descriptor (#124310) (#124399) 2025-03-08 20:13:08 +11:00
Lorenzo Dematté
48ac63430b
[Entitlements] Fix AbstractDelegateHttpsURLConnection "this" parameter type (#124304) (#124326)
Our check methods injected by the instrumenter receive "this" as the second parameter.
For internal classes like AbstractDelegateHttpsURLConnection we generally use a base type; in this case we were using javax.net.ssl.HttpsURLConnection, which is incorrect as AbstractDelegateHttpsURLConnection derives from java.net.HttpURLConnection.
This was not failing in our tests because we don't actually use that parameter in that check function.

Also, it was not failing on transform, just on retransformClasses, and only in JDK 24. Apparently, JDK 24 introduced new validation there (to be confirmed).

And it was failing just on cloud as the APM agent there (which is loaded before our agent) connects to a https endpoint - our IT tests, and ./gradlew run --with-apm-server, use a http endpoint. Using https makes the JVM load AbstractDelegateHttpsURLConnection, making it one of the classes we need to retransform, triggering the VerifyError.
2025-03-08 02:27:46 +11:00
Lorenzo Dematté
236b458de3
[Entitlements] MailToURLConnection instrumentation (#123829) (#124232) 2025-03-07 20:18:27 +11:00
Lorenzo Dematté
c709524233
[Entitlements] Add URLConnection instrumentation for file protocol (#123824) (#124065) 2025-03-05 19:18:59 +11:00
Mark Vieira
017d7bfcd8
Add inbound_network entitlement to repository-hdfs plugin (#123907) (#124027) 2025-03-05 05:44:07 +11:00
Lorenzo Dematté
bc9e18fe38
Make NotEntitledException inherit from SecurityException for compatibility purposes (#123984) (#124007) 2025-03-05 02:51:56 +11:00
Lorenzo Dematté
e9b772bc60
[8.18][Entitlements] Add URLConnection instrumentation (#123966)
* [Entitlements] Add URLConnection instrumentation (#123503)

* [Entitlements] Add URLConnection instrumentation for ftp, http and https protocols (#123802)
2025-03-04 20:30:02 +11:00
Lorenzo Dematté
ac3742f353
Add missing APM entitlements (#123462) (#123964) 2025-03-04 20:07:35 +11:00