Refine search
final Path copyFile = rootDirPath.resolve(flowFile.getAttribute(CoreAttributes.FILENAME.key())); if (!Files.exists(rootDirPath)) { if (context.getProperty(CREATE_DIRS).asBoolean()) { Files.createDirectories(rootDirPath); } else { flowFile = session.penalize(flowFile); if (Files.exists(finalCopyFileDir) && maxDestinationFiles != null) { // check if too many files already final int numFiles = finalCopyFileDir.toFile().list().length; if (owner != null && !owner.trim().isEmpty()) { try { UserPrincipalLookupService lookupService = dotCopyFile.getFileSystem().getUserPrincipalLookupService(); Files.setOwner(dotCopyFile, lookupService.lookupPrincipalByName(owner)); } catch (Exception e) { logger.warn("Could not set file owner to {} because {}", new Object[]{owner, e}); if (group != null && !group.trim().isEmpty()) { try { UserPrincipalLookupService lookupService = dotCopyFile.getFileSystem().getUserPrincipalLookupService(); PosixFileAttributeView view = Files.getFileAttributeView(dotCopyFile, PosixFileAttributeView.class); view.setGroup(lookupService.lookupPrincipalByGroupName(group)); } catch (Exception e) { logger.warn("Could not set file group to {} because {}", new Object[]{group, e});
public static void setFilePermissions(Path filePath) throws IOException { if ("root".equalsIgnoreCase(ZaleniumConfiguration.getCurrentUser())) { UserPrincipal hostUid = filePath.getFileSystem() .getUserPrincipalLookupService().lookupPrincipalByName(ZaleniumConfiguration.getHostUid()); Files.setOwner(filePath, hostUid); GroupPrincipal hostGid = filePath.getFileSystem() .getUserPrincipalLookupService().lookupPrincipalByGroupName(ZaleniumConfiguration.getHostGid()); Files.getFileAttributeView(filePath, PosixFileAttributeView.class).setGroup(hostGid); } }
static void verifyTmpDirSetup(PolicyUpdaterConfiguration configuration) throws IOException { // ensure tmp dir exists String policyTmpDir = configuration.getPolicyFileTmpDir(); Path tmpDir = Paths.get(policyTmpDir); if (java.nio.file.Files.exists(tmpDir)) { return; } LOG.warn("The temp dir doesnt exist so will create it: " + tmpDir); java.nio.file.Files.createDirectory(tmpDir); // get the user from config file to perform chown aginst the tmp dir // chown -R $zpu_user $ROOT/tmp/zpe String user = configuration.getZpuDirOwner(); if (user == null) { LOG.warn("Cannot chown of the temp dir: " + tmpDir + " : no configured user"); return; } try { java.nio.file.attribute.UserPrincipalLookupService lookupSvc = java.nio.file.FileSystems.getDefault().getUserPrincipalLookupService(); java.nio.file.attribute.UserPrincipal uprinc = lookupSvc.lookupPrincipalByName(user); Files.setOwner(tmpDir, uprinc); } catch (Exception exc) { LOG.warn("Failed to chown of the temp dir: " + tmpDir + ", user: " + user + ", exc: " + exc.getMessage()); } }
/** * Changes the local file's group. * * @param path that will change owner * @param group the new group */ public static void changeLocalFileGroup(String path, String group) throws IOException { UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService(); PosixFileAttributeView view = Files.getFileAttributeView(Paths.get(path), PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS); GroupPrincipal groupPrincipal = lookupService.lookupPrincipalByGroupName(group); view.setGroup(groupPrincipal); }
@Test @Category( { Principals.class, Posix.class, Unix.class, PermissionChecks.class } ) public void testDifferentOwnerCanNotWrite() throws IOException { UserPrincipal none = FS.getUserPrincipalLookupService().lookupPrincipalByName( "root" ); Files.setOwner( fileTA(), none ); //System.out.println( Files.getPosixFilePermissions( fileTA() ) ); assertThatThrownBy( () -> Files.write( absTA(), CONTENT_OTHER ) ).isInstanceOf( AccessDeniedException.class ); }
private UserPrincipal getUserPrincipalFrom(String userName) throws IOException { try { if (_isCacheEnabled) { return _nameToUserPrincipal.get(userName); } UserPrincipalLookupService service = FileSystems.getDefault().getUserPrincipalLookupService(); return service.lookupPrincipalByName(userName); } catch (IOException | UnsupportedOperationException e) { return null; } }
private GroupPrincipal getGroupPrincipalFrom(String groupName) throws IOException { try { if (_isCacheEnabled) { return _nameToGroupPrincipal.get(groupName); } UserPrincipalLookupService service = FileSystems.getDefault().getUserPrincipalLookupService(); return service.lookupPrincipalByGroupName(groupName); } catch (IOException | UnsupportedOperationException e) { return null; } }
FileSystems.getDefault().getUserPrincipalLookupService(); UserPrincipal user = lookupService.lookupPrincipalByName(uid); Path path = Paths.get(file.getPath()); GroupPrincipal group = lookupService.lookupPrincipalByGroupName(gid); Files.setAttribute(path, "posix:owner", user, LinkOption.NOFOLLOW_LINKS); Files.setAttribute(path, "posix:group", group, LinkOption.NOFOLLOW_LINKS); } else { Files.setAttribute(path, "posix:owner", user); Files.setAttribute(path, "posix:group", group);
@Test @Category( { Principals.class, Attributes.class, FileOwnerView.class } ) public void testFindOwner() throws IOException { UserPrincipalLookupService lookupService = FS.getUserPrincipalLookupService(); UserPrincipal owner = Files.getOwner( FS.getPath( "" ).toAbsolutePath() ); assertThat( lookupService.lookupPrincipalByName( owner.getName() ) ).isEqualTo( owner ); }
final UserPrincipalLookupService upls = fileSystem.getUserPrincipalLookupService(); UserPrincipal root = null; try { if (SystemUtils.IS_OS_UNIX) { root = upls.lookupPrincipalByName("root"); } else { root = upls.lookupPrincipalByName("Administrators"); user = upls.lookupPrincipalByName(System.getProperty("user.name")); final Path partialPath = Paths.get(fileRoot.toString(), file.subpath(0, i).toString()); try { if (Files.isSymbolicLink(partialPath)) { if (!isInSecureDir(Files.readSymbolicLink(partialPath), user, symlinkDepth - 1)) { final UserPrincipal owner = Files.getOwner(partialPath); if (!user.equals(owner) && !root.equals(owner)) {
PosixFileAttributeView attributeView = Files.getFileAttributeView(file.toPath(), PosixFileAttributeView.class); UserPrincipalLookupService lookupService = file.toPath().getFileSystem().getUserPrincipalLookupService(); Files.setAttribute(file.toPath(), "unix:uid", Integer.parseInt(ownerName.substring(4)), getLinkOptions()); else // otherwise set owner by name (look up principals first) attributeView.setOwner(lookupService.lookupPrincipalByName(ownerName)); Files.setAttribute(file.toPath(), "unix:gid", Integer.parseInt(groupOwnerName.substring(4)), getLinkOptions()); else // otherwise set group owner by name (look up principals first) attributeView.setGroup(lookupService.lookupPrincipalByGroupName(groupOwnerName));
void fixSshKeyOnWindows(File key) throws GitException { if (launcher.isUnix()) return; Path file = Paths.get(key.toURI()); AclFileAttributeView fileAttributeView = Files.getFileAttributeView(file, AclFileAttributeView.class); if (fileAttributeView == null) return; String username = getWindowsUserName(fileAttributeView); if (StringUtils.isBlank(username)) return; try { UserPrincipalLookupService userPrincipalLookupService = file.getFileSystem().getUserPrincipalLookupService(); UserPrincipal userPrincipal = userPrincipalLookupService.lookupPrincipalByName(username); AclEntry aclEntry = AclEntry.newBuilder() .setType(AclEntryType.ALLOW) .setPrincipal(userPrincipal) .setPermissions(ACL_ENTRY_PERMISSIONS) .build(); fileAttributeView.setAcl(Collections.singletonList(aclEntry)); } catch (IOException | UnsupportedOperationException e) { throw new GitException("Error updating file permission for \"" + key.getAbsolutePath() + "\""); } }
@Override public void setUnixGroup(final Path file, final String group) throws BackgroundException { try { final GroupPrincipal principal = session.getClient().getUserPrincipalLookupService().lookupPrincipalByGroupName(group); Files.getFileAttributeView(session.toPath(file), PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS).setGroup(principal); } catch(IOException e) { throw new LocalExceptionMappingService().map("Failure to write attributes of {0}", e, file); } }
/** * Set the file owner * @param path path to file * @param owner the owner * @throws IOException in case of failure in underlying layer */ public static final void setOwner(String path, String owner) throws IOException { UserPrincipal userPrincipal = Paths.get(path).getFileSystem().getUserPrincipalLookupService().lookupPrincipalByName(owner); Files.setOwner(Paths.get(path), userPrincipal); }
protected UserPrincipal toUser(Path file, UserPrincipal name) throws IOException { String username = name.toString(); FileSystem fileSystem = file.getFileSystem(); UserPrincipalLookupService lookupService = fileSystem.getUserPrincipalLookupService(); try { if (lookupService == null) { throw new UserPrincipalNotFoundException(username); } return lookupService.lookupPrincipalByName(username); } catch (IOException e) { handleUserPrincipalLookupServiceException(UserPrincipal.class, username, e); return null; } }
protected GroupPrincipal toGroup(Path file, GroupPrincipal name) throws IOException { String groupName = name.toString(); FileSystem fileSystem = file.getFileSystem(); UserPrincipalLookupService lookupService = fileSystem.getUserPrincipalLookupService(); try { if (lookupService == null) { throw new UserPrincipalNotFoundException(groupName); } return lookupService.lookupPrincipalByGroupName(groupName); } catch (IOException e) { handleUserPrincipalLookupServiceException(GroupPrincipal.class, groupName, e); return null; } }
public static void main(String[] args) throws IOException { Path file = Paths.get("c:/touch.txt"); AclFileAttributeView aclAttr = Files.getFileAttributeView(file, AclFileAttributeView.class); System.out.println(aclAttr.getOwner()); for(AclEntry aclEntry : aclAttr.getAcl()){ System.out.println(aclEntry); } System.out.println(); UserPrincipalLookupService upls = file.getFileSystem().getUserPrincipalLookupService(); UserPrincipal user = upls.lookupPrincipalByName(System.getProperty("user.name")); AclEntry.Builder builder = AclEntry.newBuilder(); builder.setPermissions( EnumSet.of(AclEntryPermission.READ_DATA, AclEntryPermission.EXECUTE, AclEntryPermission.READ_ACL, AclEntryPermission.READ_ATTRIBUTES, AclEntryPermission.READ_NAMED_ATTRS, AclEntryPermission.WRITE_ACL, AclEntryPermission.DELETE )); builder.setPrincipal(user); builder.setType(AclEntryType.ALLOW); aclAttr.setAcl(Collections.singletonList(builder.build())); }
public HadoopPosixFileAttributes(HadoopFileSystem hdfs, Object fileKey, FileStatus fileStatus) throws IOException { super(fileKey, fileStatus); this.owner = hdfs.getUserPrincipalLookupService() .lookupPrincipalByGroupName(fileStatus.getOwner()); this.group = hdfs.getUserPrincipalLookupService() .lookupPrincipalByGroupName(fileStatus.getGroup()); FsPermission fsPermission = getFileStatus().getPermission(); String perms = fsPermission.getUserAction().SYMBOL + fsPermission.getGroupAction().SYMBOL + fsPermission.getOtherAction().SYMBOL; this.permissions = PosixFilePermissions.fromString(perms); }
/** * Changes the local file's user. * * @param path that will change owner * @param user the new user */ public static void changeLocalFileUser(String path, String user) throws IOException { UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService(); PosixFileAttributeView view = Files.getFileAttributeView(Paths.get(path), PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS); UserPrincipal userPrincipal = lookupService.lookupPrincipalByName(user); view.setOwner(userPrincipal); }
private UserPrincipal getUserPrincipalFrom(String userName) throws IOException { try { UserPrincipal principal = _nameToUserPrincipal.get(userName); if (principal == null) { UserPrincipalLookupService service = FileSystems.getDefault().getUserPrincipalLookupService(); principal = service.lookupPrincipalByName(userName); _nameToUserPrincipal.put(userName, principal); } return principal; } catch (UnsupportedOperationException e) { throw new IOException(e); } }