Implements group addition and deletion.
This commit add the deletion of groups not found in Wallix and addition of groups not existing in Guacamole.
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
package cm.soungui.guacamole.ext.wallix.sync;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.guacamole.GuacamoleException;
|
||||
import org.apache.guacamole.environment.LocalEnvironment;
|
||||
@@ -19,10 +20,11 @@ public class WallixConfigSynchronizer implements Runnable {
|
||||
private long sleepDuration;
|
||||
private LocalEnvironment environment;
|
||||
|
||||
public WallixConfigSynchronizer() throws GuacamoleException {
|
||||
public WallixConfigSynchronizer() throws Exception {
|
||||
System.out.println("Wazuh synchronizer Thread started");
|
||||
environment = LocalEnvironment.getInstance();
|
||||
sleepDuration = 1000 * environment.getProperty(Configuration.UPDATE_INTERVAL, Long.valueOf(900));
|
||||
printWallixVersion();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -41,17 +43,34 @@ public class WallixConfigSynchronizer implements Runnable {
|
||||
private void synchronizeGroups() throws Exception {
|
||||
System.out.println("Synchronizing groups");
|
||||
System.out.println("Getting Wallix groups");
|
||||
printWallixVersion();
|
||||
List<UserGroup> wallixGroups = getWallixGroups();
|
||||
System.out.println("Wallix Groups");
|
||||
wallixGroups.forEach(group -> System.out.println(group.getName()));
|
||||
Set<UserGroup> wallixGroups = getWallixGroups();
|
||||
|
||||
DB db = DB.getInstance();
|
||||
List<UserGroup> dbGroups = db.getUserGroups();
|
||||
Set<UserGroup> dbGroups = db.getUserGroups();
|
||||
for (UserGroup group : dbGroups) {
|
||||
System.out.println("ID : " + group.getId() + " - Name : " + group.getName());
|
||||
if (wallixGroups.contains(group)) {
|
||||
for (UserGroup wallixGroup : wallixGroups) {
|
||||
if (group.equals(wallixGroup)) {
|
||||
db.updateGroupMembers(group, wallixGroup.getMembers());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
System.out.println("Deleting group : " + group.getName());
|
||||
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 {
|
||||
Wallix wallix = Wallix.getInstance();
|
||||
@@ -59,13 +78,13 @@ public class WallixConfigSynchronizer implements Runnable {
|
||||
System.out.println(output);
|
||||
}
|
||||
|
||||
private List<UserGroup> getWallixGroups() throws Exception {
|
||||
private Set<UserGroup> getWallixGroups() throws Exception {
|
||||
Wallix wallix = Wallix.getInstance();
|
||||
String output = wallix.get("/usergroups");
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
JsonNode jsonNode = objectMapper.readTree(output);
|
||||
List<UserGroup> list = new ArrayList<>();
|
||||
Set<UserGroup> list = new HashSet<>();
|
||||
|
||||
jsonNode.elements().forEachRemaining((e) -> {
|
||||
ObjectNode node = (ObjectNode) e;
|
||||
|
||||
@@ -2,16 +2,20 @@ package cm.soungui.guacamole.ext.wallix.sync.db;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.guacamole.GuacamoleException;
|
||||
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.Entity.EntityType;
|
||||
|
||||
public class DB {
|
||||
|
||||
@@ -39,8 +43,6 @@ public class DB {
|
||||
.append(sslMode == null ? "" : "sslMode=" + sslMode + "&")
|
||||
.toString();
|
||||
|
||||
System.out.println("JDBC URL : " + url);
|
||||
|
||||
Class.forName("org.mariadb.jdbc.Driver");
|
||||
mysqlConnection = DriverManager.getConnection(url);
|
||||
|
||||
@@ -57,14 +59,14 @@ public class DB {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public List<UserGroup> getUserGroups() throws GuacamoleException, SQLException {
|
||||
public Set<UserGroup> getUserGroups() throws GuacamoleException, SQLException {
|
||||
DB db = DB.getInstance();
|
||||
Connection connection = db.getMySQLConnection();
|
||||
ResultSet rs = connection
|
||||
.prepareStatement("select entity_id,name from guacamole_entity where type='USER_GROUP'")
|
||||
.executeQuery();
|
||||
|
||||
ArrayList<UserGroup> groups = new ArrayList<>();
|
||||
HashSet<UserGroup> groups = new HashSet<>();
|
||||
while (rs.next()) {
|
||||
UserGroup group = new UserGroup();
|
||||
group.setId(rs.getInt("entity_id"));
|
||||
@@ -76,4 +78,141 @@ public class DB {
|
||||
return groups;
|
||||
}
|
||||
|
||||
public void deleteGroup(UserGroup group) throws SQLException, GuacamoleException {
|
||||
if (group == null || group.getName() == null) {
|
||||
return;
|
||||
}
|
||||
DB db = DB.getInstance();
|
||||
Connection connection = db.getMySQLConnection();
|
||||
PreparedStatement stmt = connection
|
||||
.prepareStatement("delete from guacamole_entity where type='USER_GROUP' and name=?");
|
||||
stmt.setString(1, group.getName());
|
||||
stmt.executeUpdate();
|
||||
}
|
||||
|
||||
public User getUser(String name) throws SQLException, GuacamoleException {
|
||||
return (User) getEntity(Entity.EntityType.USER, name);
|
||||
}
|
||||
|
||||
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.toString());
|
||||
stmt.setString(2, name);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
|
||||
while (rs.next()) {
|
||||
Entity entity = new Entity();
|
||||
entity.setId(rs.getInt("entity_id"));
|
||||
entity.setName(rs.getString("name"));
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private Entity getEntity(EntityType type, int id) throws SQLException, GuacamoleException {
|
||||
if (type == 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 id=?");
|
||||
stmt.setString(1, type.toString());
|
||||
stmt.setInt(2, id);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
|
||||
while (rs.next()) {
|
||||
Entity entity = new Entity();
|
||||
entity.setId(rs.getInt("entity_id"));
|
||||
entity.setName(rs.getString("name"));
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void updateGroupMembers(UserGroup group, Set<Entity> wallixGroupMembers) throws SQLException, GuacamoleException {
|
||||
if (group == null) {
|
||||
throw new NullPointerException("Provided users group is null");
|
||||
}
|
||||
|
||||
if (wallixGroupMembers == null || wallixGroupMembers.isEmpty()) {
|
||||
DB db = DB.getInstance();
|
||||
Connection connection = db.getMySQLConnection();
|
||||
PreparedStatement stmt = connection
|
||||
.prepareStatement("delete from guacamole_user_group_member where user_group_id=?");
|
||||
stmt.setInt(1, group.getGroupId());
|
||||
stmt.executeUpdate();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
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());
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
|
||||
ArrayList<Entity> toDelete = new ArrayList<>();
|
||||
while (rs.next()) {
|
||||
Entity member = getEntity(EntityType.valueOf(rs.getString("type")), rs.getInt("entity_id"));
|
||||
if (wallixGroupMembers.contains(member)) {
|
||||
toDelete.add(member);
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("Member to delete from group " + group.getName());
|
||||
for (Entity member : toDelete) {
|
||||
System.out.println(member.getId() + " - " + member.getName());
|
||||
}
|
||||
}
|
||||
|
||||
public void addGroup(UserGroup group) throws SQLException, GuacamoleException {
|
||||
if (group == null || group.getName() == null) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
|
||||
DB db = DB.getInstance();
|
||||
Connection connection = db.getMySQLConnection();
|
||||
|
||||
connection.setAutoCommit(false);
|
||||
|
||||
PreparedStatement stmtAddEntity = connection
|
||||
.prepareStatement("insert into guacamole_entity (entity_id, name, type) values (?, ?, 'USER_GROUP')",
|
||||
Statement.RETURN_GENERATED_KEYS);
|
||||
stmtAddEntity.setInt(1, group.getGroupId());
|
||||
stmtAddEntity.setString(2, group.getName());
|
||||
stmtAddEntity.executeUpdate();
|
||||
|
||||
ResultSet rs = stmtAddEntity.getGeneratedKeys();
|
||||
rs.next();
|
||||
|
||||
PreparedStatement stmtAddGroup = connection
|
||||
.prepareStatement("insert into guacamole_user_group (entity_id) values (?)");
|
||||
stmtAddGroup.setInt(1, rs.getInt(1));
|
||||
stmtAddGroup.executeUpdate();
|
||||
|
||||
connection.commit();
|
||||
stmtAddEntity.close();
|
||||
stmtAddGroup.close();
|
||||
|
||||
connection.setAutoCommit(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,11 +12,15 @@ public class ApplicationStartedListener implements Listener {
|
||||
|
||||
@Override
|
||||
public void handleEvent(Object event) throws GuacamoleException {
|
||||
try {
|
||||
if (event instanceof ApplicationStartedEvent) {
|
||||
synchronizer = new WallixConfigSynchronizer();
|
||||
Thread thread = new Thread(synchronizer);
|
||||
thread.start();
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
throw new GuacamoleException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user