Search in sources :

Example 1 with AclEntryPermission

use of java.nio.file.attribute.AclEntryPermission in project qpid-broker-j by apache.

the class AESKeyFileEncrypterFactory method createEmptyKeyFile.

private void createEmptyKeyFile(File file) throws IOException {
    final Path parentFilePath = file.getAbsoluteFile().getParentFile().toPath();
    if (isPosixFileSystem(file)) {
        Set<PosixFilePermission> ownerOnly = EnumSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE, PosixFilePermission.OWNER_EXECUTE);
        Files.createDirectories(parentFilePath, PosixFilePermissions.asFileAttribute(ownerOnly));
        Files.createFile(file.toPath(), PosixFilePermissions.asFileAttribute(EnumSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE)));
    } else if (isAclFileSystem(file)) {
        Files.createDirectories(parentFilePath);
        final UserPrincipal owner = Files.getOwner(parentFilePath);
        AclFileAttributeView attributeView = Files.getFileAttributeView(parentFilePath, AclFileAttributeView.class);
        List<AclEntry> acls = new ArrayList<>(attributeView.getAcl());
        ListIterator<AclEntry> iter = acls.listIterator();
        boolean found = false;
        while (iter.hasNext()) {
            AclEntry acl = iter.next();
            if (!owner.equals(acl.principal())) {
                iter.remove();
            } else if (acl.type() == AclEntryType.ALLOW) {
                found = true;
                AclEntry.Builder builder = AclEntry.newBuilder(acl);
                Set<AclEntryPermission> permissions = acl.permissions().isEmpty() ? new HashSet<AclEntryPermission>() : EnumSet.copyOf(acl.permissions());
                permissions.addAll(Arrays.asList(AclEntryPermission.ADD_FILE, AclEntryPermission.ADD_SUBDIRECTORY, AclEntryPermission.LIST_DIRECTORY));
                builder.setPermissions(permissions);
                iter.set(builder.build());
            }
        }
        if (!found) {
            AclEntry.Builder builder = AclEntry.newBuilder();
            builder.setPermissions(AclEntryPermission.ADD_FILE, AclEntryPermission.ADD_SUBDIRECTORY, AclEntryPermission.LIST_DIRECTORY);
            builder.setType(AclEntryType.ALLOW);
            builder.setPrincipal(owner);
            acls.add(builder.build());
        }
        attributeView.setAcl(acls);
        Files.createFile(file.toPath(), new FileAttribute<List<AclEntry>>() {

            @Override
            public String name() {
                return "acl:acl";
            }

            @Override
            public List<AclEntry> value() {
                AclEntry.Builder builder = AclEntry.newBuilder();
                builder.setType(AclEntryType.ALLOW);
                builder.setPermissions(EnumSet.allOf(AclEntryPermission.class));
                builder.setPrincipal(owner);
                return Collections.singletonList(builder.build());
            }
        });
    } else {
        throw new IllegalArgumentException("Unable to determine a mechanism to protect access to the key file on this filesystem");
    }
}
Also used : Path(java.nio.file.Path) AclFileAttributeView(java.nio.file.attribute.AclFileAttributeView) AclEntry(java.nio.file.attribute.AclEntry) AclEntryPermission(java.nio.file.attribute.AclEntryPermission) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) ListIterator(java.util.ListIterator) UserPrincipal(java.nio.file.attribute.UserPrincipal) ArrayList(java.util.ArrayList) List(java.util.List) FileAttribute(java.nio.file.attribute.FileAttribute)

Example 2 with AclEntryPermission

use of java.nio.file.attribute.AclEntryPermission in project j2objc by google.

the class AclEntryTest method testGetters.

@Test
public void testGetters() throws Exception {
    UserPrincipal user = Files.getOwner(Paths.get("."));
    AclEntry aclEntry = AclEntry.newBuilder().setType(AclEntryType.ALLOW).setPrincipal(user).setFlags(AclEntryFlag.INHERIT_ONLY).setPermissions(AclEntryPermission.READ_DATA, AclEntryPermission.READ_ATTRIBUTES).build();
    assertEquals(AclEntryType.ALLOW, aclEntry.type());
    assertEquals(user, aclEntry.principal());
    Set<AclEntryPermission> permissions = aclEntry.permissions();
    assertEquals(2, permissions.size());
    assertTrue(permissions.contains(AclEntryPermission.READ_DATA));
    assertTrue(permissions.contains(AclEntryPermission.READ_ATTRIBUTES));
    Set<AclEntryFlag> flags = aclEntry.flags();
    assertEquals(1, flags.size());
    assertTrue(flags.contains(AclEntryFlag.INHERIT_ONLY));
}
Also used : AclEntryFlag(java.nio.file.attribute.AclEntryFlag) AclEntry(java.nio.file.attribute.AclEntry) AclEntryPermission(java.nio.file.attribute.AclEntryPermission) UserPrincipal(java.nio.file.attribute.UserPrincipal) Test(org.junit.Test)

Aggregations

AclEntry (java.nio.file.attribute.AclEntry)2 AclEntryPermission (java.nio.file.attribute.AclEntryPermission)2 UserPrincipal (java.nio.file.attribute.UserPrincipal)2 Path (java.nio.file.Path)1 AclEntryFlag (java.nio.file.attribute.AclEntryFlag)1 AclFileAttributeView (java.nio.file.attribute.AclFileAttributeView)1 FileAttribute (java.nio.file.attribute.FileAttribute)1 PosixFilePermission (java.nio.file.attribute.PosixFilePermission)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ListIterator (java.util.ListIterator)1 Test (org.junit.Test)1