Skip to content

Commit

Permalink
Merge pull request #48 from aserto-dev/query-identity-context
Browse files Browse the repository at this point in the history
Take IdentityContext in AuthorizerClient.query
  • Loading branch information
ronenh authored Nov 27, 2024
2 parents 4187a22 + f2ddb95 commit f98e250
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 152 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ hs_err_pid*

# IDE specific
.idea/
.factorypath
4 changes: 2 additions & 2 deletions examples/authz-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<dependency>
<groupId>com.aserto</groupId>
<artifactId>aserto-java</artifactId>
<version>0.31.1</version>
<version>0.31.4</version>
</dependency>
</dependencies>

Expand Down Expand Up @@ -51,4 +51,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
4 changes: 2 additions & 2 deletions examples/directory-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<dependency>
<groupId>com.aserto</groupId>
<artifactId>aserto-java</artifactId>
<version>0.31.1</version>
<version>0.31.4</version>
</dependency>
</dependencies>

Expand Down Expand Up @@ -51,4 +51,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
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.3</version>
<version>0.31.4</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.13</version>
<version>0.20.14</version>
</dependency>
<dependency>
<groupId>com.aserto</groupId>
<artifactId>java-directory</artifactId>
<version>0.31.4</version>
<version>0.31.5</version>
</dependency>

<!-- Used for unit testing -->
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/aserto/AuthorizerClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@

public interface AuthorizerClient {
public List<Module> listPolicies(String policyName, String policyLabel);

public Module getPolicy(String policyId);

public List<Decision> is(IdentityCtx identityCtx, PolicyCtx policyCtx);

public List<Decision> is(IdentityCtx identityCtx, PolicyCtx policyCtx, Map<String, Value> resourceCtx);
public Struct query(String query, PolicyCtx policyContext, Map<String, Value> values);

public Struct query(String query, PolicyCtx policyContext, Map<String, Value> resourceCtx);

public Struct query(String query, IdentityCtx identityCtx, PolicyCtx policyContext, Map<String, Value> resourceCtx);

public Map<String, Value> decisionTree(IdentityCtx identityCtx, PolicyCtx policyCtx);

public void close();
}
18 changes: 14 additions & 4 deletions src/main/java/com/aserto/authorizer/AuthzClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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.IdentityType;
import com.aserto.authorizer.v2.api.Module;
import com.aserto.authorizer.v2.api.PolicyContext;
import com.aserto.authorizer.v2.api.PolicyInstance;
Expand All @@ -32,6 +33,7 @@
public class AuthzClient implements AuthorizerClient {
private final AuthorizerGrpc.AuthorizerBlockingStub client;
private final ManagedChannel channel;

public AuthzClient(ManagedChannel channel) {
client = AuthorizerGrpc.newBlockingStub(channel);
this.channel = channel;
Expand Down Expand Up @@ -84,21 +86,29 @@ public List<Decision> is(IdentityCtx identityCtx, PolicyCtx policyCtx, Map<Strin
}

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

IdentityContext identityContext = buildIdentityContext(identityCtx);
PolicyInstance policy = buildPolicy(policyContext.getName());
Struct.Builder structBuilder = buildResourceContext(values);
Struct.Builder resourceContext = buildResourceContext(resourceCtx);

queryRequestBuilder.setIdentityContext(identityContext);
queryRequestBuilder.setPolicyInstance(policy);
queryRequestBuilder.setResourceContext(structBuilder);
queryRequestBuilder.setResourceContext(resourceContext);

QueryResponse queryResponse = client.query(queryRequestBuilder.build());

return queryResponse.getResponse();
}

@Override
public Struct query(String query, PolicyCtx policyContext, Map<String, Value> resourceCtx) {
return query(query, new IdentityCtx("", IdentityType.IDENTITY_TYPE_NONE), policyContext, resourceCtx);
}

@Override
public Map<String, Value> decisionTree(IdentityCtx identityCtx, PolicyCtx policyCtx) {
DecisionTreeRequest.Builder decisionTreeBuilder = DecisionTreeRequest.newBuilder();
Expand Down Expand Up @@ -129,7 +139,7 @@ private PolicyInstance buildPolicy(String name) {
}

private IdentityContext buildIdentityContext(IdentityCtx identityContext) {
IdentityContext.Builder identityContextBuilder = IdentityContext.newBuilder();
IdentityContext.Builder identityContextBuilder = IdentityContext.newBuilder();
identityContextBuilder.setIdentity(identityContext.getIdentity());
identityContextBuilder.setType(identityContext.getIdentityType());

Expand Down
Loading

0 comments on commit f98e250

Please sign in to comment.