|
@@ -21,6 +21,7 @@ package org.apache.hadoop.hdfs.security.token.block;
|
|
import java.io.DataInput;
|
|
import java.io.DataInput;
|
|
import java.io.DataOutput;
|
|
import java.io.DataOutput;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
|
+import java.util.Arrays;
|
|
import java.util.EnumSet;
|
|
import java.util.EnumSet;
|
|
|
|
|
|
import org.apache.hadoop.hdfs.security.token.block.BlockTokenSecretManager.AccessMode;
|
|
import org.apache.hadoop.hdfs.security.token.block.BlockTokenSecretManager.AccessMode;
|
|
@@ -35,20 +36,23 @@ public class BlockTokenIdentifier extends TokenIdentifier {
|
|
private long expiryDate;
|
|
private long expiryDate;
|
|
private int keyId;
|
|
private int keyId;
|
|
private String userId;
|
|
private String userId;
|
|
- private long blockId;
|
|
|
|
|
|
+ private long [] blockIds;
|
|
private EnumSet<AccessMode> modes;
|
|
private EnumSet<AccessMode> modes;
|
|
|
|
|
|
private byte [] cache;
|
|
private byte [] cache;
|
|
|
|
|
|
public BlockTokenIdentifier() {
|
|
public BlockTokenIdentifier() {
|
|
- this(null, 0l, EnumSet.noneOf(AccessMode.class));
|
|
|
|
|
|
+ this(null, new long [] {}, EnumSet.noneOf(AccessMode.class));
|
|
}
|
|
}
|
|
|
|
|
|
- public BlockTokenIdentifier(String userId, long blockId,
|
|
|
|
|
|
+ public BlockTokenIdentifier(String userId, long [] blockIds,
|
|
EnumSet<AccessMode> modes) {
|
|
EnumSet<AccessMode> modes) {
|
|
|
|
+ if(blockIds == null)
|
|
|
|
+ throw new IllegalArgumentException("blockIds can't be null");
|
|
this.cache = null;
|
|
this.cache = null;
|
|
this.userId = userId;
|
|
this.userId = userId;
|
|
- this.blockId = blockId;
|
|
|
|
|
|
+ this.blockIds = Arrays.copyOf(blockIds, blockIds.length);
|
|
|
|
+ Arrays.sort(this.blockIds);
|
|
this.modes = modes == null ? EnumSet.noneOf(AccessMode.class) : modes;
|
|
this.modes = modes == null ? EnumSet.noneOf(AccessMode.class) : modes;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -60,7 +64,7 @@ public class BlockTokenIdentifier extends TokenIdentifier {
|
|
@Override
|
|
@Override
|
|
public UserGroupInformation getUser() {
|
|
public UserGroupInformation getUser() {
|
|
if (userId == null || "".equals(userId)) {
|
|
if (userId == null || "".equals(userId)) {
|
|
- return UserGroupInformation.createRemoteUser(Long.toString(blockId));
|
|
|
|
|
|
+ return UserGroupInformation.createRemoteUser(Arrays.toString(blockIds));
|
|
}
|
|
}
|
|
return UserGroupInformation.createRemoteUser(userId);
|
|
return UserGroupInformation.createRemoteUser(userId);
|
|
}
|
|
}
|
|
@@ -87,8 +91,25 @@ public class BlockTokenIdentifier extends TokenIdentifier {
|
|
return userId;
|
|
return userId;
|
|
}
|
|
}
|
|
|
|
|
|
- public long getBlockId() {
|
|
|
|
- return blockId;
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Return sorted array of blockIds this {@link BlockTokenIdentifier} includes
|
|
|
|
+ */
|
|
|
|
+ public long [] getBlockIds() {
|
|
|
|
+ return blockIds;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Is specified blockId included in this BlockTokenIdentifier?
|
|
|
|
+ */
|
|
|
|
+ public boolean isBlockIncluded(long blockId) {
|
|
|
|
+ switch(blockIds.length) {
|
|
|
|
+ case 1:
|
|
|
|
+ return blockIds[0] == blockId;
|
|
|
|
+ case 2:
|
|
|
|
+ return (blockIds[0] == blockId) || (blockIds[1] == blockId);
|
|
|
|
+ default:
|
|
|
|
+ return Arrays.binarySearch(blockIds, blockId) >= 0;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
public EnumSet<AccessMode> getAccessModes() {
|
|
public EnumSet<AccessMode> getAccessModes() {
|
|
@@ -99,7 +120,7 @@ public class BlockTokenIdentifier extends TokenIdentifier {
|
|
public String toString() {
|
|
public String toString() {
|
|
return "block_token_identifier (expiryDate=" + this.getExpiryDate()
|
|
return "block_token_identifier (expiryDate=" + this.getExpiryDate()
|
|
+ ", keyId=" + this.getKeyId() + ", userId=" + this.getUserId()
|
|
+ ", keyId=" + this.getKeyId() + ", userId=" + this.getUserId()
|
|
- + ", blockIds=" + blockId + ", access modes="
|
|
|
|
|
|
+ + ", blockIds=" + Arrays.toString(blockIds) + ", access modes="
|
|
+ this.getAccessModes() + ")";
|
|
+ this.getAccessModes() + ")";
|
|
}
|
|
}
|
|
|
|
|
|
@@ -116,7 +137,7 @@ public class BlockTokenIdentifier extends TokenIdentifier {
|
|
BlockTokenIdentifier that = (BlockTokenIdentifier) obj;
|
|
BlockTokenIdentifier that = (BlockTokenIdentifier) obj;
|
|
return this.expiryDate == that.expiryDate && this.keyId == that.keyId
|
|
return this.expiryDate == that.expiryDate && this.keyId == that.keyId
|
|
&& isEqual(this.userId, that.userId)
|
|
&& isEqual(this.userId, that.userId)
|
|
- && this.blockId == that.blockId
|
|
|
|
|
|
+ && Arrays.equals(this.blockIds, that.blockIds)
|
|
&& isEqual(this.modes, that.modes);
|
|
&& isEqual(this.modes, that.modes);
|
|
}
|
|
}
|
|
return false;
|
|
return false;
|
|
@@ -124,7 +145,7 @@ public class BlockTokenIdentifier extends TokenIdentifier {
|
|
|
|
|
|
/** {@inheritDoc} */
|
|
/** {@inheritDoc} */
|
|
public int hashCode() {
|
|
public int hashCode() {
|
|
- return (int) expiryDate ^ keyId ^ (int)blockId ^ modes.hashCode()
|
|
|
|
|
|
+ return (int) expiryDate ^ keyId ^ Arrays.hashCode(blockIds) ^ modes.hashCode()
|
|
^ (userId == null ? 0 : userId.hashCode());
|
|
^ (userId == null ? 0 : userId.hashCode());
|
|
}
|
|
}
|
|
|
|
|
|
@@ -133,7 +154,9 @@ public class BlockTokenIdentifier extends TokenIdentifier {
|
|
expiryDate = WritableUtils.readVLong(in);
|
|
expiryDate = WritableUtils.readVLong(in);
|
|
keyId = WritableUtils.readVInt(in);
|
|
keyId = WritableUtils.readVInt(in);
|
|
userId = WritableUtils.readString(in);
|
|
userId = WritableUtils.readString(in);
|
|
- blockId = WritableUtils.readVLong(in);
|
|
|
|
|
|
+ blockIds = new long[WritableUtils.readVInt(in)];
|
|
|
|
+ for(int i = 0; i < blockIds.length; i++)
|
|
|
|
+ blockIds[i] = WritableUtils.readVLong(in);
|
|
int length = WritableUtils.readVInt(in);
|
|
int length = WritableUtils.readVInt(in);
|
|
for (int i = 0; i < length; i++) {
|
|
for (int i = 0; i < length; i++) {
|
|
modes.add(WritableUtils.readEnum(in, AccessMode.class));
|
|
modes.add(WritableUtils.readEnum(in, AccessMode.class));
|
|
@@ -144,7 +167,9 @@ public class BlockTokenIdentifier extends TokenIdentifier {
|
|
WritableUtils.writeVLong(out, expiryDate);
|
|
WritableUtils.writeVLong(out, expiryDate);
|
|
WritableUtils.writeVInt(out, keyId);
|
|
WritableUtils.writeVInt(out, keyId);
|
|
WritableUtils.writeString(out, userId);
|
|
WritableUtils.writeString(out, userId);
|
|
- WritableUtils.writeVLong(out, blockId);
|
|
|
|
|
|
+ WritableUtils.writeVInt(out, blockIds.length);
|
|
|
|
+ for(int i = 0; i < blockIds.length; i++)
|
|
|
|
+ WritableUtils.writeVLong(out, blockIds[i]);
|
|
WritableUtils.writeVInt(out, modes.size());
|
|
WritableUtils.writeVInt(out, modes.size());
|
|
for (AccessMode aMode : modes) {
|
|
for (AccessMode aMode : modes) {
|
|
WritableUtils.writeEnum(out, aMode);
|
|
WritableUtils.writeEnum(out, aMode);
|