Skip to content

Commit

Permalink
Merge pull request #4 from openstandia/dev
Browse files Browse the repository at this point in the history
feat: support custom user attributes
  • Loading branch information
wadahiro authored Feb 1, 2021
2 parents 9529186 + 7e5a955 commit bfd5e05
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ public interface GuacamoleClient {

String getAuthToken();

List<GuacamoleSchemaRepresentation> schema();


default String getSchemaEndpointURL(GuacamoleConfiguration configuration) {
String url = configuration.getGuacamoleURL();
return String.format("%ssession/data/%s/schema/userAttributes", url, configuration.getGuacamoleDataSource());
}

default String getUserEndpointURL(GuacamoleConfiguration configuration) {
String url = configuration.getGuacamoleURL();
Expand Down Expand Up @@ -339,6 +346,18 @@ public List<GuacamoleAttribute> toGuacamoleAttributes() {
}
}

@JsonIgnoreProperties(ignoreUnknown = true)
class GuacamoleSchemaRepresentation {
public String name;
public List<GuacamoleSchemaFieldRepresentation> fields;
}

@JsonIgnoreProperties(ignoreUnknown = true)
class GuacamoleSchemaFieldRepresentation {
public String name;
public String type;
}

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
class GuacamoleUserGroupRepresentation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public class GuacamoleConnectionGroupHandler implements GuacamoleObjectHandler {
private final GuacamoleSchema schema;
private final GuacamoleAssociationHandler associationHandler;

public GuacamoleConnectionGroupHandler(GuacamoleConfiguration configuration, GuacamoleClient client) {
public GuacamoleConnectionGroupHandler(GuacamoleConfiguration configuration, GuacamoleClient client, GuacamoleSchema schema) {
this.configuration = configuration;
this.client = client;
this.schema = new GuacamoleSchema(configuration, client);
this.schema = schema;
this.associationHandler = new GuacamoleAssociationHandler(configuration, client);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ public class GuacamoleConnectionHandler implements GuacamoleObjectHandler {
private final GuacamoleSchema schema;
private final GuacamoleAssociationHandler associationHandler;

public GuacamoleConnectionHandler(GuacamoleConfiguration configuration, GuacamoleClient client) {
public GuacamoleConnectionHandler(GuacamoleConfiguration configuration, GuacamoleClient client, GuacamoleSchema schema) {
this.configuration = configuration;
this.client = client;
this.schema = new GuacamoleSchema(configuration, client);
this.schema = schema;
this.associationHandler = new GuacamoleAssociationHandler(configuration, client);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

Expand All @@ -53,7 +51,7 @@ public class GuacamoleConnector implements PoolableConnector, CreateOp, UpdateDe
protected GuacamoleConfiguration configuration;
protected GuacamoleClient client;

private Map<String, AttributeInfo> userSchemaMap;
private GuacamoleSchema cachedSchema;
private String instanceName;

@Override
Expand Down Expand Up @@ -121,42 +119,21 @@ public Response intercept(Chain chain) throws IOException {
@Override
public Schema schema() {
try {
SchemaBuilder schemaBuilder = new SchemaBuilder(GuacamoleConnector.class);

ObjectClassInfo userSchemaInfo = GuacamoleUserHandler.createSchema();
schemaBuilder.defineObjectClass(userSchemaInfo);

ObjectClassInfo userGroupSchemaInfo = GuacamoleUserGroupHandler.createSchema();
schemaBuilder.defineObjectClass(userGroupSchemaInfo);

ObjectClassInfo connectionSchemaInfo = GuacamoleConnectionHandler.createSchema();
schemaBuilder.defineObjectClass(connectionSchemaInfo);

ObjectClassInfo connectionGroupSchemaInfo = GuacamoleConnectionGroupHandler.createSchema();
schemaBuilder.defineObjectClass(connectionGroupSchemaInfo);

schemaBuilder.defineOperationOption(OperationOptionInfoBuilder.buildAttributesToGet(), SearchOp.class);
schemaBuilder.defineOperationOption(OperationOptionInfoBuilder.buildReturnDefaultAttributes(), SearchOp.class);

userSchemaMap = new HashMap<>();
userSchemaInfo.getAttributeInfo().stream()
.forEach(a -> userSchemaMap.put(a.getName(), a));
userSchemaMap.put(Uid.NAME, AttributeInfoBuilder.define("username").build());
userSchemaMap = Collections.unmodifiableMap(userSchemaMap);

return schemaBuilder.build();
List<GuacamoleClient.GuacamoleSchemaRepresentation> guacamoleSchema = this.client.schema();
cachedSchema = new GuacamoleSchema(configuration, client, guacamoleSchema);
return cachedSchema.schema;

} catch (RuntimeException e) {
throw processRuntimeException(e);
}
}

private Map<String, AttributeInfo> getUserSchemaMap() {
private GuacamoleSchema geSchema() {
// Load schema map if it's not loaded yet
if (userSchemaMap == null) {
if (cachedSchema == null) {
schema();
}
return userSchemaMap;
return cachedSchema;
}

protected GuacamoleObjectHandler createGuacamoleObjectHandler(ObjectClass objectClass) {
Expand All @@ -165,16 +142,16 @@ protected GuacamoleObjectHandler createGuacamoleObjectHandler(ObjectClass object
}

if (objectClass.equals(USER_OBJECT_CLASS)) {
return new GuacamoleUserHandler(configuration, client, getUserSchemaMap());
return new GuacamoleUserHandler(configuration, client, geSchema());

} else if (objectClass.equals(USER_GROUP_OBJECT_CLASS)) {
return new GuacamoleUserGroupHandler(configuration, client);
return new GuacamoleUserGroupHandler(configuration, client, geSchema());

} else if (objectClass.equals(CONNECTION_OBJECT_CLASS)) {
return new GuacamoleConnectionHandler(configuration, client);
return new GuacamoleConnectionHandler(configuration, client, geSchema());

} else if (objectClass.equals(CONNECTION_GROUP_OBJECT_CLASS)) {
return new GuacamoleConnectionGroupHandler(configuration, client);
return new GuacamoleConnectionGroupHandler(configuration, client, geSchema());

} else {
throw new InvalidAttributeValueException("Unsupported object class " + objectClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

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

/**
Expand All @@ -23,13 +24,14 @@ public class GuacamoleSchema {
public final Map<String, AttributeInfo> connectionSchema;
public final Map<String, AttributeInfo> connectionGroupSchema;

public GuacamoleSchema(GuacamoleConfiguration configuration, GuacamoleClient client) {
public GuacamoleSchema(GuacamoleConfiguration configuration, GuacamoleClient client,
List<GuacamoleClient.GuacamoleSchemaRepresentation> guacamoleSchema) {
this.configuration = configuration;
this.client = client;

SchemaBuilder schemaBuilder = new SchemaBuilder(GuacamoleConnector.class);

ObjectClassInfo userSchemaInfo = GuacamoleUserHandler.createSchema();
ObjectClassInfo userSchemaInfo = GuacamoleUserHandler.createSchema(guacamoleSchema);
schemaBuilder.defineObjectClass(userSchemaInfo);

ObjectClassInfo groupSchemaInfo = GuacamoleUserGroupHandler.createSchema();
Expand All @@ -44,7 +46,7 @@ public GuacamoleSchema(GuacamoleConfiguration configuration, GuacamoleClient cli
schemaBuilder.defineOperationOption(OperationOptionInfoBuilder.buildAttributesToGet(), SearchOp.class);
schemaBuilder.defineOperationOption(OperationOptionInfoBuilder.buildReturnDefaultAttributes(), SearchOp.class);

schema = schemaBuilder.build();
this.schema = schemaBuilder.build();

Map<String, AttributeInfo> userSchemaMap = new HashMap<>();
for (AttributeInfo info : userSchemaInfo.getAttributeInfo()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ public class GuacamoleUserGroupHandler implements GuacamoleObjectHandler {
private final GuacamoleSchema schema;
private final GuacamoleAssociationHandler associationHandler;

public GuacamoleUserGroupHandler(GuacamoleConfiguration configuration, GuacamoleClient client) {
public GuacamoleUserGroupHandler(GuacamoleConfiguration configuration, GuacamoleClient client, GuacamoleSchema schema) {
this.configuration = configuration;
this.client = client;
this.schema = new GuacamoleSchema(configuration, client);
this.schema = schema;
this.associationHandler = new GuacamoleAssociationHandler(configuration, client);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
package jp.openstandia.connector.guacamole;

import org.identityconnectors.common.logging.Log;
import org.identityconnectors.common.security.GuardedString;
import org.identityconnectors.framework.common.exceptions.UnknownUidException;
import org.identityconnectors.framework.common.objects.*;

import java.time.ZonedDateTime;
import java.util.*;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -38,16 +42,7 @@ public class GuacamoleUserHandler implements GuacamoleObjectHandler {
static final String ATTR_USERNAME = "username";

// Attributes
static final String ATTR_EMAIL = "guac-email-address";
static final String ATTR_ORGANIZATIONAL_ROLE = "guac-organizational-role";
static final String ATTR_ORGANIZATION = "guac-organization";
static final String ATTR_FULL_NAME = "guac-full-name";
static final String ATTR_PASSWORD_EXPIRED = "expired";
static final String ATTR_TIMEZONE = "timezone";
static final String ATTR_ACCESS_WINDOW_START = "access-window-start";
static final String ATTR_ACCESS_WINDOW_END = "access-window-end";
static final String ATTR_VALID_UNTIL = "valid-until";
static final String ATTR_VALID_FROM = "valid-from";
// Don't define here since guacamole provides user's schema endpoint

// Permissions
private static final String ATTR_USER_PERMISSIONS = "userPermissions";
Expand All @@ -67,14 +62,14 @@ public class GuacamoleUserHandler implements GuacamoleObjectHandler {
private final GuacamoleSchema schema;

public GuacamoleUserHandler(GuacamoleConfiguration configuration, GuacamoleClient client,
Map<String, AttributeInfo> schema) {
GuacamoleSchema schema) {
this.configuration = configuration;
this.client = client;
this.schema = new GuacamoleSchema(configuration, client);
this.schema = schema;
this.associationHandler = new GuacamoleAssociationHandler(configuration, client);
}

public static ObjectClassInfo createSchema() {
public static ObjectClassInfo createSchema(List<GuacamoleClient.GuacamoleSchemaRepresentation> schema) {
ObjectClassInfoBuilder builder = new ObjectClassInfoBuilder();
builder.setType(USER_OBJECT_CLASS.getObjectClassValue());

Expand Down Expand Up @@ -102,86 +97,63 @@ public static ObjectClassInfo createSchema() {
builder.addAttributeInfo(OperationalAttributeInfos.PASSWORD);

// Other attributes
builder.addAttributeInfo(
AttributeInfoBuilder.define(ATTR_FULL_NAME)
.setRequired(false) // Must be optional
.setCreateable(true)
.setUpdateable(true)
.build()
);
builder.addAttributeInfo(
AttributeInfoBuilder.define(ATTR_EMAIL)
.setRequired(false) // Must be optional
.setCreateable(true)
.setUpdateable(true)
.build()
);
builder.addAttributeInfo(
AttributeInfoBuilder.define(ATTR_ORGANIZATION)
.setRequired(false) // Must be optional
.setCreateable(true)
.setUpdateable(true)
.build()
);
builder.addAttributeInfo(
AttributeInfoBuilder.define(ATTR_ORGANIZATIONAL_ROLE)
.setRequired(false) // Must be optional
.setCreateable(true)
.setUpdateable(true)
.build()
);
builder.addAttributeInfo(
AttributeInfoBuilder.define(ATTR_PASSWORD_EXPIRED)
.setRequired(false) // Must be optional
.setCreateable(true)
.setUpdateable(true)
.setType(Boolean.class)
.build()
);
builder.addAttributeInfo(
AttributeInfoBuilder.define(ATTR_TIMEZONE)
.setRequired(false) // Must be optional
.setCreateable(true)
.setUpdateable(true)
.build()
);
builder.addAttributeInfo(
AttributeInfoBuilder.define(ATTR_ACCESS_WINDOW_START)
.setRequired(false) // Must be optional
.setCreateable(true)
.setUpdateable(true)
.build()
);
builder.addAttributeInfo(
AttributeInfoBuilder.define(ATTR_ACCESS_WINDOW_END)
.setRequired(false) // Must be optional
.setCreateable(true)
.setUpdateable(true)
.build()
);
// builder.addAttributeInfo(
// AttributeInfoBuilder.define(ATTR_DISABLED)
// .setRequired(false) // Must be optional
// .setCreateable(true)
// .setUpdateable(true)
// .build()
// );
builder.addAttributeInfo(
AttributeInfoBuilder.define(ATTR_VALID_UNTIL)
.setRequired(false) // Must be optional
.setCreateable(true)
.setUpdateable(true)
.setType(ZonedDateTime.class)
.build()
);
builder.addAttributeInfo(
AttributeInfoBuilder.define(ATTR_VALID_FROM)
.setRequired(false) // Must be optional
.setCreateable(true)
.setUpdateable(true)
.setType(ZonedDateTime.class)
.build()
);
// Generate from fetched guacamole schema
schema.forEach(s -> {
s.fields.stream()
.filter(f -> !(s.name.equals("restrictions") && f.name.equals(ATTR_DISABLED)))
.forEach(f -> {
switch (f.type) {
case "NUMERIC":
builder.addAttributeInfo(
AttributeInfoBuilder.define(f.name)
.setRequired(false) // Must be optional
.setCreateable(true)
.setUpdateable(true)
.setType(Integer.class) // Guacamole NumericFiled is defined with Integer
.build());
break;
case "BOOLEAN":
builder.addAttributeInfo(
AttributeInfoBuilder.define(f.name)
.setRequired(false) // Must be optional
.setCreateable(true)
.setUpdateable(true)
.setType(Boolean.class)
.build());
break;
case "DATE":
builder.addAttributeInfo(
AttributeInfoBuilder.define(f.name)
.setRequired(false) // Must be optional
.setCreateable(true)
.setUpdateable(true)
.setType(ZonedDateTime.class)
.build());
break;
case "PASSWORD":
builder.addAttributeInfo(
AttributeInfoBuilder.define(f.name)
.setRequired(false) // Must be optional
.setCreateable(true)
.setUpdateable(true)
.setType(GuardedString.class)
.build());
break;
case "TEXT":
case "EMAIL":
case "TIMEZONE":
case "TIME":
default: // Define Other types as string
builder.addAttributeInfo(
AttributeInfoBuilder.define(f.name)
.setRequired(false) // Must be optional
.setCreateable(true)
.setUpdateable(true)
.build());
break;
}
});
});

// Permissions
builder.addAttributeInfo(
Expand Down
Loading

0 comments on commit bfd5e05

Please sign in to comment.