23
23
import org .ietf .jgss .GSSException ;
24
24
25
25
import java .io .IOException ;
26
- import java .nio .ByteBuffer ;
27
- import java .nio .channels .SeekableByteChannel ;
28
26
import java .nio .charset .StandardCharsets ;
29
27
import java .nio .file .Files ;
30
28
import java .nio .file .Path ;
31
- import java .nio .file .StandardOpenOption ;
32
- import java .nio .file .attribute .FileAttribute ;
33
- import java .nio .file .attribute .PosixFilePermission ;
29
+ import java .nio .file .attribute .AclEntry ;
30
+ import java .nio .file .attribute .AclEntryPermission ;
31
+ import java .nio .file .attribute .AclEntryType ;
32
+ import java .nio .file .attribute .AclFileAttributeView ;
33
+ import java .nio .file .attribute .PosixFileAttributeView ;
34
34
import java .nio .file .attribute .PosixFilePermissions ;
35
+ import java .nio .file .attribute .UserPrincipal ;
35
36
import java .util .Arrays ;
36
- import java .util .EnumSet ;
37
+ import java .util .List ;
38
+ import java .util .Locale ;
37
39
import java .util .Set ;
38
40
39
41
import javax .security .auth .login .LoginException ;
@@ -112,7 +114,6 @@ public void testKerberosRealmWithInvalidKeytabPathConfigurations() throws IOExce
112
114
final String keytabPathCase = randomFrom ("keytabPathAsDirectory" , "keytabFileDoesNotExist" , "keytabPathWithNoReadPermissions" );
113
115
final String expectedErrorMessage ;
114
116
final String keytabPath ;
115
- final Set <PosixFilePermission > filePerms ;
116
117
switch (keytabPathCase ) {
117
118
case "keytabPathAsDirectory" :
118
119
final String dirName = randomAlphaOfLength (5 );
@@ -125,14 +126,29 @@ public void testKerberosRealmWithInvalidKeytabPathConfigurations() throws IOExce
125
126
expectedErrorMessage = "configured service key tab file [" + keytabPath + "] does not exist" ;
126
127
break ;
127
128
case "keytabPathWithNoReadPermissions" :
128
- filePerms = PosixFilePermissions .fromString ("---------" );
129
- final String keytabFileName = randomAlphaOfLength (5 ) + ".keytab" ;
130
- final FileAttribute <Set <PosixFilePermission >> fileAttributes = PosixFilePermissions .asFileAttribute (filePerms );
131
- try (SeekableByteChannel byteChannel = Files .newByteChannel (dir .resolve (keytabFileName ),
132
- EnumSet .of (StandardOpenOption .CREATE_NEW , StandardOpenOption .WRITE ), fileAttributes )) {
133
- byteChannel .write (ByteBuffer .wrap (randomByteArrayOfLength (10 )));
129
+ final String fileName = randomAlphaOfLength (5 );
130
+ final Path keytabFilePath = Files .createTempFile (dir , fileName , ".keytab" );
131
+ Files .write (keytabFilePath , randomAlphaOfLength (5 ).getBytes (StandardCharsets .UTF_8 ));
132
+ final Set <String > supportedAttributes = keytabFilePath .getFileSystem ().supportedFileAttributeViews ();
133
+ if (supportedAttributes .contains ("posix" )) {
134
+ final PosixFileAttributeView fileAttributeView = Files .getFileAttributeView (keytabFilePath , PosixFileAttributeView .class );
135
+ fileAttributeView .setPermissions (PosixFilePermissions .fromString ("---------" ));
136
+ } else if (supportedAttributes .contains ("acl" )) {
137
+ final UserPrincipal principal = Files .getOwner (keytabFilePath );
138
+ final AclFileAttributeView view = Files .getFileAttributeView (keytabFilePath , AclFileAttributeView .class );
139
+ final AclEntry entry = AclEntry .newBuilder ()
140
+ .setType (AclEntryType .DENY )
141
+ .setPrincipal (principal )
142
+ .setPermissions (AclEntryPermission .READ_DATA , AclEntryPermission .READ_ATTRIBUTES ).build ();
143
+ final List <AclEntry > acl = view .getAcl ();
144
+ acl .add (0 , entry );
145
+ view .setAcl (acl );
146
+ } else {
147
+ throw new UnsupportedOperationException (
148
+ String .format (Locale .ROOT , "Don't know how to make file [%s] non-readable on a file system with attributes [%s]" ,
149
+ keytabFilePath , supportedAttributes ));
134
150
}
135
- keytabPath = dir . resolve ( keytabFileName ) .toString ();
151
+ keytabPath = keytabFilePath .toString ();
136
152
expectedErrorMessage = "configured service key tab file [" + keytabPath + "] must have read permission" ;
137
153
break ;
138
154
default :
0 commit comments