Skip to content

Commit cfd4573

Browse files
committed
engine: Add Upgrade path from 4.5.0 to 4.5.1
Encrypts: - Remote access vpn preshared key - StoragePool's user info - Keystore's key This closes apache#112 Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
1 parent c198dfd commit cfd4573

8 files changed

Lines changed: 298 additions & 68 deletions

File tree

‎engine/schema/src/com/cloud/network/dao/RemoteAccessVpnVO.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@
1616
// under the License.
1717
package com.cloud.network.dao;
1818

19-
import java.util.UUID;
19+
import com.cloud.network.RemoteAccessVpn;
20+
import com.cloud.utils.db.Encrypt;
2021

2122
import javax.persistence.Column;
2223
import javax.persistence.Entity;
2324
import javax.persistence.GeneratedValue;
2425
import javax.persistence.GenerationType;
2526
import javax.persistence.Id;
2627
import javax.persistence.Table;
27-
28-
import com.cloud.network.RemoteAccessVpn;
28+
import java.util.UUID;
2929

3030
@Entity
3131
@Table(name = ("remote_access_vpn"))
@@ -48,6 +48,7 @@ public class RemoteAccessVpnVO implements RemoteAccessVpn {
4848
@Column(name = "ip_range")
4949
private String ipRange;
5050

51+
@Encrypt
5152
@Column(name = "ipsec_psk")
5253
private String ipsecPresharedKey;
5354

‎engine/schema/src/com/cloud/upgrade/DatabaseUpgradeChecker.java‎

Lines changed: 42 additions & 39 deletions
Large diffs are not rendered by default.
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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+
}

‎engine/schema/src/org/apache/cloudstack/engine/cloud/entity/api/db/VMEntityVO.java‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
// under the License.
1717
package org.apache.cloudstack.engine.cloud.entity.api.db;
1818

19-
import java.util.Date;
20-
import java.util.List;
21-
import java.util.Map;
22-
import java.util.Random;
23-
import java.util.UUID;
19+
import com.cloud.hypervisor.Hypervisor.HypervisorType;
20+
import com.cloud.utils.db.Encrypt;
21+
import com.cloud.utils.db.GenericDao;
22+
import com.cloud.utils.db.StateMachine;
23+
import com.cloud.utils.fsm.FiniteStateObject;
24+
import com.cloud.vm.VirtualMachine;
25+
import com.cloud.vm.VirtualMachine.State;
2426

2527
import javax.persistence.Column;
2628
import javax.persistence.DiscriminatorColumn;
@@ -36,14 +38,11 @@
3638
import javax.persistence.Temporal;
3739
import javax.persistence.TemporalType;
3840
import javax.persistence.Transient;
39-
40-
import com.cloud.hypervisor.Hypervisor.HypervisorType;
41-
import com.cloud.utils.db.Encrypt;
42-
import com.cloud.utils.db.GenericDao;
43-
import com.cloud.utils.db.StateMachine;
44-
import com.cloud.utils.fsm.FiniteStateObject;
45-
import com.cloud.vm.VirtualMachine;
46-
import com.cloud.vm.VirtualMachine.State;
41+
import java.security.SecureRandom;
42+
import java.util.Date;
43+
import java.util.List;
44+
import java.util.Map;
45+
import java.util.UUID;
4746

4847
@Entity
4948
@Table(name = "vm_instance")
@@ -190,7 +189,7 @@ public VMEntityVO(long id, long serviceOfferingId, String name, String instanceN
190189
this.type = type;
191190
this.guestOSId = guestOSId;
192191
this.haEnabled = haEnabled;
193-
vncPassword = Long.toHexString(new Random().nextLong());
192+
vncPassword = Long.toHexString(new SecureRandom().nextLong());
194193
state = State.Stopped;
195194
this.accountId = accountId;
196195
this.domainId = domainId;

‎engine/schema/src/org/apache/cloudstack/storage/datastore/db/StoragePoolVO.java‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@
1616
// under the License.
1717
package org.apache.cloudstack.storage.datastore.db;
1818

19-
import java.util.Date;
20-
import java.util.UUID;
19+
import com.cloud.hypervisor.Hypervisor.HypervisorType;
20+
import com.cloud.storage.ScopeType;
21+
import com.cloud.storage.Storage.StoragePoolType;
22+
import com.cloud.storage.StoragePool;
23+
import com.cloud.storage.StoragePoolStatus;
24+
import com.cloud.utils.UriUtils;
25+
import com.cloud.utils.db.Encrypt;
26+
import com.cloud.utils.db.GenericDao;
2127

2228
import javax.persistence.Column;
2329
import javax.persistence.Entity;
@@ -28,14 +34,8 @@
2834
import javax.persistence.TableGenerator;
2935
import javax.persistence.Temporal;
3036
import javax.persistence.TemporalType;
31-
32-
import com.cloud.hypervisor.Hypervisor.HypervisorType;
33-
import com.cloud.storage.ScopeType;
34-
import com.cloud.storage.Storage.StoragePoolType;
35-
import com.cloud.storage.StoragePool;
36-
import com.cloud.storage.StoragePoolStatus;
37-
import com.cloud.utils.UriUtils;
38-
import com.cloud.utils.db.GenericDao;
37+
import java.util.Date;
38+
import java.util.UUID;
3939

4040
@Entity
4141
@Table(name = "storage_pool")
@@ -98,6 +98,7 @@ public class StoragePoolVO implements StoragePool {
9898
@Column(name = "port")
9999
private int port;
100100

101+
@Encrypt
101102
@Column(name = "user_info")
102103
private String userInfo;
103104

‎framework/security/src/org/apache/cloudstack/framework/security/keystore/KeystoreVO.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@
1616
// under the License.
1717
package org.apache.cloudstack.framework.security.keystore;
1818

19+
import com.cloud.utils.db.Encrypt;
20+
import org.apache.cloudstack.api.InternalIdentity;
21+
1922
import javax.persistence.Column;
2023
import javax.persistence.Entity;
2124
import javax.persistence.GeneratedValue;
2225
import javax.persistence.GenerationType;
2326
import javax.persistence.Id;
2427
import javax.persistence.Table;
2528

26-
import org.apache.cloudstack.api.InternalIdentity;
27-
2829
@Entity
2930
@Table(name = "keystore")
3031
public class KeystoreVO implements InternalIdentity {
@@ -39,6 +40,7 @@ public class KeystoreVO implements InternalIdentity {
3940
@Column(name = "certificate", length = 65535)
4041
private String certificate;
4142

43+
@Encrypt
4244
@Column(name = "key", length = 65535)
4345
private String key;
4446

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
--;
19+
-- Schema cleanup from 4.5.0 to 4.5.1;
20+
--;

‎setup/db/db/schema-450to451.sql‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
--;
19+
-- Schema upgrade from 4.5.0 to 4.5.1;
20+
--;
21+
22+
UPDATE IGNORE `cloud`.`configuration` SET `default_value`='PBKDF2,SHA256SALT,MD5,LDAP,SAML2,PLAINTEXT' WHERE name='user.authenticators.order';
23+
UPDATE IGNORE `cloud`.`configuration` SET `value`='PBKDF2,SHA256SALT,MD5,LDAP,SAML2,PLAINTEXT' WHERE name='user.authenticators.order';
24+
UPDATE IGNORE `cloud`.`configuration` SET `default_value`='PBKDF2,SHA256SALT,MD5,LDAP,SAML2,PLAINTEXT' WHERE name='user.password.encoders.order';
25+
UPDATE IGNORE `cloud`.`configuration` SET `value`='PBKDF2,SHA256SALT,MD5,LDAP,SAML2,PLAINTEXT' WHERE name='user.password.encoders.order';
26+
UPDATE IGNORE `cloud`.`configuration` SET `value`="MD5,LDAP,PLAINTEXT" WHERE `name`="user.password.encoders.exclude";

0 commit comments

Comments
 (0)