diff --git a/engine/schema/src/com/cloud/upgrade/dao/Upgrade450to451.java b/engine/schema/src/com/cloud/upgrade/dao/Upgrade450to451.java index d56db8563c3..b49939fc7c7 100644 --- a/engine/schema/src/com/cloud/upgrade/dao/Upgrade450to451.java +++ b/engine/schema/src/com/cloud/upgrade/dao/Upgrade450to451.java @@ -17,13 +17,6 @@ package com.cloud.upgrade.dao; -import com.cloud.utils.crypt.DBEncryptionUtil; -import com.cloud.utils.db.TransactionLegacy; -import com.cloud.utils.exception.CloudRuntimeException; -import com.cloud.utils.script.Script; -import org.apache.log4j.Logger; -import org.jasypt.exceptions.EncryptionOperationNotPossibleException; - import java.io.File; import java.io.UnsupportedEncodingException; import java.sql.Connection; @@ -33,6 +26,13 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.List; +import org.apache.log4j.Logger; +import org.jasypt.exceptions.EncryptionOperationNotPossibleException; + +import com.cloud.utils.crypt.DBEncryptionUtil; +import com.cloud.utils.exception.CloudRuntimeException; +import com.cloud.utils.script.Script; + public class Upgrade450to451 implements DbUpgrade { final static Logger s_logger = Logger.getLogger(Upgrade450to451.class); @@ -80,50 +80,30 @@ public class Upgrade450to451 implements DbUpgrade { } private void encryptKeyInKeyStore(Connection conn) { - PreparedStatement selectStatement = null; - ResultSet selectResultSet = null; - PreparedStatement updateStatement = null; - try { - selectStatement = conn.prepareStatement("SELECT ks.id, ks.key FROM cloud.keystore ks WHERE ks.key IS NOT null"); - selectResultSet = selectStatement.executeQuery(); + try ( + PreparedStatement selectStatement = conn.prepareStatement("SELECT ks.id, ks.key FROM cloud.keystore ks WHERE ks.key IS NOT null"); + ResultSet selectResultSet = selectStatement.executeQuery(); + ) { while (selectResultSet.next()) { Long keyId = selectResultSet.getLong(1); String preSharedKey = selectResultSet.getString(2); - updateStatement = conn.prepareStatement("UPDATE cloud.keystore ks SET ks.key = ? WHERE ks.id = ?"); - updateStatement.setString(1, DBEncryptionUtil.encrypt(preSharedKey)); - updateStatement.setLong(2, keyId); - updateStatement.executeUpdate(); - updateStatement.close(); + try (PreparedStatement updateStatement = conn.prepareStatement("UPDATE cloud.keystore ks SET ks.key = ? WHERE ks.id = ?");) { + updateStatement.setString(1, DBEncryptionUtil.encrypt(preSharedKey)); + updateStatement.setLong(2, keyId); + updateStatement.executeUpdate(); + } } } catch (SQLException e) { throw new CloudRuntimeException("Exception while encrypting key column in keystore table", e); - } finally { - if (selectResultSet != null) - try { - selectResultSet.close(); - } catch (SQLException e) { - } - if (selectStatement != null) - try { - selectStatement.close(); - } catch (SQLException e) { - } - if (updateStatement != null) - try { - updateStatement.close(); - } catch (SQLException e) { - } } s_logger.debug("Done encrypting keystore's key column"); } private void encryptIpSecPresharedKeysOfRemoteAccessVpn(Connection conn) { - PreparedStatement selectStatement = null; - PreparedStatement updateStatement = null; - ResultSet resultSet = null; - try { - selectStatement = conn.prepareStatement("SELECT id, ipsec_psk FROM `cloud`.`remote_access_vpn`"); - resultSet = selectStatement.executeQuery(); + try ( + PreparedStatement selectStatement = conn.prepareStatement("SELECT id, ipsec_psk FROM `cloud`.`remote_access_vpn`"); + ResultSet resultSet = selectStatement.executeQuery(); + ) { while (resultSet.next()) { Long rowId = resultSet.getLong(1); String preSharedKey = resultSet.getString(2); @@ -132,92 +112,62 @@ public class Upgrade450to451 implements DbUpgrade { } catch (EncryptionOperationNotPossibleException ignored) { s_logger.debug("The ipsec_psk preshared key id=" + rowId + "in remote_access_vpn is not encrypted, encrypting it."); } - updateStatement = conn.prepareStatement("UPDATE `cloud`.`remote_access_vpn` SET ipsec_psk=? WHERE id=?"); - updateStatement.setString(1, DBEncryptionUtil.encrypt(preSharedKey)); - updateStatement.setLong(2, rowId); - updateStatement.executeUpdate(); - updateStatement.close(); + try (PreparedStatement updateStatement = conn.prepareStatement("UPDATE `cloud`.`remote_access_vpn` SET ipsec_psk=? WHERE id=?");) { + updateStatement.setString(1, DBEncryptionUtil.encrypt(preSharedKey)); + updateStatement.setLong(2, rowId); + updateStatement.executeUpdate(); + } } } catch (SQLException e) { throw new CloudRuntimeException("Unable to update the remote_access_vpn's preshared key ipsec_psk column", e); - } finally { - try { - if (resultSet != null) { - resultSet.close(); - } - if ((selectStatement != null) && (!selectStatement.isClosed())) { - selectStatement.close(); - } - if ((updateStatement != null) && (!updateStatement.isClosed())) - updateStatement.close(); - } catch (SQLException e) { - } } s_logger.debug("Done encrypting remote_access_vpn's ipsec_psk column"); } private void encryptStoragePoolUserInfo(Connection conn) { List listOfStatements = new ArrayList(); - try { - PreparedStatement preparedStatement = conn.prepareStatement("SELECT id, user_info FROM `cloud`.`storage_pool` WHERE user_info IS NOT NULL"); - listOfStatements.add(preparedStatement); - ResultSet resultSet = preparedStatement.executeQuery(); + try ( + PreparedStatement selectStatement = conn.prepareStatement("SELECT id, user_info FROM `cloud`.`storage_pool` WHERE user_info IS NOT NULL"); + ResultSet resultSet = selectStatement.executeQuery(); + ) { while (resultSet.next()) { long id = resultSet.getLong(1); String userInfo = resultSet.getString(2); String encryptedUserInfo = DBEncryptionUtil.encrypt(userInfo); - preparedStatement = conn.prepareStatement("UPDATE `cloud`.`storage_pool` SET user_info=? WHERE id=?"); - listOfStatements.add(preparedStatement); - if (encryptedUserInfo == null) - preparedStatement.setNull(1, 12); - else { - preparedStatement.setBytes(1, encryptedUserInfo.getBytes("UTF-8")); + try (PreparedStatement preparedStatement = conn.prepareStatement("UPDATE `cloud`.`storage_pool` SET user_info=? WHERE id=?");) { + listOfStatements.add(preparedStatement); + if (encryptedUserInfo == null) + preparedStatement.setNull(1, 12); + else { + preparedStatement.setBytes(1, encryptedUserInfo.getBytes("UTF-8")); + } + preparedStatement.setLong(2, id); + preparedStatement.executeUpdate(); } - preparedStatement.setLong(2, id); - preparedStatement.executeUpdate(); - preparedStatement.close(); } } catch (SQLException e) { throw new CloudRuntimeException("Unable encrypt storage pool user info ", e); } catch (UnsupportedEncodingException e) { throw new CloudRuntimeException("Unable encrypt storage pool user info ", e); - } finally { - TransactionLegacy.closePstmts(listOfStatements); } s_logger.debug("Done encrypting storage_pool's user_info column"); } private void updateUserVmDetailsWithNicAdapterType(Connection conn) { - PreparedStatement insertPstmt = null; - try { - insertPstmt = conn.prepareStatement("INSERT INTO `cloud`.`user_vm_details`(vm_id,name,value,display) select v.id as vm_id, details.name, details.value, details.display from `cloud`.`vm_instance` as v, `cloud`.`vm_template_details` as details where v.removed is null and v.vm_template_id=details.template_id and details.name='nicAdapter' and details.template_id in (select id from `cloud`.`vm_template` where hypervisor_type = 'vmware') and v.id not in (select vm_id from `cloud`.`user_vm_details` where name='nicAdapter');"); + try (PreparedStatement insertPstmt = conn.prepareStatement("INSERT INTO `cloud`.`user_vm_details`(vm_id,name,value,display) select v.id as vm_id, details.name, details.value, details.display from `cloud`.`vm_instance` as v, `cloud`.`vm_template_details` as details where v.removed is null and v.vm_template_id=details.template_id and details.name='nicAdapter' and details.template_id in (select id from `cloud`.`vm_template` where hypervisor_type = 'vmware') and v.id not in (select vm_id from `cloud`.`user_vm_details` where name='nicAdapter');");) { insertPstmt.executeUpdate(); } catch (SQLException e) { throw new CloudRuntimeException("Failed to update user_vm_details table with nicAdapter entries by copying from vm_template_detail table", e); - } finally { - try { - if (insertPstmt != null) - insertPstmt.close(); - } catch (SQLException e) { - } } s_logger.debug("Done. Updated user_vm_details table with nicAdapter entries by copying from vm_template_detail table. This affects only VM/templates with hypervisor_type as VMware."); } private void upgradeVMWareLocalStorage(Connection conn) { - PreparedStatement updatePstmt = null; - try { - updatePstmt = conn.prepareStatement("UPDATE storage_pool SET pool_type='VMFS',host_address=@newaddress WHERE (@newaddress:=concat('VMFS datastore: ', path)) IS NOT NULL AND scope = 'HOST' AND pool_type = 'LVM' AND id IN (SELECT * FROM (SELECT storage_pool.id FROM storage_pool,cluster WHERE storage_pool.cluster_id = cluster.id AND cluster.hypervisor_type='VMware') AS t);"); + try (PreparedStatement updatePstmt = conn.prepareStatement("UPDATE storage_pool SET pool_type='VMFS',host_address=@newaddress WHERE (@newaddress:=concat('VMFS datastore: ', path)) IS NOT NULL AND scope = 'HOST' AND pool_type = 'LVM' AND id IN (SELECT * FROM (SELECT storage_pool.id FROM storage_pool,cluster WHERE storage_pool.cluster_id = cluster.id AND cluster.hypervisor_type='VMware') AS t);");) { updatePstmt.executeUpdate(); + s_logger.debug("Done, upgraded VMWare local storage pool type to VMFS and host_address to the VMFS format"); } catch (SQLException e) { throw new CloudRuntimeException("Unable to upgrade VMWare local storage pool type", e); - } finally { - try { - if (updatePstmt != null) - updatePstmt.close(); - } catch (SQLException e) { - } - s_logger.debug("Done, upgraded VMWare local storage pool type to VMFS and host_address to the VMFS format"); } } }