Skip to content

Commit d3e27ff

Browse files
authored
[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 3914a98 commit d3e27ff

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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 51 additions & 1 deletion
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;
@@ -30,6 +31,10 @@
3031
import org.junit.After;
3132
import org.junit.Before;
3233

34+
import java.io.BufferedWriter;
35+
import java.io.IOException;
36+
import java.nio.charset.StandardCharsets;
37+
import java.nio.file.Files;
3338
import java.nio.file.Path;
3439
import java.util.Arrays;
3540
import java.util.Collections;
@@ -71,7 +76,7 @@ public void setup() throws Exception {
7176
resourceWatcherService = new ResourceWatcherService(Settings.EMPTY, threadPool);
7277
dir = createTempDir();
7378
globalSettings = Settings.builder().put("path.home", dir).build();
74-
settings = KerberosTestCase.buildKerberosRealmSettings(KerberosTestCase.writeKeyTab(dir.resolve("key.keytab"), "asa").toString(),
79+
settings = buildKerberosRealmSettings(writeKeyTab(dir.resolve("key.keytab"), "asa").toString(),
7580
100, "10m", true, randomBoolean());
7681
licenseState = mock(XPackLicenseState.class);
7782
when(licenseState.isAuthorizationRealmAllowed()).thenReturn(true);
@@ -177,4 +182,49 @@ protected String maybeRemoveRealmName(final String principalName) {
177182
}
178183
return principalName;
179184
}
185+
186+
/**
187+
* Write content to provided keytab file.
188+
*
189+
* @param keytabPath {@link Path} to keytab file.
190+
* @param content Content for keytab
191+
* @return key tab path
192+
* @throws IOException if I/O error occurs while writing keytab file
193+
*/
194+
public static Path writeKeyTab(final Path keytabPath, final String content) throws IOException {
195+
try (BufferedWriter bufferedWriter = Files.newBufferedWriter(keytabPath, StandardCharsets.US_ASCII)) {
196+
bufferedWriter.write(Strings.isNullOrEmpty(content) ? "test-content" : content);
197+
}
198+
return keytabPath;
199+
}
200+
201+
/**
202+
* Build kerberos realm settings with default config and given keytab
203+
*
204+
* @param keytabPath key tab file path
205+
* @return {@link Settings} for kerberos realm
206+
*/
207+
public static Settings buildKerberosRealmSettings(final String keytabPath) {
208+
return buildKerberosRealmSettings(keytabPath, 100, "10m", true, false);
209+
}
210+
211+
/**
212+
* Build kerberos realm settings
213+
*
214+
* @param keytabPath key tab file path
215+
* @param maxUsersInCache max users to be maintained in cache
216+
* @param cacheTTL time to live for cached entries
217+
* @param enableDebugging for krb5 logs
218+
* @param removeRealmName {@code true} if we want to remove realm name from the username of form 'user@REALM'
219+
* @return {@link Settings} for kerberos realm
220+
*/
221+
public static Settings buildKerberosRealmSettings(final String keytabPath, final int maxUsersInCache, final String cacheTTL,
222+
final boolean enableDebugging, final boolean removeRealmName) {
223+
final Settings.Builder builder = Settings.builder().put(KerberosRealmSettings.HTTP_SERVICE_KEYTAB_PATH.getKey(), keytabPath)
224+
.put(KerberosRealmSettings.CACHE_MAX_USERS_SETTING.getKey(), maxUsersInCache)
225+
.put(KerberosRealmSettings.CACHE_TTL_SETTING.getKey(), cacheTTL)
226+
.put(KerberosRealmSettings.SETTING_KRB_DEBUG_ENABLE.getKey(), enableDebugging)
227+
.put(KerberosRealmSettings.SETTING_REMOVE_REALM_NAME.getKey(), removeRealmName);
228+
return builder.build();
229+
}
180230
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public void testKerberosRealmThrowsErrorWhenKeytabFileHasNoReadPermissions() thr
155155
}
156156

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

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

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

33
dependencies {
4-
testCompile "org.elasticsearch.plugin:x-pack-core:${version}"
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
}
Lines changed: 4 additions & 52 deletions
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
}
Lines changed: 2 additions & 2 deletions
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)