Skip to content

Commit e4184bc

Browse files
bizybotYogesh Gaikwad
authored and
Yogesh Gaikwad
committed
[Kerberos] Move tests based on SimpleKdc to evil-tests (#33492)
We have a test dependency on Apache Mina when using SimpleKdcServer for testing Kerberos. When checking for LDAP backend connectivity, the code checks for deadlocks which require additional security permissions accessClassInPackage.sun.reflect. As this is only for test and we do not want to add security permissions to production, this commit moves these tests and related classes to x-pack evil-tests where they can run with security manager disabled. The plan is to handle the security manager exception in the upstream issue DIRMINA-1093 and then once the release is available to run these tests with security manager enabled. Closes #32739
1 parent 70d804d commit e4184bc

File tree

11 files changed

+65
-61
lines changed

11 files changed

+65
-61
lines changed

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealmCacheTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ public void testCacheInvalidationScenarios() throws LoginException, GSSException
102102
public void testAuthenticateWithValidTicketSucessAuthnWithUserDetailsWhenCacheDisabled()
103103
throws LoginException, GSSException, IOException {
104104
// if cache.ttl <= 0 then the cache is disabled
105-
settings = KerberosTestCase.buildKerberosRealmSettings(
106-
KerberosTestCase.writeKeyTab(dir.resolve("key.keytab"), randomAlphaOfLength(4)).toString(), 100, "0m", true,
105+
settings = buildKerberosRealmSettings(
106+
writeKeyTab(dir.resolve("key.keytab"), randomAlphaOfLength(4)).toString(), 100, "0m", true,
107107
randomBoolean());
108108
final String username = randomPrincipalName();
109109
final String outToken = randomAlphaOfLength(10);

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealmSettingsTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ public void testKerberosRealmSettings() throws IOException {
2727
configDir = Files.createDirectory(configDir);
2828
}
2929
final String keytabPathConfig = "config" + dir.getFileSystem().getSeparator() + "http.keytab";
30-
KerberosTestCase.writeKeyTab(dir.resolve(keytabPathConfig), null);
30+
KerberosRealmTestCase.writeKeyTab(dir.resolve(keytabPathConfig), null);
3131
final Integer maxUsers = randomInt();
3232
final String cacheTTL = randomLongBetween(10L, 100L) + "m";
3333
final boolean enableDebugLogs = randomBoolean();
3434
final boolean removeRealmName = randomBoolean();
35-
final Settings settings = KerberosTestCase.buildKerberosRealmSettings(keytabPathConfig, maxUsers, cacheTTL, enableDebugLogs,
35+
final Settings settings = KerberosRealmTestCase.buildKerberosRealmSettings(keytabPathConfig, maxUsers, cacheTTL, enableDebugLogs,
3636
removeRealmName);
3737

3838
assertThat(KerberosRealmSettings.HTTP_SERVICE_KEYTAB_PATH.get(settings), equalTo(keytabPathConfig));

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealmTestCase.java

+51-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import org.elasticsearch.action.ActionListener;
1010
import org.elasticsearch.client.Client;
11+
import org.elasticsearch.common.Strings;
1112
import org.elasticsearch.common.collect.Tuple;
1213
import org.elasticsearch.common.settings.Settings;
1314
import org.elasticsearch.common.util.concurrent.ThreadContext;
@@ -28,6 +29,10 @@
2829
import org.junit.After;
2930
import org.junit.Before;
3031

32+
import java.io.BufferedWriter;
33+
import java.io.IOException;
34+
import java.nio.charset.StandardCharsets;
35+
import java.nio.file.Files;
3136
import java.nio.file.Path;
3237
import java.util.Arrays;
3338
import java.util.List;
@@ -67,7 +72,7 @@ public void setup() throws Exception {
6772
resourceWatcherService = new ResourceWatcherService(Settings.EMPTY, threadPool);
6873
dir = createTempDir();
6974
globalSettings = Settings.builder().put("path.home", dir).build();
70-
settings = KerberosTestCase.buildKerberosRealmSettings(KerberosTestCase.writeKeyTab(dir.resolve("key.keytab"), "asa").toString(),
75+
settings = buildKerberosRealmSettings(writeKeyTab(dir.resolve("key.keytab"), "asa").toString(),
7176
100, "10m", true, randomBoolean());
7277
}
7378

@@ -165,4 +170,49 @@ protected String maybeRemoveRealmName(final String principalName) {
165170
}
166171
return principalName;
167172
}
173+
174+
/**
175+
* Write content to provided keytab file.
176+
*
177+
* @param keytabPath {@link Path} to keytab file.
178+
* @param content Content for keytab
179+
* @return key tab path
180+
* @throws IOException if I/O error occurs while writing keytab file
181+
*/
182+
public static Path writeKeyTab(final Path keytabPath, final String content) throws IOException {
183+
try (BufferedWriter bufferedWriter = Files.newBufferedWriter(keytabPath, StandardCharsets.US_ASCII)) {
184+
bufferedWriter.write(Strings.isNullOrEmpty(content) ? "test-content" : content);
185+
}
186+
return keytabPath;
187+
}
188+
189+
/**
190+
* Build kerberos realm settings with default config and given keytab
191+
*
192+
* @param keytabPath key tab file path
193+
* @return {@link Settings} for kerberos realm
194+
*/
195+
public static Settings buildKerberosRealmSettings(final String keytabPath) {
196+
return buildKerberosRealmSettings(keytabPath, 100, "10m", true, false);
197+
}
198+
199+
/**
200+
* Build kerberos realm settings
201+
*
202+
* @param keytabPath key tab file path
203+
* @param maxUsersInCache max users to be maintained in cache
204+
* @param cacheTTL time to live for cached entries
205+
* @param enableDebugging for krb5 logs
206+
* @param removeRealmName {@code true} if we want to remove realm name from the username of form 'user@REALM'
207+
* @return {@link Settings} for kerberos realm
208+
*/
209+
public static Settings buildKerberosRealmSettings(final String keytabPath, final int maxUsersInCache, final String cacheTTL,
210+
final boolean enableDebugging, final boolean removeRealmName) {
211+
final Settings.Builder builder = Settings.builder().put(KerberosRealmSettings.HTTP_SERVICE_KEYTAB_PATH.getKey(), keytabPath)
212+
.put(KerberosRealmSettings.CACHE_MAX_USERS_SETTING.getKey(), maxUsersInCache)
213+
.put(KerberosRealmSettings.CACHE_TTL_SETTING.getKey(), cacheTTL)
214+
.put(KerberosRealmSettings.SETTING_KRB_DEBUG_ENABLE.getKey(), enableDebugging)
215+
.put(KerberosRealmSettings.SETTING_REMOVE_REALM_NAME.getKey(), removeRealmName);
216+
return builder.build();
217+
}
168218
}

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealmTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public void testKerberosRealmThrowsErrorWhenKeytabFileHasNoReadPermissions() thr
151151
}
152152

153153
private void assertKerberosRealmConstructorFails(final String keytabPath, final String expectedErrorMessage) {
154-
settings = KerberosTestCase.buildKerberosRealmSettings(keytabPath, 100, "10m", true, randomBoolean());
154+
settings = buildKerberosRealmSettings(keytabPath, 100, "10m", true, randomBoolean());
155155
config = new RealmConfig("test-kerb-realm", settings, globalSettings, TestEnvironment.newEnvironment(globalSettings),
156156
new ThreadContext(globalSettings));
157157
mockNativeRoleMappingStore = roleMappingStore(Arrays.asList("user"));

x-pack/qa/evil-tests/build.gradle

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
apply plugin: 'elasticsearch.standalone-test'
22

33
dependencies {
4-
testCompile project(path: xpackModule('core'), configuration: 'shadow')
4+
testCompile project(path: xpackModule('core'), configuration: 'testArtifacts')
5+
testCompile project(path: xpackModule('security'), configuration: 'testArtifacts')
56
}
67

78
test {
89
systemProperty 'tests.security.manager', 'false'
10+
include '**/*Tests.class'
911
}
+4-52
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,15 @@
99
import org.apache.logging.log4j.Logger;
1010
import org.elasticsearch.ExceptionsHelper;
1111
import org.elasticsearch.common.Randomness;
12-
import org.elasticsearch.common.Strings;
1312
import org.elasticsearch.common.logging.Loggers;
1413
import org.elasticsearch.common.settings.Settings;
1514
import org.elasticsearch.test.ESTestCase;
16-
import org.elasticsearch.xpack.core.security.authc.kerberos.KerberosRealmSettings;
1715
import org.junit.After;
1816
import org.junit.AfterClass;
1917
import org.junit.Before;
2018
import org.junit.BeforeClass;
2119

22-
import java.io.BufferedWriter;
2320
import java.io.IOException;
24-
import java.nio.charset.StandardCharsets;
25-
import java.nio.file.Files;
2621
import java.nio.file.Path;
2722
import java.security.AccessController;
2823
import java.security.PrivilegedActionException;
@@ -130,12 +125,14 @@ public void startSimpleKdcLdapServer() throws Exception {
130125
throw ExceptionsHelper.convertToRuntime(e);
131126
}
132127
});
133-
settings = buildKerberosRealmSettings(ktabPathForService.toString());
128+
settings = KerberosRealmTestCase.buildKerberosRealmSettings(ktabPathForService.toString());
134129
}
135130

136131
@After
137132
public void tearDownMiniKdc() throws IOException, PrivilegedActionException {
138-
simpleKdcLdapServer.stop();
133+
if (simpleKdcLdapServer != null) {
134+
simpleKdcLdapServer.stop();
135+
}
139136
}
140137

141138
/**
@@ -186,49 +183,4 @@ static <T> T doAsWrapper(final Subject subject, final PrivilegedExceptionAction<
186183
return AccessController.doPrivileged((PrivilegedExceptionAction<T>) () -> Subject.doAs(subject, action));
187184
}
188185

189-
/**
190-
* Write content to provided keytab file.
191-
*
192-
* @param keytabPath {@link Path} to keytab file.
193-
* @param content Content for keytab
194-
* @return key tab path
195-
* @throws IOException if I/O error occurs while writing keytab file
196-
*/
197-
public static Path writeKeyTab(final Path keytabPath, final String content) throws IOException {
198-
try (BufferedWriter bufferedWriter = Files.newBufferedWriter(keytabPath, StandardCharsets.US_ASCII)) {
199-
bufferedWriter.write(Strings.isNullOrEmpty(content) ? "test-content" : content);
200-
}
201-
return keytabPath;
202-
}
203-
204-
/**
205-
* Build kerberos realm settings with default config and given keytab
206-
*
207-
* @param keytabPath key tab file path
208-
* @return {@link Settings} for kerberos realm
209-
*/
210-
public static Settings buildKerberosRealmSettings(final String keytabPath) {
211-
return buildKerberosRealmSettings(keytabPath, 100, "10m", true, false);
212-
}
213-
214-
/**
215-
* Build kerberos realm settings
216-
*
217-
* @param keytabPath key tab file path
218-
* @param maxUsersInCache max users to be maintained in cache
219-
* @param cacheTTL time to live for cached entries
220-
* @param enableDebugging for krb5 logs
221-
* @param removeRealmName {@code true} if we want to remove realm name from the username of form 'user@REALM'
222-
* @return {@link Settings} for kerberos realm
223-
*/
224-
public static Settings buildKerberosRealmSettings(final String keytabPath, final int maxUsersInCache, final String cacheTTL,
225-
final boolean enableDebugging, final boolean removeRealmName) {
226-
final Settings.Builder builder = Settings.builder().put(KerberosRealmSettings.HTTP_SERVICE_KEYTAB_PATH.getKey(), keytabPath)
227-
.put(KerberosRealmSettings.CACHE_MAX_USERS_SETTING.getKey(), maxUsersInCache)
228-
.put(KerberosRealmSettings.CACHE_TTL_SETTING.getKey(), cacheTTL)
229-
.put(KerberosRealmSettings.SETTING_KRB_DEBUG_ENABLE.getKey(), enableDebugging)
230-
.put(KerberosRealmSettings.SETTING_REMOVE_REALM_NAME.getKey(), removeRealmName);
231-
return builder.build();
232-
}
233-
234186
}
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ public void testWhenKeyTabWithInvalidContentFailsValidation()
8686
final String base64KerbToken = spnegoClient.getBase64EncodedTokenForSpnegoHeader();
8787
assertThat(base64KerbToken, is(notNullValue()));
8888

89-
final Path ktabPath = writeKeyTab(workDir.resolve("invalid.keytab"), "not - a - valid - key - tab");
90-
settings = buildKerberosRealmSettings(ktabPath.toString());
89+
final Path ktabPath = KerberosRealmTestCase.writeKeyTab(workDir.resolve("invalid.keytab"), "not - a - valid - key - tab");
90+
settings = KerberosRealmTestCase.buildKerberosRealmSettings(ktabPath.toString());
9191
final Environment env = TestEnvironment.newEnvironment(globalSettings);
9292
final Path keytabPath = env.configFile().resolve(KerberosRealmSettings.HTTP_SERVICE_KEYTAB_PATH.get(settings));
9393
final PlainActionFuture<Tuple<String, String>> future = new PlainActionFuture<>();

0 commit comments

Comments
 (0)