[#47] Add APE rule deserializer
Signed-off-by: Ori Bruk <o.bruk@yadro.com>
This commit is contained in:
parent
db74919401
commit
39158348dd
21 changed files with 504 additions and 79 deletions
13
CHANGELOG.md
13
CHANGELOG.md
|
@ -1,26 +1,37 @@
|
|||
# Changelog
|
||||
|
||||
## [0.9.0] - 2025-03-05
|
||||
|
||||
### Added
|
||||
|
||||
- APE rule deserializer
|
||||
|
||||
## [0.8.0] - 2025-03-04
|
||||
|
||||
### Added
|
||||
|
||||
- Creating client via wallet and password
|
||||
|
||||
## [0.7.0] - 2025-02-20
|
||||
|
||||
### Added
|
||||
|
||||
- Expanding the parameters for creating a container
|
||||
|
||||
### Fixed
|
||||
- Creating a session for working with objects
|
||||
|
||||
- Creating a session for working with objects
|
||||
|
||||
## [0.6.0] - 2025-02-13
|
||||
|
||||
### Added
|
||||
|
||||
- APE rules serializer
|
||||
|
||||
## [0.5.0] - 2025-02-11
|
||||
|
||||
### Fixed
|
||||
|
||||
- Loading large objects in chunks
|
||||
- .gitignore
|
||||
- pom revision
|
|
@ -1,6 +1,7 @@
|
|||
package info.frostfs.sdk;
|
||||
|
||||
import frostfs.accounting.Types;
|
||||
import info.frostfs.sdk.dto.ape.Chain;
|
||||
import info.frostfs.sdk.dto.container.Container;
|
||||
import info.frostfs.sdk.dto.container.ContainerId;
|
||||
import info.frostfs.sdk.dto.netmap.NetmapSnapshot;
|
||||
|
@ -197,7 +198,7 @@ public class FrostFSClient implements CommonClient {
|
|||
}
|
||||
|
||||
@Override
|
||||
public List<frostfs.ape.Types.Chain> listChains(PrmApeChainList args, CallContext ctx) {
|
||||
public List<Chain> listChains(PrmApeChainList args, CallContext ctx) {
|
||||
return apeManagerClient.listChains(args, ctx);
|
||||
}
|
||||
|
||||
|
|
|
@ -17,10 +17,13 @@ public class RuleConst {
|
|||
// https://github.com/neo-project/neo/blob/38218bbee5bbe8b33cd8f9453465a19381c9a547/src/Neo/IO/Helper.cs#L77
|
||||
public static final int MAX_SLICE_LENGTH = 0x1000000;
|
||||
|
||||
public static final int MAX_VAR_INT_LENGTH = 10;
|
||||
|
||||
public static final int CHAIN_MARSHAL_VERSION = 0;
|
||||
|
||||
public static final long OFFSET127 = 0x7f;
|
||||
public static final long OFFSET128 = 0x80;
|
||||
public static final long UNSIGNED_SERIALIZE_SIZE = 7;
|
||||
public static final int UNSIGNED_SERIALIZE_SIZE = 7;
|
||||
|
||||
private RuleConst() {
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package info.frostfs.sdk.pool;
|
||||
|
||||
import frostfs.refs.Types;
|
||||
import info.frostfs.sdk.dto.ape.Chain;
|
||||
import info.frostfs.sdk.dto.container.Container;
|
||||
import info.frostfs.sdk.dto.container.ContainerId;
|
||||
import info.frostfs.sdk.dto.netmap.NetmapSnapshot;
|
||||
|
@ -514,7 +515,7 @@ public class Pool implements CommonClient {
|
|||
}
|
||||
|
||||
@Override
|
||||
public List<frostfs.ape.Types.Chain> listChains(PrmApeChainList args, CallContext ctx) {
|
||||
public List<Chain> listChains(PrmApeChainList args, CallContext ctx) {
|
||||
ClientWrapper client = connection();
|
||||
return client.getClient().listChains(args, ctx);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package info.frostfs.sdk.services;
|
||||
|
||||
import frostfs.ape.Types;
|
||||
import info.frostfs.sdk.dto.ape.Chain;
|
||||
import info.frostfs.sdk.jdo.parameters.CallContext;
|
||||
import info.frostfs.sdk.jdo.parameters.ape.PrmApeChainAdd;
|
||||
import info.frostfs.sdk.jdo.parameters.ape.PrmApeChainList;
|
||||
|
@ -13,5 +13,5 @@ public interface ApeManagerClient {
|
|||
|
||||
void removeChain(PrmApeChainRemove args, CallContext ctx);
|
||||
|
||||
List<Types.Chain> listChains(PrmApeChainList args, CallContext ctx);
|
||||
List<Chain> listChains(PrmApeChainList args, CallContext ctx);
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import com.google.protobuf.ByteString;
|
|||
import frostfs.ape.Types;
|
||||
import frostfs.apemanager.APEManagerServiceGrpc;
|
||||
import frostfs.apemanager.Service;
|
||||
import info.frostfs.sdk.dto.ape.Chain;
|
||||
import info.frostfs.sdk.jdo.ClientEnvironment;
|
||||
import info.frostfs.sdk.jdo.parameters.CallContext;
|
||||
import info.frostfs.sdk.jdo.parameters.ape.PrmApeChainAdd;
|
||||
|
@ -14,10 +15,12 @@ import info.frostfs.sdk.services.ApeManagerClient;
|
|||
import info.frostfs.sdk.services.ContextAccessor;
|
||||
import info.frostfs.sdk.tools.RequestConstructor;
|
||||
import info.frostfs.sdk.tools.RequestSigner;
|
||||
import info.frostfs.sdk.tools.RuleSerializer;
|
||||
import info.frostfs.sdk.tools.Verifier;
|
||||
import info.frostfs.sdk.tools.ape.RuleDeserializer;
|
||||
import info.frostfs.sdk.tools.ape.RuleSerializer;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static info.frostfs.sdk.utils.DeadLineUtil.deadLineAfter;
|
||||
import static info.frostfs.sdk.utils.Validator.validate;
|
||||
|
@ -57,7 +60,7 @@ public class ApeManagerClientImpl extends ContextAccessor implements ApeManagerC
|
|||
}
|
||||
|
||||
@Override
|
||||
public List<Types.Chain> listChains(PrmApeChainList args, CallContext ctx) {
|
||||
public List<Chain> listChains(PrmApeChainList args, CallContext ctx) {
|
||||
validate(args);
|
||||
|
||||
var request = createListChainsRequest(args);
|
||||
|
@ -67,7 +70,9 @@ public class ApeManagerClientImpl extends ContextAccessor implements ApeManagerC
|
|||
|
||||
Verifier.checkResponse(response);
|
||||
|
||||
return response.getBody().getChainsList();
|
||||
return response.getBody().getChainsList().stream()
|
||||
.map(chain -> RuleDeserializer.deserialize(chain.getRaw().toByteArray()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private Service.AddChainRequest createAddChainRequest(PrmApeChainAdd args) {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package info.frostfs.sdk.tools;
|
||||
package info.frostfs.sdk.tools.ape;
|
||||
|
||||
public interface MarshalFunction<T> {
|
||||
int marshal(byte[] buf, int offset, T t);
|
|
@ -0,0 +1,198 @@
|
|||
package info.frostfs.sdk.tools.ape;
|
||||
|
||||
import info.frostfs.sdk.dto.ape.*;
|
||||
import info.frostfs.sdk.enums.ConditionKindType;
|
||||
import info.frostfs.sdk.enums.ConditionType;
|
||||
import info.frostfs.sdk.enums.RuleMatchType;
|
||||
import info.frostfs.sdk.enums.RuleStatus;
|
||||
import info.frostfs.sdk.exceptions.FrostFSException;
|
||||
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static info.frostfs.sdk.constants.ErrorConst.*;
|
||||
import static info.frostfs.sdk.constants.FieldConst.EMPTY_STRING;
|
||||
import static info.frostfs.sdk.constants.RuleConst.*;
|
||||
|
||||
public class RuleDeserializer {
|
||||
private RuleDeserializer() {
|
||||
}
|
||||
|
||||
public static Chain deserialize(byte[] data) {
|
||||
if (ArrayUtils.isEmpty(data)) {
|
||||
throw new ValidationFrostFSException(INPUT_PARAM_IS_MISSING);
|
||||
}
|
||||
|
||||
AtomicInteger offset = new AtomicInteger(0);
|
||||
Chain chain = new Chain();
|
||||
|
||||
var version = uInt8Unmarshal(data, offset);
|
||||
if (version != VERSION) {
|
||||
throw new FrostFSException(String.format(UNSUPPORTED_MARSHALLER_VERSION_TEMPLATE, version));
|
||||
}
|
||||
|
||||
var chainVersion = uInt8Unmarshal(data, offset);
|
||||
if (chainVersion != CHAIN_MARSHAL_VERSION) {
|
||||
throw new FrostFSException(String.format(UNSUPPORTED_CHAIN_VERSION_TEMPLATE, chainVersion));
|
||||
}
|
||||
|
||||
chain.setId(sliceUnmarshal(data, offset, Byte.class, RuleDeserializer::uInt8Unmarshal));
|
||||
chain.setRules(sliceUnmarshal(data, offset, Rule.class, RuleDeserializer::unmarshalRule));
|
||||
chain.setMatchType(RuleMatchType.get(uInt8Unmarshal(data, offset)));
|
||||
|
||||
verifyUnmarshal(data, offset);
|
||||
|
||||
return chain;
|
||||
}
|
||||
|
||||
private static Byte uInt8Unmarshal(byte[] buf, AtomicInteger offset) {
|
||||
if (buf.length - offset.get() < 1) {
|
||||
throw new FrostFSException(
|
||||
String.format(BYTES_ARE_OVER_FOR_DESERIALIZE_TEMPLATE, Byte.class.getName(), offset.get()));
|
||||
}
|
||||
|
||||
return buf[offset.getAndAdd(1)];
|
||||
}
|
||||
|
||||
public static long varInt(byte[] buf, AtomicInteger offset) {
|
||||
long ux = uVarInt(buf, offset); // ok to continue in presence of error
|
||||
long x = ux >> 1;
|
||||
if ((ux & 1) != 0) {
|
||||
x = ~x;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
|
||||
public static long uVarInt(byte[] buf, AtomicInteger offset) {
|
||||
long x = 0;
|
||||
int s = 0;
|
||||
|
||||
for (int i = offset.get(); i < buf.length; i++) {
|
||||
long b = buf[i];
|
||||
if (i == MAX_VAR_INT_LENGTH) {
|
||||
offset.set(-(i + 1));
|
||||
return 0; // overflow
|
||||
}
|
||||
if (b >= 0) {
|
||||
if (i == MAX_VAR_INT_LENGTH - 1 && b > 1) {
|
||||
offset.set(-(i + 1));
|
||||
return 0; // overflow
|
||||
}
|
||||
offset.set(i + 1);
|
||||
return x | (b << s);
|
||||
}
|
||||
x |= (b & OFFSET127) << s;
|
||||
s += UNSIGNED_SERIALIZE_SIZE;
|
||||
}
|
||||
offset.set(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static <T> T[] sliceUnmarshal(byte[] buf,
|
||||
AtomicInteger offset,
|
||||
Class<T> clazz,
|
||||
UnmarshalFunction<T> unmarshalT) {
|
||||
var size = (int) varInt(buf, offset);
|
||||
if (size == NULL_SLICE) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (size > MAX_SLICE_LENGTH) {
|
||||
throw new ValidationFrostFSException(String.format(SLICE_IS_TOO_BIG_TEMPLATE, size));
|
||||
}
|
||||
|
||||
if (size < 0) {
|
||||
throw new ValidationFrostFSException(String.format(SLICE_SIZE_IS_INVALID_TEMPLATE, size));
|
||||
}
|
||||
|
||||
T[] result = (T[]) Array.newInstance(clazz, size);
|
||||
for (int i = 0; i < result.length; i++) {
|
||||
result[i] = unmarshalT.unmarshal(buf, offset);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static boolean boolUnmarshal(byte[] buf, AtomicInteger offset) {
|
||||
return uInt8Unmarshal(buf, offset) == BYTE_TRUE;
|
||||
}
|
||||
|
||||
private static long int64Unmarshal(byte[] buf, AtomicInteger offset) {
|
||||
if (buf.length - offset.get() < Long.BYTES) {
|
||||
throw new ValidationFrostFSException(
|
||||
String.format(BYTES_ARE_OVER_FOR_DESERIALIZE_TEMPLATE, Long.class.getName(), offset.get())
|
||||
);
|
||||
}
|
||||
|
||||
return varInt(buf, offset);
|
||||
}
|
||||
|
||||
private static String stringUnmarshal(byte[] buf, AtomicInteger offset) {
|
||||
int size = (int) int64Unmarshal(buf, offset);
|
||||
if (size == 0) {
|
||||
return EMPTY_STRING;
|
||||
}
|
||||
|
||||
if (size > MAX_SLICE_LENGTH) {
|
||||
throw new ValidationFrostFSException(String.format(STRING_IS_TOO_BIG_TEMPLATE, size));
|
||||
}
|
||||
|
||||
if (size < 0) {
|
||||
throw new ValidationFrostFSException(String.format(STRING_SIZE_IS_INVALID_TEMPLATE, size));
|
||||
}
|
||||
|
||||
if (buf.length - offset.get() < size) {
|
||||
throw new ValidationFrostFSException(
|
||||
String.format(BYTES_ARE_OVER_FOR_DESERIALIZE_TEMPLATE, String.class.getName(), offset.get())
|
||||
);
|
||||
}
|
||||
|
||||
return new String(buf, offset.getAndAdd(size), size, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private static Actions unmarshalActions(byte[] buf, AtomicInteger offset) {
|
||||
Actions actions = new Actions();
|
||||
actions.setInverted(boolUnmarshal(buf, offset));
|
||||
actions.setNames(sliceUnmarshal(buf, offset, String.class, RuleDeserializer::stringUnmarshal));
|
||||
return actions;
|
||||
}
|
||||
|
||||
private static Condition unmarshalCondition(byte[] buf, AtomicInteger offset) {
|
||||
Condition condition = new Condition();
|
||||
condition.setOp(ConditionType.get(uInt8Unmarshal(buf, offset)));
|
||||
condition.setKind(ConditionKindType.get(uInt8Unmarshal(buf, offset)));
|
||||
condition.setKey(stringUnmarshal(buf, offset));
|
||||
condition.setValue(stringUnmarshal(buf, offset));
|
||||
|
||||
return condition;
|
||||
}
|
||||
|
||||
private static Rule unmarshalRule(byte[] buf, AtomicInteger offset) {
|
||||
Rule rule = new Rule();
|
||||
rule.setStatus(RuleStatus.get(uInt8Unmarshal(buf, offset)));
|
||||
rule.setActions(unmarshalActions(buf, offset));
|
||||
rule.setResources(unmarshalResources(buf, offset));
|
||||
rule.setAny(boolUnmarshal(buf, offset));
|
||||
rule.setConditions(sliceUnmarshal(buf, offset, Condition.class, RuleDeserializer::unmarshalCondition));
|
||||
|
||||
return rule;
|
||||
}
|
||||
|
||||
private static Resources unmarshalResources(byte[] buf, AtomicInteger offset) {
|
||||
Resources resources = new Resources();
|
||||
resources.setInverted(boolUnmarshal(buf, offset));
|
||||
resources.setNames(sliceUnmarshal(buf, offset, String.class, RuleDeserializer::stringUnmarshal));
|
||||
|
||||
return resources;
|
||||
}
|
||||
|
||||
private static void verifyUnmarshal(byte[] buf, AtomicInteger offset) {
|
||||
if (buf.length != offset.get()) {
|
||||
throw new ValidationFrostFSException(UNMARSHAL_SIZE_DIFFERS);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package info.frostfs.sdk.tools;
|
||||
package info.frostfs.sdk.tools.ape;
|
||||
|
||||
import info.frostfs.sdk.dto.ape.*;
|
||||
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
|
||||
|
@ -17,6 +17,10 @@ public class RuleSerializer {
|
|||
}
|
||||
|
||||
public static byte[] serialize(Chain chain) {
|
||||
if (isNull(chain)) {
|
||||
throw new ValidationFrostFSException(String.format(INPUT_PARAM_IS_MISSING_TEMPLATE, Chain.class.getName()));
|
||||
}
|
||||
|
||||
int s = U_INT_8_SIZE // Marshaller version
|
||||
+ U_INT_8_SIZE // Chain version
|
||||
+ sliceSize(chain.getId(), b -> BYTE_SIZE)
|
|
@ -0,0 +1,7 @@
|
|||
package info.frostfs.sdk.tools.ape;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public interface UnmarshalFunction<T> {
|
||||
T unmarshal(byte[] buf, AtomicInteger offset);
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
package info.frostfs.sdk;
|
||||
|
||||
import com.google.common.io.ByteStreams;
|
||||
import lombok.SneakyThrows;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
import static java.util.Objects.isNull;
|
||||
|
||||
public class FileUtils {
|
||||
|
||||
@SneakyThrows
|
||||
public static byte[] resourceToBytes(String resourcePath) {
|
||||
if (StringUtils.isBlank(resourcePath)) {
|
||||
throw new IllegalArgumentException("Blank filename!");
|
||||
}
|
||||
|
||||
ClassLoader loader = FileUtils.class.getClassLoader();
|
||||
if (isNull(loader)) {
|
||||
throw new RuntimeException("Class loader is null!");
|
||||
}
|
||||
|
||||
InputStream certStream = loader.getResourceAsStream(resourcePath);
|
||||
if (isNull(certStream)) {
|
||||
throw new RuntimeException("Resource could not be found!");
|
||||
}
|
||||
|
||||
return ByteStreams.toByteArray(certStream);
|
||||
}
|
||||
}
|
|
@ -208,8 +208,7 @@ class ApeManagerClientTest {
|
|||
);
|
||||
verifierMock.verify(() -> Verifier.checkResponse(response), times(1));
|
||||
|
||||
var expected = response.getBody().getChainsList();
|
||||
assertThat(result).hasSize(10).containsAll(expected);
|
||||
assertThat(result).hasSize(10);
|
||||
|
||||
var request = captor.getValue();
|
||||
assertEquals(chainTarget.getName(), request.getBody().getTarget().getName());
|
||||
|
|
|
@ -3,22 +3,21 @@ package info.frostfs.sdk.testgenerator;
|
|||
import com.google.protobuf.ByteString;
|
||||
import frostfs.ape.Types;
|
||||
import frostfs.apemanager.Service;
|
||||
import info.frostfs.sdk.FileUtils;
|
||||
import info.frostfs.sdk.Helper;
|
||||
import org.bouncycastle.util.encoders.Base64;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class ApeManagerGenerator {
|
||||
private static final String BASE64_CHAIN = "AAAaY2hhaW4taWQtdGVzdAIAAAICKgACHm5hdGl2ZTpvYmplY3QvKgAAAA==";
|
||||
|
||||
private static ByteString generateChainID() {
|
||||
return ByteString.copyFrom(Helper.getByteArrayFromHex("616c6c6f774f626a476574436e72"));
|
||||
}
|
||||
|
||||
private static Types.Chain generateRawChain() {
|
||||
byte[] chainRaw = FileUtils.resourceToBytes("test_chain_raw.json");
|
||||
|
||||
return Types.Chain.newBuilder()
|
||||
.setRaw(ByteString.copyFrom(chainRaw))
|
||||
.setRaw(ByteString.copyFrom(Base64.decode(BASE64_CHAIN)))
|
||||
.build();
|
||||
}
|
||||
|
||||
|
|
179
client/src/test/java/info/frostfs/sdk/tools/ape/ApeRuleTest.java
Normal file
179
client/src/test/java/info/frostfs/sdk/tools/ape/ApeRuleTest.java
Normal file
|
@ -0,0 +1,179 @@
|
|||
package info.frostfs.sdk.tools.ape;
|
||||
|
||||
import info.frostfs.sdk.dto.ape.*;
|
||||
import info.frostfs.sdk.enums.ConditionKindType;
|
||||
import info.frostfs.sdk.enums.ConditionType;
|
||||
import info.frostfs.sdk.enums.RuleMatchType;
|
||||
import info.frostfs.sdk.enums.RuleStatus;
|
||||
import info.frostfs.sdk.exceptions.FrostFSException;
|
||||
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.bouncycastle.util.encoders.Base64;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static java.util.Objects.isNull;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class ApeRuleTest {
|
||||
|
||||
@Test
|
||||
void apeRuleTest() {
|
||||
//Given
|
||||
var resources = new Resources(false, new String[]{"native:object/*"});
|
||||
var actions = new Actions(false, new String[]{"*"});
|
||||
var rule = new Rule();
|
||||
rule.setStatus(RuleStatus.ALLOW);
|
||||
rule.setResources(resources);
|
||||
rule.setActions(actions);
|
||||
rule.setAny(false);
|
||||
rule.setConditions(new Condition[]{});
|
||||
|
||||
|
||||
var chain = new Chain();
|
||||
chain.setId(ArrayUtils.toObject("chain-id-test".getBytes(StandardCharsets.UTF_8)));
|
||||
chain.setRules(new Rule[]{rule});
|
||||
chain.setMatchType(RuleMatchType.DENY_PRIORITY);
|
||||
|
||||
//When
|
||||
var serialized = RuleSerializer.serialize(chain);
|
||||
var t = Base64.encode(serialized);
|
||||
var restoredChain = RuleDeserializer.deserialize(serialized);
|
||||
|
||||
//Then
|
||||
assertThat(restoredChain.getId()).isNotEmpty().containsOnly(chain.getId());
|
||||
assertEquals(chain.getMatchType(), restoredChain.getMatchType());
|
||||
compareRules(chain.getRules(), restoredChain.getRules());
|
||||
}
|
||||
|
||||
@Test
|
||||
void apeRuleTest2() {
|
||||
//Given
|
||||
var resources = new Resources(true, new String[]{"native:object/*", "U.S.S. ENTERPRISE"});
|
||||
var actions = new Actions(true, new String[]{"put", "get"});
|
||||
var cond1 = new Condition(
|
||||
ConditionType.COND_STRING_EQUALS, ConditionKindType.RESOURCE, "key1", "value1"
|
||||
);
|
||||
var cond2 = new Condition(
|
||||
ConditionType.COND_NUMERIC_GREATER_THAN, ConditionKindType.REQUEST, "key2", "value2"
|
||||
);
|
||||
var rule = new Rule();
|
||||
rule.setStatus(RuleStatus.ACCESS_DENIED);
|
||||
rule.setResources(resources);
|
||||
rule.setActions(actions);
|
||||
rule.setAny(true);
|
||||
rule.setConditions(new Condition[]{cond1, cond2});
|
||||
|
||||
|
||||
var chain = new Chain();
|
||||
chain.setId(ArrayUtils.toObject("dumptext".getBytes(StandardCharsets.UTF_8)));
|
||||
chain.setRules(new Rule[]{rule});
|
||||
chain.setMatchType(RuleMatchType.FIRST_MATCH);
|
||||
|
||||
//When
|
||||
var serialized = RuleSerializer.serialize(chain);
|
||||
var restoredChain = RuleDeserializer.deserialize(serialized);
|
||||
|
||||
//Then
|
||||
assertThat(restoredChain.getId()).isNotEmpty().containsOnly(chain.getId());
|
||||
assertEquals(chain.getMatchType(), restoredChain.getMatchType());
|
||||
compareRules(chain.getRules(), restoredChain.getRules());
|
||||
}
|
||||
|
||||
@Test
|
||||
void apeRuleTest3() {
|
||||
//Given
|
||||
var chain = new Chain();
|
||||
chain.setMatchType(RuleMatchType.DENY_PRIORITY);
|
||||
|
||||
//When
|
||||
var serialized = RuleSerializer.serialize(chain);
|
||||
var restoredChain = RuleDeserializer.deserialize(serialized);
|
||||
|
||||
//Then
|
||||
assertNull(restoredChain.getId());
|
||||
assertEquals(chain.getMatchType(), restoredChain.getMatchType());
|
||||
assertNull(restoredChain.getRules());
|
||||
}
|
||||
|
||||
@Test
|
||||
void apeRule_deserialize_wrong() {
|
||||
//When + Then
|
||||
assertThrows(ValidationFrostFSException.class, () -> RuleDeserializer.deserialize(null));
|
||||
assertThrows(ValidationFrostFSException.class, () -> RuleDeserializer.deserialize(new byte[]{}));
|
||||
assertThrows(FrostFSException.class, () -> RuleDeserializer.deserialize(new byte[]{1, 2, 3}));
|
||||
assertThrows(ValidationFrostFSException.class, () -> RuleDeserializer.deserialize(new byte[]{
|
||||
0x00, 0x00, 0x3A, 0x77, 0x73, 0x3A, 0x69, 0x61, 0x6D, 0x3A, 0x3A, 0x6E, 0x61, 0x6D, 0x65, 0x73,
|
||||
0x70, 0x61, 0x63, 0x65, 0x3A, 0x67, 0x72, 0x6F, 0x75, 0x70, 0x2F, 0x73, 0x6F, (byte) 0x82, (byte) 0x82,
|
||||
(byte) 0x82, (byte) 0x82, (byte) 0x82, (byte) 0x82, 0x75, (byte) 0x82
|
||||
}));
|
||||
}
|
||||
|
||||
@Test
|
||||
void apeRule_serialize_wrong() {
|
||||
//When + Then
|
||||
assertThrows(ValidationFrostFSException.class, () -> RuleSerializer.serialize(null));
|
||||
}
|
||||
|
||||
private void compareRules(Rule[] rules1, Rule[] rules2) {
|
||||
assertThat(rules1).isNotEmpty();
|
||||
assertThat(rules2).isNotEmpty();
|
||||
|
||||
assertEquals(rules1.length, rules2.length);
|
||||
|
||||
for (int ri = 0; ri < rules1.length; ri++) {
|
||||
var rule1 = rules1[ri];
|
||||
var rule2 = rules2[ri];
|
||||
|
||||
assertEquals(rule1.getStatus(), rule2.getStatus());
|
||||
assertEquals(rule1.isAny(), rule2.isAny());
|
||||
|
||||
compareActions(rule1.getActions(), rule2.getActions());
|
||||
compareResources(rule1.getResources(), rule2.getResources());
|
||||
compareConditions(rule1.getConditions(), rule2.getConditions());
|
||||
}
|
||||
}
|
||||
|
||||
private void compareActions(Actions actions1, Actions actions2) {
|
||||
if (isNull(actions1) && isNull(actions2)) {
|
||||
return;
|
||||
}
|
||||
|
||||
assertEquals(actions1.isInverted(), actions2.isInverted());
|
||||
if (ArrayUtils.isEmpty(actions1.getNames()) && ArrayUtils.isEmpty(actions2.getNames())) {
|
||||
return;
|
||||
}
|
||||
|
||||
assertThat(actions2.getNames()).hasSize(actions1.getNames().length).containsOnly(actions1.getNames());
|
||||
}
|
||||
|
||||
private void compareResources(Resources resources1, Resources resources2) {
|
||||
if (isNull(resources1) && isNull(resources2)) {
|
||||
return;
|
||||
}
|
||||
|
||||
assertEquals(resources1.isInverted(), resources2.isInverted());
|
||||
if (ArrayUtils.isEmpty(resources1.getNames()) && ArrayUtils.isEmpty(resources2.getNames())) {
|
||||
return;
|
||||
}
|
||||
|
||||
assertThat(resources2.getNames()).hasSize(resources1.getNames().length).containsOnly(resources1.getNames());
|
||||
}
|
||||
|
||||
private void compareConditions(Condition[] conditions1, Condition[] conditions2) {
|
||||
if (ArrayUtils.isEmpty(conditions1) && ArrayUtils.isEmpty(conditions2)) {
|
||||
return;
|
||||
}
|
||||
|
||||
assertEquals(conditions1.length, conditions2.length);
|
||||
for (int i = 0; i < conditions1.length; i++) {
|
||||
assertEquals(conditions1[i].getOp(), conditions2[i].getOp());
|
||||
assertEquals(conditions1[i].getKind(), conditions2[i].getKind());
|
||||
assertEquals(conditions1[i].getKey(), conditions2[i].getKey());
|
||||
assertEquals(conditions1[i].getValue(), conditions2[i].getValue());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
{
|
||||
"ID": "",
|
||||
"Rules": [
|
||||
{
|
||||
"Status": "Allow",
|
||||
"Actions": {
|
||||
"Inverted": false,
|
||||
"Names": [
|
||||
"GetObject"
|
||||
]
|
||||
},
|
||||
"Resources": {
|
||||
"Inverted": false,
|
||||
"Names": [
|
||||
"native:object/*"
|
||||
]
|
||||
},
|
||||
"Any": false,
|
||||
"Condition": [
|
||||
{
|
||||
"Op": "StringEquals",
|
||||
"Object": "Resource",
|
||||
"Key": "Department",
|
||||
"Value": "HR"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"MatchType": "DenyPriority"
|
||||
}
|
|
@ -46,11 +46,18 @@ public class ErrorConst {
|
|||
public static final String FIELDS_DELIMITER_COMMA = ", ";
|
||||
public static final String FIELDS_DELIMITER_OR = " or ";
|
||||
|
||||
public static final String UNSUPPORTED_MARSHALLER_VERSION_TEMPLATE = "unsupported marshaller version %s";
|
||||
public static final String UNSUPPORTED_CHAIN_VERSION_TEMPLATE = "unsupported chain version %s";
|
||||
public static final String MARSHAL_SIZE_DIFFERS = "actual data size differs from expected";
|
||||
public static final String UNMARSHAL_SIZE_DIFFERS = "unmarshalled bytes left";
|
||||
public static final String BYTES_ARE_OVER_FOR_SERIALIZE_TEMPLATE =
|
||||
"not enough bytes left to serialize value of type %s with length=%s";
|
||||
public static final String BYTES_ARE_OVER_FOR_DESERIALIZE_TEMPLATE =
|
||||
"not enough bytes left to deserialize a value of type %s from offset=%s";
|
||||
public static final String SLICE_IS_TOO_BIG_TEMPLATE = "slice size is too big=%s";
|
||||
public static final String SLICE_SIZE_IS_INVALID_TEMPLATE = "invalid slice size=%s";
|
||||
public static final String STRING_IS_TOO_BIG_TEMPLATE = "string size is too big=%s";
|
||||
public static final String STRING_SIZE_IS_INVALID_TEMPLATE = "invalid string size=%s";
|
||||
|
||||
private ErrorConst() {
|
||||
}
|
||||
|
|
|
@ -1,13 +1,31 @@
|
|||
package info.frostfs.sdk.enums;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public enum ConditionKindType {
|
||||
RESOURCE(0),
|
||||
REQUEST(1),
|
||||
;
|
||||
|
||||
private static final Map<Integer, ConditionKindType> ENUM_MAP_BY_VALUE;
|
||||
|
||||
static {
|
||||
Map<Integer, ConditionKindType> map = new HashMap<>();
|
||||
for (ConditionKindType conditionKindType : ConditionKindType.values()) {
|
||||
map.put(conditionKindType.value, conditionKindType);
|
||||
}
|
||||
ENUM_MAP_BY_VALUE = Collections.unmodifiableMap(map);
|
||||
}
|
||||
|
||||
public final int value;
|
||||
|
||||
ConditionKindType(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public static ConditionKindType get(int value) {
|
||||
return ENUM_MAP_BY_VALUE.get(value);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
package info.frostfs.sdk.enums;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public enum ConditionType {
|
||||
COND_STRING_EQUALS(0),
|
||||
COND_STRING_NOT_EQUALS(1),
|
||||
|
@ -28,9 +32,23 @@ public enum ConditionType {
|
|||
COND_NOT_IP_ADDRESS(18),
|
||||
;
|
||||
|
||||
private static final Map<Integer, ConditionType> ENUM_MAP_BY_VALUE;
|
||||
|
||||
static {
|
||||
Map<Integer, ConditionType> map = new HashMap<>();
|
||||
for (ConditionType conditionType : ConditionType.values()) {
|
||||
map.put(conditionType.value, conditionType);
|
||||
}
|
||||
ENUM_MAP_BY_VALUE = Collections.unmodifiableMap(map);
|
||||
}
|
||||
|
||||
public final int value;
|
||||
|
||||
ConditionType(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public static ConditionType get(int value) {
|
||||
return ENUM_MAP_BY_VALUE.get(value);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
package info.frostfs.sdk.enums;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public enum RuleMatchType {
|
||||
// DENY_PRIORITY rejects the request if any `Deny` is specified.
|
||||
DENY_PRIORITY(0),
|
||||
|
@ -8,9 +12,23 @@ public enum RuleMatchType {
|
|||
FIRST_MATCH(1),
|
||||
;
|
||||
|
||||
private static final Map<Integer, RuleMatchType> ENUM_MAP_BY_VALUE;
|
||||
|
||||
static {
|
||||
Map<Integer, RuleMatchType> map = new HashMap<>();
|
||||
for (RuleMatchType ruleMatchType : RuleMatchType.values()) {
|
||||
map.put(ruleMatchType.value, ruleMatchType);
|
||||
}
|
||||
ENUM_MAP_BY_VALUE = Collections.unmodifiableMap(map);
|
||||
}
|
||||
|
||||
public final int value;
|
||||
|
||||
RuleMatchType(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public static RuleMatchType get(int value) {
|
||||
return ENUM_MAP_BY_VALUE.get(value);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
package info.frostfs.sdk.enums;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public enum RuleStatus {
|
||||
ALLOW(0),
|
||||
NO_RULE_FOUND(1),
|
||||
|
@ -7,9 +11,23 @@ public enum RuleStatus {
|
|||
QUOTA_LIMIT_REACHED(3),
|
||||
;
|
||||
|
||||
private static final Map<Integer, RuleStatus> ENUM_MAP_BY_VALUE;
|
||||
|
||||
static {
|
||||
Map<Integer, RuleStatus> map = new HashMap<>();
|
||||
for (RuleStatus ruleStatus : RuleStatus.values()) {
|
||||
map.put(ruleStatus.value, ruleStatus);
|
||||
}
|
||||
ENUM_MAP_BY_VALUE = Collections.unmodifiableMap(map);
|
||||
}
|
||||
|
||||
public final int value;
|
||||
|
||||
RuleStatus(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public static RuleStatus get(int value) {
|
||||
return ENUM_MAP_BY_VALUE.get(value);
|
||||
}
|
||||
}
|
||||
|
|
2
pom.xml
2
pom.xml
|
@ -17,7 +17,7 @@
|
|||
</modules>
|
||||
|
||||
<properties>
|
||||
<revision>0.8.0</revision>
|
||||
<revision>0.9.0</revision>
|
||||
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
|
|
Loading…
Add table
Reference in a new issue