Skip to content

Commit cae4124

Browse files
author
Jayapal
committed
Fixed few coverity issues
1 parent 9fb2a76 commit cae4124

4 files changed

Lines changed: 100 additions & 70 deletions

File tree

api/src/org/apache/cloudstack/api/command/user/firewall/CreateEgressFirewallRuleCmd.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,10 @@ public void create() {
260260

261261
try {
262262
FirewallRule result = _firewallService.createEgressFirewallRule(this);
263-
setEntityId(result.getId());
264-
setEntityUuid(result.getUuid());
263+
if (result != null) {
264+
setEntityId(result.getId());
265+
setEntityUuid(result.getUuid());
266+
}
265267
} catch (NetworkRuleConflictException ex) {
266268
s_logger.info("Network rule conflict: " + ex.getMessage());
267269
s_logger.trace("Network Rule Conflict: ", ex);

api/src/org/apache/cloudstack/api/command/user/firewall/ListEgressFirewallRulesCmd.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,14 @@ public void execute() {
8585
ListResponse<FirewallResponse> response = new ListResponse<FirewallResponse>();
8686
List<FirewallResponse> fwResponses = new ArrayList<FirewallResponse>();
8787

88-
for (FirewallRule fwRule : result.first()) {
89-
FirewallResponse ruleData = _responseGenerator.createFirewallResponse(fwRule);
90-
ruleData.setObjectName("firewallrule");
91-
fwResponses.add(ruleData);
88+
if (result != null) {
89+
for (FirewallRule fwRule : result.first()) {
90+
FirewallResponse ruleData = _responseGenerator.createFirewallResponse(fwRule);
91+
ruleData.setObjectName("firewallrule");
92+
fwResponses.add(ruleData);
93+
}
94+
response.setResponses(fwResponses, result.second());
9295
}
93-
response.setResponses(fwResponses, result.second());
9496
response.setResponseName(getCommandName());
9597
setResponseObject(response);
9698
}

api/src/org/apache/cloudstack/api/command/user/vm/ListNicsCmd.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,15 @@ public void execute() throws ResourceUnavailableException, ResourceAllocationExc
124124
try {
125125
List<? extends Nic> results = _networkService.listNics(this);
126126
ListResponse<NicResponse> response = new ListResponse<NicResponse>();
127-
List<NicResponse> resList = new ArrayList<NicResponse>(results.size());
128-
for (Nic r : results) {
129-
NicResponse resp = _responseGenerator.createNicResponse(r);
130-
resp.setObjectName("nic");
131-
resList.add(resp);
127+
List<NicResponse> resList = null;
128+
if (results != null) {
129+
resList = new ArrayList<NicResponse>(results.size());
130+
for (Nic r : results) {
131+
NicResponse resp = _responseGenerator.createNicResponse(r);
132+
resp.setObjectName("nic");
133+
resList.add(resp);
134+
}
135+
response.setResponses(resList);
132136
}
133137
response.setResponses(resList);
134138
response.setResponseName(getCommandName());

engine/schema/src/com/cloud/upgrade/dao/Upgrade40to41.java

Lines changed: 80 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -103,72 +103,94 @@ private void updateRegionEntries(Connection conn) {
103103
}
104104

105105
private void upgradeEgressFirewallRules(Connection conn) {
106-
PreparedStatement pstmt = null;
107-
ResultSet rs = null;
108-
ResultSet rsId = null;
109-
ResultSet rsNw = null;
110-
try {
111-
// update the existing ingress rules traffic type
112-
pstmt = conn.prepareStatement("update `cloud`.`firewall_rules` set traffic_type='Ingress' where purpose='Firewall' and ip_address_id is " +
113-
"not null and traffic_type is null");
114-
s_logger.debug("Updating firewall Ingress rule traffic type: " + pstmt);
115-
pstmt.executeUpdate();
116106

117-
pstmt = conn.prepareStatement("select network_id FROM `cloud`.`ntwk_service_map` where service='Firewall' and provider='VirtualRouter' ");
118-
rs = pstmt.executeQuery();
119-
while (rs.next()) {
120-
long netId = rs.getLong(1);
107+
// update the existing ingress rules traffic type
108+
try (PreparedStatement updateNwpstmt = conn.prepareStatement("update `cloud`.`firewall_rules` set traffic_type='Ingress' where purpose='Firewall' and ip_address_id is " +
109+
"not null and traffic_type is null");)
110+
{
111+
updateNwpstmt.executeUpdate();
112+
s_logger.debug("Updating firewall Ingress rule traffic type: " + updateNwpstmt);
113+
} catch (SQLException e) {
114+
throw new CloudRuntimeException("Unable to update ingress firewall rules ", e);
115+
}
116+
117+
118+
try (PreparedStatement vrNwpstmt = conn.prepareStatement("select network_id FROM `cloud`.`ntwk_service_map` where service='Firewall' and provider='VirtualRouter' ");
119+
ResultSet vrNwsRs = vrNwpstmt.executeQuery();
120+
) {
121+
while (vrNwsRs.next()) {
122+
long netId = vrNwsRs.getLong(1);
121123
//When upgraded from 2.2.14 to 3.0.6 guest_type is updated to Isolated in the 2214to30 clean up sql. clean up executes
122124
//after this. So checking for Isolated OR Virtual
123-
pstmt = conn.prepareStatement("select account_id, domain_id FROM `cloud`.`networks` where (guest_type='Isolated' OR " +
124-
"guest_type='Virtual') and traffic_type='Guest' and vpc_id is NULL and " +
125-
"(state='implemented' OR state='Shutdown') and id=? ");
126-
pstmt.setLong(1, netId);
127-
s_logger.debug("Getting account_id, domain_id from networks table: " + pstmt);
128-
rsNw = pstmt.executeQuery();
129-
130-
if (rsNw.next()) {
131-
long accountId = rsNw.getLong(1);
132-
long domainId = rsNw.getLong(2);
133-
134-
//Add new rule for the existing networks
135-
s_logger.debug("Adding default egress firewall rule for network " + netId);
136-
pstmt = conn.prepareStatement("INSERT INTO firewall_rules (uuid, state, protocol, purpose, account_id, domain_id, network_id, xid, created, traffic_type) VALUES (?, 'Active', 'all', 'Firewall', ?, ?, ?, ?, now(), 'Egress')");
137-
pstmt.setString(1, UUID.randomUUID().toString());
138-
pstmt.setLong(2, accountId);
139-
pstmt.setLong(3, domainId);
140-
pstmt.setLong(4, netId);
141-
pstmt.setString(5, UUID.randomUUID().toString());
142-
s_logger.debug("Inserting default egress firewall rule " + pstmt);
143-
pstmt.executeUpdate();
144-
145-
pstmt = conn.prepareStatement("select id from firewall_rules where protocol='all' and network_id=?");
146-
pstmt.setLong(1, netId);
147-
rsId = pstmt.executeQuery();
148-
149-
long firewallRuleId;
150-
if (rsId.next()) {
151-
firewallRuleId = rsId.getLong(1);
152-
pstmt = conn.prepareStatement("insert into firewall_rules_cidrs (firewall_rule_id,source_cidr) values (?, '0.0.0.0/0')");
153-
pstmt.setLong(1, firewallRuleId);
154-
s_logger.debug("Inserting rule for cidr 0.0.0.0/0 for the new Firewall rule id=" + firewallRuleId + " with statement " + pstmt);
155-
pstmt.executeUpdate();
125+
try (PreparedStatement NwAcctDomIdpstmt = conn.prepareStatement("select account_id, domain_id FROM `cloud`.`networks` where (guest_type='Isolated' OR " +
126+
"guest_type='Virtual') and traffic_type='Guest' and vpc_id is NULL and " +
127+
"(state='implemented' OR state='Shutdown') and id=? "); ) {
128+
NwAcctDomIdpstmt.setLong(1, netId);
129+
130+
try (ResultSet NwAcctDomIdps = NwAcctDomIdpstmt.executeQuery();) {
131+
s_logger.debug("Getting account_id, domain_id from networks table: " + NwAcctDomIdpstmt);
132+
133+
if (NwAcctDomIdps.next()) {
134+
long accountId = NwAcctDomIdps.getLong(1);
135+
long domainId = NwAcctDomIdps.getLong(2);
136+
//Add new rule for the existing networks
137+
s_logger.debug("Adding default egress firewall rule for network " + netId);
138+
try (PreparedStatement fwRulespstmt = conn.prepareStatement("INSERT INTO firewall_rules "+
139+
" (uuid, state, protocol, purpose, account_id, domain_id, network_id, xid, created,"
140+
+ " traffic_type) VALUES (?, 'Active', 'all', 'Firewall', ?, ?, ?, ?, now(), "
141+
+"'Egress')");
142+
) {
143+
fwRulespstmt.setString(1, UUID.randomUUID().toString());
144+
fwRulespstmt.setLong(2, accountId);
145+
fwRulespstmt.setLong(3, domainId);
146+
fwRulespstmt.setLong(4, netId);
147+
fwRulespstmt.setString(5, UUID.randomUUID().toString());
148+
s_logger.debug("Inserting default egress firewall rule " + fwRulespstmt);
149+
fwRulespstmt.executeUpdate();
150+
} catch (SQLException e) {
151+
throw new CloudRuntimeException("failed to insert default egress firewall rule ", e);
152+
}
153+
154+
try (PreparedStatement protoAllpstmt = conn.prepareStatement("select id from firewall_rules where protocol='all' and network_id=?");)
155+
{
156+
protoAllpstmt.setLong(1, netId);
157+
158+
try (ResultSet protoAllRs = protoAllpstmt.executeQuery();) {
159+
long firewallRuleId;
160+
if (protoAllRs.next()) {
161+
firewallRuleId = protoAllRs.getLong(1);
162+
163+
try (PreparedStatement fwCidrsPstmt = conn.prepareStatement("insert into firewall_rules_cidrs (firewall_rule_id,source_cidr) values (?, '0.0.0.0/0')");) {
164+
fwCidrsPstmt.setLong(1, firewallRuleId);
165+
s_logger.debug("Inserting rule for cidr 0.0.0.0/0 for the new Firewall rule id=" + firewallRuleId + " with statement " + fwCidrsPstmt);
166+
fwCidrsPstmt.executeUpdate();
167+
} catch (SQLException e) {
168+
throw new CloudRuntimeException("Unable to set egress firewall rules ", e);
169+
}
170+
171+
}
172+
} catch (SQLException e) {
173+
throw new CloudRuntimeException("Unable to set egress firewall rules ", e);
174+
}
175+
176+
} catch (SQLException e) {
177+
throw new CloudRuntimeException("Unable to set egress firewall rules ", e);
178+
}
179+
180+
} //if
181+
} catch (SQLException e) {
182+
throw new CloudRuntimeException("Unable execute update query ", e);
156183
}
184+
185+
} catch (SQLException e) {
186+
throw new CloudRuntimeException("Unable to get account id domainid of networks ", e);
157187
}
158-
}
188+
} //while
159189
} catch (SQLException e) {
160190
throw new CloudRuntimeException("Unable to set egress firewall rules ", e);
161-
} finally {
162-
try {
163-
if (rs != null) {
164-
rs.close();
165-
}
166-
if (pstmt != null) {
167-
pstmt.close();
168-
}
169-
} catch (SQLException e) {
170-
}
191+
171192
}
172193
}
173194

174195
}
196+

0 commit comments

Comments
 (0)