Skip to content

Commit 9e2a506

Browse files
committed
Entitle com.unboundid.ldap.listener as test package
1 parent 05fc6f2 commit 9e2a506

File tree

6 files changed

+41
-10
lines changed

6 files changed

+41
-10
lines changed

test/framework/src/main/java/org/elasticsearch/entitlement/bootstrap/TestEntitlementBootstrap.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ public static void setTriviallyAllowingTestCode(boolean newValue) {
8787
policyManager.setTriviallyAllowingTestCode(newValue);
8888
}
8989

90+
public static void addEntitledTestPackages(String[] entitledTestPackages) {
91+
policyManager.addEntitledTestPackages(entitledTestPackages);
92+
}
93+
9094
public static void reset() {
9195
if (policyManager != null) {
9296
policyManager.reset();

test/framework/src/main/java/org/elasticsearch/entitlement/runtime/policy/TestPolicyManager.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
package org.elasticsearch.entitlement.runtime.policy;
1111

12+
import org.elasticsearch.common.util.ArrayUtils;
1213
import org.elasticsearch.entitlement.runtime.policy.entitlements.Entitlement;
1314
import org.elasticsearch.test.ESTestCase;
1415

@@ -17,6 +18,7 @@
1718
import java.nio.file.Path;
1819
import java.security.CodeSource;
1920
import java.security.ProtectionDomain;
21+
import java.util.Arrays;
2022
import java.util.Collection;
2123
import java.util.List;
2224
import java.util.Map;
@@ -29,6 +31,7 @@ public class TestPolicyManager extends PolicyManager {
2931

3032
boolean isActive;
3133
boolean isTriviallyAllowingTestCode;
34+
String[] entitledTestPackages = TEST_FRAMEWORK_PACKAGE_PREFIXES;
3235

3336
/**
3437
* We don't have modules in tests, so we can't use the inherited map of entitlements per module.
@@ -60,6 +63,12 @@ public void setTriviallyAllowingTestCode(boolean newValue) {
6063
this.isTriviallyAllowingTestCode = newValue;
6164
}
6265

66+
public void addEntitledTestPackages(String[] entitledTestPackages) {
67+
String[] packages = ArrayUtils.concat(this.entitledTestPackages, entitledTestPackages);
68+
Arrays.sort(packages);
69+
this.entitledTestPackages = packages;
70+
}
71+
6372
/**
6473
* Called between tests so each test is not affected by prior tests
6574
*/
@@ -110,17 +119,14 @@ private boolean isEntitlementClass(Class<?> requestingClass) {
110119
&& (requestingClass.getName().contains("Test") == false);
111120
}
112121

113-
@Deprecated // TODO: reevaluate whether we want this.
114-
// If we can simply check for dependencies the gradle worker has that aren't
115-
// declared in the gradle config (namely org.gradle) that would be simpler.
116122
private boolean isTestFrameworkClass(Class<?> requestingClass) {
117123
String packageName = requestingClass.getPackageName();
118-
for (String prefix : TEST_FRAMEWORK_PACKAGE_PREFIXES) {
119-
if (packageName.startsWith(prefix)) {
120-
return true;
121-
}
124+
int idx = Arrays.binarySearch(entitledTestPackages, packageName);
125+
if (idx >= 0) {
126+
return true;
122127
}
123-
return false;
128+
idx = -idx - 2; // candidate package (insertion point - 1)
129+
return idx >= 0 && idx < entitledTestPackages.length && packageName.startsWith(entitledTestPackages[idx]);
124130
}
125131

126132
private boolean isTestCode(Class<?> requestingClass) {
@@ -163,6 +169,10 @@ private boolean isTestCode(Class<?> requestingClass) {
163169
"org.bouncycastle.jsse.provider" // Used in test code if FIPS is enabled, support more fine-grained config in ES-12128
164170
};
165171

172+
static {
173+
Arrays.sort(TEST_FRAMEWORK_PACKAGE_PREFIXES);
174+
}
175+
166176
@Override
167177
protected ModuleEntitlements getEntitlements(Class<?> requestingClass) {
168178
return classEntitlementsMap.computeIfAbsent(requestingClass, this::computeEntitlements);

test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,16 +517,30 @@ protected void afterIfSuccessful() throws Exception {}
517517
public @interface WithEntitlementsOnTestCode {
518518
}
519519

520+
@Retention(RetentionPolicy.RUNTIME)
521+
@Target(ElementType.TYPE)
522+
@Inherited
523+
public @interface EntitledTestPackages {
524+
String[] value();
525+
}
526+
520527
@BeforeClass
521528
public static void setupEntitlementsForClass() {
522529
boolean withoutEntitlements = getTestClass().isAnnotationPresent(WithoutEntitlements.class);
523530
boolean withEntitlementsOnTestCode = getTestClass().isAnnotationPresent(WithEntitlementsOnTestCode.class);
531+
EntitledTestPackages entitledPackages = getTestClass().getAnnotation(EntitledTestPackages.class);
532+
524533
if (TestEntitlementBootstrap.isEnabledForTest()) {
525534
TestEntitlementBootstrap.setActive(false == withoutEntitlements);
526535
TestEntitlementBootstrap.setTriviallyAllowingTestCode(false == withEntitlementsOnTestCode);
536+
if (entitledPackages != null) {
537+
assert withEntitlementsOnTestCode == false : "Cannot use @WithEntitlementsOnTestCode together with @EntitledTestPackages";
538+
assert entitledPackages.value().length > 0 : "No test packages specified in @EntitledTestPackages";
539+
TestEntitlementBootstrap.addEntitledTestPackages(entitledPackages.value());
540+
}
527541
} else if (withEntitlementsOnTestCode) {
528542
throw new AssertionError(
529-
"Cannot use WithEntitlementsOnTestCode on tests that are not configured to use entitlements for testing"
543+
"Cannot use @WithEntitlementsOnTestCode on tests that are not configured to use entitlements for testing"
530544
);
531545
}
532546
}

x-pack/plugin/core/src/main/plugin-metadata/entitlement-policy.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ org.apache.httpcomponents.httpasyncclient:
1818
- manage_threads
1919
unboundid.ldapsdk:
2020
- set_https_connection_properties # TODO: review if we need this once we have proper test coverage
21-
- inbound_network # For com.unboundid.ldap.listener.LDAPListener
2221
- outbound_network
2322
- manage_threads
2423
- write_system_properties:

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectoryRealmTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.elasticsearch.script.ScriptService;
3434
import org.elasticsearch.script.mustache.MustacheScriptEngine;
3535
import org.elasticsearch.test.ESTestCase;
36+
import org.elasticsearch.test.ESTestCase.EntitledTestPackages;
3637
import org.elasticsearch.threadpool.TestThreadPool;
3738
import org.elasticsearch.threadpool.ThreadPool;
3839
import org.elasticsearch.watcher.ResourceWatcherService;
@@ -106,6 +107,7 @@
106107
* The username used to authenticate then has to be in the form of CN=user. Finally the username needs to be added as an
107108
* additional bind DN with a password in the test setup since it really is not a DN in the ldif file
108109
*/
110+
@EntitledTestPackages(value = { "com.unboundid.ldap.listener" }) // tests start LDAP server that listens for incoming connections
109111
public class ActiveDirectoryRealmTests extends ESTestCase {
110112

111113
private static final String PASSWORD = "password";

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/support/LdapTestCase.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.elasticsearch.core.TimeValue;
3030
import org.elasticsearch.env.TestEnvironment;
3131
import org.elasticsearch.test.ESTestCase;
32+
import org.elasticsearch.test.ESTestCase.EntitledTestPackages;
3233
import org.elasticsearch.watcher.ResourceWatcherService;
3334
import org.elasticsearch.xpack.core.XPackSettings;
3435
import org.elasticsearch.xpack.core.security.authc.RealmConfig;
@@ -73,6 +74,7 @@
7374
import static org.elasticsearch.xpack.core.security.authc.ldap.support.SessionFactorySettings.URLS_SETTING;
7475
import static org.hamcrest.Matchers.is;
7576

77+
@EntitledTestPackages(value = { "com.unboundid.ldap.listener" }) // tests start LDAP server that listens for incoming connections
7678
public abstract class LdapTestCase extends ESTestCase {
7779

7880
protected static final RealmConfig.RealmIdentifier REALM_IDENTIFIER = new RealmConfig.RealmIdentifier("ldap", "ldap1");

0 commit comments

Comments
 (0)