From b3eb0aa72ddb71c317bfeb20c53cdf3670a505ea Mon Sep 17 00:00:00 2001 From: Viren Baraiya Date: Mon, 12 Sep 2022 23:40:30 -0700 Subject: [PATCH] changes to SDK --- .../client/model/CreateAccessKeyResponse.java | 16 +++ .../conductor/client/model/SubjectRef.java | 3 +- .../conductor/client/model/TargetRef.java | 53 +------ .../client/api/AuthorizationClientTests.java | 130 +++++++++++++++++- 4 files changed, 147 insertions(+), 55 deletions(-) diff --git a/src/main/java/io/orkes/conductor/client/model/CreateAccessKeyResponse.java b/src/main/java/io/orkes/conductor/client/model/CreateAccessKeyResponse.java index ba86f2d9..ff610b8b 100644 --- a/src/main/java/io/orkes/conductor/client/model/CreateAccessKeyResponse.java +++ b/src/main/java/io/orkes/conductor/client/model/CreateAccessKeyResponse.java @@ -15,4 +15,20 @@ public class CreateAccessKeyResponse { private String id; private String secret; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getSecret() { + return secret; + } + + public void setSecret(String secret) { + this.secret = secret; + } } diff --git a/src/main/java/io/orkes/conductor/client/model/SubjectRef.java b/src/main/java/io/orkes/conductor/client/model/SubjectRef.java index 71399740..2d0b54b2 100644 --- a/src/main/java/io/orkes/conductor/client/model/SubjectRef.java +++ b/src/main/java/io/orkes/conductor/client/model/SubjectRef.java @@ -33,7 +33,8 @@ public class SubjectRef { public enum TypeEnum { USER("USER"), ROLE("ROLE"), - GROUP("GROUP"); + GROUP("GROUP"), + TAG("TAG"); private String value; diff --git a/src/main/java/io/orkes/conductor/client/model/TargetRef.java b/src/main/java/io/orkes/conductor/client/model/TargetRef.java index a9a765df..b1f47f78 100644 --- a/src/main/java/io/orkes/conductor/client/model/TargetRef.java +++ b/src/main/java/io/orkes/conductor/client/model/TargetRef.java @@ -25,53 +25,9 @@ /** The object over which access is being granted or removed */ @Schema(description = "The object over which access is being granted or removed") public class TargetRef { - /** Gets or Sets id */ - @JsonAdapter(IdEnum.Adapter.class) - public enum IdEnum { - IDENTIFIER_OF_THE_TARGET_E_G_NAME_IN_CASE_IT_S_A_WORKFLOW_DEF( - "Identifier of the target e.g. `name` in case it's a WORKFLOW_DEF"); - - private String value; - - IdEnum(String value) { - this.value = value; - } - - public String getValue() { - return value; - } - - @Override - public String toString() { - return String.valueOf(value); - } - - public static IdEnum fromValue(String input) { - for (IdEnum b : IdEnum.values()) { - if (b.value.equals(input)) { - return b; - } - } - return null; - } - - public static class Adapter extends TypeAdapter { - @Override - public void write(final JsonWriter jsonWriter, final IdEnum enumeration) - throws IOException { - jsonWriter.value(String.valueOf(enumeration.getValue())); - } - - @Override - public IdEnum read(final JsonReader jsonReader) throws IOException { - Object value = jsonReader.nextString(); - return IdEnum.fromValue((String) (value)); - } - } - } @SerializedName("id") - private IdEnum id = null; + private String id = null; /** Gets or Sets type */ @JsonAdapter(TypeEnum.Adapter.class) @@ -79,6 +35,7 @@ public enum TypeEnum { WORKFLOW_DEF("WORKFLOW_DEF"), TASK_DEF("TASK_DEF"), APPLICATION("APPLICATION"), + TAG("TAG"), USER("USER"); private String value; @@ -123,7 +80,7 @@ public TypeEnum read(final JsonReader jsonReader) throws IOException { @SerializedName("type") private TypeEnum type = null; - public TargetRef id(IdEnum id) { + public TargetRef id(String id) { this.id = id; return this; } @@ -134,11 +91,11 @@ public TargetRef id(IdEnum id) { * @return id */ @Schema(required = true, description = "") - public IdEnum getId() { + public String getId() { return id; } - public void setId(IdEnum id) { + public void setId(String id) { this.id = id; } diff --git a/src/test/java/io/orkes/conductor/client/api/AuthorizationClientTests.java b/src/test/java/io/orkes/conductor/client/api/AuthorizationClientTests.java index c68c6d27..4cfc09d7 100644 --- a/src/test/java/io/orkes/conductor/client/api/AuthorizationClientTests.java +++ b/src/test/java/io/orkes/conductor/client/api/AuthorizationClientTests.java @@ -12,20 +12,17 @@ */ package io.orkes.conductor.client.api; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; +import java.util.*; +import io.orkes.conductor.client.model.*; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import io.orkes.conductor.client.AuthorizationClient; -import io.orkes.conductor.client.model.Group; -import io.orkes.conductor.client.model.UpsertGroupRequest; import io.orkes.conductor.client.model.UpsertGroupRequest.RolesEnum; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; public class AuthorizationClientTests extends ClientTest { private final AuthorizationClient authorizationClient; @@ -42,6 +39,127 @@ public void autoAssignWorkflowPermissions() { validateGroupPermissions(group.getId()); } + @Test + void testAddUser() { + UpsertUserRequest request = new UpsertUserRequest(); + request.setName("Orkes User"); + request.setGroups(Arrays.asList("Example Group")); + request.setRoles(Arrays.asList(UpsertUserRequest.RolesEnum.USER)); + String userId = "user@orkes.io"; //MUST be the email addressed used to login to Conductor + ConductorUser user = authorizationClient.upsertUser(request, userId); + assertNotNull(user); + + + ConductorUser found = authorizationClient.getUser(userId); + assertNotNull(found); + assertEquals(user.getName(), found.getName()); + assertEquals(user.getGroups().get(0).getId(), found.getGroups().get(0).getId()); + assertEquals(user.getRoles().get(0).getName(), found.getRoles().get(0).getName()); + + } + + @Test + void testAddGroup() { + UpsertGroupRequest request = new UpsertGroupRequest(); + + //Default Access for the group. When specified, any new workflow or task created by the members of this group + //get this default permission inside the group. + Map> defaultAccess = new HashMap<>(); + + //Grant READ access to the members of the group for any new workflow created by a member of this group + defaultAccess.put("WORKFLOW_DEF", List.of("READ")); + + //Grant EXECUTE access to the members of the group for any new task created by a member of this group + defaultAccess.put("TASK_DEF", List.of("EXECUTE")); + request.setDefaultAccess(defaultAccess); + + request.setDescription("Example group created for testing"); + request.setRoles(Arrays.asList(UpsertGroupRequest.RolesEnum.USER)); + + String groupId = "Test Group"; + Group group = authorizationClient.upsertGroup(request, groupId); + assertNotNull(group); + + + Group found = authorizationClient.getGroup(groupId); + assertNotNull(found); + assertEquals(group.getId(), found.getId()); + assertEquals(group.getDefaultAccess().keySet(), found.getDefaultAccess().keySet()); + + } + + + @Test + void testAddApplication() { + + CreateOrUpdateApplicationRequest request = new CreateOrUpdateApplicationRequest(); + request.setName("Test Application for the testing"); + + //WARNING: Application Name is not a UNIQUE value and if called multiple times, it will create a new application + ConductorApplication application = authorizationClient.createApplication(request); + assertNotNull(application); + assertNotNull(application.getId()); + + + //Get the list of applications + List apps = authorizationClient.listApplications(); + assertNotNull(apps); + long found = apps.stream().map(ConductorApplication::getId).filter(id -> id.equals(application.getId())).count(); + assertEquals(1, found); + + //Create new access key + CreateAccessKeyResponse accessKey = authorizationClient.createAccessKey(application.getId()); + assertNotNull(accessKey.getId()); + assertNotNull(accessKey.getSecret()); + System.out.println(accessKey.getId() + ":" + accessKey.getSecret()); + + authorizationClient.deleteApplication(application.getId()); + } + + @Test + void testGrangPermissionsToGroup() { + + + AuthorizationRequest request = new AuthorizationRequest(); + request.access(Arrays.asList(AuthorizationRequest.AccessEnum.READ)); + SubjectRef subject = new SubjectRef(); + subject.setId("Example Group"); + subject.setType(SubjectRef.TypeEnum.GROUP); + request.setSubject(subject); + TargetRef target = new TargetRef(); + target.setId("Test_032"); + target.setType(TargetRef.TypeEnum.WORKFLOW_DEF); + request.setTarget(target); + authorizationClient.grantPermissions(request); + + + } + + @Test + void testGrantPermissionsToTag() { + + + AuthorizationRequest request = new AuthorizationRequest(); + request.access(Arrays.asList(AuthorizationRequest.AccessEnum.READ)); + + + SubjectRef subject = new SubjectRef(); + subject.setId("Example Group"); + subject.setType(SubjectRef.TypeEnum.GROUP); + + request.setSubject(subject); + + //Grant permissions to the tag with accounting org + TargetRef target = new TargetRef(); + target.setId("org:accounting"); + target.setType(TargetRef.TypeEnum.TAG); + + request.setTarget(target); + authorizationClient.grantPermissions(request); + + + } + void giveApplicationPermissions(String applicationId) { authorizationClient.addRoleToApplicationUser(applicationId, "ADMIN"); }