[#1] Add additional security

Signed-off-by: Ori Bruk <o.bruk@yadro.com>
This commit is contained in:
Ori Bruk 2024-07-24 14:41:42 +03:00
parent bf2f19f08d
commit 1be65c63ae
62 changed files with 670 additions and 281 deletions

View file

@ -1,18 +1,28 @@
package info.frostfs.sdk.dto;
import static info.frostfs.sdk.constants.AppConst.DEFAULT_MAJOR_VERSION;
import static info.frostfs.sdk.constants.AppConst.DEFAULT_MINOR_VERSION;
import static java.util.Objects.isNull;
public class MetaHeader {
private Version version;
private int epoch;
private int ttl;
public MetaHeader(Version version, int epoch, int ttl) {
if (isNull(version) || epoch < 0 || ttl <= 0) {
throw new IllegalArgumentException("One of the input attributes is invalid or missing");
}
this.version = version;
this.epoch = epoch;
this.ttl = ttl;
}
public static MetaHeader getDefault() {
return new MetaHeader(new Version(2, 13), 0, 2);
public MetaHeader() {
this.version = new Version(DEFAULT_MAJOR_VERSION, DEFAULT_MINOR_VERSION);
this.epoch = 0;
this.ttl = 2;
}
public Version getVersion() {
@ -20,6 +30,10 @@ public class MetaHeader {
}
public void setVersion(Version version) {
if (isNull(version)) {
throw new IllegalArgumentException("Version is missing.");
}
this.version = version;
}
@ -28,6 +42,10 @@ public class MetaHeader {
}
public void setEpoch(int epoch) {
if (epoch < 0) {
throw new IllegalArgumentException("The epoch must be greater than or equal to zero.");
}
this.epoch = epoch;
}
@ -36,6 +54,10 @@ public class MetaHeader {
}
public void setTtl(int ttl) {
if (ttl <= 0) {
throw new IllegalArgumentException("The ttl must be greater than zero.");
}
this.ttl = ttl;
}
}

View file

@ -3,6 +3,7 @@ package info.frostfs.sdk.dto;
import info.frostfs.sdk.Base58;
import static info.frostfs.sdk.KeyExtension.publicKeyToAddress;
import static java.util.Objects.isNull;
public class OwnerId {
private final String value;
@ -11,8 +12,12 @@ public class OwnerId {
this.value = value;
}
public static OwnerId fromKey(byte[] publicKey) {
return new OwnerId(publicKeyToAddress(publicKey));
public OwnerId(byte[] publicKey) {
if (isNull(publicKey) || publicKey.length == 0) {
throw new IllegalArgumentException("PublicKey is invalid");
}
this.value = publicKeyToAddress(publicKey);
}
public String getValue() {

View file

@ -6,9 +6,11 @@ import info.frostfs.sdk.dto.object.ObjectId;
import java.util.ArrayList;
import java.util.List;
import static java.util.Objects.isNull;
public class Split {
private final List<ObjectId> children;
private SplitId splitId;
private final SplitId splitId;
private ObjectId parent;
private ObjectId previous;
private Signature parentSignature;
@ -19,6 +21,10 @@ public class Split {
}
public Split(SplitId splitId) {
if (isNull(splitId)) {
throw new IllegalArgumentException("SplitId is not present");
}
this.splitId = splitId;
this.children = new ArrayList<>();
}
@ -27,10 +33,6 @@ public class Split {
return splitId;
}
private void setSplitId(SplitId splitId) {
this.splitId = splitId;
}
public ObjectId getParent() {
return parent;
}

View file

@ -9,30 +9,22 @@ import static java.util.Objects.isNull;
public class SplitId {
private final UUID id;
public SplitId(UUID uuid) {
this.id = uuid;
}
public SplitId() {
this.id = UUID.randomUUID();
}
private SplitId(byte[] binary) {
public SplitId(UUID uuid) {
this.id = uuid;
}
public SplitId(byte[] binary) {
this.id = asUuid(binary);
}
private SplitId(String str) {
public SplitId(String str) {
this.id = UUID.fromString(str);
}
public static SplitId createFromBinary(byte[] binaryData) {
return new SplitId(binaryData);
}
public static SplitId createFromString(String stringData) {
return new SplitId(stringData);
}
@Override
public String toString() {
return id.toString();

View file

@ -2,6 +2,7 @@ package info.frostfs.sdk.dto;
import info.frostfs.sdk.enums.StatusCode;
import static info.frostfs.sdk.constants.FieldConst.EMPTY_STRING;
import static java.util.Objects.isNull;
public class Status {
@ -10,12 +11,12 @@ public class Status {
public Status(StatusCode code, String message) {
this.code = code;
this.message = isNull(message) ? "" : message;
this.message = isNull(message) ? EMPTY_STRING : message;
}
public Status(StatusCode code) {
this.code = code;
this.message = "";
this.message = EMPTY_STRING;
}
public StatusCode getCode() {

View file

@ -1,36 +1,41 @@
package info.frostfs.sdk.dto;
import static info.frostfs.sdk.constants.AppConst.DEFAULT_MAJOR_VERSION;
import static info.frostfs.sdk.constants.AppConst.DEFAULT_MINOR_VERSION;
import static java.util.Objects.isNull;
public class Version {
private int major;
private int minor;
private final int major;
private final int minor;
public Version(int major, int minor) {
this.major = major;
this.minor = minor;
}
public Version() {
this.major = DEFAULT_MAJOR_VERSION;
this.minor = DEFAULT_MINOR_VERSION;
}
public int getMajor() {
return major;
}
public void setMajor(int major) {
this.major = major;
}
public int getMinor() {
return minor;
}
public void setMinor(int minor) {
this.minor = minor;
}
@Override
public String toString() {
return "v" + major + "." + minor;
}
public boolean isSupported(Version version) {
if (isNull(version)) {
return false;
}
return major == version.getMajor();
}
}

View file

@ -2,29 +2,33 @@ package info.frostfs.sdk.dto.container;
import info.frostfs.sdk.Base58;
import info.frostfs.sdk.constants.AppConst;
import org.apache.commons.lang3.StringUtils;
import static java.util.Objects.isNull;
public class ContainerId {
private String value;
private final String value;
public ContainerId(String value) {
if (StringUtils.isEmpty(value)) {
throw new IllegalArgumentException("ContainerId value is missing");
}
this.value = value;
}
public static ContainerId fromHash(byte[] hash) {
if (hash.length != AppConst.SHA256_HASH_LENGTH) {
throw new IllegalArgumentException("ContainerID must be a sha256 hash.");
public ContainerId(byte[] hash) {
if (isNull(hash) || hash.length != AppConst.SHA256_HASH_LENGTH) {
throw new IllegalArgumentException("ContainerId must be a sha256 hash.");
}
return new ContainerId(Base58.encode(hash));
this.value = Base58.encode(hash);
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return value;

View file

@ -1,34 +1,36 @@
package info.frostfs.sdk.dto.netmap;
import static java.util.Objects.isNull;
import org.apache.commons.lang3.StringUtils;
import static info.frostfs.sdk.constants.FieldConst.EMPTY_STRING;
public class Replica {
private int count;
private String selector;
private final int count;
private final String selector;
public Replica(int count, String selector) {
if (count <= 0) {
throw new IllegalArgumentException("Replica count must be positive");
}
this.count = count;
this.selector = isNull(selector) ? "" : selector;
this.selector = StringUtils.isEmpty(selector) ? EMPTY_STRING : selector;
}
public Replica(int count) {
if (count <= 0) {
throw new IllegalArgumentException("Replica count must be positive");
}
this.count = count;
this.selector = "";
this.selector = EMPTY_STRING;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getSelector() {
return selector;
}
public void setSelector(String selector) {
this.selector = selector;
}
}

View file

@ -5,6 +5,7 @@ import info.frostfs.sdk.dto.container.ContainerId;
import java.security.MessageDigest;
import static info.frostfs.sdk.Helper.getSha256Instance;
import static java.util.Objects.isNull;
public class LargeObject extends ObjectFrostFS {
private final MessageDigest payloadHash;
@ -15,6 +16,10 @@ public class LargeObject extends ObjectFrostFS {
}
public void appendBlock(byte[] bytes, int count) {
if (count == 0 || isNull(bytes) || bytes.length == 0) {
return;
}
this.getHeader().increasePayloadLength(count);
this.payloadHash.update(bytes, 0, count);
}

View file

@ -3,6 +3,7 @@ package info.frostfs.sdk.dto.object;
import info.frostfs.sdk.dto.Split;
import info.frostfs.sdk.dto.SplitId;
import info.frostfs.sdk.dto.container.ContainerId;
import org.apache.commons.collections4.CollectionUtils;
import java.util.List;
@ -16,6 +17,10 @@ public class LinkObject extends ObjectFrostFS {
}
public void addChildren(List<ObjectId> objectIds) {
if (CollectionUtils.isEmpty(objectIds)) {
return;
}
this.getHeader().getSplit().getChildren().addAll(objectIds);
}
}

View file

@ -1,10 +1,16 @@
package info.frostfs.sdk.dto.object;
import org.apache.commons.lang3.StringUtils;
public class ObjectAttribute {
private String key;
private String value;
private final String key;
private final String value;
public ObjectAttribute(String key, String value) {
if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) {
throw new IllegalArgumentException("One of the input attributes is missing");
}
this.key = key;
this.value = value;
}
@ -13,15 +19,7 @@ public class ObjectAttribute {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

View file

@ -10,34 +10,34 @@ import java.util.List;
import static java.util.Objects.isNull;
public class ObjectFrostFS {
private ObjectHeader header;
private final ObjectHeader header;
private ObjectId objectId;
private byte[] payload;
public ObjectFrostFS(ObjectHeader header, ObjectId objectId, byte[] payload) {
if (isNull(header)) {
throw new IllegalArgumentException("Object header is missing");
}
this.header = header;
this.objectId = objectId;
this.payload = payload;
}
public ObjectFrostFS(ContainerId container, byte[] payload) {
public ObjectFrostFS(ContainerId containerId, byte[] payload) {
this.payload = payload;
this.header = new ObjectHeader(container, new ArrayList<>());
this.header = new ObjectHeader(containerId, new ArrayList<>());
}
public ObjectFrostFS(ContainerId container, byte[] payload, ObjectType objectType) {
public ObjectFrostFS(ContainerId containerId, byte[] payload, ObjectType objectType) {
this.payload = payload;
this.header = new ObjectHeader(container, objectType, new ArrayList<>());
this.header = new ObjectHeader(containerId, objectType, new ArrayList<>());
}
public ObjectHeader getHeader() {
return header;
}
public void setHeader(ObjectHeader header) {
this.header = header;
}
public ObjectId getObjectId() {
return objectId;
}

View file

@ -8,11 +8,13 @@ import info.frostfs.sdk.enums.ObjectType;
import java.util.List;
import static java.util.Objects.isNull;
public class ObjectHeader {
private final ContainerId containerId;
private final ObjectType objectType;
private List<ObjectAttribute> attributes;
private ContainerId containerId;
private long size;
private ObjectType objectType;
private Version version;
private OwnerId ownerId;
private long payloadLength;
@ -21,6 +23,10 @@ public class ObjectHeader {
public ObjectHeader(ContainerId containerId, ObjectType objectType,
List<ObjectAttribute> attributes, long size, Version version) {
if (isNull(containerId) || isNull(objectType)) {
throw new IllegalArgumentException("ContainerId or objectType is not present");
}
this.attributes = attributes;
this.containerId = containerId;
this.size = size;
@ -28,13 +34,21 @@ public class ObjectHeader {
this.version = version;
}
public ObjectHeader(ContainerId containerId, ObjectType type, List<ObjectAttribute> attributes) {
public ObjectHeader(ContainerId containerId, ObjectType objectType, List<ObjectAttribute> attributes) {
if (isNull(containerId) || isNull(objectType)) {
throw new IllegalArgumentException("ContainerId or objectType is not present");
}
this.attributes = attributes;
this.containerId = containerId;
this.objectType = type;
this.objectType = objectType;
}
public ObjectHeader(ContainerId containerId, List<ObjectAttribute> attributes) {
if (isNull(containerId)) {
throw new IllegalArgumentException("ContainerId is not present");
}
this.attributes = attributes;
this.containerId = containerId;
this.objectType = ObjectType.REGULAR;
@ -73,6 +87,10 @@ public class ObjectHeader {
}
public void setSplit(Split split) {
if (isNull(split)) {
throw new IllegalArgumentException("Split is not present");
}
this.split = split;
}
@ -88,10 +106,6 @@ public class ObjectHeader {
return containerId;
}
public void setContainerId(ContainerId containerId) {
this.containerId = containerId;
}
public long getSize() {
return size;
}
@ -104,10 +118,6 @@ public class ObjectHeader {
return objectType;
}
public void setObjectType(ObjectType objectType) {
this.objectType = objectType;
}
public Version getVersion() {
return version;
}

View file

@ -2,19 +2,27 @@ package info.frostfs.sdk.dto.object;
import info.frostfs.sdk.Base58;
import info.frostfs.sdk.constants.AppConst;
import org.apache.commons.lang3.StringUtils;
import static java.util.Objects.isNull;
public class ObjectId {
private final String value;
public ObjectId(String id) {
this.value = id;
public ObjectId(String value) {
if (StringUtils.isEmpty(value)) {
throw new IllegalArgumentException("ObjectId value is missing");
}
this.value = value;
}
public static ObjectId fromHash(byte[] hash) {
if (hash.length != AppConst.SHA256_HASH_LENGTH) {
public ObjectId(byte[] hash) {
if (isNull(hash) || hash.length != AppConst.SHA256_HASH_LENGTH) {
throw new IllegalArgumentException("ObjectId must be a sha256 hash.");
}
return new ObjectId(Base58.encode(hash));
this.value = Base58.encode(hash);
}
public String getValue() {