Skip to content
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

[Entitlements] Add support for IT testing always allowed actions #124195

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -233,9 +236,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 @@ -398,13 +400,25 @@ 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))
.map(Entry::getKey)
.collect(Collectors.toSet());
}

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

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
@@ -0,0 +1,36 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

package org.elasticsearch.entitlement.qa;

import com.carrotsearch.randomizedtesting.annotations.Name;
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;

import org.elasticsearch.entitlement.qa.test.RestEntitlementsCheckAction;
import org.junit.ClassRule;

public class EntitlementsAlwaysAllowedIT extends AbstractEntitlementsIT {

@ClassRule
public static EntitlementsTestRule testRule = new EntitlementsTestRule(true, null);

public EntitlementsAlwaysAllowedIT(@Name("actionName") String actionName) {
super(actionName, true);
}

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

@Override
protected String getTestRestCluster() {
return testRule.cluster.getHttpAddresses();
}
}
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