Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Query API #36

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/main/java/dev/warrant/WarrantBaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import dev.warrant.exception.WarrantException;
import dev.warrant.model.WarrantSubject;
import dev.warrant.model.QueryResultSet;
import dev.warrant.model.UserSession;
import dev.warrant.model.UserSessionSpec;
import dev.warrant.model.Warrant;
Expand Down Expand Up @@ -96,6 +97,17 @@ public Warrant[] listWarrants(WarrantFilters filters, ListParams listParams, Req
return makeGetRequest("/v1/warrants", queryParams, Warrant[].class, requestOptions.asMap());
}

public QueryResultSet query(String query, ListParams listParams) throws WarrantException {
return query(query, listParams, new RequestOptions());
}

public QueryResultSet query(String query, ListParams listParams, RequestOptions requestOptions)
throws WarrantException {
Map<String, Object> queryParams = listParams.asMap();
queryParams.put("q", query);
return makeGetRequest("/v1/query", queryParams, QueryResultSet.class, requestOptions.asMap());
}

public boolean check(WarrantObject object, String relation, WarrantSubject subject) throws WarrantException {
return check(object, relation, subject, Collections.emptyMap(), new RequestOptions());
}
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/dev/warrant/model/QueryResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package dev.warrant.model;

public class QueryResult {

private String objectType;
private String objectId;
private Warrant warrant;
private Boolean isImplicit;

public QueryResult() {
// For json serialization
}

public QueryResult(String objectType, String objectId, Warrant warrant, Boolean isImplicit) {
this.objectType = objectType;
this.objectId = objectId;
this.warrant = warrant;
this.isImplicit = isImplicit;
}

public String getObjectType() {
return this.objectType;
}

public void setObjectType(String objectType) {
this.objectType = objectType;
}

public String getObjectId() {
return this.objectId;
}

public void setObjectId(String objectId) {
this.objectId = objectId;
}

public Warrant getWarrant() {
return this.warrant;
}

public void setWarrant(Warrant warrant) {
this.warrant = warrant;
}

public Boolean isImplicit() {
return this.isImplicit;
}

public void setIsImplicit(Boolean isImplicit) {
this.isImplicit = isImplicit;
}
}
24 changes: 24 additions & 0 deletions src/main/java/dev/warrant/model/QueryResultSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dev.warrant.model;

public class QueryResultSet {

private QueryResult[] results;
private String lastId;

public QueryResultSet() {
// For json serialization
}

public QueryResultSet(QueryResult[] results, String lastId) {
this.results = results;
this.lastId = lastId;
}

public QueryResult[] getResults() {
return this.results;
}

public String getLastId() {
return this.lastId;
}
}
29 changes: 18 additions & 11 deletions src/main/java/dev/warrant/model/Warrant.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ public class Warrant {
private String relation;
private WarrantSubject subject;
private String policy;
private boolean isImplicit;

public Warrant() {
// For json serialization
Expand All @@ -28,35 +27,43 @@ public Warrant(String objectType, String objectId, String relation, WarrantSubje
this.policy = policy;
}

public Warrant(String objectType, String objectId, String relation, WarrantSubject subject, boolean isImplicit) {
this.objectType = objectType;
this.objectId = objectId;
this.relation = relation;
this.subject = subject;
this.isImplicit = isImplicit;
}

public String getObjectType() {
return objectType;
}

public void setObjectType(String objectType) {
this.objectType = objectType;
}

public String getObjectId() {
return objectId;
}

public void setObjectId(String objectId) {
this.objectId = objectId;
}

public String getRelation() {
return relation;
}

public void setRelation(String relation) {
this.relation = relation;
}

public WarrantSubject getSubject() {
return subject;
}

public void setSubject(WarrantSubject subject) {
this.subject = subject;
}

public String getPolicy() {
return policy;
}

public boolean getIsImplicit() {
return isImplicit;
public void setPolicy(String policy) {
this.policy = policy;
}
}
53 changes: 53 additions & 0 deletions src/test/java/dev/warrant/LiveTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.junit.jupiter.api.Test;

import dev.warrant.exception.WarrantException;
import dev.warrant.model.QueryResultSet;
import dev.warrant.model.Warrant;
import dev.warrant.model.WarrantCheck;
import dev.warrant.model.WarrantSpec;
Expand Down Expand Up @@ -558,4 +559,56 @@ public void testAllOfAnyOfBatchCheck() throws WarrantException {
client.deletePermission(permission2);
client.deletePermission(permission3);
}

@Test
public void testQuery() throws WarrantException {
User userA = client.createUser(new User("userA"));
User userB = client.createUser(new User("userB"));
Permission permission1 = client.createPermission(
new Permission("perm1", "Permission 1", "This is permission 1."));
Permission permission2 = client.createPermission(new Permission("perm2"));
Permission permission3 = client.createPermission(
new Permission("perm3", "Permission 3", "This is permission 3."));
Role role1 = client.createRole(new Role("role1", "Role 1", "This is role 1."));
Role role2 = client.createRole(new Role("role2", "Role 2"));

client.assignPermissionToRole("perm1", "role1");
client.assignPermissionToRole("perm2", "role2");
client.assignPermissionToRole("perm3", "role2");
client.createWarrant(role2, "member", new WarrantSubject("role", "role1"));
client.assignRoleToUser("role1", "userA");
client.assignRoleToUser("role2", "userB");

QueryResultSet resultSet = client.query("select role where user:userA is member",
new ListParams().withLimit(1));
Assertions.assertEquals(1, resultSet.getResults().length);
Assertions.assertEquals("role", resultSet.getResults()[0].getObjectType());
Assertions.assertEquals("role1", resultSet.getResults()[0].getObjectId());
Assertions.assertFalse(resultSet.getResults()[0].isImplicit());
Assertions.assertEquals("role", resultSet.getResults()[0].getWarrant().getObjectType());
Assertions.assertEquals("role1", resultSet.getResults()[0].getWarrant().getObjectId());
Assertions.assertEquals("member", resultSet.getResults()[0].getWarrant().getRelation());
Assertions.assertEquals("user", resultSet.getResults()[0].getWarrant().getSubject().getObjectType());
Assertions.assertEquals("userA", resultSet.getResults()[0].getWarrant().getSubject().getObjectId());

resultSet = client.query("select role where user:userA is member",
new ListParams().withLimit(1).withAfterId(resultSet.getLastId()));
Assertions.assertEquals(1, resultSet.getResults().length);
Assertions.assertEquals("role", resultSet.getResults()[0].getObjectType());
Assertions.assertEquals("role2", resultSet.getResults()[0].getObjectId());
Assertions.assertTrue(resultSet.getResults()[0].isImplicit());
Assertions.assertEquals("role", resultSet.getResults()[0].getWarrant().getObjectType());
Assertions.assertEquals("role2", resultSet.getResults()[0].getWarrant().getObjectId());
Assertions.assertEquals("member", resultSet.getResults()[0].getWarrant().getRelation());
Assertions.assertEquals("role", resultSet.getResults()[0].getWarrant().getSubject().getObjectType());
Assertions.assertEquals("role1", resultSet.getResults()[0].getWarrant().getSubject().getObjectId());

client.deleteRole(role1);
client.deleteRole(role2);
client.deletePermission(permission3);
client.deletePermission(permission2);
client.deletePermission(permission1);
client.deleteUser(userB);
client.deleteUser(userA);
}
}
87 changes: 54 additions & 33 deletions src/test/java/dev/warrant/WarrantClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

import dev.warrant.exception.WarrantException;
import dev.warrant.model.WarrantSubject;
import dev.warrant.model.QueryResult;
import dev.warrant.model.QueryResultSet;
import dev.warrant.model.Warrant;
import dev.warrant.model.object.Tenant;
import dev.warrant.model.object.User;
Expand Down Expand Up @@ -90,37 +92,56 @@ public void testCreateTenantWithId() throws WarrantException {
Assertions.assertNull(newTenant.getName());
}

// @Test
// public void testQueryWarrants() throws WarrantException {
// Mockito.when(httpResponse.statusCode()).thenReturn(200);
// Mockito.when(httpResponse.body())
// .thenReturn(
// "[\n {\n \"objectType\": \"role\",\n \"objectId\": \"admin\",\n \"relation\": \"member\",\n \"subject\": {\n \"objectType\": \"user\",\n \"objectId\": \"6\"\n },\n \"isImplicit\": false\n },\n {\n \"objectType\": \"role\",\n \"objectId\": \"manager\",\n \"relation\": \"member\",\n \"subject\": {\n \"objectType\": \"user\",\n \"objectId\": \"6\"\n },\n \"isImplicit\": true\n }\n]");

// Query q = Query.selectWarrants()
// .forClause("subject=user:6")
// .where("subject=user:6");
// WarrantClient warrantClient = new WarrantClient(WarrantConfig.withApiKey("sample_key"), httpClient);
// Warrant[] warrants = warrantClient.queryWarrants(q, 100, 1);
// Warrant[] expectedWarrants = {
// new Warrant("role", "admin", "member", new WarrantSubject("user", "6"), false),
// new Warrant("role", "manager", "member", new WarrantSubject("user", "6"), true)
// };

// Assertions.assertEquals(expectedWarrants[0].getObjectType(), warrants[0].getObjectType());
// Assertions.assertEquals(expectedWarrants[0].getObjectId(), warrants[0].getObjectId());
// Assertions.assertEquals(expectedWarrants[0].getRelation(), warrants[0].getRelation());
// Assertions.assertEquals(expectedWarrants[0].getSubject().getObjectType(),
// warrants[0].getSubject().getObjectType());
// Assertions.assertEquals(expectedWarrants[0].getSubject().getObjectId(), warrants[0].getSubject().getObjectId());
// Assertions.assertEquals(expectedWarrants[0].getIsImplicit(), warrants[0].getIsImplicit());

// Assertions.assertEquals(expectedWarrants[1].getObjectType(), warrants[1].getObjectType());
// Assertions.assertEquals(expectedWarrants[1].getObjectId(), warrants[1].getObjectId());
// Assertions.assertEquals(expectedWarrants[1].getRelation(), warrants[1].getRelation());
// Assertions.assertEquals(expectedWarrants[1].getSubject().getObjectType(),
// warrants[1].getSubject().getObjectType());
// Assertions.assertEquals(expectedWarrants[1].getSubject().getObjectId(), warrants[1].getSubject().getObjectId());
// Assertions.assertEquals(expectedWarrants[1].getIsImplicit(), warrants[1].getIsImplicit());
// }
@Test
public void testQuery() throws WarrantException {
Mockito.when(httpResponse.statusCode()).thenReturn(200);
Mockito.when(httpResponse.body())
.thenReturn(
"{\"results\":[{\"objectType\":\"role\",\"objectId\":\"admin\",\"warrant\":{\"objectType\":\"role\",\"objectId\":\"admin\",\"relation\":\"member\",\"subject\":{\"objectType\":\"user\",\"objectId\":\"6\"}},\"isImplicit\":false,\"meta\":{\"name\":\"Admin\"}},{\"objectType\":\"role\",\"objectId\":\"manager\",\"warrant\":{\"objectType\":\"role\",\"objectId\":\"manager\",\"relation\":\"member\",\"subject\":{\"objectType\":\"role\",\"objectId\":\"admin\"}},\"isImplicit\":true,\"meta\":{\"name\":\"Manager\"}}]}\n");

WarrantClient warrantClient = new WarrantClient(WarrantConfig.withApiKey("sample_key"), httpClient);
QueryResultSet queryResultSet = warrantClient.query("select role where user:6 is member", new ListParams(),
new RequestOptions());
QueryResult[] expectedQueryResults = {
new QueryResult("role", "admin",
new Warrant("role", "admin", "member", new WarrantSubject("user", "6")), false),
new QueryResult("role", "manager",
new Warrant("role", "manager", "member", new WarrantSubject("role", "admin")), true)
};
QueryResultSet expectedQueryResultSet = new QueryResultSet(expectedQueryResults, "");

Assertions.assertEquals(expectedQueryResultSet.getResults().length, queryResultSet.getResults().length);
Assertions.assertEquals(expectedQueryResultSet.getResults()[0].getObjectType(),
queryResultSet.getResults()[0].getObjectType());
Assertions.assertEquals(expectedQueryResultSet.getResults()[0].getObjectId(),
queryResultSet.getResults()[0].getObjectId());
Assertions.assertEquals(expectedQueryResultSet.getResults()[0].getWarrant().getObjectType(),
queryResultSet.getResults()[0].getWarrant().getObjectType());
Assertions.assertEquals(expectedQueryResultSet.getResults()[0].getWarrant().getObjectId(),
queryResultSet.getResults()[0].getWarrant().getObjectId());
Assertions.assertEquals(expectedQueryResultSet.getResults()[0].getWarrant().getRelation(),
queryResultSet.getResults()[0].getWarrant().getRelation());
Assertions.assertEquals(expectedQueryResultSet.getResults()[0].getWarrant().getSubject().getObjectType(),
queryResultSet.getResults()[0].getWarrant().getSubject().getObjectType());
Assertions.assertEquals(expectedQueryResultSet.getResults()[0].getWarrant().getSubject().getObjectId(),
queryResultSet.getResults()[0].getWarrant().getSubject().getObjectId());
Assertions.assertEquals(expectedQueryResultSet.getResults()[0].isImplicit(),
queryResultSet.getResults()[0].isImplicit());
Assertions.assertEquals(expectedQueryResultSet.getResults()[1].getObjectType(),
queryResultSet.getResults()[1].getObjectType());
Assertions.assertEquals(expectedQueryResultSet.getResults()[1].getObjectId(),
queryResultSet.getResults()[1].getObjectId());
Assertions.assertEquals(expectedQueryResultSet.getResults()[1].getWarrant().getObjectType(),
queryResultSet.getResults()[1].getWarrant().getObjectType());
Assertions.assertEquals(expectedQueryResultSet.getResults()[1].getWarrant().getObjectId(),
queryResultSet.getResults()[1].getWarrant().getObjectId());
Assertions.assertEquals(expectedQueryResultSet.getResults()[1].getWarrant().getRelation(),
queryResultSet.getResults()[1].getWarrant().getRelation());
Assertions.assertEquals(expectedQueryResultSet.getResults()[1].getWarrant().getSubject().getObjectType(),
queryResultSet.getResults()[1].getWarrant().getSubject().getObjectType());
Assertions.assertEquals(expectedQueryResultSet.getResults()[1].getWarrant().getSubject().getObjectId(),
queryResultSet.getResults()[1].getWarrant().getSubject().getObjectId());
Assertions.assertEquals(expectedQueryResultSet.getResults()[1].isImplicit(),
queryResultSet.getResults()[1].isImplicit());
}
}