Skip to content

Commit d7733ba

Browse files
bizybotYogesh Gaikwad
authored andcommitted
[TEST] Split tests and skip file permission test on Windows (#32781)
Changes to split tests for keytab file test cases instead of randomized testing for testing branches in the code in the same test. On windows platform, for keytab file permission test, we required additional security permissions for the test framework. As this was the only test that required those permissions, skipping that test on windows platform. The same scenario gets tested in *nix environments. Closes#32768
1 parent 7533e95 commit d7733ba

File tree

1 file changed

+37
-26
lines changed

1 file changed

+37
-26
lines changed

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

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@
66

77
package org.elasticsearch.xpack.security.authc.kerberos;
88

9+
import org.apache.lucene.util.Constants;
910
import org.elasticsearch.ElasticsearchSecurityException;
1011
import org.elasticsearch.action.ActionListener;
1112
import org.elasticsearch.action.support.PlainActionFuture;
1213
import org.elasticsearch.common.collect.Tuple;
1314
import org.elasticsearch.common.settings.SecureString;
1415
import org.elasticsearch.common.util.concurrent.ThreadContext;
1516
import org.elasticsearch.env.TestEnvironment;
17+
import org.elasticsearch.protocol.xpack.security.User;
1618
import org.elasticsearch.rest.RestStatus;
1719
import org.elasticsearch.xpack.core.security.authc.AuthenticationResult;
1820
import org.elasticsearch.xpack.core.security.authc.RealmConfig;
1921
import org.elasticsearch.xpack.core.security.authc.kerberos.KerberosRealmSettings;
2022
import org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken;
21-
import org.elasticsearch.protocol.xpack.security.User;
2223
import org.elasticsearch.xpack.security.authc.support.UserRoleMapper.UserData;
2324
import org.ietf.jgss.GSSException;
2425

@@ -34,6 +35,7 @@
3435
import java.nio.file.attribute.PosixFilePermissions;
3536
import java.util.Arrays;
3637
import java.util.EnumSet;
38+
import java.util.Locale;
3739
import java.util.Set;
3840

3941
import javax.security.auth.login.LoginException;
@@ -108,38 +110,47 @@ public void testLookupUser() {
108110
assertThat(future.actionGet(), is(nullValue()));
109111
}
110112

111-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/32768")
112-
public void testKerberosRealmWithInvalidKeytabPathConfigurations() throws IOException {
113-
final String keytabPathCase = randomFrom("keytabPathAsDirectory", "keytabFileDoesNotExist", "keytabPathWithNoReadPermissions");
114-
final String expectedErrorMessage;
115-
final String keytabPath;
116-
final Set<PosixFilePermission> filePerms;
117-
switch (keytabPathCase) {
118-
case "keytabPathAsDirectory":
119-
final String dirName = randomAlphaOfLength(5);
120-
Files.createDirectory(dir.resolve(dirName));
121-
keytabPath = dir.resolve(dirName).toString();
122-
expectedErrorMessage = "configured service key tab file [" + keytabPath + "] is a directory";
123-
break;
124-
case "keytabFileDoesNotExist":
125-
keytabPath = dir.resolve(randomAlphaOfLength(5) + ".keytab").toString();
126-
expectedErrorMessage = "configured service key tab file [" + keytabPath + "] does not exist";
127-
break;
128-
case "keytabPathWithNoReadPermissions":
129-
filePerms = PosixFilePermissions.fromString("---------");
130-
final String keytabFileName = randomAlphaOfLength(5) + ".keytab";
113+
public void testKerberosRealmThrowsErrorWhenKeytabPathIsConfiguredAsDirectory() throws IOException {
114+
final String dirName = randomAlphaOfLength(5);
115+
Files.createDirectory(dir.resolve(dirName));
116+
final String keytabPath = dir.resolve(dirName).toString();
117+
final String expectedErrorMessage = "configured service key tab file [" + keytabPath + "] is a directory";
118+
119+
assertKerberosRealmConstructorFails(keytabPath, expectedErrorMessage);
120+
}
121+
122+
public void testKerberosRealmThrowsErrorWhenKeytabFileDoesNotExist() throws IOException {
123+
final String keytabPath = dir.resolve(randomAlphaOfLength(5) + ".keytab").toString();
124+
final String expectedErrorMessage = "configured service key tab file [" + keytabPath + "] does not exist";
125+
126+
assertKerberosRealmConstructorFails(keytabPath, expectedErrorMessage);
127+
}
128+
129+
public void testKerberosRealmThrowsErrorWhenKeytabFileHasNoReadPermissions() throws IOException {
130+
assumeFalse("Not running this test on Windows, as it requires additional access permissions for test framework.",
131+
Constants.WINDOWS);
132+
final Set<String> supportedAttributes = dir.getFileSystem().supportedFileAttributeViews();
133+
final String keytabFileName = randomAlphaOfLength(5) + ".keytab";
134+
final Path keytabPath;
135+
if (supportedAttributes.contains("posix")) {
136+
final Set<PosixFilePermission> filePerms = PosixFilePermissions.fromString("---------");
131137
final FileAttribute<Set<PosixFilePermission>> fileAttributes = PosixFilePermissions.asFileAttribute(filePerms);
132138
try (SeekableByteChannel byteChannel = Files.newByteChannel(dir.resolve(keytabFileName),
133139
EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE), fileAttributes)) {
134140
byteChannel.write(ByteBuffer.wrap(randomByteArrayOfLength(10)));
135141
}
136-
keytabPath = dir.resolve(keytabFileName).toString();
137-
expectedErrorMessage = "configured service key tab file [" + keytabPath + "] must have read permission";
138-
break;
139-
default:
140-
throw new IllegalArgumentException("Unknown test case :" + keytabPathCase);
142+
keytabPath = dir.resolve(keytabFileName);
143+
} else {
144+
throw new UnsupportedOperationException(
145+
String.format(Locale.ROOT, "Don't know how to make file [%s] non-readable on a file system with attributes [%s]",
146+
dir.resolve(keytabFileName), supportedAttributes));
141147
}
148+
final String expectedErrorMessage = "configured service key tab file [" + keytabPath + "] must have read permission";
149+
150+
assertKerberosRealmConstructorFails(keytabPath.toString(), expectedErrorMessage);
151+
}
142152

153+
private void assertKerberosRealmConstructorFails(final String keytabPath, final String expectedErrorMessage) {
143154
settings = KerberosTestCase.buildKerberosRealmSettings(keytabPath, 100, "10m", true, randomBoolean());
144155
config = new RealmConfig("test-kerb-realm", settings, globalSettings, TestEnvironment.newEnvironment(globalSettings),
145156
new ThreadContext(globalSettings));

0 commit comments

Comments
 (0)