Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 51 additions & 87 deletions engine/schema/src/com/cloud/upgrade/dao/Upgrade421to430.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ public void performDataMigration(Connection conn) {
}

private void upgradeMemoryOfSsvmOffering(Connection conn) {
PreparedStatement updatePstmt = null;
PreparedStatement selectPstmt = null;
ResultSet selectResultSet = null;
int newRamSize = 512; //512MB
long serviceOfferingId = 0;

Expand All @@ -79,10 +76,11 @@ private void upgradeMemoryOfSsvmOffering(Connection conn) {
* We should not update/modify any user-defined offering.
*/

try {
selectPstmt = conn.prepareStatement("SELECT id FROM `cloud`.`service_offering` WHERE vm_type='secondarystoragevm'");
updatePstmt = conn.prepareStatement("UPDATE `cloud`.`service_offering` SET ram_size=? WHERE id=?");
selectResultSet = selectPstmt.executeQuery();
try (
PreparedStatement selectPstmt = conn.prepareStatement("SELECT id FROM `cloud`.`service_offering` WHERE vm_type='secondarystoragevm'");
PreparedStatement updatePstmt = conn.prepareStatement("UPDATE `cloud`.`service_offering` SET ram_size=? WHERE id=?");
ResultSet selectResultSet = selectPstmt.executeQuery();
) {
if(selectResultSet.next()) {
serviceOfferingId = selectResultSet.getLong("id");
}
Expand All @@ -92,26 +90,11 @@ private void upgradeMemoryOfSsvmOffering(Connection conn) {
updatePstmt.executeUpdate();
} catch (SQLException e) {
throw new CloudRuntimeException("Unable to upgrade ram_size of service offering for secondary storage vm. ", e);
} finally {
try {
if (selectPstmt != null) {
selectPstmt.close();
}
if (selectResultSet != null) {
selectResultSet.close();
}
if (updatePstmt != null) {
updatePstmt.close();
}
} catch (SQLException e) {
}
}
s_logger.debug("Done upgrading RAM for service offering of Secondary Storage VM to " + newRamSize);
}

private void encryptLdapConfigParams(Connection conn) {
PreparedStatement pstmt = null;

String[][] ldapParams = { {"ldap.user.object", "inetOrgPerson", "Sets the object type of users within LDAP"},
{"ldap.username.attribute", "uid", "Sets the username attribute used within LDAP"}, {"ldap.email.attribute", "mail", "Sets the email attribute used within LDAP"},
{"ldap.firstname.attribute", "givenname", "Sets the firstname attribute used within LDAP"},
Expand All @@ -122,108 +105,89 @@ private void encryptLdapConfigParams(Connection conn) {
String insertSql = "INSERT INTO `cloud`.`configuration`(category, instance, component, name, value, description) VALUES ('Secure', 'DEFAULT', 'management-server', ?, ?, "
+ "?) ON DUPLICATE KEY UPDATE category='Secure';";

try {

pstmt = conn.prepareStatement(insertSql);
try (PreparedStatement pstmt_insert_ldap_parameters = conn.prepareStatement(insertSql);){
for (String[] ldapParam : ldapParams) {
String name = ldapParam[0];
String value = ldapParam[1];
String desc = ldapParam[2];
String encryptedValue = DBEncryptionUtil.encrypt(value);
pstmt.setString(1, name);
pstmt.setBytes(2, encryptedValue.getBytes("UTF-8"));
pstmt.setString(3, desc);
pstmt.executeUpdate();
pstmt_insert_ldap_parameters.setString(1, name);
pstmt_insert_ldap_parameters.setBytes(2, encryptedValue.getBytes("UTF-8"));
pstmt_insert_ldap_parameters.setString(3, desc);
pstmt_insert_ldap_parameters.executeUpdate();
}

pstmt.close();

/**
* if encrypted, decrypt the ldap hostname and port and then update as they are not encrypted now.
*/
pstmt = conn.prepareStatement("SELECT conf.value FROM `cloud`.`configuration` conf WHERE conf.name='ldap.hostname'");
ResultSet resultSet = pstmt.executeQuery();
String hostname = null;
String port;
int portNumber = 0;
if (resultSet.next()) {
hostname = DBEncryptionUtil.decrypt(resultSet.getString(1));
}

pstmt.close();

pstmt = conn.prepareStatement("SELECT conf.value FROM `cloud`.`configuration` conf WHERE conf.name='ldap.port'");
resultSet = pstmt.executeQuery();
if (resultSet.next()) {
port = DBEncryptionUtil.decrypt(resultSet.getString(1));
if (StringUtils.isNotBlank(port)) {
portNumber = Integer.parseInt(port);
try (
PreparedStatement pstmt_ldap_hostname = conn.prepareStatement("SELECT conf.value FROM `cloud`.`configuration` conf WHERE conf.name='ldap.hostname'");
ResultSet resultSet_ldap_hostname = pstmt_ldap_hostname.executeQuery();
) {
String hostname = null;
String port;
int portNumber = 0;
if (resultSet_ldap_hostname.next()) {
hostname = DBEncryptionUtil.decrypt(resultSet_ldap_hostname.getString(1));
}
}
pstmt.close();

if (StringUtils.isNotBlank(hostname)) {
pstmt = conn.prepareStatement("INSERT INTO `cloud`.`ldap_configuration`(hostname, port) VALUES(?,?)");
pstmt.setString(1, hostname);
if (portNumber != 0) {
pstmt.setInt(2, portNumber);
} else {
pstmt.setNull(2, Types.INTEGER);

try (
PreparedStatement pstmt_ldap_port = conn.prepareStatement("SELECT conf.value FROM `cloud`.`configuration` conf WHERE conf.name='ldap.port'");
ResultSet resultSet_ldap_port = pstmt_ldap_port.executeQuery();
) {
if (resultSet_ldap_port.next()) {
port = DBEncryptionUtil.decrypt(resultSet_ldap_port.getString(1));
if (StringUtils.isNotBlank(port)) {
portNumber = Integer.parseInt(port);
}
}

if (StringUtils.isNotBlank(hostname)) {
try (PreparedStatement pstmt_insert_ldap_hostname_port = conn.prepareStatement("INSERT INTO `cloud`.`ldap_configuration`(hostname, port) VALUES(?,?)");) {
pstmt_insert_ldap_hostname_port.setString(1, hostname);
if (portNumber != 0) {
pstmt_insert_ldap_hostname_port.setInt(2, portNumber);
} else {
pstmt_insert_ldap_hostname_port.setNull(2, Types.INTEGER);
}
pstmt_insert_ldap_hostname_port.executeUpdate();
}
}
}
pstmt.executeUpdate();
pstmt.close();
}

} catch (SQLException e) {
throw new CloudRuntimeException("Unable to insert ldap configuration values ", e);
} catch (UnsupportedEncodingException e) {
throw new CloudRuntimeException("Unable to insert ldap configuration values ", e);
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
}
}
s_logger.debug("Done encrypting ldap Config values");

}

private void encryptImageStoreDetails(Connection conn) {
s_logger.debug("Encrypting image store details");
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = conn.prepareStatement("select id, value from `cloud`.`image_store_details` where name = 'key' or name = 'secretkey'");
rs = pstmt.executeQuery();
try (
PreparedStatement selectPstmt = conn.prepareStatement("select id, value from `cloud`.`image_store_details` where name = 'key' or name = 'secretkey'");
ResultSet rs = selectPstmt.executeQuery();
) {
while (rs.next()) {
long id = rs.getLong(1);
String value = rs.getString(2);
if (value == null) {
continue;
}
String encryptedValue = DBEncryptionUtil.encrypt(value);
pstmt = conn.prepareStatement("update `cloud`.`image_store_details` set value=? where id=?");
pstmt.setBytes(1, encryptedValue.getBytes("UTF-8"));
pstmt.setLong(2, id);
pstmt.executeUpdate();
try (PreparedStatement updatePstmt = conn.prepareStatement("update `cloud`.`image_store_details` set value=? where id=?");) {
updatePstmt.setBytes(1, encryptedValue.getBytes("UTF-8"));
updatePstmt.setLong(2, id);
updatePstmt.executeUpdate();
}
}
} catch (SQLException e) {
throw new CloudRuntimeException("Unable encrypt image_store_details values ", e);
} catch (UnsupportedEncodingException e) {
throw new CloudRuntimeException("Unable encrypt image_store_details values ", e);
} finally {
try {
if (rs != null) {
rs.close();
}

if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
}
}
s_logger.debug("Done encrypting image_store_details");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,10 @@ public void start() throws IOException {
@Override
public void run() {

try {
Socket socket = serverSocket.accept();
try (Socket socket = serverSocket.accept();) {

if (verbose)
System.out.println("[" + this + "] INFO: Clien connected: " + socket.getRemoteSocketAddress() + ".");
System.out.println("[" + this + "] INFO: Client connected: " + socket.getRemoteSocketAddress() + ".");

InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
Expand Down Expand Up @@ -137,10 +136,6 @@ public void run() {
os.close();
} catch (Throwable e) {
}
try {
socket.close();
} catch (Throwable e) {
}
try {
serverSocket.close();
} catch (Throwable e) {
Expand Down