Skip to content

Commit

Permalink
fix(bug): fixed action type bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathias Brunkow Moser committed May 11, 2024
1 parent 6ca6b26 commit 050b8e0
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 12 deletions.
4 changes: 2 additions & 2 deletions charts/digital-product-pass/values-int.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
#################################################################################

backend:
image:
pullPolicy: Always
ingress:
enabled: true
# className: "nginx"
image:
pullPolicy: Always
annotations:
ingressClassName: nginx
# kubernetes.io/tls-acme: "true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class Action {
**/
@JsonProperty("odrl:action")
@JsonAlias({"action", "odrl:action"})
String action;
ActionType action;
@JsonProperty("odrl:constraint")
@JsonAlias({"constraint", "odrl:constraint"})
LogicalConstraint constraint;
Expand All @@ -58,7 +58,7 @@ public class Action {
public Action() {
}

public Action(String action, LogicalConstraint constraint) {
public Action(ActionType action, LogicalConstraint constraint) {
this.action = action;
this.constraint = constraint;
}
Expand All @@ -75,17 +75,28 @@ public Action(PolicyCheckConfig.ActionConfig actionConfig) {
*/
public void buildAction(PolicyCheckConfig.ActionConfig actionConfig){
// Create clean list of constraints
this.action = actionConfig.getAction();
addAction(actionConfig.getAction());
this.constraint = new LogicalConstraint(actionConfig);
}
/**
* GETTERS AND SETTERS
**/
public String getAction() {
public String retrieveAction() {
if(this.action == null){
return null;
}
return this.action.getType();
}
public void addAction(String action) {
this.action = new ActionType();
this.action.setType(action);
}

public ActionType getAction() {
return action;
}

public void setAction(String action) {
public void setAction(ActionType action) {
this.action = action;
}

Expand All @@ -107,10 +118,31 @@ public void setConstraint(LogicalConstraint constraint) {
public Boolean compare(Action action){
try{
if(action == null){return false;} // If action is null not continue
if(!action.getAction().equalsIgnoreCase(this.getAction())){return false;} // If actions strings are not the same
if(!action.retrieveAction().equalsIgnoreCase(this.retrieveAction())){return false;} // If actions strings are not the same
return action.getConstraint().compare(this.getConstraint()); //If constraints are the same
}catch (Exception e){
throw new ModelException(this.getClass().getName(), e, "It was not possible to compare the actions!");
}
}

static class ActionType{
@JsonProperty("odrl:type")
@JsonAlias({"type", "odrl:type", "@type"})
String type;

public ActionType(String type) {
this.type = type;
}

public ActionType() {
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public List<Set> getValidPoliciesByConstraints(Object policies, PolicyCheckConfi

if (policies instanceof LinkedHashMap) {
// Check if policy is valid or not

LogUtil.printMessage("DEBUG: Single Policy!");
Set policy = this.parsePolicy(policies);
LogUtil.printMessage("DEBUG: Policy: " + jsonUtil.toJson(policy, true));

Expand All @@ -158,6 +158,7 @@ public List<Set> getValidPoliciesByConstraints(Object policies, PolicyCheckConfi
// If the policy is not valid return an empty list
return new ArrayList<>();
}
LogUtil.printMessage("DEBUG: Multiple Policies!");
List<Set> policyList = this.parsePolicies(policies);
LogUtil.printMessage("DEBUG: Policies: " + jsonUtil.toJson(policyList, true));

Expand Down Expand Up @@ -275,18 +276,28 @@ public Set parsePolicy(Object rawPolicy){
try {
// Parse policy to json node
JsonNode policy = jsonUtil.toJsonNode(rawPolicy);
LogUtil.printMessage("DEBUG: Json Node Policy: " + jsonUtil.toJson(policy, true));
// Get permission, prohibition and obligation
JsonNode permission = policy.get("odrl:permission");
LogUtil.printMessage("DEBUG: Permission Json Node: " + jsonUtil.toJson(permission, true));

JsonNode prohibition = policy.get("odrl:prohibition");
LogUtil.printMessage("DEBUG: prohibition Json Node: " + jsonUtil.toJson(prohibition, true));

JsonNode obligation = policy.get("odrl:obligation");
LogUtil.printMessage("DEBUG: prohibition Json Node: " + jsonUtil.toJson(obligation, true));

// Check if its null
if(permission == null || prohibition == null || obligation == null){
throw new UtilException(PolicyUtil.class, "One of the policy action constraints is empty!");
}
// Check if all them are array then parse as default
if(permission.isArray() && prohibition.isArray() && obligation.isArray()){
LogUtil.printMessage("DEBUG: All is array ");

return jsonUtil.bind(rawPolicy, new TypeReference<>(){});
}
LogUtil.printMessage("DEBUG: One is not array ");
// If not parse the set by action type
return new Set(this.parseActions(permission), this.parseActions(prohibition), this.parseActions(obligation));
}catch (Exception e) {
Expand All @@ -304,8 +315,10 @@ public Set parsePolicy(Object rawPolicy){
public List<Action> parseActions(JsonNode node){
// If node is not array parse a single action object
if(!node.isArray()){
LogUtil.printMessage("DEBUG: Node is not array " +jsonUtil.toJson(node, true));
return new ArrayList<>(){{add(jsonUtil.bind(node, new TypeReference<>(){}));}};
}
LogUtil.printMessage("DEBUG: Node is array " +jsonUtil.toJson(node, true));
// If node is array parse the action node as a list
return jsonUtil.bind(node, new TypeReference<>(){});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,13 @@ void setUp() {

correctLogicalConstraint.put("odrl:and", correctContraints);

action.put("odrl:action", "USE");
action.put("odrl:action", new LinkedHashMap<>(){{put("odrl:type","USE");}});
action.put("odrl:constraint", logicalConstraint);

correctAction.put("odrl:action", "USE");
correctAction.put("odrl:action", new LinkedHashMap<>(){{put("odrl:type","USE");}});
correctAction.put("odrl:constraint", correctLogicalConstraint);

actionDtr.put("odrl:action", "USE");
actionDtr.put("odrl:action", new LinkedHashMap<>(){{put("odrl:type","USE");}});
actionDtr.put("odrl:constraint", logicalConstraintDtr);

singlePermission = correctAction;
Expand Down

0 comments on commit 050b8e0

Please sign in to comment.