Skip to content

Commit 80740bc

Browse files
authored
Fix entitlements in internalClusterTest (elastic#131539) (elastic#132070)
Previously, entitlement checks got disabled when resetting the policy manager (which defaults to inactive). This change makes sure entitlements are correctly enabled during tests. Due to the lack of entitlement delegation (and usage of server's FileSystemUtils and similar in test code), there's a few remaining issues: - various tests have to run without entitlements - node base dirs cannot be removed immediately when shutting down the node due to pending cleanups (wipePendingDataDirectories) Due to Netty dependency issues (ES-12435), azure and inference tests have to run without entitlements. (cherry picked from commit 5d72a3f)
1 parent c1c00e1 commit 80740bc

File tree

21 files changed

+97
-28
lines changed

21 files changed

+97
-28
lines changed

libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/policy/PolicyManager.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,7 @@ private ModuleEntitlements getModuleScopeEntitlements(
367367
* @return true if permission is granted regardless of the entitlement
368368
*/
369369
boolean isTriviallyAllowed(Class<?> requestingClass) {
370-
if (generalLogger.isTraceEnabled()) {
371-
generalLogger.trace("Stack trace for upcoming trivially-allowed check", new Exception());
372-
}
370+
// note: do not log exceptions in here, this could interfere with loading of additionally necessary classes such as ThrowableProxy
373371
if (requestingClass == null) {
374372
generalLogger.debug("Entitlement trivially allowed: no caller frames outside the entitlement library");
375373
return true;

modules/repository-azure/src/internalClusterTest/java/org/elasticsearch/repositories/azure/AzureBlobStoreRepositoryTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.elasticsearch.plugins.Plugin;
3232
import org.elasticsearch.repositories.blobstore.ESMockAPIBasedRepositoryIntegTestCase;
3333
import org.elasticsearch.rest.RestStatus;
34+
import org.elasticsearch.test.ESTestCase;
3435

3536
import java.io.ByteArrayInputStream;
3637
import java.io.IOException;
@@ -53,6 +54,7 @@
5354
import static org.hamcrest.Matchers.is;
5455

5556
@SuppressForbidden(reason = "this test uses a HttpServer to emulate an Azure endpoint")
57+
@ESTestCase.WithoutEntitlements // due to dependency issue ES-12435
5658
public class AzureBlobStoreRepositoryTests extends ESMockAPIBasedRepositoryIntegTestCase {
5759

5860
private static final String DEFAULT_ACCOUNT_NAME = "account";

modules/repository-azure/src/internalClusterTest/java/org/elasticsearch/repositories/azure/AzureRepositoryMissingCredentialsIT.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
import org.elasticsearch.plugins.Plugin;
1919
import org.elasticsearch.repositories.RepositoryVerificationException;
2020
import org.elasticsearch.test.ESIntegTestCase;
21+
import org.elasticsearch.test.ESTestCase;
2122

2223
import java.util.Collection;
2324

2425
import static org.hamcrest.Matchers.allOf;
2526
import static org.hamcrest.Matchers.containsString;
2627

28+
@ESTestCase.WithoutEntitlements // due to dependency issue ES-12435
2729
public class AzureRepositoryMissingCredentialsIT extends ESIntegTestCase {
2830

2931
@Override

server/src/internalClusterTest/java/org/elasticsearch/action/admin/ReloadSecureSettingsIT.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.elasticsearch.plugins.PluginsService;
2828
import org.elasticsearch.plugins.ReloadablePlugin;
2929
import org.elasticsearch.test.ESIntegTestCase;
30+
import org.elasticsearch.test.ESTestCase;
3031
import org.junit.BeforeClass;
3132

3233
import java.io.InputStream;
@@ -47,6 +48,7 @@
4748
import static org.hamcrest.Matchers.nullValue;
4849

4950
@ESIntegTestCase.ClusterScope(minNumDataNodes = 2)
51+
@ESTestCase.WithoutEntitlements // requires entitlement delegation ES-10920
5052
public class ReloadSecureSettingsIT extends ESIntegTestCase {
5153

5254
private static final String VALID_SECURE_SETTING_NAME = "some.setting.that.exists";

server/src/internalClusterTest/java/org/elasticsearch/action/admin/cluster/tasks/PendingTasksBlocksIT.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.common.settings.Settings;
1313
import org.elasticsearch.gateway.GatewayService;
1414
import org.elasticsearch.test.ESIntegTestCase;
15+
import org.elasticsearch.test.ESTestCase;
1516
import org.elasticsearch.test.InternalTestCluster;
1617

1718
import java.util.Arrays;
@@ -22,6 +23,7 @@
2223
import static org.elasticsearch.cluster.metadata.IndexMetadata.SETTING_READ_ONLY;
2324
import static org.elasticsearch.cluster.metadata.IndexMetadata.SETTING_READ_ONLY_ALLOW_DELETE;
2425

26+
@ESTestCase.WithoutEntitlements // requires entitlement delegation ES-10920
2527
public class PendingTasksBlocksIT extends ESIntegTestCase {
2628

2729
public void testPendingTasksWithIndexBlocks() {

server/src/internalClusterTest/java/org/elasticsearch/cluster/coordination/RemoveCustomsCommandIT.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
import org.elasticsearch.env.Environment;
1919
import org.elasticsearch.env.TestEnvironment;
2020
import org.elasticsearch.test.ESIntegTestCase;
21+
import org.elasticsearch.test.ESTestCase;
2122

2223
import java.util.Map;
2324

2425
import static org.hamcrest.Matchers.containsString;
2526

2627
@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0, autoManageMasterNodes = false)
28+
@ESTestCase.WithoutEntitlements // commands don't run with entitlements enforced
2729
public class RemoveCustomsCommandIT extends ESIntegTestCase {
2830

2931
public void testRemoveCustomsAbortedByUser() throws Exception {

server/src/internalClusterTest/java/org/elasticsearch/cluster/coordination/RemoveIndexSettingsCommandIT.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.elasticsearch.env.TestEnvironment;
2222
import org.elasticsearch.plugins.Plugin;
2323
import org.elasticsearch.test.ESIntegTestCase;
24+
import org.elasticsearch.test.ESTestCase;
2425

2526
import java.util.Collection;
2627
import java.util.List;
@@ -31,6 +32,7 @@
3132
import static org.hamcrest.Matchers.not;
3233

3334
@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0, autoManageMasterNodes = false)
35+
@ESTestCase.WithoutEntitlements // commands don't run with entitlements enforced
3436
public class RemoveIndexSettingsCommandIT extends ESIntegTestCase {
3537

3638
static final Setting<Integer> FOO = Setting.intSetting("index.foo", 1, Setting.Property.IndexScope, Setting.Property.Dynamic);

server/src/internalClusterTest/java/org/elasticsearch/cluster/coordination/RemoveSettingsCommandIT.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.elasticsearch.env.Environment;
2020
import org.elasticsearch.env.TestEnvironment;
2121
import org.elasticsearch.test.ESIntegTestCase;
22+
import org.elasticsearch.test.ESTestCase;
2223

2324
import java.util.Map;
2425

@@ -27,6 +28,7 @@
2728
import static org.hamcrest.Matchers.not;
2829

2930
@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0, autoManageMasterNodes = false)
31+
@ESTestCase.WithoutEntitlements // commands don't run with entitlements enforced
3032
public class RemoveSettingsCommandIT extends ESIntegTestCase {
3133

3234
public void testRemoveSettingsAbortedByUser() throws Exception {

server/src/internalClusterTest/java/org/elasticsearch/index/shard/RemoveCorruptedShardDataCommandIT.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import org.elasticsearch.plugins.Plugin;
5959
import org.elasticsearch.test.CorruptionUtils;
6060
import org.elasticsearch.test.ESIntegTestCase;
61+
import org.elasticsearch.test.ESTestCase;
6162
import org.elasticsearch.test.InternalSettingsPlugin;
6263
import org.elasticsearch.test.InternalTestCluster;
6364
import org.elasticsearch.test.engine.MockEngineSupport;
@@ -93,6 +94,7 @@
9394
import static org.hamcrest.Matchers.startsWith;
9495

9596
@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0)
97+
@ESTestCase.WithoutEntitlements // commands don't run with entitlements enforced
9698
public class RemoveCorruptedShardDataCommandIT extends ESIntegTestCase {
9799

98100
@Override

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

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

1010
package org.elasticsearch.entitlement.bootstrap;
1111

12+
import org.apache.lucene.tests.mockfile.FilterPath;
1213
import org.elasticsearch.bootstrap.TestBuildInfo;
1314
import org.elasticsearch.bootstrap.TestBuildInfoParser;
1415
import org.elasticsearch.bootstrap.TestScopeResolver;
@@ -38,6 +39,7 @@
3839
import java.util.ArrayList;
3940
import java.util.Arrays;
4041
import java.util.Collection;
42+
import java.util.Collections;
4143
import java.util.HashMap;
4244
import java.util.HashSet;
4345
import java.util.List;
@@ -83,34 +85,56 @@ public static void registerNodeBaseDirs(Settings settings, Path configPath) {
8385
if (policyManager == null) {
8486
return;
8587
}
86-
Path homeDir = absolutePath(PATH_HOME_SETTING.get(settings));
87-
Path configDir = configPath != null ? configPath : homeDir.resolve("config");
88+
89+
Path homeDir = homeDir(settings);
90+
Path configDir = configDir(configPath, homeDir);
8891
Collection<Path> dataDirs = dataDirs(settings, homeDir);
8992
Collection<Path> sharedDataDir = sharedDataDir(settings);
9093
Collection<Path> repoDirs = repoDirs(settings);
91-
logger.debug("Registering node dirs: config [{}], dataDirs [{}], repoDirs [{}]", configDir, dataDirs, repoDirs);
94+
logger.debug(
95+
"Registering node dirs: config [{}], dataDirs [{}], sharedDataDir [{}], repoDirs [{}]",
96+
configDir,
97+
dataDirs,
98+
sharedDataDir,
99+
repoDirs
100+
);
92101
baseDirPaths.compute(BaseDir.CONFIG, baseDirModifier(paths -> paths.add(configDir)));
93102
baseDirPaths.compute(BaseDir.DATA, baseDirModifier(paths -> paths.addAll(dataDirs)));
94103
baseDirPaths.compute(BaseDir.SHARED_DATA, baseDirModifier(paths -> paths.addAll(sharedDataDir)));
95104
baseDirPaths.compute(BaseDir.SHARED_REPO, baseDirModifier(paths -> paths.addAll(repoDirs)));
96-
policyManager.reset();
105+
policyManager.clearModuleEntitlementsCache();
97106
}
98107

99108
public static void unregisterNodeBaseDirs(Settings settings, Path configPath) {
100109
if (policyManager == null) {
101110
return;
102111
}
103-
Path homeDir = absolutePath(PATH_HOME_SETTING.get(settings));
104-
Path configDir = configPath != null ? configPath : homeDir.resolve("config");
112+
113+
Path homeDir = homeDir(settings);
114+
Path configDir = configDir(configPath, homeDir);
105115
Collection<Path> dataDirs = dataDirs(settings, homeDir);
106116
Collection<Path> sharedDataDir = sharedDataDir(settings);
107117
Collection<Path> repoDirs = repoDirs(settings);
108-
logger.debug("Unregistering node dirs: config [{}], dataDirs [{}], repoDirs [{}]", configDir, dataDirs, repoDirs);
118+
logger.debug(
119+
"Unregistering node dirs: config [{}], dataDirs [{}], sharedDataDir [{}], repoDirs [{}]",
120+
configDir,
121+
dataDirs,
122+
sharedDataDir,
123+
repoDirs
124+
);
109125
baseDirPaths.compute(BaseDir.CONFIG, baseDirModifier(paths -> paths.remove(configDir)));
110126
baseDirPaths.compute(BaseDir.DATA, baseDirModifier(paths -> paths.removeAll(dataDirs)));
111127
baseDirPaths.compute(BaseDir.SHARED_DATA, baseDirModifier(paths -> paths.removeAll(sharedDataDir)));
112128
baseDirPaths.compute(BaseDir.SHARED_REPO, baseDirModifier(paths -> paths.removeAll(repoDirs)));
113-
policyManager.reset();
129+
policyManager.clearModuleEntitlementsCache();
130+
}
131+
132+
private static Path homeDir(Settings settings) {
133+
return absolutePath(PATH_HOME_SETTING.get(settings));
134+
}
135+
136+
private static Path configDir(Path configDir, Path homeDir) {
137+
return configDir != null ? unwrapFilterPath(configDir) : homeDir.resolve("config");
114138
}
115139

116140
private static Collection<Path> dataDirs(Settings settings, Path homeDir) {
@@ -130,15 +154,21 @@ private static Collection<Path> repoDirs(Settings settings) {
130154
}
131155

132156
private static BiFunction<BaseDir, Collection<Path>, Collection<Path>> baseDirModifier(Consumer<Collection<Path>> consumer) {
157+
// always return a new unmodifiable copy
133158
return (BaseDir baseDir, Collection<Path> paths) -> {
134-
if (paths == null) {
135-
paths = new HashSet<>();
136-
}
159+
paths = paths == null ? new HashSet<>() : new HashSet<>(paths);
137160
consumer.accept(paths);
138-
return paths;
161+
return Collections.unmodifiableCollection(paths);
139162
};
140163
}
141164

165+
private static Path unwrapFilterPath(Path path) {
166+
while (path instanceof FilterPath fPath) {
167+
path = fPath.getDelegate();
168+
}
169+
return path;
170+
}
171+
142172
@SuppressForbidden(reason = "must be resolved using the default file system, rather then the mocked test file system")
143173
private static Path absolutePath(String path) {
144174
return Paths.get(path).toAbsolutePath().normalize();
@@ -168,9 +198,11 @@ public static void setEntitledTestPackages(String[] entitledTestPackages) {
168198
policyManager.setEntitledTestPackages(entitledTestPackages);
169199
}
170200

171-
public static void reset() {
201+
public static void resetAfterTest() {
202+
// reset all base dirs except TEMP, which is initialized just once statically
203+
baseDirPaths.keySet().retainAll(List.of(TEMP));
172204
if (policyManager != null) {
173-
policyManager.reset();
205+
policyManager.resetAfterTest();
174206
}
175207
}
176208

0 commit comments

Comments
 (0)