Skip to content

Commit

Permalink
[Entitlements] Add support for IT testing always allowed actions (#12…
Browse files Browse the repository at this point in the history
  • Loading branch information
ldematte authored Mar 8, 2025
1 parent a15259c commit e90d6d2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ enum ExpectedAccess {
PLUGINS,
ES_MODULES_ONLY,
SERVER_ONLY,
ALWAYS_DENIED
ALWAYS_DENIED,
ALWAYS_ALLOWED
}

ExpectedAccess expectedAccess();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@
import javax.net.ssl.SSLContext;

import static java.util.Map.entry;
import static org.elasticsearch.entitlement.qa.test.EntitlementTest.ExpectedAccess.ALWAYS_ALLOWED;
import static org.elasticsearch.entitlement.qa.test.EntitlementTest.ExpectedAccess.ALWAYS_DENIED;
import static org.elasticsearch.entitlement.qa.test.EntitlementTest.ExpectedAccess.PLUGINS;
import static org.elasticsearch.entitlement.qa.test.EntitlementTest.ExpectedAccess.SERVER_ONLY;
import static org.elasticsearch.entitlement.qa.test.RestEntitlementsCheckAction.CheckAction.alwaysDenied;
import static org.elasticsearch.entitlement.qa.test.RestEntitlementsCheckAction.CheckAction.deniedToPlugins;
import static org.elasticsearch.entitlement.qa.test.RestEntitlementsCheckAction.CheckAction.forPlugins;
Expand All @@ -65,20 +68,20 @@
public class RestEntitlementsCheckAction extends BaseRestHandler {
private static final Logger logger = LogManager.getLogger(RestEntitlementsCheckAction.class);

record CheckAction(CheckedRunnable<Exception> action, boolean isAlwaysDeniedToPlugins, Integer fromJavaVersion) {
record CheckAction(CheckedRunnable<Exception> action, EntitlementTest.ExpectedAccess expectedAccess, Integer fromJavaVersion) {
/**
* These cannot be granted to plugins, so our test plugins cannot test the "allowed" case.
*/
static CheckAction deniedToPlugins(CheckedRunnable<Exception> action) {
return new CheckAction(action, true, null);
return new CheckAction(action, SERVER_ONLY, null);
}

static CheckAction forPlugins(CheckedRunnable<Exception> action) {
return new CheckAction(action, false, null);
return new CheckAction(action, PLUGINS, null);
}

static CheckAction alwaysDenied(CheckedRunnable<Exception> action) {
return new CheckAction(action, true, null);
return new CheckAction(action, ALWAYS_DENIED, null);
}
}

Expand Down Expand Up @@ -125,7 +128,7 @@ static CheckAction alwaysDenied(CheckedRunnable<Exception> action) {
entry("responseCache_setDefault", alwaysDenied(RestEntitlementsCheckAction::setDefaultResponseCache)),
entry(
"createInetAddressResolverProvider",
new CheckAction(VersionSpecificNetworkChecks::createInetAddressResolverProvider, true, 18)
new CheckAction(VersionSpecificNetworkChecks::createInetAddressResolverProvider, SERVER_ONLY, 18)
),
entry("createURLStreamHandlerProvider", alwaysDenied(RestEntitlementsCheckAction::createURLStreamHandlerProvider)),
entry("createURLWithURLStreamHandler", alwaysDenied(RestEntitlementsCheckAction::createURLWithURLStreamHandler)),
Expand Down Expand Up @@ -235,9 +238,8 @@ private static Stream<Entry<String, CheckAction>> getTestEntries(Class<?> action
}
}
};
boolean deniedToPlugins = testAnnotation.expectedAccess() != PLUGINS;
Integer fromJavaVersion = testAnnotation.fromJavaVersion() == -1 ? null : testAnnotation.fromJavaVersion();
entries.add(entry(method.getName(), new CheckAction(runnable, deniedToPlugins, fromJavaVersion)));
entries.add(entry(method.getName(), new CheckAction(runnable, testAnnotation.expectedAccess(), fromJavaVersion)));
}
return entries.stream();
}
Expand Down Expand Up @@ -400,13 +402,17 @@ private static void receiveDatagramSocket() throws IOException {
public static Set<String> getCheckActionsAllowedInPlugins() {
return checkActions.entrySet()
.stream()
.filter(kv -> kv.getValue().isAlwaysDeniedToPlugins() == false)
.filter(kv -> kv.getValue().expectedAccess().equals(PLUGINS) || kv.getValue().expectedAccess().equals(ALWAYS_ALLOWED))
.map(Entry::getKey)
.collect(Collectors.toSet());
}

public static Set<String> getAllCheckActions() {
return checkActions.keySet();
public static Set<String> getDeniableCheckActions() {
return checkActions.entrySet()
.stream()
.filter(kv -> kv.getValue().expectedAccess().equals(ALWAYS_ALLOWED) == false)
.map(Entry::getKey)
.collect(Collectors.toSet());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public EntitlementsDeniedIT(@Name("actionName") String actionName) {

@ParametersFactory
public static Iterable<Object[]> data() {
return RestEntitlementsCheckAction.getAllCheckActions().stream().map(action -> new Object[] { action }).toList();
return RestEntitlementsCheckAction.getDeniableCheckActions().stream().map(action -> new Object[] { action }).toList();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public EntitlementsDeniedNonModularIT(@Name("actionName") String actionName) {

@ParametersFactory
public static Iterable<Object[]> data() {
return RestEntitlementsCheckAction.getAllCheckActions().stream().map(action -> new Object[] { action }).toList();
return RestEntitlementsCheckAction.getDeniableCheckActions().stream().map(action -> new Object[] { action }).toList();
}

@Override
Expand Down

0 comments on commit e90d6d2

Please sign in to comment.