Implement group members synchronization.

This commit add membership synchronization.
This commit is contained in:
2025-11-05 16:17:55 +01:00
parent c366bdb6af
commit 555933379b
3 changed files with 97 additions and 57 deletions

View File

@@ -9,6 +9,7 @@ import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.net.ssl.SSLContext;
@@ -137,11 +138,15 @@ public class Wallix {
UserGroup group = new UserGroup();
group.setName(node.findValue(Wallix.API.ATTRIBUTE_GROUP_NAME).asText());
node.findValues(Wallix.API.ATTRIBUTE_GROUP_USERS).forEach(userNode -> {
User user = new User();
user.setName(userNode.textValue());
group.getMembers().add(user);
});
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.add(group);
});

View File

@@ -81,7 +81,21 @@ public class WallixConfigSynchronizer implements Runnable {
DB db = DB.getInstance();
Set<UserGroup> dbGroups = db.getUserGroups();
for (UserGroup group : dbGroups) {
wallixGroups.forEach(group -> {
try {
if (! dbGroups.contains(group)) {
System.out.println("Adding group " + group.getName());
db.addGroup(group);
}
} catch (SQLException | GuacamoleException e) {
System.err.println(e.getMessage());
}
});
Set<UserGroup> newDbGroups = db.getUserGroups();
for (UserGroup group : newDbGroups) {
if (wallixGroups.contains(group)) {
for (UserGroup wallixGroup : wallixGroups) {
if (group.equals(wallixGroup)) {
@@ -93,17 +107,6 @@ public class WallixConfigSynchronizer implements Runnable {
db.deleteGroup(group);
}
}
wallixGroups.forEach(group -> {
try {
if (! dbGroups.contains(group)) {
System.out.println("Adding group " + group.getName());
db.addGroup(group);
}
} catch (SQLException | GuacamoleException e) {
System.err.println(e.getMessage());
}
});
}
private void printWallixVersion() throws Exception {

View File

@@ -6,7 +6,6 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
@@ -65,7 +64,7 @@ public class DB {
DB db = DB.getInstance();
Connection connection = db.getMySQLConnection();
ResultSet rs = connection
.prepareStatement("select entity_id,name from guacamole_entity where type='USER_GROUP'")
.prepareStatement("select e.entity_id,e.name,u.user_group_id from guacamole_entity e,guacamole_user_group u where type='USER_GROUP' and e.entity_id=u.entity_id")
.executeQuery();
HashSet<UserGroup> groups = new HashSet<>();
@@ -73,6 +72,7 @@ public class DB {
UserGroup group = new UserGroup();
group.setId(rs.getInt("entity_id"));
group.setName(rs.getString("name"));
group.setGroupId(rs.getInt("user_group_id"));
groups.add(group);
}
@@ -140,55 +140,53 @@ public class DB {
}
public UserGroup getGroup(String name) throws SQLException, GuacamoleException {
return (UserGroup) getEntity(Entity.EntityType.GROUP, name);
}
private Entity getEntity(EntityType type, String name) throws SQLException, GuacamoleException {
if (type == null || name == null) {
return null;
}
DB db = DB.getInstance();
Connection connection = db.getMySQLConnection();
PreparedStatement stmt = connection
.prepareStatement("select entity_id,name from guacamole_entity where type=? and name=?");
stmt.setString(1, type == EntityType.USER ? "USER" : "USER_GROUP");
stmt.setString(2, name);
.prepareStatement("select entity_id from guacamole_entity where type='USER_GROUP' and name=?");
stmt.setString(1, name);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Entity entity = type == EntityType.USER ? new User() : new UserGroup();
entity.setId(rs.getInt("entity_id"));
entity.setName(rs.getString("name"));
UserGroup group = new UserGroup();
group.setId(rs.getInt("entity_id"));
group.setName(name);
group.getMembers().addAll(getGroupMembers(name));
return entity;
return group;
}
return null;
}
private Entity getEntity(EntityType type, int id) throws SQLException, GuacamoleException {
if (type == null) {
return null;
}
public Set<Entity> getGroupMembers(String name) throws GuacamoleException, SQLException {
DB db = DB.getInstance();
Connection connection = db.getMySQLConnection();
PreparedStatement stmt = connection
.prepareStatement("select entity_id,name from guacamole_entity where type=? and id=?");
stmt.setString(1, type.toString());
stmt.setInt(2, id);
.prepareStatement("select entity_id,name,type from guacamole_entity where entity_id in (select member_entity_id from guacamole_user_group_member where user_group_id in (select user_group_id from guacamole_user_group where entity_id=(select entity_id from guacamole_entity where name=?)))");
stmt.setString(1, name);
ResultSet rs = stmt.executeQuery();
Set<Entity> result = new HashSet<>();
while (rs.next()) {
Entity entity = new Entity();
String type = rs.getString("type");
Entity entity;
if ("USER".equals(type)) {
entity = new User();
} else {
entity = new UserGroup();
}
entity.setId(rs.getInt("entity_id"));
entity.setName(rs.getString("name"));
return entity;
if (entity.getType()==EntityType.GROUP) {
UserGroup group = (UserGroup) entity;
group.getMembers().addAll(getGroupMembers(entity.getName()));
}
result.add(entity);
}
return null;
return result;
}
public void updateGroupMembers(UserGroup group, Set<Entity> wallixGroupMembers) throws SQLException, GuacamoleException {
@@ -197,6 +195,7 @@ public class DB {
}
if (wallixGroupMembers == null || wallixGroupMembers.isEmpty()) {
// There is no member for this group, so we delete all existing entries and return
DB db = DB.getInstance();
Connection connection = db.getMySQLConnection();
PreparedStatement stmt = connection
@@ -207,25 +206,58 @@ public class DB {
return;
}
Set<String> entitiesNames = new HashSet<>();
wallixGroupMembers.forEach(member -> entitiesNames.add(member.getName()));
DB db = DB.getInstance();
Connection connection = db.getMySQLConnection();
PreparedStatement stmt = connection
.prepareStatement("select user_group_id,member_entity_id,type from guacamole_user_group_member,guacamole_entity where user_group_id=? and member_entity_id=entity_id");
stmt.setInt(1, group.getGroupId());
// We get Entity ID of each new member
StringBuffer sqlQuery = new StringBuffer("select entity_id from guacamole_entity where name in ('").append(String.join("','", entitiesNames)).append("')");
PreparedStatement stmt = connection.prepareStatement(sqlQuery.toString());
ResultSet rs = stmt.executeQuery();
ArrayList<Entity> toDelete = new ArrayList<>();
Set<Integer> newMemberIds = new HashSet<>();
while (rs.next()) {
Entity member = getEntity(EntityType.valueOf(rs.getString("type")), rs.getInt("entity_id"));
if (wallixGroupMembers.contains(member)) {
toDelete.add(member);
newMemberIds.add(rs.getInt("entity_id"));
}
rs.close();
stmt.close();
// We get current members of the group
stmt = connection.prepareStatement("select member_entity_id from guacamole_user_group_member,guacamole_entity where user_group_id=? and member_entity_id=entity_id");
stmt.setInt(1, group.getGroupId());
rs = stmt.executeQuery();
Set<Integer> toAdd = new HashSet<>(newMemberIds);
Set<Integer> toDelete = new HashSet<>();
while (rs.next()) {
int id = rs.getInt("member_entity_id");
if (newMemberIds.contains(id)) {
toAdd.remove(id);
} else {
toDelete.add(id);
}
}
rs.close();
stmt.close();
System.out.println("Member to delete from group " + group.getName());
for (Entity member : toDelete) {
System.out.println(member.getId() + " - " + member.getName());
stmt = connection.prepareStatement("insert into guacamole_user_group_member values (?, ?)");
for (int id : toAdd) {
stmt.setInt(1, group.getGroupId());
stmt.setInt(2, id);
stmt.executeUpdate();
}
stmt.close();
stmt = connection.prepareStatement("delete from guacamole_user_group_member values (?, ?)");
for (int id : toDelete) {
stmt.setInt(1, group.getGroupId());
stmt.setInt(2, id);
stmt.executeUpdate();
}
stmt.close();
}
public void addGroup(UserGroup group) throws SQLException, GuacamoleException {