File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@
4343
/**
4444
* Thread-safe file implementation of a credential store.
4545
*
46-
* <p>For security purposes, the file's permissions are set to be accessible only by the file's
47-
* owner. Note that Java 1.5 does not support manipulating file permissions, and must be done
48-
* manually or using the JNI.
46+
* <p>For security purposes, the file's permissions are set such that the
47+
* file is only accessible by the file's owner.
4948
*
5049
* @since 1.16
5150
* @author Yaniv Inbar
@@ -136,26 +135,21 @@ public FileDataStoreFactory getDataStoreFactory() {
136135
* executed by the file's owner.
137136
*
138137
* @param file the file's permissions to modify
139-
* @throws IOException
138+
* @throws IOException if the permissions can't be set
140139
*/
141-
static void setPermissionsToOwnerOnly(File file) throws IOException {
140+
private static void setPermissionsToOwnerOnly(File file) throws IOException {
142141
Set permissions = new HashSet<PosixFilePermission>();
143142
permissions.add(PosixFilePermission.OWNER_READ);
144143
permissions.add(PosixFilePermission.OWNER_WRITE);
145144
permissions.add(PosixFilePermission.OWNER_EXECUTE);
146145
try {
147146
Files.setPosixFilePermissions(Paths.get(file.getAbsolutePath()), permissions);
148-
} catch (UnsupportedOperationException exception) {
149-
LOGGER.warning("Unable to set permissions for " + file
150-
+ ", because you are running on a non-POSIX file system.");
151-
} catch (SecurityException exception) {
152-
// ignored
153-
} catch (IllegalArgumentException exception) {
154-
// ignored
147+
} catch (RuntimeException exception) {
148+
throw new IOException("Unable to set permissions for " + file, exception);
155149
}
156150
}
157151

158-
static void setPermissionsToOwnerOnlyWindows(File file) throws IOException {
152+
private static void setPermissionsToOwnerOnlyWindows(File file) throws IOException {
159153
Path path = Paths.get(file.getAbsolutePath());
160154
FileOwnerAttributeView fileAttributeView = Files.getFileAttributeView(path, FileOwnerAttributeView.class);
161155
UserPrincipal owner = fileAttributeView.getOwner();
@@ -188,6 +182,11 @@ static void setPermissionsToOwnerOnlyWindows(File file) throws IOException {
188182
.build();
189183

190184
// Overwrite the ACL with only this permission
191-
view.setAcl(ImmutableList.of(entry));
185+
try {
186+
view.setAcl(ImmutableList.of(entry));
187+
} catch (SecurityException ex) {
188+
throw new IOException("Unable to set permissions for " + file, ex);
189+
}
190+
192191
}
193192
}

0 commit comments

Comments
 (0)