Add connection parameter fetching from Wallix.
This commit is contained in:
@@ -85,4 +85,20 @@ public class Configuration {
|
||||
return "wallix-auth-key";
|
||||
}
|
||||
};
|
||||
|
||||
public static StringGuacamoleProperty WALLIX_USERNAME_PREFIX = new StringGuacamoleProperty() {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "wallix-username-prefix";
|
||||
}
|
||||
};
|
||||
|
||||
public static StringGuacamoleProperty WALLIX_CONNECTION_HOST = new StringGuacamoleProperty() {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "wallix-connection-host";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -25,6 +25,8 @@ import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
|
||||
import cm.soungui.guacamole.ext.wallix.sync.db.Connection;
|
||||
import cm.soungui.guacamole.ext.wallix.sync.db.TargetGroup;
|
||||
import cm.soungui.guacamole.ext.wallix.sync.db.User;
|
||||
import cm.soungui.guacamole.ext.wallix.sync.db.UserGroup;
|
||||
|
||||
@@ -41,12 +43,18 @@ public class Wallix {
|
||||
private String user;
|
||||
|
||||
private String authKey;
|
||||
|
||||
private String usernamePrefix;
|
||||
|
||||
private String connectionHost;
|
||||
|
||||
private Wallix() throws GuacamoleException {
|
||||
Environment environment = LocalEnvironment.getInstance();
|
||||
url = environment.getRequiredProperty(Configuration.WALLIX_URL);
|
||||
user = environment.getRequiredProperty(Configuration.WALLIX_USER);
|
||||
authKey = environment.getRequiredProperty(Configuration.WALLIX_AUTH_KEY);
|
||||
usernamePrefix = environment.getRequiredProperty(Configuration.WALLIX_USERNAME_PREFIX);
|
||||
connectionHost = environment.getRequiredProperty(Configuration.WALLIX_CONNECTION_HOST);
|
||||
}
|
||||
|
||||
TrustManager[] trustAllCerts = new X509ExtendedTrustManager[]{
|
||||
@@ -136,9 +144,9 @@ public class Wallix {
|
||||
ObjectNode node = (ObjectNode) e;
|
||||
|
||||
UserGroup group = new UserGroup();
|
||||
group.setName(node.findValue(Wallix.API.ATTRIBUTE_GROUP_NAME).asText());
|
||||
group.setName(node.findValue(Wallix.API.ATTRIBUTE_USER_GROUP_NAME).asText());
|
||||
|
||||
List<JsonNode> membersList = node.findValues(Wallix.API.ATTRIBUTE_GROUP_USERS);
|
||||
List<JsonNode> membersList = node.findValues(Wallix.API.ATTRIBUTE_USER_GROUP_USERS);
|
||||
if (! membersList.isEmpty()) {
|
||||
JsonNode usernames = membersList.get(0);
|
||||
usernames.forEach(userNode -> {
|
||||
@@ -188,28 +196,39 @@ public class Wallix {
|
||||
}
|
||||
}
|
||||
|
||||
public Set<TargetGroup> getTargetGroups() throws Exception {
|
||||
public Set<TargetGroup> getTargetGroups() throws Exception {
|
||||
String output = get("/targetgroups?fields=group_name,session");
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
JsonNode jsonNode = objectMapper.readTree(output);
|
||||
Set<TargetGroup> list = new HashSet<>();
|
||||
|
||||
|
||||
String usernamePrefix = Wallix.getInstance().usernamePrefix;
|
||||
String connectionHost = Wallix.getInstance().connectionHost;
|
||||
|
||||
jsonNode.elements().forEachRemaining((e) -> {
|
||||
ObjectNode node = (ObjectNode) e;
|
||||
|
||||
TargetGroup group = new TargetGroup();
|
||||
group.setName(node.findValue(Wallix.API.ATTRIBUTE_GROUP_NAME).asText());
|
||||
group.setName(node.findValue(Wallix.API.ATTRIBUTE_CONNECTION_GROUP_NAME).asText());
|
||||
|
||||
// List<JsonNode> membersList = node.findValues(Wallix.API.ATTRIBUTE_GROUP_USERS);
|
||||
// if (! membersList.isEmpty()) {
|
||||
// JsonNode usernames = membersList.get(0);
|
||||
// usernames.forEach(userNode -> {
|
||||
// User user = new User();
|
||||
// user.setName(getNameWithoutDomain(userNode.textValue()));
|
||||
// group.getMembers().add(user);
|
||||
// });
|
||||
// }
|
||||
List<JsonNode> sessionsList = node.findValues(Wallix.API.ATTRIBUTE_CONNECTION_GROUP_SESSIONS);
|
||||
if (! sessionsList.isEmpty()) {
|
||||
JsonNode sessions = sessionsList.get(0);
|
||||
sessions = sessions.findValue("accounts");
|
||||
if (! sessions.isEmpty()) {
|
||||
sessions.forEach(accountNode -> {
|
||||
if (! accountNode.isEmpty()) {
|
||||
Connection connection = new Connection();
|
||||
connection.setGroup(group);
|
||||
connection.setProtocol(Connection.Protocol.valueOf(accountNode.findValue("service").textValue()));
|
||||
connection.getParameters().put(Connection.Parameter.USERNAME, getFormattedUsername(accountNode, group.getName(), usernamePrefix));
|
||||
connection.getParameters().put(Connection.Parameter.HOSTNAME, connectionHost);
|
||||
group.getConnections().add(connection);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
list.add(group);
|
||||
});
|
||||
@@ -217,6 +236,21 @@ public class Wallix {
|
||||
return list;
|
||||
}
|
||||
|
||||
public static String getFormattedUsername(JsonNode accountNode, String targetGroupName, String usernamePrefix) {
|
||||
if (accountNode == null || targetGroupName == null || targetGroupName.isBlank()) {
|
||||
throw new IllegalArgumentException("Null object or empty string provided as argument");
|
||||
}
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
buffer.append(accountNode.findValue("account").asText());
|
||||
buffer.append("@").append(accountNode.findValue("domain").asText());
|
||||
buffer.append("@").append(accountNode.findValue("device").asText());
|
||||
buffer.append(":").append(accountNode.findValue("service").asText());
|
||||
buffer.append(":").append(targetGroupName);
|
||||
buffer.append(":").append(usernamePrefix == null ? "" : usernamePrefix).append("${TOKEN_USERNAME}");
|
||||
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public final class API {
|
||||
|
||||
public static final String ATTRIBUTE_USER_DISABLED = "is_disabled";
|
||||
@@ -225,9 +259,13 @@ public class Wallix {
|
||||
|
||||
public static final String ATTRIBUTE_USER_NAME = "user_name";
|
||||
|
||||
public static final String ATTRIBUTE_GROUP_NAME = "group_name";
|
||||
public static final String ATTRIBUTE_USER_GROUP_NAME = "group_name";
|
||||
|
||||
public static final String ATTRIBUTE_GROUP_USERS = "users";
|
||||
public static final String ATTRIBUTE_USER_GROUP_USERS = "users";
|
||||
|
||||
public static final String ATTRIBUTE_CONNECTION_GROUP_NAME = "group_name";
|
||||
|
||||
public static final String ATTRIBUTE_CONNECTION_GROUP_SESSIONS = "session";
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import org.apache.guacamole.GuacamoleException;
|
||||
import org.apache.guacamole.environment.LocalEnvironment;
|
||||
|
||||
import cm.soungui.guacamole.ext.wallix.sync.db.DB;
|
||||
import cm.soungui.guacamole.ext.wallix.sync.db.TargetGroup;
|
||||
import cm.soungui.guacamole.ext.wallix.sync.db.User;
|
||||
import cm.soungui.guacamole.ext.wallix.sync.db.UserGroup;
|
||||
|
||||
@@ -118,21 +119,22 @@ public class WallixConfigSynchronizer implements Runnable {
|
||||
DB db = DB.getInstance();
|
||||
Set<TargetGroup> dbGroups = db.getTargetGroups();
|
||||
|
||||
Set<TargetGroup> wallixGroups = Wallix.getInstance().getTargetGroups();
|
||||
Set<TargetGroup> wallixTargetGroups = Wallix.getInstance().getTargetGroups();
|
||||
|
||||
for (TargetGroup group : wallixGroups) {
|
||||
for (TargetGroup group : wallixTargetGroups) {
|
||||
if (! dbGroups.contains(group)) {
|
||||
System.out.println("Adding target group " + group.getName());
|
||||
db.addTargetGroup(group);
|
||||
}
|
||||
group.getConnections().forEach(connection -> System.out.println(connection.getParameters()));
|
||||
}
|
||||
|
||||
Set<TargetGroup> newDbGroups = db.getTargetGroups();
|
||||
|
||||
for (TargetGroup group : newDbGroups) {
|
||||
if (wallixGroups.contains(group)) {
|
||||
for (TargetGroup wallixGroup : wallixGroups) {
|
||||
if (group.equals(wallixGroup)) {
|
||||
if (wallixTargetGroups.contains(group)) {
|
||||
for (TargetGroup wallixTargetGroup : wallixTargetGroups) {
|
||||
if (group.equals(wallixTargetGroup)) {
|
||||
// db.updateGroupMembers(group, wallixGroup.getMembers());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package cm.soungui.guacamole.ext.wallix.sync.db;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Connection {
|
||||
|
||||
public enum Protocol { SSH, RDP, VNC };
|
||||
|
||||
private Protocol protocol;
|
||||
|
||||
private TargetGroup group;
|
||||
|
||||
private final HashMap<String, String> parameters = new HashMap<>();
|
||||
|
||||
public Protocol getProtocol() {
|
||||
return protocol;
|
||||
}
|
||||
|
||||
public void setProtocol(Protocol protocol) {
|
||||
this.protocol = protocol;
|
||||
}
|
||||
|
||||
public TargetGroup getGroup() {
|
||||
return group;
|
||||
}
|
||||
|
||||
public void setGroup(TargetGroup group) {
|
||||
this.group = group;
|
||||
}
|
||||
|
||||
public HashMap<String, String> getParameters() {
|
||||
return parameters;
|
||||
}
|
||||
|
||||
public static interface Parameter {
|
||||
String USERNAME = "username";
|
||||
String HOSTNAME = "hostname";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -14,7 +14,6 @@ import org.apache.guacamole.environment.Environment;
|
||||
import org.apache.guacamole.environment.LocalEnvironment;
|
||||
|
||||
import cm.soungui.guacamole.ext.wallix.sync.Configuration;
|
||||
import cm.soungui.guacamole.ext.wallix.sync.TargetGroup;
|
||||
import cm.soungui.guacamole.ext.wallix.sync.db.Entity.EntityType;
|
||||
|
||||
public class DB {
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
package cm.soungui.guacamole.ext.wallix.sync;
|
||||
package cm.soungui.guacamole.ext.wallix.sync.db;
|
||||
|
||||
import cm.soungui.guacamole.ext.wallix.sync.db.Entity;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class TargetGroup {
|
||||
|
||||
private int id;
|
||||
|
||||
private String name;
|
||||
|
||||
private final Set<Connection> connections = new HashSet<Connection>();
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
@@ -24,6 +27,10 @@ public class TargetGroup {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Set<Connection> getConnections() {
|
||||
return connections;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return getName() == null ? super.hashCode() : getName().hashCode();
|
||||
Reference in New Issue
Block a user