Add connection parameter change detection and synchronization.
This commit is contained in:
@@ -226,7 +226,7 @@ public class Wallix {
|
|||||||
HashMap<String, String> parameters = connection.getParameters();
|
HashMap<String, String> parameters = connection.getParameters();
|
||||||
parameters.put(Connection.Parameter.USERNAME, getFormattedUsername(accountNode, group.getName(), usernamePrefix));
|
parameters.put(Connection.Parameter.USERNAME, getFormattedUsername(accountNode, group.getName(), usernamePrefix));
|
||||||
parameters.put(Connection.Parameter.HOSTNAME, connectionHost);
|
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);
|
group.getConnections().add(connection);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package cm.soungui.guacamole.ext.wallix.sync;
|
|||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.apache.guacamole.GuacamoleException;
|
import org.apache.guacamole.GuacamoleException;
|
||||||
@@ -41,8 +43,6 @@ public class WallixConfigSynchronizer implements Runnable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void synchronizeUsers() throws Exception {
|
private void synchronizeUsers() throws Exception {
|
||||||
System.out.println("Synchronizing users");
|
|
||||||
System.out.println("Getting Wallix groups");
|
|
||||||
Set<User> wallixUsers = Wallix.getInstance().getUsers();
|
Set<User> wallixUsers = Wallix.getInstance().getUsers();
|
||||||
DB db = DB.getInstance();
|
DB db = DB.getInstance();
|
||||||
wallixUsers.forEach(wallixUser -> {
|
wallixUsers.forEach(wallixUser -> {
|
||||||
@@ -80,8 +80,6 @@ public class WallixConfigSynchronizer implements Runnable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void synchronizeUserGroups() throws Exception {
|
private void synchronizeUserGroups() throws Exception {
|
||||||
System.out.println("Synchronizing groups");
|
|
||||||
System.out.println("Getting Wallix groups");
|
|
||||||
Set<UserGroup> wallixGroups = Wallix.getInstance().getUserGroups();
|
Set<UserGroup> wallixGroups = Wallix.getInstance().getUserGroups();
|
||||||
|
|
||||||
DB db = DB.getInstance();
|
DB db = DB.getInstance();
|
||||||
@@ -115,42 +113,68 @@ public class WallixConfigSynchronizer implements Runnable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void synchronizeTargetGroups() throws Exception {
|
private void synchronizeTargetGroups() throws Exception {
|
||||||
System.out.println("Synchronizing target groups");
|
|
||||||
System.out.println("Getting Wallix target groups");
|
|
||||||
|
|
||||||
DB db = DB.getInstance();
|
DB db = DB.getInstance();
|
||||||
Set<TargetGroup> dbTargetGroups = db.getTargetGroups();
|
|
||||||
|
|
||||||
Set<TargetGroup> wallixTargetGroups = Wallix.getInstance().getTargetGroups();
|
Set<TargetGroup> wallixTargetGroups = Wallix.getInstance().getTargetGroups();
|
||||||
|
|
||||||
|
// Fetch targets from Wallix
|
||||||
for (TargetGroup wallixGroup : wallixTargetGroups) {
|
for (TargetGroup wallixGroup : wallixTargetGroups) {
|
||||||
if (dbTargetGroups.contains(wallixGroup)) {
|
TargetGroup dbTargetGroup = db.getTargetGroup(wallixGroup.getName());
|
||||||
Set<Connection> dbConnections = db.getConnections(wallixGroup);
|
|
||||||
for (Connection connection : wallixGroup.getConnections()) {
|
// Target not found in Guacamole, so we have to create it.
|
||||||
if (! dbConnections.contains(connection) && connection.getName() != null) {
|
if (dbTargetGroup == 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());
|
System.out.println("Adding target group " + wallixGroup.getName());
|
||||||
db.addTargetGroup(wallixGroup);
|
db.addTargetGroup(wallixGroup);
|
||||||
}
|
|
||||||
|
// We fetch the newly inserted target
|
||||||
|
dbTargetGroup = db.getTargetGroup(wallixGroup.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
dbTargetGroups = db.getTargetGroups();
|
// Processing of connections parameters fetched from Wallix
|
||||||
|
for (Connection wallixConnection : wallixGroup.getConnections()) {
|
||||||
|
Optional<Connection> matchingConnections = dbTargetGroup.getConnections().stream()
|
||||||
|
.filter(c -> wallixConnection.equals(c))
|
||||||
|
.findFirst();
|
||||||
|
|
||||||
for (TargetGroup group : dbTargetGroups) {
|
if (matchingConnections.isEmpty()) {
|
||||||
if (wallixTargetGroups.contains(group)) {
|
// No matching connection found in database, so we need to add it
|
||||||
for (Connection connection : group.getConnections()) {
|
System.out.println("Adding connection '" + wallixConnection.getName() + "' to group " + wallixGroup.getName());
|
||||||
HashMap<String, String> dbParameters = db.getConnectionParameters(connection.getName(), connection.getProtocol());
|
db.addConnection(wallixConnection, wallixGroup.getName());
|
||||||
// HashMap<String, String> wallixParameters = group.
|
} else {
|
||||||
for (String parameterName : dbParameters.keySet()) {
|
// Matching connection found in database, we will update parameters if needed
|
||||||
}
|
Connection dbConnection = matchingConnections.get();
|
||||||
|
HashMap<String, String> wallixParameter = wallixConnection.getParameters();
|
||||||
|
HashMap<String, String> dbParameters = dbConnection.getParameters();
|
||||||
|
Map<String, String> toAdd = new HashMap<String, String>(wallixParameter.size());
|
||||||
|
Map<String, String> toUpdate = new HashMap<String, String>(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 {
|
} else {
|
||||||
System.out.println("Deleting target group : " + group.getName());
|
toAdd.put(parameter, value);
|
||||||
db.deleteTargetGroup(group);
|
}
|
||||||
|
});
|
||||||
|
if (! toAdd.isEmpty()) {
|
||||||
|
db.addConnectionParameters(dbConnection.getId(), toAdd);
|
||||||
|
}
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -64,19 +64,22 @@ public class Connection {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (obj == null || ! (obj instanceof Connection) || getName() == null || getProtocol() == null) {
|
if (obj == null || ! (obj instanceof Connection)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (getName() == null || getProtocol() == null) {
|
||||||
|
return super.equals(obj);
|
||||||
|
}
|
||||||
Connection connection = (Connection) obj;
|
Connection connection = (Connection) obj;
|
||||||
return getProtocol() == connection.getProtocol() && getName().equals(((Connection) obj).getName());
|
return getProtocol() == connection.getProtocol() && getName().equals(connection.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuffer output = new StringBuffer("{id: ").append(getId()).append(", name: ").append(getName()).append(", protocol: ").append(getProtocol())
|
StringBuffer output = new StringBuffer("{id: ").append(getId()).append(", name: ").append(getName()).append(", protocol: ").append(getProtocol())
|
||||||
.append(", parameters : {");
|
.append(", parameters : {");
|
||||||
getParameters().keySet().forEach(parameter -> output.append(parameter).append(":").append(getParameters().get(parameter)).append(", "));
|
getParameters().keySet().forEach(parameter -> output.append("{").append(parameter).append(":").append(getParameters().get(parameter)).append(" }, "));
|
||||||
output.append("} }");
|
output.append("}");
|
||||||
return output.toString();
|
return output.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import java.sql.SQLException;
|
|||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.apache.guacamole.GuacamoleException;
|
import org.apache.guacamole.GuacamoleException;
|
||||||
@@ -369,7 +370,7 @@ public class DB {
|
|||||||
DB db = DB.getInstance();
|
DB db = DB.getInstance();
|
||||||
Connection connection = db.getMySQLConnection();
|
Connection connection = db.getMySQLConnection();
|
||||||
PreparedStatement stmt = connection
|
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);
|
stmt.setString(1, name);
|
||||||
ResultSet rs = stmt.executeQuery();
|
ResultSet rs = stmt.executeQuery();
|
||||||
|
|
||||||
@@ -377,6 +378,7 @@ public class DB {
|
|||||||
TargetGroup group = new TargetGroup();
|
TargetGroup group = new TargetGroup();
|
||||||
group.setId(rs.getInt("connection_group_id"));
|
group.setId(rs.getInt("connection_group_id"));
|
||||||
group.setName(name);
|
group.setName(name);
|
||||||
|
group.getConnections().addAll(getConnections(group));
|
||||||
|
|
||||||
return group;
|
return group;
|
||||||
}
|
}
|
||||||
@@ -396,6 +398,7 @@ public class DB {
|
|||||||
TargetGroup group = new TargetGroup();
|
TargetGroup group = new TargetGroup();
|
||||||
group.setId(rs.getInt("connection_group_id"));
|
group.setId(rs.getInt("connection_group_id"));
|
||||||
group.setName(rs.getString("connection_group_name"));
|
group.setName(rs.getString("connection_group_name"));
|
||||||
|
group.getConnections().addAll(getConnections(group));
|
||||||
|
|
||||||
groups.add(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<String, String> 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<String, String> 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<String> 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();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,4 +50,13 @@ public class TargetGroup {
|
|||||||
return false;
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ public class UserGroup extends Entity {
|
|||||||
for (Entity member : getMembers()) {
|
for (Entity member : getMembers()) {
|
||||||
output.append(member.getType()).append(":").append(member.getName()).append(",");
|
output.append(member.getType()).append(":").append(member.getName()).append(",");
|
||||||
}
|
}
|
||||||
output.deleteCharAt(output.length()-1).append("}");
|
output.deleteCharAt(output.length()-1).append("] }");
|
||||||
return output.toString();
|
return output.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user