From 8320905ab51fc6f2b00d8cbcb18f49f8346b356d Mon Sep 17 00:00:00 2001 From: Abba Soungui YOUNOUSS Date: Sun, 9 Nov 2025 14:14:40 +0100 Subject: [PATCH] Add connection and associated parameters registration in Guacamole. --- .../guacamole/ext/wallix/sync/Wallix.java | 7 +- .../wallix/sync/WallixConfigSynchronizer.java | 42 ++++--- .../ext/wallix/sync/db/Connection.java | 43 ++++++++ .../guacamole/ext/wallix/sync/db/DB.java | 103 ++++++++++++++++++ 4 files changed, 177 insertions(+), 18 deletions(-) diff --git a/src/main/java/cm/soungui/guacamole/ext/wallix/sync/Wallix.java b/src/main/java/cm/soungui/guacamole/ext/wallix/sync/Wallix.java index 2675f1b..bed8006 100644 --- a/src/main/java/cm/soungui/guacamole/ext/wallix/sync/Wallix.java +++ b/src/main/java/cm/soungui/guacamole/ext/wallix/sync/Wallix.java @@ -8,6 +8,7 @@ import java.net.http.HttpResponse; import java.security.SecureRandom; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -222,8 +223,10 @@ public class Wallix { 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); + HashMap parameters = connection.getParameters(); + parameters.put(Connection.Parameter.USERNAME, getFormattedUsername(accountNode, group.getName(), usernamePrefix)); + parameters.put(Connection.Parameter.HOSTNAME, connectionHost); + connection.setName(parameters.get(Connection.Parameter.USERNAME).replace(":" + usernamePrefix + "${TOKEN_USERNAME}", "@") + parameters.get(Connection.Parameter.HOSTNAME)); group.getConnections().add(connection); } }); diff --git a/src/main/java/cm/soungui/guacamole/ext/wallix/sync/WallixConfigSynchronizer.java b/src/main/java/cm/soungui/guacamole/ext/wallix/sync/WallixConfigSynchronizer.java index f2b7261..6012c35 100644 --- a/src/main/java/cm/soungui/guacamole/ext/wallix/sync/WallixConfigSynchronizer.java +++ b/src/main/java/cm/soungui/guacamole/ext/wallix/sync/WallixConfigSynchronizer.java @@ -1,11 +1,13 @@ package cm.soungui.guacamole.ext.wallix.sync; import java.sql.SQLException; +import java.util.HashMap; import java.util.Set; import org.apache.guacamole.GuacamoleException; import org.apache.guacamole.environment.LocalEnvironment; +import cm.soungui.guacamole.ext.wallix.sync.db.Connection; 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; @@ -117,27 +119,35 @@ public class WallixConfigSynchronizer implements Runnable { System.out.println("Getting Wallix target groups"); DB db = DB.getInstance(); - Set dbGroups = db.getTargetGroups(); + Set dbTargetGroups = db.getTargetGroups(); Set wallixTargetGroups = Wallix.getInstance().getTargetGroups(); - 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 newDbGroups = db.getTargetGroups(); - - for (TargetGroup group : newDbGroups) { - if (wallixTargetGroups.contains(group)) { - for (TargetGroup wallixTargetGroup : wallixTargetGroups) { - if (group.equals(wallixTargetGroup)) { -// db.updateGroupMembers(group, wallixGroup.getMembers()); + for (TargetGroup wallixGroup : wallixTargetGroups) { + if (dbTargetGroups.contains(wallixGroup)) { + Set dbConnections = db.getConnections(wallixGroup); + for (Connection connection : wallixGroup.getConnections()) { + if (! dbConnections.contains(connection) && connection.getName() != null) { + System.out.println("Adding connection '" + connection.getName() + "' to group " + wallixGroup.getName()); + db.addConnection(connection, wallixGroup.getName()); } } + } else { + System.out.println("Adding target group " + wallixGroup.getName()); + db.addTargetGroup(wallixGroup); + } + } + + dbTargetGroups = db.getTargetGroups(); + + for (TargetGroup group : dbTargetGroups) { + if (wallixTargetGroups.contains(group)) { + for (Connection connection : group.getConnections()) { + HashMap dbParameters = db.getConnectionParameters(connection.getName(), connection.getProtocol()); +// HashMap wallixParameters = group. + for (String parameterName : dbParameters.keySet()) { + } + } } else { System.out.println("Deleting target group : " + group.getName()); db.deleteTargetGroup(group); diff --git a/src/main/java/cm/soungui/guacamole/ext/wallix/sync/db/Connection.java b/src/main/java/cm/soungui/guacamole/ext/wallix/sync/db/Connection.java index 109b5e8..5714bd3 100644 --- a/src/main/java/cm/soungui/guacamole/ext/wallix/sync/db/Connection.java +++ b/src/main/java/cm/soungui/guacamole/ext/wallix/sync/db/Connection.java @@ -11,6 +11,26 @@ public class Connection { private TargetGroup group; private final HashMap parameters = new HashMap<>(); + + private int id; + + private String name; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } public Protocol getProtocol() { return protocol; @@ -36,5 +56,28 @@ public class Connection { String USERNAME = "username"; String HOSTNAME = "hostname"; } + + @Override + public int hashCode() { + return 0; + } + + @Override + public boolean equals(Object obj) { + if (obj == null || ! (obj instanceof Connection) || getName() == null || getProtocol() == null) { + return false; + } + Connection connection = (Connection) obj; + return getProtocol() == connection.getProtocol() && getName().equals(((Connection) obj).getName()); + } + + @Override + public String toString() { + StringBuffer output = new StringBuffer("{id: ").append(getId()).append(", name: ").append(getName()).append(", protocol: ").append(getProtocol()) + .append(", parameters : {"); + getParameters().keySet().forEach(parameter -> output.append(parameter).append(":").append(getParameters().get(parameter)).append(", ")); + output.append("} }"); + return output.toString(); + } } diff --git a/src/main/java/cm/soungui/guacamole/ext/wallix/sync/db/DB.java b/src/main/java/cm/soungui/guacamole/ext/wallix/sync/db/DB.java index e4e6a1f..376724b 100644 --- a/src/main/java/cm/soungui/guacamole/ext/wallix/sync/db/DB.java +++ b/src/main/java/cm/soungui/guacamole/ext/wallix/sync/db/DB.java @@ -6,6 +6,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.util.HashMap; import java.util.HashSet; import java.util.Set; @@ -14,6 +15,7 @@ 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.db.Connection.Protocol; import cm.soungui.guacamole.ext.wallix.sync.db.Entity.EntityType; public class DB { @@ -400,6 +402,70 @@ public class DB { return groups; } + + public int getTargetGroupsId(String name) throws GuacamoleException, SQLException { + if (name == null || name.isBlank()) { + throw new IllegalArgumentException(); + } + DB db = DB.getInstance(); + Connection connection = db.getMySQLConnection(); + PreparedStatement stmt = connection + .prepareStatement("select g.connection_group_id from guacamole_connection_group g where type='ORGANIZATIONAL' and connection_group_name=?"); + stmt.setString(1, name); + + ResultSet rs = stmt.executeQuery(); + int result = -1; + while (rs.next()) { + result = rs.getInt("connection_group_id"); + } + + return result; + } + + public Set getConnections(TargetGroup group) throws GuacamoleException, SQLException { + DB db = DB.getInstance(); + Connection dbConnection = db.getMySQLConnection(); + ResultSet rs = dbConnection + .prepareStatement("select g.connection_group_id,g.connection_group_name,c.connection_id,c.connection_name,c.protocol from guacamole_connection_group g, guacamole_connection c where g.connection_group_id=c.parent_id") + .executeQuery(); + + PreparedStatement parameterStmt = dbConnection.prepareStatement("select parameter_name,parameter_value from guacamole_connection_parameter where connection_id=?"); + HashSet groups = new HashSet<>(); + while (rs.next()) { + cm.soungui.guacamole.ext.wallix.sync.db.Connection connection = new cm.soungui.guacamole.ext.wallix.sync.db.Connection(); + connection.setGroup(group); + int connectionId = rs.getInt("connection_id"); + connection.setId(connectionId); + connection.setName(rs.getString("connection_name")); + connection.setProtocol(Protocol.valueOf(rs.getString("protocol").toUpperCase())); + + parameterStmt.setInt(1, connectionId); + ResultSet parameterRs = parameterStmt.executeQuery(); + while (parameterRs.next()) { + connection.getParameters().put(parameterRs.getString("parameter_name"), parameterRs.getString("parameter_value")); + } + + groups.add(connection); + } + + return groups; + } + + public HashMap getConnectionParameters(String name, Protocol protocol) throws GuacamoleException, SQLException { + DB db = DB.getInstance(); + Connection dbConnection = db.getMySQLConnection(); + + PreparedStatement parameterStmt = dbConnection.prepareStatement("select parameter_name,parameter_value from guacamole_connection_parameter where connection_id=(select connection_id from guacamole_connection where connection_name=?)"); + parameterStmt.setString(1, name); + parameterStmt.setString(2, protocol.toString()); + HashMap result = new HashMap<>(); + ResultSet parameterRs = parameterStmt.executeQuery(); + while (parameterRs.next()) { + result.put(parameterRs.getString("parameter_name"), parameterRs.getString("parameter_value")); + } + + return result; + } public void deleteTargetGroup(TargetGroup group) throws SQLException, GuacamoleException { if (group == null || group.getName() == null) { @@ -413,4 +479,41 @@ public class DB { stmt.executeUpdate(); } + public void addConnection(cm.soungui.guacamole.ext.wallix.sync.db.Connection connection, String parentName) throws SQLException, GuacamoleException { + Connection dbConnection = DB.getInstance().getMySQLConnection(); + PreparedStatement stmt = dbConnection.prepareStatement("insert into guacamole_connection (connection_name, parent_id, protocol) values (?,?,?)", Statement.RETURN_GENERATED_KEYS); + stmt.setString(1, connection.getName()); + stmt.setInt(2, getTargetGroupsId(parentName)); + stmt.setString(3, connection.getProtocol().toString().toLowerCase()); + + try { + dbConnection.setAutoCommit(false); + + stmt.executeUpdate(); + ResultSet rs = stmt.getGeneratedKeys(); + while (rs.next()) { + int connectionId = rs.getInt(1); + stmt.close(); + + HashMap parameters = connection.getParameters(); + for (String parameterName : parameters.keySet()) { + stmt = DB.getInstance().getMySQLConnection().prepareStatement("insert into guacamole_connection_parameter (connection_id, parameter_name, parameter_value) values (?,?,?)"); + stmt.setInt(1, connectionId); + stmt.setString(2, parameterName); + stmt.setString(3, parameters.get(parameterName)); + stmt.executeUpdate(); + } + + dbConnection.commit(); + dbConnection.setAutoCommit(true); + } + stmt.close(); + } catch (Exception e) { + if (! dbConnection.isClosed() && ! dbConnection.getAutoCommit()) { + dbConnection.setAutoCommit(true); + } + throw e; + } + } + }