|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package com.cloud.upgrade.dao; |
| 19 | + |
| 20 | +import com.cloud.utils.crypt.DBEncryptionUtil; |
| 21 | +import com.cloud.utils.db.TransactionLegacy; |
| 22 | +import com.cloud.utils.exception.CloudRuntimeException; |
| 23 | +import com.cloud.utils.script.Script; |
| 24 | +import org.apache.log4j.Logger; |
| 25 | + |
| 26 | +import java.io.File; |
| 27 | +import java.io.UnsupportedEncodingException; |
| 28 | +import java.sql.Connection; |
| 29 | +import java.sql.PreparedStatement; |
| 30 | +import java.sql.ResultSet; |
| 31 | +import java.sql.SQLException; |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.List; |
| 34 | + |
| 35 | +public class Upgrade450to451 implements DbUpgrade { |
| 36 | + final static Logger s_logger = Logger.getLogger(Upgrade450to451.class); |
| 37 | + |
| 38 | + @Override |
| 39 | + public String[] getUpgradableVersionRange() { |
| 40 | + return new String[] {"4.5.0", "4.5.1"}; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public String getUpgradedVersion() { |
| 45 | + return "4.5.1"; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public boolean supportsRollingUpgrade() { |
| 50 | + return false; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public File[] getPrepareScripts() { |
| 55 | + String script = Script.findScript("", "db/schema-450to451.sql"); |
| 56 | + if (script == null) { |
| 57 | + throw new CloudRuntimeException("Unable to find db/schema-450to451.sql"); |
| 58 | + } |
| 59 | + return new File[] {new File(script)}; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public File[] getCleanupScripts() { |
| 64 | + String script = Script.findScript("", "db/schema-450to451-cleanup.sql"); |
| 65 | + if (script == null) { |
| 66 | + throw new CloudRuntimeException("Unable to find db/schema-450to451-cleanup.sql"); |
| 67 | + } |
| 68 | + |
| 69 | + return new File[] {new File(script)}; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void performDataMigration(Connection conn) { |
| 74 | + encryptKeyInKeyStore(conn); |
| 75 | + encryptIpSecPresharedKeysOfRemoteAccessVpn(conn); |
| 76 | + encryptStoragePoolUserInfo(conn); |
| 77 | + } |
| 78 | + |
| 79 | + private void encryptKeyInKeyStore(Connection conn) { |
| 80 | + PreparedStatement selectStatement = null; |
| 81 | + ResultSet selectResultSet = null; |
| 82 | + PreparedStatement updateStatement = null; |
| 83 | + try { |
| 84 | + selectStatement = conn.prepareStatement("SELECT ks.id, ks.key FROM cloud.keystore ks WHERE ks.key IS NOT null"); |
| 85 | + selectResultSet = selectStatement.executeQuery(); |
| 86 | + while (selectResultSet.next()) { |
| 87 | + updateStatement = conn.prepareStatement("UPDATE cloud.keystore ks SET ks.key = ? WHERE ks.id = ?"); |
| 88 | + updateStatement.setString(1, DBEncryptionUtil.encrypt(selectResultSet.getString(2))); |
| 89 | + updateStatement.setLong(2, selectResultSet.getLong(1)); |
| 90 | + updateStatement.executeUpdate(); |
| 91 | + updateStatement.close(); |
| 92 | + } |
| 93 | + } catch (SQLException e) { |
| 94 | + throw new CloudRuntimeException("Exception while encrypting key column in keystore table", e); |
| 95 | + } finally { |
| 96 | + if (selectResultSet != null) |
| 97 | + try { |
| 98 | + selectResultSet.close(); |
| 99 | + } catch (SQLException e) { |
| 100 | + } |
| 101 | + if (selectStatement != null) |
| 102 | + try { |
| 103 | + selectStatement.close(); |
| 104 | + } catch (SQLException e) { |
| 105 | + } |
| 106 | + if (updateStatement != null) |
| 107 | + try { |
| 108 | + updateStatement.close(); |
| 109 | + } catch (SQLException e) { |
| 110 | + } |
| 111 | + } |
| 112 | + s_logger.debug("Done encrypting keystore's key column"); |
| 113 | + } |
| 114 | + |
| 115 | + private void encryptIpSecPresharedKeysOfRemoteAccessVpn(Connection conn) { |
| 116 | + PreparedStatement selectStatement = null; |
| 117 | + PreparedStatement updateStatement = null; |
| 118 | + ResultSet resultSet = null; |
| 119 | + try { |
| 120 | + selectStatement = conn.prepareStatement("SELECT id, ipsec_psk FROM `cloud`.`remote_access_vpn`"); |
| 121 | + resultSet = selectStatement.executeQuery(); |
| 122 | + while (resultSet.next()) { |
| 123 | + String preSharedKey = resultSet.getString(2); |
| 124 | + updateStatement = conn.prepareStatement("UPDATE `cloud`.`remote_access_vpn` SET ipsec_psk=? WHERE id=?"); |
| 125 | + updateStatement.setString(1, DBEncryptionUtil.encrypt(preSharedKey)); |
| 126 | + updateStatement.setLong(2, resultSet.getLong(1)); |
| 127 | + updateStatement.executeUpdate(); |
| 128 | + updateStatement.close(); |
| 129 | + } |
| 130 | + } catch (SQLException e) { |
| 131 | + throw new CloudRuntimeException("Unable to update the remote_access_vpn's preshared key ipsec_psk column", e); |
| 132 | + } finally { |
| 133 | + try { |
| 134 | + if (resultSet != null) { |
| 135 | + resultSet.close(); |
| 136 | + } |
| 137 | + if ((selectStatement != null) && (!selectStatement.isClosed())) { |
| 138 | + selectStatement.close(); |
| 139 | + } |
| 140 | + if ((updateStatement != null) && (!updateStatement.isClosed())) |
| 141 | + updateStatement.close(); |
| 142 | + } catch (SQLException e) { |
| 143 | + } |
| 144 | + } |
| 145 | + s_logger.debug("Done encrypting remote_access_vpn's ipsec_psk column"); |
| 146 | + } |
| 147 | + |
| 148 | + private void encryptStoragePoolUserInfo(Connection conn) { |
| 149 | + List<PreparedStatement> listOfStatements = new ArrayList<PreparedStatement>(); |
| 150 | + try { |
| 151 | + PreparedStatement preparedStatement = conn.prepareStatement("SELECT id, user_info FROM `cloud`.`storage_pool` WHERE user_info IS NOT NULL"); |
| 152 | + listOfStatements.add(preparedStatement); |
| 153 | + ResultSet resultSet = preparedStatement.executeQuery(); |
| 154 | + while (resultSet.next()) { |
| 155 | + long id = resultSet.getLong(1); |
| 156 | + String userInfo = resultSet.getString(2); |
| 157 | + String encryptedUserInfo = DBEncryptionUtil.encrypt(userInfo); |
| 158 | + preparedStatement = conn.prepareStatement("UPDATE `cloud`.`storage_pool` SET user_info=? WHERE id=?"); |
| 159 | + listOfStatements.add(preparedStatement); |
| 160 | + if (encryptedUserInfo == null) |
| 161 | + preparedStatement.setNull(1, 12); |
| 162 | + else { |
| 163 | + preparedStatement.setBytes(1, encryptedUserInfo.getBytes("UTF-8")); |
| 164 | + } |
| 165 | + preparedStatement.setLong(2, id); |
| 166 | + preparedStatement.executeUpdate(); |
| 167 | + preparedStatement.close(); |
| 168 | + } |
| 169 | + } catch (SQLException e) { |
| 170 | + throw new CloudRuntimeException("Unable encrypt storage pool user info ", e); |
| 171 | + } catch (UnsupportedEncodingException e) { |
| 172 | + throw new CloudRuntimeException("Unable encrypt storage pool user info ", e); |
| 173 | + } finally { |
| 174 | + TransactionLegacy.closePstmts(listOfStatements); |
| 175 | + } |
| 176 | + s_logger.debug("Done encrypting storage_pool's user_info column"); |
| 177 | + } |
| 178 | +} |
0 commit comments