Skip to content

Commit

Permalink
Merge pull request #45 from aserto-dev/instance-label
Browse files Browse the repository at this point in the history
Update deps, remove instance label, and update importer
  • Loading branch information
ronenh authored Oct 15, 2024
2 parents 988d072 + 9622dce commit 92249eb
Show file tree
Hide file tree
Showing 14 changed files with 592 additions and 213 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.aserto</groupId>
<artifactId>aserto-java</artifactId>
<version>0.31.1</version>
<version>0.31.2</version>

<name>${project.groupId}:${project.artifactId}</name>
<description>Java SDK to interact with aserto services</description>
Expand Down Expand Up @@ -50,12 +50,12 @@
<dependency>
<groupId>com.aserto</groupId>
<artifactId>java-authorizer</artifactId>
<version>0.20.12</version>
<version>0.20.13</version>
</dependency>
<dependency>
<groupId>com.aserto</groupId>
<artifactId>java-directory</artifactId>
<version>0.31.3</version>
<version>0.31.4</version>
</dependency>

<!-- Used for unit testing -->
Expand Down
49 changes: 34 additions & 15 deletions src/main/java/com/aserto/authorizer/AuthzClient.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,55 @@
package com.aserto.authorizer;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.aserto.AuthorizerClient;
import com.aserto.authorizer.v2.*;
import com.aserto.authorizer.v2.AuthorizerGrpc;
import com.aserto.authorizer.v2.Decision;
import com.aserto.authorizer.v2.api.*;
import com.aserto.authorizer.v2.DecisionTreeRequest;
import com.aserto.authorizer.v2.DecisionTreeResponse;
import com.aserto.authorizer.v2.GetPolicyRequest;
import com.aserto.authorizer.v2.GetPolicyResponse;
import com.aserto.authorizer.v2.IsRequest;
import com.aserto.authorizer.v2.IsResponse;
import com.aserto.authorizer.v2.ListPoliciesRequest;
import com.aserto.authorizer.v2.ListPoliciesResponse;
import com.aserto.authorizer.v2.QueryRequest;
import com.aserto.authorizer.v2.QueryResponse;
import com.aserto.authorizer.v2.api.IdentityContext;
import com.aserto.authorizer.v2.api.Module;
import com.aserto.authorizer.v2.api.PolicyContext;
import com.aserto.authorizer.v2.api.PolicyInstance;
import com.aserto.model.IdentityCtx;
import com.aserto.model.PolicyCtx;
import com.google.protobuf.Struct;
import com.google.protobuf.Value;
import io.grpc.ManagedChannel;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import io.grpc.ManagedChannel;

public class AuthzClient implements AuthorizerClient {
private AuthorizerGrpc.AuthorizerBlockingStub client;
private ManagedChannel channel;
private final AuthorizerGrpc.AuthorizerBlockingStub client;
private final ManagedChannel channel;
public AuthzClient(ManagedChannel channel) {
client = AuthorizerGrpc.newBlockingStub(channel);
this.channel = channel;
}

@Override
public List<Module> listPolicies(String policyName, String policyLabel) {
ListPoliciesRequest.Builder policyBuilder = ListPoliciesRequest.newBuilder();

PolicyInstance policy = buildPolicy(policyName, policyLabel);
PolicyInstance policy = buildPolicy(policyName);
policyBuilder.setPolicyInstance(policy);

ListPoliciesResponse response = client.listPolicies(policyBuilder.build());

return response.getResultList();
}

@Override
public Module getPolicy(String policyId) {
GetPolicyRequest.Builder getPolicyBuilder = GetPolicyRequest.newBuilder();
getPolicyBuilder.setId(policyId);
Expand All @@ -44,16 +59,18 @@ public Module getPolicy(String policyId) {
return policyResponse.getResult();
}

@Override
public List<Decision> is(IdentityCtx identityCtx, PolicyCtx policyCtx) {
return this.is(identityCtx, policyCtx, Collections.emptyMap());
}

@Override
public List<Decision> is(IdentityCtx identityCtx, PolicyCtx policyCtx, Map<String, Value> values) {
IsRequest.Builder isBuilder = IsRequest.newBuilder();

IdentityContext identityContext = buildIdentityContext(identityCtx);
PolicyContext policyContext = buildPolicyContext(policyCtx);
PolicyInstance policy = buildPolicy(policyCtx.getName(), policyCtx.getLabel());
PolicyInstance policy = buildPolicy(policyCtx.getName());
Struct.Builder resourceContext = buildResourceContext(values);

isBuilder.setIdentityContext(identityContext);
Expand All @@ -66,11 +83,12 @@ public List<Decision> is(IdentityCtx identityCtx, PolicyCtx policyCtx, Map<Strin
return isResponse.getDecisionsList();
}

@Override
public Struct query(String query, PolicyCtx policyContext, Map<String, Value> values) {
QueryRequest.Builder queryRequestBuilder = QueryRequest.newBuilder();
queryRequestBuilder.setQuery(query);

PolicyInstance policy = buildPolicy(policyContext.getName(), policyContext.getLabel());
PolicyInstance policy = buildPolicy(policyContext.getName());
Struct.Builder structBuilder = buildResourceContext(values);

queryRequestBuilder.setPolicyInstance(policy);
Expand All @@ -81,12 +99,13 @@ public Struct query(String query, PolicyCtx policyContext, Map<String, Value> va
return queryResponse.getResponse();
}

@Override
public Map<String, Value> decisionTree(IdentityCtx identityCtx, PolicyCtx policyCtx) {
DecisionTreeRequest.Builder decisionTreeBuilder = DecisionTreeRequest.newBuilder();

IdentityContext identityContext = buildIdentityContext(identityCtx);
PolicyContext policyContext = buildPolicyContext(policyCtx);
PolicyInstance policy = buildPolicy(policyCtx.getName(), policyCtx.getLabel());
PolicyInstance policy = buildPolicy(policyCtx.getName());

decisionTreeBuilder.setIdentityContext(identityContext);
decisionTreeBuilder.setPolicyContext(policyContext);
Expand All @@ -97,14 +116,14 @@ public Map<String, Value> decisionTree(IdentityCtx identityCtx, PolicyCtx policy
return decisionTree.getPath().getFieldsMap();
}

@Override
public void close() {
channel.shutdown();
}

private PolicyInstance buildPolicy(String name, String label) {
private PolicyInstance buildPolicy(String name) {
PolicyInstance.Builder policyInstance = PolicyInstance.newBuilder();
policyInstance.setName(name);
policyInstance.setInstanceLabel(label);

return policyInstance.build();
}
Expand Down
Loading

0 comments on commit 92249eb

Please sign in to comment.