From f4a3c5b31e410fa03463064df7a8eb144d2fd9cf Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Wed, 27 Apr 2016 13:12:31 -0700 Subject: [PATCH] GUACAMOLE-52: Foreign keys in history table should be ON DELETE SET NULL. For sake of context, always store username and connection name in history table. --- .../AbstractGuacamoleTunnelService.java | 5 +- .../schema/001-create-schema.sql | 12 +-- .../schema/upgrade/upgrade-pre-0.9.10.sql | 89 ++++++++++++++++++ .../connection/ConnectionRecordMapper.xml | 27 +++--- .../schema/001-create-schema.sql | 12 +-- .../schema/upgrade/upgrade-pre-0.9.10.sql | 90 +++++++++++++++++++ .../connection/ConnectionRecordMapper.xml | 29 +++--- 7 files changed, 225 insertions(+), 39 deletions(-) create mode 100644 extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/schema/upgrade/upgrade-pre-0.9.10.sql create mode 100644 extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-postgresql/schema/upgrade/upgrade-pre-0.9.10.sql diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/tunnel/AbstractGuacamoleTunnelService.java b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/tunnel/AbstractGuacamoleTunnelService.java index 482a2fab1..95d5ab640 100644 --- a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/tunnel/AbstractGuacamoleTunnelService.java +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/tunnel/AbstractGuacamoleTunnelService.java @@ -246,13 +246,12 @@ public abstract class AbstractGuacamoleTunnelService implements GuacamoleTunnelS // Get associated models AuthenticatedUser user = record.getUser(); - UserModel userModel = user.getUser().getModel(); ConnectionRecordModel recordModel = new ConnectionRecordModel(); // Copy user information and timestamps into new record - recordModel.setUserID(userModel.getObjectID()); - recordModel.setUsername(userModel.getIdentifier()); + recordModel.setUsername(user.getIdentifier()); recordModel.setConnectionIdentifier(connection.getIdentifier()); + recordModel.setConnectionName(connection.getName()); recordModel.setStartDate(record.getStartDate()); recordModel.setEndDate(new Date()); diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/schema/001-create-schema.sql b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/schema/001-create-schema.sql index 339236a0b..6982896ff 100644 --- a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/schema/001-create-schema.sql +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/schema/001-create-schema.sql @@ -230,11 +230,13 @@ CREATE TABLE `guacamole_user_permission` ( CREATE TABLE `guacamole_connection_history` ( - `history_id` int(11) NOT NULL AUTO_INCREMENT, - `user_id` int(11) NOT NULL, - `connection_id` int(11) NOT NULL, - `start_date` datetime NOT NULL, - `end_date` datetime DEFAULT NULL, + `history_id` int(11) NOT NULL AUTO_INCREMENT, + `user_id` int(11) DEFAULT NULL, + `username` varchar(128) NOT NULL, + `connection_id` int(11) DEFAULT NULL, + `connection_name` varchar(128) NOT NULL, + `start_date` datetime NOT NULL, + `end_date` datetime DEFAULT NULL, PRIMARY KEY (`history_id`), KEY `user_id` (`user_id`), diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/schema/upgrade/upgrade-pre-0.9.10.sql b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/schema/upgrade/upgrade-pre-0.9.10.sql new file mode 100644 index 000000000..f69ba41b5 --- /dev/null +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/schema/upgrade/upgrade-pre-0.9.10.sql @@ -0,0 +1,89 @@ +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. +-- + +-- +-- User and connection IDs within history table can now be null +-- + +ALTER TABLE guacamole_connection_history + MODIFY COLUMN user_id INT(11) DEFAULT NULL; + +ALTER TABLE guacamole_connection_history + MODIFY COLUMN connection_id INT(11) DEFAULT NULL; + +-- +-- Add new username and connection_name columns to history table +-- + +ALTER TABLE guacamole_connection_history + ADD COLUMN username VARCHAR(128); + +ALTER TABLE guacamole_connection_history + ADD COLUMN connection_name VARCHAR(128); + +-- +-- Populate new name columns by joining corresponding tables +-- + +UPDATE guacamole_connection_history +JOIN guacamole_user + ON guacamole_user.user_id = guacamole_connection_history.user_id +SET guacamole_connection_history.username = guacamole_user.username; + +UPDATE guacamole_connection_history +JOIN guacamole_connection + ON guacamole_connection.connection_id = + guacamole_connection_history.connection_id +SET guacamole_connection_history.connection_name = + guacamole_connection.connection_name; + +-- +-- Set NOT NULL now that the column is fully populated +-- + +ALTER TABLE guacamole_connection_history + MODIFY username VARCHAR(128) NOT NULL; + +ALTER TABLE guacamole_connection_history + MODIFY connection_name VARCHAR(128) NOT NULL; + +-- +-- Remove old foreign key constraints with ON DELETE CASCADE +-- + +ALTER TABLE guacamole_connection_history + DROP FOREIGN KEY guacamole_connection_history_ibfk_1; + +ALTER TABLE guacamole_connection_history + DROP FOREIGN KEY guacamole_connection_history_ibfk_2; + +-- +-- Recreate foreign key constraints with ON DELETE SET NULL +-- + +ALTER TABLE guacamole_connection_history + ADD CONSTRAINT guacamole_connection_history_ibfk_1 + FOREIGN KEY (user_id) + REFERENCES guacamole_user (user_id) ON DELETE SET NULL; + +ALTER TABLE guacamole_connection_history + ADD CONSTRAINT guacamole_connection_history_ibfk_2 + FOREIGN KEY (connection_id) + REFERENCES guacamole_connection (connection_id) ON DELETE SET NULL; + diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/src/main/resources/org/apache/guacamole/auth/jdbc/connection/ConnectionRecordMapper.xml b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/src/main/resources/org/apache/guacamole/auth/jdbc/connection/ConnectionRecordMapper.xml index d1825fab6..3adcde2c2 100644 --- a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/src/main/resources/org/apache/guacamole/auth/jdbc/connection/ConnectionRecordMapper.xml +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/src/main/resources/org/apache/guacamole/auth/jdbc/connection/ConnectionRecordMapper.xml @@ -37,17 +37,15 @@ SELECT - guacamole_connection.connection_id, - guacamole_connection.connection_name, - guacamole_user.user_id, - guacamole_user.username, + guacamole_connection_history.connection_id, + guacamole_connection_history.connection_name, + guacamole_connection_history.user_id, + guacamole_connection_history.username, guacamole_connection_history.start_date, guacamole_connection_history.end_date FROM guacamole_connection_history - JOIN guacamole_connection ON guacamole_connection_history.connection_id = guacamole_connection.connection_id - JOIN guacamole_user ON guacamole_connection_history.user_id = guacamole_user.user_id WHERE - guacamole_connection.connection_id = #{identifier,jdbcType=INTEGER}::integer + guacamole_connection_history.connection_id = #{identifier,jdbcType=INTEGER}::integer ORDER BY guacamole_connection_history.start_date DESC, guacamole_connection_history.end_date DESC @@ -59,13 +57,18 @@ INSERT INTO guacamole_connection_history ( connection_id, + connection_name, user_id, + username, start_date, end_date ) VALUES ( #{record.connectionIdentifier,jdbcType=INTEGER}::integer, - #{record.userID,jdbcType=INTEGER}, + #{record.connectionName,jdbcType=VARCHAR}, + (SELECT user_id FROM guacamole_user + WHERE username = #{record.username,jdbcType=VARCHAR}), + #{record.username,jdbcType=VARCHAR}, #{record.startDate,jdbcType=TIMESTAMP}, #{record.endDate,jdbcType=TIMESTAMP} ) @@ -77,14 +80,12 @@ SELECT guacamole_connection_history.connection_id, - guacamole_connection.connection_name, + guacamole_connection_history.connection_name, guacamole_connection_history.user_id, - guacamole_user.username, + guacamole_connection_history.username, guacamole_connection_history.start_date, guacamole_connection_history.end_date FROM guacamole_connection_history - LEFT JOIN guacamole_connection ON guacamole_connection_history.connection_id = guacamole_connection.connection_id - LEFT JOIN guacamole_user ON guacamole_connection_history.user_id = guacamole_user.user_id