Skip to content

Commit bbe8a6d

Browse files
author
Kishan Kavala
committed
CLOUDSTACK-4115 : Encrypt password in cluster_details table. This fix is to handle upgrades from versions earlier than 3.0.5 and 4.0. Upgrade was not handled when the cluster_details password encryption was introduced.
1 parent a0f23d0 commit bbe8a6d

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

‎engine/schema/src/com/cloud/upgrade/dao/Upgrade302to40.java‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public void performDataMigration(Connection conn) {
7474
setupExternalNetworkDevices(conn);
7575
fixZoneUsingExternalDevices(conn);
7676
encryptConfig(conn);
77+
encryptClusterDetails(conn);
7778
}
7879

7980
@Override
@@ -1072,4 +1073,42 @@ private void encryptConfig(Connection conn){
10721073
}
10731074
s_logger.debug("Done encrypting Config values");
10741075
}
1076+
1077+
private void encryptClusterDetails(Connection conn) {
1078+
s_logger.debug("Encrypting cluster details");
1079+
PreparedStatement pstmt = null;
1080+
ResultSet rs = null;
1081+
try {
1082+
pstmt = conn.prepareStatement("select id, value from `cloud`.`cluster_details` where name = 'password'");
1083+
rs = pstmt.executeQuery();
1084+
while (rs.next()) {
1085+
long id = rs.getLong(1);
1086+
String value = rs.getString(2);
1087+
if (value == null) {
1088+
continue;
1089+
}
1090+
String encryptedValue = DBEncryptionUtil.encrypt(value);
1091+
pstmt = conn.prepareStatement("update `cloud`.`cluster_details` set value=? where id=?");
1092+
pstmt.setBytes(1, encryptedValue.getBytes("UTF-8"));
1093+
pstmt.setLong(2, id);
1094+
pstmt.executeUpdate();
1095+
}
1096+
} catch (SQLException e) {
1097+
throw new CloudRuntimeException("Unable encrypt cluster_details values ", e);
1098+
} catch (UnsupportedEncodingException e) {
1099+
throw new CloudRuntimeException("Unable encrypt cluster_details values ", e);
1100+
} finally {
1101+
try {
1102+
if (rs != null) {
1103+
rs.close();
1104+
}
1105+
1106+
if (pstmt != null) {
1107+
pstmt.close();
1108+
}
1109+
} catch (SQLException e) {
1110+
}
1111+
}
1112+
s_logger.debug("Done encrypting cluster_details");
1113+
}
10751114
}

‎engine/schema/src/com/cloud/upgrade/dao/Upgrade304to305.java‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package com.cloud.upgrade.dao;
2020

2121
import java.io.File;
22+
import java.io.UnsupportedEncodingException;
2223
import java.sql.Connection;
2324
import java.sql.PreparedStatement;
2425
import java.sql.ResultSet;
@@ -27,6 +28,7 @@
2728
import java.util.List;
2829
import java.util.UUID;
2930

31+
import com.cloud.utils.crypt.DBEncryptionUtil;
3032
import org.apache.log4j.Logger;
3133

3234
import com.cloud.utils.exception.CloudRuntimeException;
@@ -68,6 +70,7 @@ public void performDataMigration(Connection conn) {
6870
fixZoneUsingExternalDevices(conn);
6971
// updateSystemVms(conn);
7072
fixForeignKeys(conn);
73+
encryptClusterDetails(conn);
7174
}
7275

7376
@Override
@@ -455,4 +458,42 @@ private void fixForeignKeys(Connection conn) {
455458
throw new CloudRuntimeException("Unable to execute ssh_keypairs table update for adding domain_id foreign key", e);
456459
}
457460
}
461+
462+
private void encryptClusterDetails(Connection conn) {
463+
s_logger.debug("Encrypting cluster details");
464+
PreparedStatement pstmt = null;
465+
ResultSet rs = null;
466+
try {
467+
pstmt = conn.prepareStatement("select id, value from `cloud`.`cluster_details` where name = 'password'");
468+
rs = pstmt.executeQuery();
469+
while (rs.next()) {
470+
long id = rs.getLong(1);
471+
String value = rs.getString(2);
472+
if (value == null) {
473+
continue;
474+
}
475+
String encryptedValue = DBEncryptionUtil.encrypt(value);
476+
pstmt = conn.prepareStatement("update `cloud`.`cluster_details` set value=? where id=?");
477+
pstmt.setBytes(1, encryptedValue.getBytes("UTF-8"));
478+
pstmt.setLong(2, id);
479+
pstmt.executeUpdate();
480+
}
481+
} catch (SQLException e) {
482+
throw new CloudRuntimeException("Unable encrypt cluster_details values ", e);
483+
} catch (UnsupportedEncodingException e) {
484+
throw new CloudRuntimeException("Unable encrypt cluster_details values ", e);
485+
} finally {
486+
try {
487+
if (rs != null) {
488+
rs.close();
489+
}
490+
491+
if (pstmt != null) {
492+
pstmt.close();
493+
}
494+
} catch (SQLException e) {
495+
}
496+
}
497+
s_logger.debug("Done encrypting cluster_details");
498+
}
458499
}

0 commit comments

Comments
 (0)