|
6 | 6 |
|
7 | 7 | package org.elasticsearch.xpack.security.authc.kerberos; |
8 | 8 |
|
| 9 | +import org.apache.lucene.util.Constants; |
9 | 10 | import org.elasticsearch.ElasticsearchSecurityException; |
10 | 11 | import org.elasticsearch.action.ActionListener; |
11 | 12 | import org.elasticsearch.action.support.PlainActionFuture; |
12 | 13 | import org.elasticsearch.common.collect.Tuple; |
13 | 14 | import org.elasticsearch.common.settings.SecureString; |
14 | 15 | import org.elasticsearch.common.util.concurrent.ThreadContext; |
15 | 16 | import org.elasticsearch.env.TestEnvironment; |
| 17 | +import org.elasticsearch.protocol.xpack.security.User; |
16 | 18 | import org.elasticsearch.rest.RestStatus; |
17 | 19 | import org.elasticsearch.xpack.core.security.authc.AuthenticationResult; |
18 | 20 | import org.elasticsearch.xpack.core.security.authc.RealmConfig; |
19 | 21 | import org.elasticsearch.xpack.core.security.authc.kerberos.KerberosRealmSettings; |
20 | 22 | import org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken; |
21 | | -import org.elasticsearch.protocol.xpack.security.User; |
22 | 23 | import org.elasticsearch.xpack.security.authc.support.UserRoleMapper.UserData; |
23 | 24 | import org.ietf.jgss.GSSException; |
24 | 25 |
|
|
34 | 35 | import java.nio.file.attribute.PosixFilePermissions; |
35 | 36 | import java.util.Arrays; |
36 | 37 | import java.util.EnumSet; |
| 38 | +import java.util.Locale; |
37 | 39 | import java.util.Set; |
38 | 40 |
|
39 | 41 | import javax.security.auth.login.LoginException; |
@@ -108,38 +110,47 @@ public void testLookupUser() { |
108 | 110 | assertThat(future.actionGet(), is(nullValue())); |
109 | 111 | } |
110 | 112 |
|
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("---------"); |
131 | 137 | final FileAttribute<Set<PosixFilePermission>> fileAttributes = PosixFilePermissions.asFileAttribute(filePerms); |
132 | 138 | try (SeekableByteChannel byteChannel = Files.newByteChannel(dir.resolve(keytabFileName), |
133 | 139 | EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE), fileAttributes)) { |
134 | 140 | byteChannel.write(ByteBuffer.wrap(randomByteArrayOfLength(10))); |
135 | 141 | } |
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)); |
141 | 147 | } |
| 148 | + final String expectedErrorMessage = "configured service key tab file [" + keytabPath + "] must have read permission"; |
| 149 | + |
| 150 | + assertKerberosRealmConstructorFails(keytabPath.toString(), expectedErrorMessage); |
| 151 | + } |
142 | 152 |
|
| 153 | + private void assertKerberosRealmConstructorFails(final String keytabPath, final String expectedErrorMessage) { |
143 | 154 | settings = KerberosTestCase.buildKerberosRealmSettings(keytabPath, 100, "10m", true, randomBoolean()); |
144 | 155 | config = new RealmConfig("test-kerb-realm", settings, globalSettings, TestEnvironment.newEnvironment(globalSettings), |
145 | 156 | new ThreadContext(globalSettings)); |
|
0 commit comments