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 bed8006..85e24fc 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 @@ -226,7 +226,7 @@ public class Wallix { 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)); + connection.setName(parameters.get(Connection.Parameter.USERNAME).replace(":" + usernamePrefix + "${TOKEN_USERNAME}", "")); 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 6012c35..3d5b0b0 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 @@ -2,6 +2,8 @@ package cm.soungui.guacamole.ext.wallix.sync; import java.sql.SQLException; import java.util.HashMap; +import java.util.Map; +import java.util.Optional; import java.util.Set; import org.apache.guacamole.GuacamoleException; @@ -41,8 +43,6 @@ public class WallixConfigSynchronizer implements Runnable { } private void synchronizeUsers() throws Exception { - System.out.println("Synchronizing users"); - System.out.println("Getting Wallix groups"); Set wallixUsers = Wallix.getInstance().getUsers(); DB db = DB.getInstance(); wallixUsers.forEach(wallixUser -> { @@ -80,8 +80,6 @@ public class WallixConfigSynchronizer implements Runnable { } private void synchronizeUserGroups() throws Exception { - System.out.println("Synchronizing groups"); - System.out.println("Getting Wallix groups"); Set wallixGroups = Wallix.getInstance().getUserGroups(); DB db = DB.getInstance(); @@ -115,42 +113,68 @@ public class WallixConfigSynchronizer implements Runnable { } private void synchronizeTargetGroups() throws Exception { - System.out.println("Synchronizing target groups"); - System.out.println("Getting Wallix target groups"); - DB db = DB.getInstance(); - Set dbTargetGroups = db.getTargetGroups(); - + Set wallixTargetGroups = Wallix.getInstance().getTargetGroups(); + // Fetch targets from Wallix 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 { + TargetGroup dbTargetGroup = db.getTargetGroup(wallixGroup.getName()); + + // Target not found in Guacamole, so we have to create it. + if (dbTargetGroup == null) { System.out.println("Adding target group " + wallixGroup.getName()); db.addTargetGroup(wallixGroup); + + // We fetch the newly inserted target + dbTargetGroup = db.getTargetGroup(wallixGroup.getName()); } - } - - 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()) { + + // Processing of connections parameters fetched from Wallix + for (Connection wallixConnection : wallixGroup.getConnections()) { + Optional matchingConnections = dbTargetGroup.getConnections().stream() + .filter(c -> wallixConnection.equals(c)) + .findFirst(); + + if (matchingConnections.isEmpty()) { + // No matching connection found in database, so we need to add it + System.out.println("Adding connection '" + wallixConnection.getName() + "' to group " + wallixGroup.getName()); + db.addConnection(wallixConnection, wallixGroup.getName()); + } else { + // Matching connection found in database, we will update parameters if needed + Connection dbConnection = matchingConnections.get(); + HashMap wallixParameter = wallixConnection.getParameters(); + HashMap dbParameters = dbConnection.getParameters(); + Map toAdd = new HashMap(wallixParameter.size()); + Map toUpdate = new HashMap(wallixParameter.size()); + + // We detect changes + wallixConnection.getParameters().forEach((parameter, value) -> { + if (dbParameters.containsKey(parameter)) { + if (! value.equals(dbParameters.get(parameter))) { + toUpdate.put(parameter, value); + } + } else { + toAdd.put(parameter, value); } + }); + if (! toAdd.isEmpty()) { + db.addConnectionParameters(dbConnection.getId(), toAdd); } - } else { - System.out.println("Deleting target group : " + group.getName()); - db.deleteTargetGroup(group); + if (! toUpdate.isEmpty()) { + db.updateConnectionParameters(dbConnection.getId(), toUpdate); + } + + toUpdate.clear(); + dbParameters.forEach((parameter, value) -> { + if (! wallixParameter.containsKey(parameter)) { + toUpdate.put(parameter, null); + } + }); + if (! toUpdate.isEmpty()) { + db.deleteConnectionParameters(dbConnection.getId(), toUpdate.keySet()); + } + } } } 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 5714bd3..7c092e5 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 @@ -64,19 +64,22 @@ public class Connection { @Override public boolean equals(Object obj) { - if (obj == null || ! (obj instanceof Connection) || getName() == null || getProtocol() == null) { + if (obj == null || ! (obj instanceof Connection)) { return false; } + if (getName() == null || getProtocol() == null) { + return super.equals(obj); + } Connection connection = (Connection) obj; - return getProtocol() == connection.getProtocol() && getName().equals(((Connection) obj).getName()); + return getProtocol() == connection.getProtocol() && getName().equals(connection.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("} }"); + getParameters().keySet().forEach(parameter -> output.append("{").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 376724b..7cea6cb 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 @@ -8,6 +8,7 @@ import java.sql.SQLException; import java.sql.Statement; import java.util.HashMap; import java.util.HashSet; +import java.util.Map; import java.util.Set; import org.apache.guacamole.GuacamoleException; @@ -369,7 +370,7 @@ public class DB { DB db = DB.getInstance(); Connection connection = db.getMySQLConnection(); PreparedStatement stmt = connection - .prepareStatement("select connection_group_id from guacamole_connection_group where type='ORGANIZATIONAL' and name=?"); + .prepareStatement("select connection_group_id from guacamole_connection_group where type='ORGANIZATIONAL' and connection_group_name=?"); stmt.setString(1, name); ResultSet rs = stmt.executeQuery(); @@ -377,6 +378,7 @@ public class DB { TargetGroup group = new TargetGroup(); group.setId(rs.getInt("connection_group_id")); group.setName(name); + group.getConnections().addAll(getConnections(group)); return group; } @@ -396,6 +398,7 @@ public class DB { TargetGroup group = new TargetGroup(); group.setId(rs.getInt("connection_group_id")); group.setName(rs.getString("connection_group_name")); + group.getConnections().addAll(getConnections(group)); groups.add(group); } @@ -516,4 +519,66 @@ public class DB { } } + public void deleteConnection(int connecionId) throws SQLException, GuacamoleException { + PreparedStatement stmt = DB.getInstance().getMySQLConnection().prepareStatement("delete from guacamole_connection where connection_id=?"); + stmt.setInt(1, connecionId); + stmt.executeUpdate(); + } + + public void addConnectionParameters(int connectionId, Map toAdd) throws SQLException, GuacamoleException { + if (toAdd == null) { + throw new NullPointerException(); + } + toAdd.forEach((parameter, value) -> { + try { + PreparedStatement stmt = DB.getInstance().getMySQLConnection().prepareStatement("insert into guacamole_connection_parameter values (?,?,?)"); + stmt.setInt(1, connectionId); + stmt.setString(2, parameter); + stmt.setString(3, value); + stmt.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } catch (GuacamoleException e) { + e.printStackTrace(); + } + }); + } + + public void updateConnectionParameters(int connectionId, Map toUpdate) throws SQLException, GuacamoleException { + if (toUpdate == null) { + throw new NullPointerException(); + } + toUpdate.forEach((parameter, value) -> { + try { + PreparedStatement stmt = DB.getInstance().getMySQLConnection().prepareStatement("update guacamole_connection_parameter set parameter_value=? where connection_id=? and parameter_name=?"); + stmt.setString(1, value); + stmt.setInt(2, connectionId); + stmt.setString(3, parameter); + stmt.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } catch (GuacamoleException e) { + e.printStackTrace(); + } + }); + } + + public void deleteConnectionParameters(int connectionId, Set parameters) { + if (parameters == null) { + throw new NullPointerException(); + } + parameters.forEach((parameter) -> { + try { + PreparedStatement stmt = DB.getInstance().getMySQLConnection().prepareStatement("delete from guacamole_connection_parameter where connection_id=? and parameter_name=?"); + stmt.setInt(1, connectionId); + stmt.setString(2, parameter); + stmt.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } catch (GuacamoleException e) { + e.printStackTrace(); + } + }); + } + } diff --git a/src/main/java/cm/soungui/guacamole/ext/wallix/sync/db/TargetGroup.java b/src/main/java/cm/soungui/guacamole/ext/wallix/sync/db/TargetGroup.java index 811daaf..39acf40 100644 --- a/src/main/java/cm/soungui/guacamole/ext/wallix/sync/db/TargetGroup.java +++ b/src/main/java/cm/soungui/guacamole/ext/wallix/sync/db/TargetGroup.java @@ -49,5 +49,14 @@ public class TargetGroup { } return false; } + + @Override + public String toString() { + StringBuffer output = new StringBuffer("{id: ").append(getId()).append(", name: ") + .append(getName()).append(", connections: {"); + getConnections().forEach(connection -> output.append(connection).append(",")); + output.append("} }"); + return output.toString(); + } } diff --git a/src/main/java/cm/soungui/guacamole/ext/wallix/sync/db/UserGroup.java b/src/main/java/cm/soungui/guacamole/ext/wallix/sync/db/UserGroup.java index 36f633c..13b3141 100644 --- a/src/main/java/cm/soungui/guacamole/ext/wallix/sync/db/UserGroup.java +++ b/src/main/java/cm/soungui/guacamole/ext/wallix/sync/db/UserGroup.java @@ -40,7 +40,7 @@ public class UserGroup extends Entity { for (Entity member : getMembers()) { output.append(member.getType()).append(":").append(member.getName()).append(","); } - output.deleteCharAt(output.length()-1).append("}"); + output.deleteCharAt(output.length()-1).append("] }"); return output.toString(); }