From 2efcb2c5a9fadc52bcac411898154735eea4a66a Mon Sep 17 00:00:00 2001 From: Zack Shoylev Date: Fri, 13 Nov 2015 01:16:25 -0600 Subject: [PATCH] Fixes a windows locale bug with the "Everyone" principal --- .../org/jclouds/filesystem/util/Utils.java | 36 +++++++++++++++++-- .../FilesystemStorageStrategyImplTest.java | 15 ++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/apis/filesystem/src/main/java/org/jclouds/filesystem/util/Utils.java b/apis/filesystem/src/main/java/org/jclouds/filesystem/util/Utils.java index ec1f56aeb8..6b7ec492c7 100644 --- a/apis/filesystem/src/main/java/org/jclouds/filesystem/util/Utils.java +++ b/apis/filesystem/src/main/java/org/jclouds/filesystem/util/Utils.java @@ -18,8 +18,10 @@ package org.jclouds.filesystem.util; import static java.nio.file.FileSystems.getDefault; +import java.io.BufferedReader; import java.io.File; import java.io.IOException; +import java.io.InputStreamReader; import java.nio.file.AccessDeniedException; import java.nio.file.DirectoryNotEmptyException; import java.nio.file.Files; @@ -99,13 +101,41 @@ public class Utils { throw new IOException("Could not delete: " + file.toPath()); } + /** + * @return Localized name for the "Everyone" Windows principal. + */ + public static final String getWindowsEveryonePrincipalName() { + if (isWindows()) { + try { + Process process = new ProcessBuilder("whoami", "/groups").start(); + try { + String line; + try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) { + while ((line = reader.readLine()) != null) { + if (line.indexOf("S-1-1-0") != -1) { + return line.split(" ")[0]; + } + } + } + } finally { + process.destroy(); + } + } catch (IOException e) { + } + } + // Default/fallback value + return "Everyone"; + } + + public static final String WINDOWS_EVERYONE = getWindowsEveryonePrincipalName(); + /** * @param path The path to a Windows file or directory. * @return true if path has permissions set to Everyone on windows. The exact permissions are not checked. */ public static boolean isPrivate(Path path) throws IOException { UserPrincipal everyone = getDefault().getUserPrincipalLookupService() - .lookupPrincipalByName("Everyone"); + .lookupPrincipalByName(WINDOWS_EVERYONE); AclFileAttributeView aclFileAttributes = java.nio.file.Files.getFileAttributeView( path, AclFileAttributeView.class); for (AclEntry aclEntry : aclFileAttributes.getAcl()) { @@ -121,7 +151,7 @@ public class Utils { */ public static void setPrivate(Path path) throws IOException { UserPrincipal everyone = getDefault().getUserPrincipalLookupService() - .lookupPrincipalByName("Everyone"); + .lookupPrincipalByName(WINDOWS_EVERYONE); AclFileAttributeView aclFileAttributes = java.nio.file.Files.getFileAttributeView( path, AclFileAttributeView.class); CopyOnWriteArrayList aclList = new CopyOnWriteArrayList(aclFileAttributes.getAcl()); @@ -138,7 +168,7 @@ public class Utils { */ public static void setPublic(Path path) throws IOException { UserPrincipal everyone = getDefault().getUserPrincipalLookupService() - .lookupPrincipalByName("Everyone"); + .lookupPrincipalByName(WINDOWS_EVERYONE); AclFileAttributeView aclFileAttributes = java.nio.file.Files.getFileAttributeView( path, AclFileAttributeView.class); List list = aclFileAttributes.getAcl(); diff --git a/apis/filesystem/src/test/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImplTest.java b/apis/filesystem/src/test/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImplTest.java index c16c9b12c8..61f08fce83 100644 --- a/apis/filesystem/src/test/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImplTest.java +++ b/apis/filesystem/src/test/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImplTest.java @@ -37,6 +37,7 @@ import javax.inject.Provider; import org.jclouds.blobstore.domain.Blob; import org.jclouds.blobstore.domain.BlobBuilder; +import org.jclouds.blobstore.domain.ContainerAccess; import org.jclouds.blobstore.domain.internal.BlobBuilderImpl; import org.jclouds.blobstore.options.ListContainerOptions; import org.jclouds.filesystem.predicates.validators.internal.FilesystemBlobKeyValidatorImpl; @@ -127,6 +128,20 @@ public class FilesystemStorageStrategyImplTest { TestUtils.directoryExists(TARGET_CONTAINER_NAME, true); } + public void testCreateContainerAccess() { + boolean result; + + TestUtils.directoryExists(TARGET_CONTAINER_NAME, false); + result = storageStrategy.createContainer(CONTAINER_NAME); + assertTrue(result, "Container not created"); + TestUtils.directoryExists(TARGET_CONTAINER_NAME, true); + + storageStrategy.setContainerAccess(CONTAINER_NAME, ContainerAccess.PRIVATE); + assertEquals(storageStrategy.getContainerAccess(CONTAINER_NAME), ContainerAccess.PRIVATE); + storageStrategy.setContainerAccess(CONTAINER_NAME, ContainerAccess.PUBLIC_READ); + assertEquals(storageStrategy.getContainerAccess(CONTAINER_NAME), ContainerAccess.PUBLIC_READ); + } + public void testCreateContainer_ContainerAlreadyExists() { boolean result;