Skip to content

Commit 9681fef

Browse files
committed
coverity in upgrade code 1164030: memory upgrade for ssvm 1164031: image store details encryption
Signed-off-by: Daan Hoogland <daan.hoogland@gmail.com>
1 parent c539595 commit 9681fef

1 file changed

Lines changed: 51 additions & 87 deletions

File tree

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

Lines changed: 51 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ public void performDataMigration(Connection conn) {
6868
}
6969

7070
private void upgradeMemoryOfSsvmOffering(Connection conn) {
71-
PreparedStatement updatePstmt = null;
72-
PreparedStatement selectPstmt = null;
73-
ResultSet selectResultSet = null;
7471
int newRamSize = 512; //512MB
7572
long serviceOfferingId = 0;
7673

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

82-
try {
83-
selectPstmt = conn.prepareStatement("SELECT id FROM `cloud`.`service_offering` WHERE vm_type='secondarystoragevm'");
84-
updatePstmt = conn.prepareStatement("UPDATE `cloud`.`service_offering` SET ram_size=? WHERE id=?");
85-
selectResultSet = selectPstmt.executeQuery();
79+
try (
80+
PreparedStatement selectPstmt = conn.prepareStatement("SELECT id FROM `cloud`.`service_offering` WHERE vm_type='secondarystoragevm'");
81+
PreparedStatement updatePstmt = conn.prepareStatement("UPDATE `cloud`.`service_offering` SET ram_size=? WHERE id=?");
82+
ResultSet selectResultSet = selectPstmt.executeQuery();
83+
) {
8684
if(selectResultSet.next()) {
8785
serviceOfferingId = selectResultSet.getLong("id");
8886
}
@@ -92,26 +90,11 @@ private void upgradeMemoryOfSsvmOffering(Connection conn) {
9290
updatePstmt.executeUpdate();
9391
} catch (SQLException e) {
9492
throw new CloudRuntimeException("Unable to upgrade ram_size of service offering for secondary storage vm. ", e);
95-
} finally {
96-
try {
97-
if (selectPstmt != null) {
98-
selectPstmt.close();
99-
}
100-
if (selectResultSet != null) {
101-
selectResultSet.close();
102-
}
103-
if (updatePstmt != null) {
104-
updatePstmt.close();
105-
}
106-
} catch (SQLException e) {
107-
}
10893
}
10994
s_logger.debug("Done upgrading RAM for service offering of Secondary Storage VM to " + newRamSize);
11095
}
11196

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

125-
try {
126-
127-
pstmt = conn.prepareStatement(insertSql);
108+
try (PreparedStatement pstmt_insert_ldap_parameters = conn.prepareStatement(insertSql);){
128109
for (String[] ldapParam : ldapParams) {
129110
String name = ldapParam[0];
130111
String value = ldapParam[1];
131112
String desc = ldapParam[2];
132113
String encryptedValue = DBEncryptionUtil.encrypt(value);
133-
pstmt.setString(1, name);
134-
pstmt.setBytes(2, encryptedValue.getBytes("UTF-8"));
135-
pstmt.setString(3, desc);
136-
pstmt.executeUpdate();
114+
pstmt_insert_ldap_parameters.setString(1, name);
115+
pstmt_insert_ldap_parameters.setBytes(2, encryptedValue.getBytes("UTF-8"));
116+
pstmt_insert_ldap_parameters.setString(3, desc);
117+
pstmt_insert_ldap_parameters.executeUpdate();
137118
}
138119

139-
pstmt.close();
140-
141120
/**
142121
* if encrypted, decrypt the ldap hostname and port and then update as they are not encrypted now.
143122
*/
144-
pstmt = conn.prepareStatement("SELECT conf.value FROM `cloud`.`configuration` conf WHERE conf.name='ldap.hostname'");
145-
ResultSet resultSet = pstmt.executeQuery();
146-
String hostname = null;
147-
String port;
148-
int portNumber = 0;
149-
if (resultSet.next()) {
150-
hostname = DBEncryptionUtil.decrypt(resultSet.getString(1));
151-
}
152-
153-
pstmt.close();
154-
155-
pstmt = conn.prepareStatement("SELECT conf.value FROM `cloud`.`configuration` conf WHERE conf.name='ldap.port'");
156-
resultSet = pstmt.executeQuery();
157-
if (resultSet.next()) {
158-
port = DBEncryptionUtil.decrypt(resultSet.getString(1));
159-
if (StringUtils.isNotBlank(port)) {
160-
portNumber = Integer.parseInt(port);
123+
try (
124+
PreparedStatement pstmt_ldap_hostname = conn.prepareStatement("SELECT conf.value FROM `cloud`.`configuration` conf WHERE conf.name='ldap.hostname'");
125+
ResultSet resultSet_ldap_hostname = pstmt_ldap_hostname.executeQuery();
126+
) {
127+
String hostname = null;
128+
String port;
129+
int portNumber = 0;
130+
if (resultSet_ldap_hostname.next()) {
131+
hostname = DBEncryptionUtil.decrypt(resultSet_ldap_hostname.getString(1));
161132
}
162-
}
163-
pstmt.close();
164-
165-
if (StringUtils.isNotBlank(hostname)) {
166-
pstmt = conn.prepareStatement("INSERT INTO `cloud`.`ldap_configuration`(hostname, port) VALUES(?,?)");
167-
pstmt.setString(1, hostname);
168-
if (portNumber != 0) {
169-
pstmt.setInt(2, portNumber);
170-
} else {
171-
pstmt.setNull(2, Types.INTEGER);
133+
134+
try (
135+
PreparedStatement pstmt_ldap_port = conn.prepareStatement("SELECT conf.value FROM `cloud`.`configuration` conf WHERE conf.name='ldap.port'");
136+
ResultSet resultSet_ldap_port = pstmt_ldap_port.executeQuery();
137+
) {
138+
if (resultSet_ldap_port.next()) {
139+
port = DBEncryptionUtil.decrypt(resultSet_ldap_port.getString(1));
140+
if (StringUtils.isNotBlank(port)) {
141+
portNumber = Integer.parseInt(port);
142+
}
143+
}
144+
145+
if (StringUtils.isNotBlank(hostname)) {
146+
try (PreparedStatement pstmt_insert_ldap_hostname_port = conn.prepareStatement("INSERT INTO `cloud`.`ldap_configuration`(hostname, port) VALUES(?,?)");) {
147+
pstmt_insert_ldap_hostname_port.setString(1, hostname);
148+
if (portNumber != 0) {
149+
pstmt_insert_ldap_hostname_port.setInt(2, portNumber);
150+
} else {
151+
pstmt_insert_ldap_hostname_port.setNull(2, Types.INTEGER);
152+
}
153+
pstmt_insert_ldap_hostname_port.executeUpdate();
154+
}
155+
}
172156
}
173-
pstmt.executeUpdate();
174-
pstmt.close();
175157
}
176158

177159
} catch (SQLException e) {
178160
throw new CloudRuntimeException("Unable to insert ldap configuration values ", e);
179161
} catch (UnsupportedEncodingException e) {
180162
throw new CloudRuntimeException("Unable to insert ldap configuration values ", e);
181-
} finally {
182-
try {
183-
if (pstmt != null) {
184-
pstmt.close();
185-
}
186-
} catch (SQLException e) {
187-
}
188163
}
189164
s_logger.debug("Done encrypting ldap Config values");
190165

191166
}
192167

193168
private void encryptImageStoreDetails(Connection conn) {
194169
s_logger.debug("Encrypting image store details");
195-
PreparedStatement pstmt = null;
196-
ResultSet rs = null;
197-
try {
198-
pstmt = conn.prepareStatement("select id, value from `cloud`.`image_store_details` where name = 'key' or name = 'secretkey'");
199-
rs = pstmt.executeQuery();
170+
try (
171+
PreparedStatement selectPstmt = conn.prepareStatement("select id, value from `cloud`.`image_store_details` where name = 'key' or name = 'secretkey'");
172+
ResultSet rs = selectPstmt.executeQuery();
173+
) {
200174
while (rs.next()) {
201175
long id = rs.getLong(1);
202176
String value = rs.getString(2);
203177
if (value == null) {
204178
continue;
205179
}
206180
String encryptedValue = DBEncryptionUtil.encrypt(value);
207-
pstmt = conn.prepareStatement("update `cloud`.`image_store_details` set value=? where id=?");
208-
pstmt.setBytes(1, encryptedValue.getBytes("UTF-8"));
209-
pstmt.setLong(2, id);
210-
pstmt.executeUpdate();
181+
try (PreparedStatement updatePstmt = conn.prepareStatement("update `cloud`.`image_store_details` set value=? where id=?");) {
182+
updatePstmt.setBytes(1, encryptedValue.getBytes("UTF-8"));
183+
updatePstmt.setLong(2, id);
184+
updatePstmt.executeUpdate();
185+
}
211186
}
212187
} catch (SQLException e) {
213188
throw new CloudRuntimeException("Unable encrypt image_store_details values ", e);
214189
} catch (UnsupportedEncodingException e) {
215190
throw new CloudRuntimeException("Unable encrypt image_store_details values ", e);
216-
} finally {
217-
try {
218-
if (rs != null) {
219-
rs.close();
220-
}
221-
222-
if (pstmt != null) {
223-
pstmt.close();
224-
}
225-
} catch (SQLException e) {
226-
}
227191
}
228192
s_logger.debug("Done encrypting image_store_details");
229193
}

0 commit comments

Comments
 (0)