Skip to content

Commit

Permalink
Merge pull request #22 from orkes-io/sdk_fixes
Browse files Browse the repository at this point in the history
Fix for Tags
  • Loading branch information
manan164 authored Sep 13, 2022
2 parents 6f9e643 + b3eb0aa commit 720c56e
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public class SubjectRef {
public enum TypeEnum {
USER("USER"),
ROLE("ROLE"),
GROUP("GROUP");
GROUP("GROUP"),
TAG("TAG");

private String value;

Expand Down
53 changes: 5 additions & 48 deletions src/main/java/io/orkes/conductor/client/model/TargetRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,60 +25,17 @@
/** 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<IdEnum> {
@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)
public enum TypeEnum {
WORKFLOW_DEF("WORKFLOW_DEF"),
TASK_DEF("TASK_DEF"),
APPLICATION("APPLICATION"),
TAG("TAG"),
USER("USER");

private String value;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 = "[email protected]"; //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<String, List<String>> 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<ConductorApplication> 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");
}
Expand Down

0 comments on commit 720c56e

Please sign in to comment.