Skip to content

Commit 41e5e38

Browse files
committed
bug 9336: securityGroups can be used by other securityGroups in the same domain only; no cross domain SG authentication
status 9336: resolved fixed Following changes were made: * deleteSecurityGroup/authorizeSecurityGroupIngress - removed account/domainId parameters as SG is uniquely identified by id now * removed account_name field from securityGroup DB table; removed allowed_security_group/allowed_sec_grp_acct from security_ingress_rule. These values were used for api response generation only for performance purposes; added caching on API level to improve performance * Added missing security checks for securityGroups/ingressRules
1 parent 09b4b06 commit 41e5e38

18 files changed

Lines changed: 2564 additions & 2787 deletions

‎api/src/com/cloud/api/commands/AuthorizeSecurityGroupIngressCmd.java‎

Lines changed: 41 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -37,67 +37,53 @@
3737
import com.cloud.async.AsyncJob;
3838
import com.cloud.event.EventTypes;
3939
import com.cloud.network.security.IngressRule;
40+
import com.cloud.network.security.SecurityGroup;
4041
import com.cloud.user.Account;
41-
import com.cloud.user.UserContext;
4242
import com.cloud.utils.StringUtils;
4343

44-
@Implementation(responseObject=IngressRuleResponse.class, description="Authorizes a particular ingress rule for this security group") @SuppressWarnings("rawtypes")
44+
@Implementation(responseObject = IngressRuleResponse.class, description = "Authorizes a particular ingress rule for this security group")
45+
@SuppressWarnings("rawtypes")
4546
public class AuthorizeSecurityGroupIngressCmd extends BaseAsyncCmd {
46-
public static final Logger s_logger = Logger.getLogger(AuthorizeSecurityGroupIngressCmd.class.getName());
47+
public static final Logger s_logger = Logger.getLogger(AuthorizeSecurityGroupIngressCmd.class.getName());
4748

4849
private static final String s_name = "authorizesecuritygroupingress";
4950

50-
/////////////////////////////////////////////////////
51-
//////////////// API parameters /////////////////////
52-
/////////////////////////////////////////////////////
51+
// ///////////////////////////////////////////////////
52+
// ////////////// API parameters /////////////////////
53+
// ///////////////////////////////////////////////////
5354

54-
@Parameter(name=ApiConstants.PROTOCOL, type=CommandType.STRING, description="TCP is default. UDP is the other supported protocol")
55+
@Parameter(name = ApiConstants.PROTOCOL, type = CommandType.STRING, description = "TCP is default. UDP is the other supported protocol")
5556
private String protocol;
5657

57-
@Parameter(name=ApiConstants.START_PORT, type=CommandType.INTEGER, description="start port for this ingress rule")
58+
@Parameter(name = ApiConstants.START_PORT, type = CommandType.INTEGER, description = "start port for this ingress rule")
5859
private Integer startPort;
5960

60-
@Parameter(name=ApiConstants.END_PORT, type=CommandType.INTEGER, description="end port for this ingress rule")
61+
@Parameter(name = ApiConstants.END_PORT, type = CommandType.INTEGER, description = "end port for this ingress rule")
6162
private Integer endPort;
6263

63-
@Parameter(name=ApiConstants.ICMP_TYPE, type=CommandType.INTEGER, description="type of the icmp message being sent")
64+
@Parameter(name = ApiConstants.ICMP_TYPE, type = CommandType.INTEGER, description = "type of the icmp message being sent")
6465
private Integer icmpType;
6566

66-
@Parameter(name=ApiConstants.ICMP_CODE, type=CommandType.INTEGER, description="error code for this icmp message")
67+
@Parameter(name = ApiConstants.ICMP_CODE, type = CommandType.INTEGER, description = "error code for this icmp message")
6768
private Integer icmpCode;
6869

69-
@Parameter(name=ApiConstants.SECURITY_GROUP_ID, type=CommandType.LONG, required=true, description="The ID of the security group")
70+
@Parameter(name = ApiConstants.SECURITY_GROUP_ID, type = CommandType.LONG, required = true, description = "The ID of the security group")
7071
private Long securityGroupId;
7172

72-
@Parameter(name=ApiConstants.CIDR_LIST, type=CommandType.LIST, collectionType=CommandType.STRING, description="the cidr list associated")
73-
private List cidrList;
73+
@Parameter(name = ApiConstants.CIDR_LIST, type = CommandType.LIST, collectionType = CommandType.STRING, description = "the cidr list associated")
74+
private List<String> cidrList;
7475

75-
@Parameter(name=ApiConstants.USER_SECURITY_GROUP_LIST, type=CommandType.MAP, description="user to security group mapping")
76+
@Parameter(name = ApiConstants.USER_SECURITY_GROUP_LIST, type = CommandType.MAP, description = "user to security group mapping")
7677
private Map userSecurityGroupList;
7778

78-
@Parameter(name=ApiConstants.ACCOUNT, type=CommandType.STRING, description="an optional account for the security group. Must be used with domainId.")
79-
private String accountName;
79+
// ///////////////////////////////////////////////////
80+
// ///////////////// Accessors ///////////////////////
81+
// ///////////////////////////////////////////////////
8082

81-
@Parameter(name=ApiConstants.DOMAIN_ID, type=CommandType.LONG, description="an optional domainId for the security group. If the account parameter is used, domainId must also be used.")
82-
private Long domainId;
83-
84-
85-
/////////////////////////////////////////////////////
86-
/////////////////// Accessors ///////////////////////
87-
/////////////////////////////////////////////////////
88-
89-
public String getAccountName() {
90-
return accountName;
91-
}
92-
93-
public List getCidrList() {
83+
public List<String> getCidrList() {
9484
return cidrList;
9585
}
9686

97-
public Long getDomainId() {
98-
return domainId;
99-
}
100-
10187
public Integer getEndPort() {
10288
return endPort;
10389
}
@@ -129,34 +115,24 @@ public Map getUserSecurityGroupList() {
129115
return userSecurityGroupList;
130116
}
131117

132-
133-
/////////////////////////////////////////////////////
134-
/////////////// API Implementation///////////////////
135-
/////////////////////////////////////////////////////
118+
// ///////////////////////////////////////////////////
119+
// ///////////// API Implementation///////////////////
120+
// ///////////////////////////////////////////////////
136121

137122
@Override
138123
public String getCommandName() {
139124
return s_name;
140125
}
141126

142127
public static String getResultObjectName() {
143-
return "securitygroup";
128+
return "securitygroup";
144129
}
145130

146131
@Override
147132
public long getEntityOwnerId() {
148-
Account account = UserContext.current().getCaller();
149-
if ((account == null) || isAdmin(account.getType())) {
150-
if ((domainId != null) && (accountName != null)) {
151-
Account userAccount = _responseGenerator.findAccountByNameDomain(accountName, domainId);
152-
if (userAccount != null) {
153-
return userAccount.getId();
154-
}
155-
}
156-
}
157-
158-
if (account != null) {
159-
return account.getId();
133+
SecurityGroup group = _entityMgr.findById(SecurityGroup.class, getSecurityGroupId());
134+
if (group != null) {
135+
return group.getAccountId();
160136
}
161137

162138
return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked
@@ -175,15 +151,15 @@ public String getEventDescription() {
175151
Collection userGroupCollection = getUserSecurityGroupList().values();
176152
Iterator iter = userGroupCollection.iterator();
177153

178-
HashMap userGroup = (HashMap)iter.next();
179-
String group = (String)userGroup.get("group");
180-
String authorizedAccountName = (String)userGroup.get("account");
154+
HashMap userGroup = (HashMap) iter.next();
155+
String group = (String) userGroup.get("group");
156+
String authorizedAccountName = (String) userGroup.get("account");
181157
sb.append(group + "/" + authorizedAccountName);
182158

183159
while (iter.hasNext()) {
184-
userGroup = (HashMap)iter.next();
185-
group = (String)userGroup.get("group");
186-
authorizedAccountName = (String)userGroup.get("account");
160+
userGroup = (HashMap) iter.next();
161+
group = (String) userGroup.get("group");
162+
authorizedAccountName = (String) userGroup.get("account");
187163
sb.append(", " + group + "/" + authorizedAccountName);
188164
}
189165
} else if (getCidrList() != null) {
@@ -193,26 +169,26 @@ public String getEventDescription() {
193169
sb.append("<error: no ingress parameters>");
194170
}
195171

196-
return "authorizing ingress to group: " + getSecurityGroupId() + " to " + sb.toString();
172+
return "authorizing ingress to group: " + getSecurityGroupId() + " to " + sb.toString();
197173
}
198-
174+
199175
@Override
200-
public void execute(){
176+
public void execute() {
201177
List<? extends IngressRule> ingressRules = _securityGroupService.authorizeSecurityGroupIngress(this);
202-
if (ingressRules != null && ! ingressRules.isEmpty()) {
203-
SecurityGroupResponse response = _responseGenerator.createSecurityGroupResponseFromIngressRule(ingressRules);
178+
if (ingressRules != null && !ingressRules.isEmpty()) {
179+
SecurityGroupResponse response = _responseGenerator.createSecurityGroupResponseFromIngressRule(ingressRules);
204180
this.setResponseObject(response);
205181
} else {
206182
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to authorize security group ingress rule(s)");
207183
}
208-
184+
209185
}
210-
186+
211187
@Override
212188
public AsyncJob.Type getInstanceType() {
213189
return AsyncJob.Type.SecurityGroup;
214190
}
215-
191+
216192
@Override
217193
public Long getInstanceId() {
218194
return getSecurityGroupId();

‎api/src/com/cloud/api/commands/DeleteSecurityGroupCmd.java‎

Lines changed: 69 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -16,89 +16,72 @@
1616
*
1717
*/
1818

19-
package com.cloud.api.commands;
20-
21-
import org.apache.log4j.Logger;
22-
23-
import com.cloud.api.ApiConstants;
24-
import com.cloud.api.BaseCmd;
25-
import com.cloud.api.Implementation;
26-
import com.cloud.api.Parameter;
27-
import com.cloud.api.ServerApiException;
28-
import com.cloud.api.response.SuccessResponse;
29-
import com.cloud.exception.ResourceInUseException;
30-
import com.cloud.network.security.SecurityGroup;
31-
import com.cloud.user.Account;
32-
33-
@Implementation(description="Deletes security group", responseObject=SuccessResponse.class)
34-
public class DeleteSecurityGroupCmd extends BaseCmd {
35-
public static final Logger s_logger = Logger.getLogger(DeleteSecurityGroupCmd.class.getName());
36-
private static final String s_name = "deletesecuritygroupresponse";
37-
38-
/////////////////////////////////////////////////////
39-
//////////////// API parameters /////////////////////
40-
/////////////////////////////////////////////////////
41-
42-
@Parameter(name=ApiConstants.ACCOUNT, type=CommandType.STRING, description="the account of the security group. Must be specified with domain ID")
43-
private String accountName;
44-
45-
@Parameter(name=ApiConstants.DOMAIN_ID, type=CommandType.LONG, description="the domain ID of account owning the security group")
46-
private Long domainId;
47-
48-
@Parameter(name=ApiConstants.ID, type=CommandType.LONG, required=true, description="The ID of the security group")
49-
private Long id;
50-
51-
52-
/////////////////////////////////////////////////////
53-
/////////////////// Accessors ///////////////////////
54-
/////////////////////////////////////////////////////
55-
56-
public String getAccountName() {
57-
return accountName;
58-
}
59-
60-
public Long getDomainId() {
61-
return domainId;
62-
}
63-
64-
public Long getId() {
65-
return id;
66-
}
67-
68-
69-
70-
/////////////////////////////////////////////////////
71-
/////////////// API Implementation///////////////////
72-
/////////////////////////////////////////////////////
73-
74-
@Override
75-
public String getCommandName() {
76-
return s_name;
77-
}
78-
79-
@Override
80-
public long getEntityOwnerId() {
81-
SecurityGroup group = _entityMgr.findById(SecurityGroup.class, getId());
82-
if (group != null) {
83-
return group.getAccountId();
84-
}
85-
86-
return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked
87-
}
88-
89-
@Override
90-
public void execute(){
91-
try{
92-
boolean result = _securityGroupService.deleteSecurityGroup(this);
93-
if (result) {
94-
SuccessResponse response = new SuccessResponse(getCommandName());
95-
this.setResponseObject(response);
96-
} else {
97-
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to delete security group");
98-
}
99-
} catch (ResourceInUseException ex) {
100-
s_logger.warn("Exception: ", ex);
101-
throw new ServerApiException(BaseCmd.RESOURCE_IN_USE_ERROR, ex.getMessage());
102-
}
103-
}
104-
}
19+
package com.cloud.api.commands;
20+
21+
import org.apache.log4j.Logger;
22+
23+
import com.cloud.api.ApiConstants;
24+
import com.cloud.api.BaseCmd;
25+
import com.cloud.api.Implementation;
26+
import com.cloud.api.Parameter;
27+
import com.cloud.api.ServerApiException;
28+
import com.cloud.api.response.SuccessResponse;
29+
import com.cloud.exception.ResourceInUseException;
30+
import com.cloud.network.security.SecurityGroup;
31+
import com.cloud.user.Account;
32+
33+
@Implementation(description = "Deletes security group", responseObject = SuccessResponse.class)
34+
public class DeleteSecurityGroupCmd extends BaseCmd {
35+
public static final Logger s_logger = Logger.getLogger(DeleteSecurityGroupCmd.class.getName());
36+
private static final String s_name = "deletesecuritygroupresponse";
37+
38+
// ///////////////////////////////////////////////////
39+
// ////////////// API parameters /////////////////////
40+
// ///////////////////////////////////////////////////
41+
42+
@Parameter(name = ApiConstants.ID, type = CommandType.LONG, required = true, description = "The ID of the security group")
43+
private Long id;
44+
45+
// ///////////////////////////////////////////////////
46+
// ///////////////// Accessors ///////////////////////
47+
// ///////////////////////////////////////////////////
48+
49+
public Long getId() {
50+
return id;
51+
}
52+
53+
// ///////////////////////////////////////////////////
54+
// ///////////// API Implementation///////////////////
55+
// ///////////////////////////////////////////////////
56+
57+
@Override
58+
public String getCommandName() {
59+
return s_name;
60+
}
61+
62+
@Override
63+
public long getEntityOwnerId() {
64+
SecurityGroup group = _entityMgr.findById(SecurityGroup.class, getId());
65+
if (group != null) {
66+
return group.getAccountId();
67+
}
68+
69+
return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked
70+
}
71+
72+
@Override
73+
public void execute() {
74+
try {
75+
boolean result = _securityGroupService.deleteSecurityGroup(this);
76+
if (result) {
77+
SuccessResponse response = new SuccessResponse(getCommandName());
78+
this.setResponseObject(response);
79+
} else {
80+
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to delete security group");
81+
}
82+
} catch (ResourceInUseException ex) {
83+
s_logger.warn("Exception: ", ex);
84+
throw new ServerApiException(BaseCmd.RESOURCE_IN_USE_ERROR, ex.getMessage());
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)