Add connection and associated parameters registration in Guacamole.
This commit is contained in:
@@ -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<String, String> 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);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -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<TargetGroup> dbGroups = db.getTargetGroups();
|
||||
Set<TargetGroup> dbTargetGroups = db.getTargetGroups();
|
||||
|
||||
Set<TargetGroup> 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<TargetGroup> 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<Connection> 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<String, String> dbParameters = db.getConnectionParameters(connection.getName(), connection.getProtocol());
|
||||
// HashMap<String, String> wallixParameters = group.
|
||||
for (String parameterName : dbParameters.keySet()) {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
System.out.println("Deleting target group : " + group.getName());
|
||||
db.deleteTargetGroup(group);
|
||||
|
||||
@@ -12,6 +12,26 @@ public class Connection {
|
||||
|
||||
private final HashMap<String, String> 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;
|
||||
}
|
||||
@@ -37,4 +57,27 @@ public class Connection {
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
@@ -401,6 +403,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<cm.soungui.guacamole.ext.wallix.sync.db.Connection> 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<cm.soungui.guacamole.ext.wallix.sync.db.Connection> 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<String, String> 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<String, String> 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) {
|
||||
return;
|
||||
@@ -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<String, String> 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user