Skip to content

Commit

Permalink
Rename FsAction and FsPermission to full name
Browse files Browse the repository at this point in the history
  • Loading branch information
dongche committed Nov 20, 2015
1 parent 5fa892d commit b9ae1a5
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 169 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/** /**
* POSIX style file system actions, e.g. read, write, etc. * POSIX style file system actions, e.g. read, write, etc.
*/ */
public enum FsAction { public enum FileSystemAction {
NONE("---"), NONE("---"),
EXECUTE("--x"), EXECUTE("--x"),
WRITE("-w-"), WRITE("-w-"),
Expand All @@ -32,9 +32,9 @@ public enum FsAction {
private final String mSymbol; private final String mSymbol;


/** Retain reference to value array. */ /** Retain reference to value array. */
private static final FsAction[] SVALS = values(); private static final FileSystemAction[] SVALS = values();


FsAction(String s) { FileSystemAction(String s) {
mSymbol = s; mSymbol = s;
} }


Expand All @@ -47,7 +47,7 @@ public String getSymbol() {
* @param that a passed-in action * @param that a passed-in action
* @return true when this action implies the passed-in action * @return true when this action implies the passed-in action
*/ */
public boolean imply(FsAction that) { public boolean imply(FileSystemAction that) {
if (that != null) { if (that != null) {
return (ordinal() & that.ordinal()) == that.ordinal(); return (ordinal() & that.ordinal()) == that.ordinal();
} }
Expand All @@ -59,7 +59,7 @@ public boolean imply(FsAction that) {
* @param that a passed-in action * @param that a passed-in action
* @return the intersection of this action and the passed-in action * @return the intersection of this action and the passed-in action
*/ */
public FsAction and(FsAction that) { public FileSystemAction and(FileSystemAction that) {
if (that != null) { if (that != null) {
return SVALS[ordinal() & that.ordinal()]; return SVALS[ordinal() & that.ordinal()];
} }
Expand All @@ -71,7 +71,7 @@ public FsAction and(FsAction that) {
* @param that a passed-in action * @param that a passed-in action
* @return the union of this action and the passed-in action * @return the union of this action and the passed-in action
*/ */
public FsAction or(FsAction that) { public FileSystemAction or(FileSystemAction that) {
if (that != null) { if (that != null) {
return SVALS[ordinal() | that.ordinal()]; return SVALS[ordinal() | that.ordinal()];
} }
Expand All @@ -82,7 +82,7 @@ public FsAction or(FsAction that) {
* NOT operation * NOT operation
* @return the complement of this action * @return the complement of this action
*/ */
public FsAction not() { public FileSystemAction not() {
return SVALS[7 - ordinal()]; return SVALS[7 - ordinal()];
} }
} }
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,71 +22,72 @@
/** /**
* A class for file/directory permissions. * A class for file/directory permissions.
*/ */
public final class FsPermission { public final class FileSystemPermission {
//POSIX permission style //POSIX permission style
private FsAction mUseraction; private FileSystemAction mUseraction;
private FsAction mGroupaction; private FileSystemAction mGroupaction;
private FsAction mOtheraction; private FileSystemAction mOtheraction;


/** /**
* Constructs an instance of {@link FsPermission} with the given {@link FsAction}. * Constructs an instance of {@link FileSystemPermission} with the given {@link FileSystemAction}.
* @param userFsAction user action * @param userFileSystemAction user action
* @param groupFsAction group action * @param groupFileSystemAction group action
* @param otherFsAction other action * @param otherFileSystemAction other action
*/ */
public FsPermission(FsAction userFsAction, FsAction groupFsAction, FsAction otherFsAction) { public FileSystemPermission(FileSystemAction userFileSystemAction, FileSystemAction
set(userFsAction, groupFsAction, otherFsAction); groupFileSystemAction, FileSystemAction otherFileSystemAction) {
set(userFileSystemAction, groupFileSystemAction, otherFileSystemAction);
} }


/** /**
* Constructs an instance of {@link FsPermission} with the given mode. * Constructs an instance of {@link FileSystemPermission} with the given mode.
* @param mode * @param mode
* @see #toShort() * @see #toShort()
*/ */
public FsPermission(short mode) { public FileSystemPermission(short mode) {
fromShort(mode); fromShort(mode);
} }


/** /**
* Copy constructor. * Copy constructor.
* @param otherPermission * @param otherPermission
*/ */
public FsPermission(FsPermission otherPermission) { public FileSystemPermission(FileSystemPermission otherPermission) {
set(otherPermission.mUseraction, otherPermission.mGroupaction, otherPermission.mOtheraction); set(otherPermission.mUseraction, otherPermission.mGroupaction, otherPermission.mOtheraction);
} }


/** /**
* Get user action * Get user action
* @return the user {@link FsAction} * @return the user {@link FileSystemAction}
*/ */
public FsAction getUserAction() { public FileSystemAction getUserAction() {
return mUseraction; return mUseraction;
} }


/** /**
* Get group action * Get group action
* @return the group {@link FsAction} * @return the group {@link FileSystemAction}
*/ */
public FsAction getGroupAction() { public FileSystemAction getGroupAction() {
return mGroupaction; return mGroupaction;
} }


/** /**
* Get other action * Get other action
* @return the other {@link FsAction} * @return the other {@link FileSystemAction}
*/ */
public FsAction getOtherAction() { public FileSystemAction getOtherAction() {
return mOtheraction; return mOtheraction;
} }


private void set(FsAction u, FsAction g, FsAction o) { private void set(FileSystemAction u, FileSystemAction g, FileSystemAction o) {
mUseraction = u; mUseraction = u;
mGroupaction = g; mGroupaction = g;
mOtheraction = o; mOtheraction = o;
} }


public void fromShort(short n) { public void fromShort(short n) {
FsAction[] v = FsAction.values(); FileSystemAction[] v = FileSystemAction.values();
set(v[(n >>> 6) & 7], v[(n >>> 3) & 7], v[n & 7]); set(v[(n >>> 6) & 7], v[(n >>> 3) & 7], v[n & 7]);
} }


Expand All @@ -101,8 +102,8 @@ public short toShort() {


@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (obj instanceof FsPermission) { if (obj instanceof FileSystemPermission) {
FsPermission that = (FsPermission)obj; FileSystemPermission that = (FileSystemPermission)obj;
return mUseraction == that.mUseraction return mUseraction == that.mUseraction
&& mGroupaction == that.mGroupaction && mGroupaction == that.mGroupaction
&& mOtheraction == that.mOtheraction; && mOtheraction == that.mOtheraction;
Expand All @@ -124,11 +125,11 @@ public String toString() {
/** /**
* Applies a umask to this permission and return a new one. * Applies a umask to this permission and return a new one.
* *
* @param umask the umask {@link FsPermission} * @param umask the umask {@link FileSystemPermission}
* @return a new {@link FsPermission} * @return a new {@link FileSystemPermission}
*/ */
public FsPermission applyUMask(FsPermission umask) { public FileSystemPermission applyUMask(FileSystemPermission umask) {
return new FsPermission(mUseraction.and(umask.mUseraction.not()), return new FileSystemPermission(mUseraction.and(umask.mUseraction.not()),
mGroupaction.and(umask.mGroupaction.not()), mGroupaction.and(umask.mGroupaction.not()),
mOtheraction.and(umask.mOtheraction.not())); mOtheraction.and(umask.mOtheraction.not()));
} }
Expand All @@ -137,33 +138,34 @@ public FsPermission applyUMask(FsPermission umask) {
* Applies a umask to this permission and return a new one. * Applies a umask to this permission and return a new one.
* *
* @param conf the configuration to read the umask permission from * @param conf the configuration to read the umask permission from
* @return a new {@link FsPermission} * @return a new {@link FileSystemPermission}
*/ */
public FsPermission applyUMask(TachyonConf conf) { public FileSystemPermission applyUMask(TachyonConf conf) {
return applyUMask(getUMask(conf)); return applyUMask(getUMask(conf));
} }


/** /**
* Get the default permission. * Get the default permission.
* @return the default {@link FsPermission} * @return the default {@link FileSystemPermission}
*/ */
public static FsPermission getDefault() { public static FileSystemPermission getDefault() {
return new FsPermission(Constants.DEFAULT_TFS_FULL_PERMISSION); return new FileSystemPermission(Constants.DEFAULT_TFS_FULL_PERMISSION);
} }


/** /**
* Get the none permission for NOSASL authentication mode * Get the none permission for NOSASL authentication mode
* @return the none {@link FsPermission} * @return the none {@link FileSystemPermission}
*/ */
public static FsPermission getNoneFsPermission() { public static FileSystemPermission getNoneFsPermission() {
return new FsPermission(FsAction.NONE, FsAction.NONE, FsAction.NONE); return new FileSystemPermission(FileSystemAction.NONE, FileSystemAction.NONE,
FileSystemAction.NONE);
} }


/** /**
* Get the file/directory creation umask * Get the file/directory creation umask
* @return the umask {@link FsPermission} * @return the umask {@link FileSystemPermission}
*/ */
public static FsPermission getUMask(TachyonConf conf) { public static FileSystemPermission getUMask(TachyonConf conf) {
int umask = Constants.DEFAULT_TFS_PERMISSIONS_UMASK; int umask = Constants.DEFAULT_TFS_PERMISSIONS_UMASK;
if (conf != null) { if (conf != null) {
String confUmask = conf.get(Constants.SECURITY_AUTHORIZATION_PERMISSIONS_UMASK); String confUmask = conf.get(Constants.SECURITY_AUTHORIZATION_PERMISSIONS_UMASK);
Expand All @@ -180,7 +182,7 @@ public static FsPermission getUMask(TachyonConf conf) {
umask = newUmask; umask = newUmask;
} }
} }
return new FsPermission((short)umask); return new FileSystemPermission((short)umask);
} }


private static boolean tryParseInt(String value) { private static boolean tryParseInt(String value) {
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@
public final class PermissionStatus { public final class PermissionStatus {
private String mUserName; private String mUserName;
private String mGroupName; private String mGroupName;
private FsPermission mPermission; private FileSystemPermission mPermission;


/** /**
* Constructs an instance of {@link PermissionStatus} * Constructs an instance of {@link PermissionStatus}
* @param userName the user name * @param userName the user name
* @param groupName the group name which the user belongs to * @param groupName the group name which the user belongs to
* @param permission the {@link FsPermission} * @param permission the {@link FileSystemPermission}
*/ */
public PermissionStatus(String userName, String groupName, FsPermission permission) { public PermissionStatus(String userName, String groupName, FileSystemPermission permission) {
mUserName = userName; mUserName = userName;
mGroupName = groupName; mGroupName = groupName;
if (permission == null) { if (permission == null) {
Expand All @@ -48,10 +48,10 @@ public PermissionStatus(String userName, String groupName, FsPermission permissi
* Constructs an instance of {@link PermissionStatus}. The permission is represented by short. * Constructs an instance of {@link PermissionStatus}. The permission is represented by short.
* @param userName the user name * @param userName the user name
* @param groupName the group name which the user belongs to * @param groupName the group name which the user belongs to
* @param permission the {@link FsPermission} represented by short value * @param permission the {@link FileSystemPermission} represented by short value
*/ */
public PermissionStatus(String userName, String groupName, short permission) { public PermissionStatus(String userName, String groupName, short permission) {
this(userName, groupName, new FsPermission(permission)); this(userName, groupName, new FileSystemPermission(permission));
} }


/** /**
Expand All @@ -72,28 +72,29 @@ public String getGroupName() {


/** /**
* Return permission * Return permission
* @return the {@link FsPermission} * @return the {@link FileSystemPermission}
*/ */
public FsPermission getPermission() { public FileSystemPermission getPermission() {
return mPermission; return mPermission;
} }


/** /**
* Applies umask. * Applies umask.
* @return a new {@link PermissionStatus} * @return a new {@link PermissionStatus}
* @see FsPermission#applyUMask(FsPermission) * @see FileSystemPermission#applyUMask(FileSystemPermission)
*/ */
public PermissionStatus applyUMask(FsPermission umask) { public PermissionStatus applyUMask(FileSystemPermission umask) {
FsPermission newFsPermission = mPermission.applyUMask(umask); FileSystemPermission newFileSystemPermission = mPermission.applyUMask(umask);
return new PermissionStatus(mUserName, mGroupName, newFsPermission); return new PermissionStatus(mUserName, mGroupName, newFileSystemPermission);
} }


/** /**
* Get the Directory default PermissionStatus. Currently the default dir permission is 0777. * Get the Directory default PermissionStatus. Currently the default dir permission is 0777.
* @return the default {@link PermissionStatus} for directories * @return the default {@link PermissionStatus} for directories
*/ */
public static PermissionStatus getDirDefault() { public static PermissionStatus getDirDefault() {
return new PermissionStatus("", "", new FsPermission(Constants.DEFAULT_TFS_FULL_PERMISSION)); return new PermissionStatus("", "",
new FileSystemPermission(Constants.DEFAULT_TFS_FULL_PERMISSION));
} }


/** /**
Expand All @@ -109,20 +110,20 @@ public static PermissionStatus get(TachyonConf conf, boolean remote) throws IOEx
AuthType authType = conf.getEnum(Constants.SECURITY_AUTHENTICATION_TYPE, AuthType.class); AuthType authType = conf.getEnum(Constants.SECURITY_AUTHENTICATION_TYPE, AuthType.class);
if (authType == AuthType.NOSASL) { if (authType == AuthType.NOSASL) {
// no authentication // no authentication
return new PermissionStatus("", "", FsPermission.getNoneFsPermission()); return new PermissionStatus("", "", FileSystemPermission.getNoneFsPermission());
} }
if (remote) { if (remote) {
// get the username through the authentication mechanism // get the username through the authentication mechanism
return new PermissionStatus(PlainSaslServer.AuthorizedClientUser.get().getName(), return new PermissionStatus(PlainSaslServer.AuthorizedClientUser.get().getName(),
"",//TODO(dong) group permission binding into Inode "",//TODO(dong) group permission binding into Inode
FsPermission.getDefault().applyUMask(conf)); FileSystemPermission.getDefault().applyUMask(conf));
} }


// get the username through the login module // get the username through the login module
String loginUserName = LoginUser.get(conf).getName(); String loginUserName = LoginUser.get(conf).getName();
return new PermissionStatus(loginUserName, return new PermissionStatus(loginUserName,
"",//TODO(dong) group permission binding into Inode "",//TODO(dong) group permission binding into Inode
FsPermission.getDefault().applyUMask(conf)); FileSystemPermission.getDefault().applyUMask(conf));
} }


@Override @Override
Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Licensed to the University of California, Berkeley under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional information regarding
* copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License. You may obtain a
* copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package tachyon.security.authorization;

import org.junit.Assert;
import org.junit.Test;

public class FileSystemActionTest {

@Test
public void impliesTest() throws Exception {
Assert.assertTrue(FileSystemAction.ALL.imply(FileSystemAction.READ));
Assert.assertTrue(FileSystemAction.ALL.imply(FileSystemAction.WRITE));
Assert.assertTrue(FileSystemAction.ALL.imply(FileSystemAction.EXECUTE));
Assert.assertTrue(FileSystemAction.ALL.imply(FileSystemAction.READ_EXECUTE));
Assert.assertTrue(FileSystemAction.ALL.imply(FileSystemAction.WRITE_EXECUTE));
Assert.assertTrue(FileSystemAction.ALL.imply(FileSystemAction.ALL));

Assert.assertTrue(FileSystemAction.READ_EXECUTE.imply(FileSystemAction.READ));
Assert.assertTrue(FileSystemAction.READ_EXECUTE.imply(FileSystemAction.EXECUTE));
Assert.assertFalse(FileSystemAction.READ_EXECUTE.imply(FileSystemAction.WRITE));

Assert.assertTrue(FileSystemAction.WRITE_EXECUTE.imply(FileSystemAction.WRITE));
Assert.assertTrue(FileSystemAction.WRITE_EXECUTE.imply(FileSystemAction.EXECUTE));
Assert.assertFalse(FileSystemAction.WRITE_EXECUTE.imply(FileSystemAction.READ));

Assert.assertTrue(FileSystemAction.READ_WRITE.imply(FileSystemAction.WRITE));
Assert.assertTrue(FileSystemAction.READ_WRITE.imply(FileSystemAction.READ));
Assert.assertFalse(FileSystemAction.READ_WRITE.imply(FileSystemAction.EXECUTE));
}

@Test
public void notOperationTest() throws Exception {
Assert.assertEquals(FileSystemAction.WRITE, FileSystemAction.READ_EXECUTE.not());
Assert.assertEquals(FileSystemAction.READ, FileSystemAction.WRITE_EXECUTE.not());
Assert.assertEquals(FileSystemAction.EXECUTE, FileSystemAction.READ_WRITE.not());
}

@Test
public void orOperationTest() throws Exception {
Assert.assertEquals(FileSystemAction.WRITE_EXECUTE,
FileSystemAction.WRITE.or(FileSystemAction.EXECUTE));
Assert.assertEquals(FileSystemAction.READ_EXECUTE,
FileSystemAction.READ.or(FileSystemAction.EXECUTE));
Assert.assertEquals(FileSystemAction.READ_WRITE,
FileSystemAction.WRITE.or(FileSystemAction.READ));
}

@Test
public void andOperationTest() throws Exception {
Assert.assertEquals(FileSystemAction.NONE, FileSystemAction.READ.and(FileSystemAction.WRITE));
Assert.assertEquals(FileSystemAction.READ,
FileSystemAction.READ_EXECUTE.and(FileSystemAction.READ));
Assert.assertEquals(FileSystemAction.WRITE,
FileSystemAction.READ_WRITE.and(FileSystemAction.WRITE));
}
}
Loading

0 comments on commit b9ae1a5

Please sign in to comment.