use of java.nio.file.attribute.UserPrincipal in project java-chassis by ServiceComb.
the class FortifyUtils method getDefaultFileAttributes.
public static FileAttribute<?> getDefaultFileAttributes(String filePath) {
Path file = new File(filePath).toPath();
if (isPosix()) {
return PosixFilePermissions.asFileAttribute(FilePerm.getDefaultPosixPerm());
} else {
// for not posix must support ACL, or failed.
String userName = System.getProperty("user.name");
UserPrincipal user = null;
try {
user = file.getFileSystem().getUserPrincipalLookupService().lookupPrincipalByName(userName);
} catch (IOException e) {
throw new RuntimeException("Unknown user error.");
}
final AclEntry entry = AclEntry.newBuilder().setType(AclEntryType.ALLOW).setPrincipal(user).setPermissions(FilePerm.getDefaultAclPerm()).setFlags(new AclEntryFlag[] { AclEntryFlag.FILE_INHERIT, AclEntryFlag.DIRECTORY_INHERIT }).build();
return new FileAttribute<List<AclEntry>>() {
public String name() {
return "acl:acl";
}
/* Windows ACL */
//public Object value() { ArrayList l = new ArrayList(); l.add(entry); return l; }
public List<AclEntry> value() {
ArrayList<AclEntry> l = new ArrayList<AclEntry>();
l.add(entry);
return l;
}
};
}
}
use of java.nio.file.attribute.UserPrincipal in project che by eclipse.
the class WindowsSshScript method protectPrivateKeyFile.
@Override
protected void protectPrivateKeyFile(File sshKey) throws ServerException {
try {
AclFileAttributeView attributes = Files.getFileAttributeView(sshKey.toPath(), AclFileAttributeView.class);
AclEntry.Builder builder = AclEntry.newBuilder();
builder.setType(ALLOW);
String ownerName = System.getProperty(OWNER_NAME_PROPERTY);
UserPrincipal userPrincipal = FileSystems.getDefault().getUserPrincipalLookupService().lookupPrincipalByName(ownerName);
builder.setPrincipal(userPrincipal);
builder.setPermissions(READ_DATA, APPEND_DATA, READ_NAMED_ATTRS, READ_ATTRIBUTES, DELETE, READ_ACL, SYNCHRONIZE);
AclEntry entry = builder.build();
List<AclEntry> aclEntryList = new ArrayList<>();
aclEntryList.add(entry);
attributes.setAcl(aclEntryList);
} catch (IOException e) {
throw new ServerException("Failed to set file permissions");
}
}
use of java.nio.file.attribute.UserPrincipal in project vert.x by eclipse.
the class FileSystemImpl method chownInternal.
protected BlockingAction<Void> chownInternal(String path, String user, String group, Handler<AsyncResult<Void>> handler) {
Objects.requireNonNull(path);
return new BlockingAction<Void>(handler) {
public Void perform() {
try {
Path target = vertx.resolveFile(path).toPath();
UserPrincipalLookupService service = target.getFileSystem().getUserPrincipalLookupService();
UserPrincipal userPrincipal = user == null ? null : service.lookupPrincipalByName(user);
GroupPrincipal groupPrincipal = group == null ? null : service.lookupPrincipalByGroupName(group);
if (groupPrincipal != null) {
PosixFileAttributeView view = Files.getFileAttributeView(target, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
if (view == null) {
throw new FileSystemException("Change group of file not supported");
}
view.setGroup(groupPrincipal);
}
if (userPrincipal != null) {
Files.setOwner(target, userPrincipal);
}
} catch (SecurityException e) {
throw new FileSystemException("Accessed denied for chown on " + path);
} catch (IOException e) {
throw new FileSystemException(e);
}
return null;
}
};
}
use of java.nio.file.attribute.UserPrincipal in project vert.x by eclipse.
the class FileSystemTest method testChownToOwnUser.
@Test
public void testChownToOwnUser() throws Exception {
String file1 = "some-file.dat";
createFileWithJunk(file1, 100);
String fullPath = testDir + pathSep + file1;
Path path = Paths.get(fullPath);
UserPrincipal owner = Files.getOwner(path);
String user = owner.getName();
vertx.fileSystem().chown(fullPath, user, null, ar -> {
deleteFile(file1);
assertTrue(ar.succeeded());
testComplete();
});
await();
}
use of java.nio.file.attribute.UserPrincipal in project elasticsearch by elastic.
the class InstallPluginCommandTests method assertPlugin.
void assertPlugin(String name, Path original, Environment env) throws IOException {
Path got = env.pluginsFile().resolve(name);
assertTrue("dir " + name + " exists", Files.exists(got));
if (isPosix) {
Set<PosixFilePermission> perms = Files.getPosixFilePermissions(got);
assertThat(perms, containsInAnyOrder(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE, PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.GROUP_READ, PosixFilePermission.GROUP_EXECUTE, PosixFilePermission.OTHERS_READ, PosixFilePermission.OTHERS_EXECUTE));
}
assertTrue("jar was copied", Files.exists(got.resolve("plugin.jar")));
assertFalse("bin was not copied", Files.exists(got.resolve("bin")));
assertFalse("config was not copied", Files.exists(got.resolve("config")));
if (Files.exists(original.resolve("bin"))) {
Path binDir = env.binFile().resolve(name);
assertTrue("bin dir exists", Files.exists(binDir));
assertTrue("bin is a dir", Files.isDirectory(binDir));
PosixFileAttributes binAttributes = null;
if (isPosix) {
binAttributes = Files.readAttributes(env.binFile(), PosixFileAttributes.class);
}
try (DirectoryStream<Path> stream = Files.newDirectoryStream(binDir)) {
for (Path file : stream) {
assertFalse("not a dir", Files.isDirectory(file));
if (isPosix) {
PosixFileAttributes attributes = Files.readAttributes(file, PosixFileAttributes.class);
assertEquals(InstallPluginCommand.BIN_FILES_PERMS, attributes.permissions());
}
}
}
}
if (Files.exists(original.resolve("config"))) {
Path configDir = env.configFile().resolve(name);
assertTrue("config dir exists", Files.exists(configDir));
assertTrue("config is a dir", Files.isDirectory(configDir));
UserPrincipal user = null;
GroupPrincipal group = null;
if (isPosix) {
PosixFileAttributes configAttributes = Files.getFileAttributeView(env.configFile(), PosixFileAttributeView.class).readAttributes();
user = configAttributes.owner();
group = configAttributes.group();
PosixFileAttributes attributes = Files.getFileAttributeView(configDir, PosixFileAttributeView.class).readAttributes();
assertThat(attributes.owner(), equalTo(user));
assertThat(attributes.group(), equalTo(group));
}
try (DirectoryStream<Path> stream = Files.newDirectoryStream(configDir)) {
for (Path file : stream) {
assertFalse("not a dir", Files.isDirectory(file));
if (isPosix) {
PosixFileAttributes attributes = Files.readAttributes(file, PosixFileAttributes.class);
if (user != null) {
assertThat(attributes.owner(), equalTo(user));
}
if (group != null) {
assertThat(attributes.group(), equalTo(group));
}
}
}
}
}
assertInstallCleaned(env);
}
Aggregations