Securitygroups.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service_Amazon
  17. * @subpackage Ec2
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. require_once 'Zend/Service/Amazon/Ec2/Abstract.php';
  23. /**
  24. * An Amazon EC2 interface to create, delete, describe, grand and revoke sercurity permissions.
  25. *
  26. * @category Zend
  27. * @package Zend_Service_Amazon
  28. * @subpackage Ec2
  29. * @copyright Copyright (c) 22005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Service_Amazon_Ec2_Securitygroups extends Zend_Service_Amazon_Ec2_Abstract
  33. {
  34. /**
  35. * Creates a new security group.
  36. *
  37. * Every instance is launched in a security group. If no security group is specified
  38. * during launch, the instances are launched in the default security group. Instances
  39. * within the same security group have unrestricted network access to each other.
  40. * Instances will reject network access attempts from other instances in a different
  41. * security group. As the owner of instances you can grant or revoke specific permissions
  42. * using the {@link authorizeIp}, {@link authorizeGroup}, {@link revokeGroup} and
  43. * {$link revokeIp} operations.
  44. *
  45. * @param string $name Name of the new security group.
  46. * @param string $description Description of the new security group.
  47. * @return boolean
  48. */
  49. public function create($name, $description)
  50. {
  51. $params = array();
  52. $params['Action'] = 'CreateSecurityGroup';
  53. $params['GroupName'] = $name;
  54. $params['GroupDescription'] = $description;
  55. $response = $this->sendRequest($params);
  56. $xpath = $response->getXPath();
  57. $success = $xpath->evaluate('string(//ec2:return/text())');
  58. return ($success === "true");
  59. }
  60. /**
  61. * Returns information about security groups that you own.
  62. *
  63. * If you specify security group names, information about those security group is returned.
  64. * Otherwise, information for all security group is returned. If you specify a group
  65. * that does not exist, a fault is returned.
  66. *
  67. * @param string|array $name List of security groups to describe
  68. * @return array
  69. */
  70. public function describe($name = null)
  71. {
  72. $params = array();
  73. $params['Action'] = 'DescribeSecurityGroups';
  74. if(is_array($name) && !empty($name)) {
  75. foreach($name as $k=>$name) {
  76. $params['GroupName.' . ($k+1)] = $name;
  77. }
  78. } elseif($name) {
  79. $params['GroupName.1'] = $name;
  80. }
  81. $response = $this->sendRequest($params);
  82. $xpath = $response->getXPath();
  83. $return = array();
  84. $nodes = $xpath->query('//ec2:securityGroupInfo/ec2:item');
  85. foreach($nodes as $node) {
  86. $item = array();
  87. $item['ownerId'] = $xpath->evaluate('string(ec2:ownerId/text())', $node);
  88. $item['groupName'] = $xpath->evaluate('string(ec2:groupName/text())', $node);
  89. $item['groupDescription'] = $xpath->evaluate('string(ec2:groupDescription/text())', $node);
  90. $ip_nodes = $xpath->query('ec2:ipPermissions/ec2:item', $node);
  91. foreach($ip_nodes as $ip_node) {
  92. $sItem = array();
  93. $sItem['ipProtocol'] = $xpath->evaluate('string(ec2:ipProtocol/text())', $ip_node);
  94. $sItem['fromPort'] = $xpath->evaluate('string(ec2:fromPort/text())', $ip_node);
  95. $sItem['toPort'] = $xpath->evaluate('string(ec2:toPort/text())', $ip_node);
  96. $sItem['ipRanges'] = $xpath->evaluate('string(ec2:ipRanges/ec2:item/ec2:cidrIp/text())', $ip_node);
  97. $item['ipPermissions'][] = $sItem;
  98. unset($ip_node, $sItem);
  99. }
  100. $return[] = $item;
  101. unset($item, $node);
  102. }
  103. return $return;
  104. }
  105. /**
  106. * Deletes a security group.
  107. *
  108. * If you attempt to delete a security group that contains instances, a fault is returned.
  109. * If you attempt to delete a security group that is referenced by another security group,
  110. * a fault is returned. For example, if security group B has a rule that allows access
  111. * from security group A, security group A cannot be deleted until the allow rule is removed.
  112. *
  113. * @param string $name Name of the security group to delete.
  114. * @return boolean
  115. */
  116. public function delete($name)
  117. {
  118. $params = array();
  119. $params['Action'] = 'DeleteSecurityGroup';
  120. $params['GroupName'] = $name;
  121. $response = $this->sendRequest($params);
  122. $xpath = $response->getXPath();
  123. $success = $xpath->evaluate('string(//ec2:return/text())');
  124. return ($success === "true");
  125. }
  126. /**
  127. * Adds permissions to a security group
  128. *
  129. * Permissions are specified by the IP protocol (TCP, UDP or ICMP), the source of the request
  130. * (by IP range or an Amazon EC2 user-group pair), the source and destination port ranges
  131. * (for TCP and UDP), and the ICMP codes and types (for ICMP). When authorizing ICMP, -1
  132. * can be used as a wildcard in the type and code fields.
  133. *
  134. * Permission changes are propagated to instances within the security group as quickly as
  135. * possible. However, depending on the number of instances, a small delay might occur.
  136. *
  137. *
  138. * @param string $name Name of the group to modify.
  139. * @param string $ipProtocol IP protocol to authorize access to when operating on a CIDR IP.
  140. * @param integer $fromPort Bottom of port range to authorize access to when operating on a CIDR IP.
  141. * This contains the ICMP type if ICMP is being authorized.
  142. * @param integer $toPort Top of port range to authorize access to when operating on a CIDR IP.
  143. * This contains the ICMP code if ICMP is being authorized.
  144. * @param string $cidrIp CIDR IP range to authorize access to when operating on a CIDR IP.
  145. * @return boolean
  146. */
  147. public function authorizeIp($name, $ipProtocol, $fromPort, $toPort, $cidrIp)
  148. {
  149. $params = array();
  150. $params['Action'] = 'AuthorizeSecurityGroupIngress';
  151. $params['GroupName'] = $name;
  152. $params['IpProtocol'] = $ipProtocol;
  153. $params['FromPort'] = $fromPort;
  154. $params['ToPort'] = $toPort;
  155. $params['CidrIp'] = $cidrIp;
  156. $response = $this->sendRequest($params);
  157. $xpath = $response->getXPath();
  158. $success = $xpath->evaluate('string(//ec2:return/text())');
  159. return ($success === "true");
  160. }
  161. /**
  162. * Adds permissions to a security group
  163. *
  164. * When authorizing a user/group pair permission, GroupName, SourceSecurityGroupName and
  165. * SourceSecurityGroupOwnerId must be specified.
  166. *
  167. * Permission changes are propagated to instances within the security group as quickly as
  168. * possible. However, depending on the number of instances, a small delay might occur.
  169. *
  170. * @param string $name Name of the group to modify.
  171. * @param string $groupName Name of security group to authorize access to when operating on a user/group pair.
  172. * @param string $ownerId Owner of security group to authorize access to when operating on a user/group pair.
  173. * @return boolean
  174. */
  175. public function authorizeGroup($name, $groupName, $ownerId)
  176. {
  177. $params = array();
  178. $params['Action'] = 'AuthorizeSecurityGroupIngress';
  179. $params['GroupName'] = $name;
  180. $params['SourceSecurityGroupName'] = $groupName;
  181. $params['SourceSecurityGroupOwnerId'] = $ownerId;
  182. $response = $this->sendRequest($params);
  183. $xpath = $response->getXPath();
  184. $success = $xpath->evaluate('string(//ec2:return/text())');
  185. return ($success === "true");
  186. }
  187. /**
  188. * Revokes permissions from a security group. The permissions used to revoke must be specified
  189. * using the same values used to grant the permissions.
  190. *
  191. * Permissions are specified by the IP protocol (TCP, UDP or ICMP), the source of the request
  192. * (by IP range or an Amazon EC2 user-group pair), the source and destination port ranges
  193. * (for TCP and UDP), and the ICMP codes and types (for ICMP). When authorizing ICMP, -1
  194. * can be used as a wildcard in the type and code fields.
  195. *
  196. * Permission changes are propagated to instances within the security group as quickly as
  197. * possible. However, depending on the number of instances, a small delay might occur.
  198. *
  199. *
  200. * @param string $name Name of the group to modify.
  201. * @param string $ipProtocol IP protocol to revoke access to when operating on a CIDR IP.
  202. * @param integer $fromPort Bottom of port range to revoke access to when operating on a CIDR IP.
  203. * This contains the ICMP type if ICMP is being revoked.
  204. * @param integer $toPort Top of port range to revoked access to when operating on a CIDR IP.
  205. * This contains the ICMP code if ICMP is being revoked.
  206. * @param string $cidrIp CIDR IP range to revoke access to when operating on a CIDR IP.
  207. * @return boolean
  208. */
  209. public function revokeIp($name, $ipProtocol, $fromPort, $toPort, $cidrIp)
  210. {
  211. $params = array();
  212. $params['Action'] = 'RevokeSecurityGroupIngress';
  213. $params['GroupName'] = $name;
  214. $params['IpProtocol'] = $ipProtocol;
  215. $params['FromPort'] = $fromPort;
  216. $params['ToPort'] = $toPort;
  217. $params['CidrIp'] = $cidrIp;
  218. $response = $this->sendRequest($params);
  219. $xpath = $response->getXPath();
  220. $success = $xpath->evaluate('string(//ec2:return/text())');
  221. return ($success === "true");
  222. }
  223. /**
  224. * Revokes permissions from a security group. The permissions used to revoke must be specified
  225. * using the same values used to grant the permissions.
  226. *
  227. * Permission changes are propagated to instances within the security group as quickly as
  228. * possible. However, depending on the number of instances, a small delay might occur.
  229. *
  230. * When revoking a user/group pair permission, GroupName, SourceSecurityGroupName and
  231. * SourceSecurityGroupOwnerId must be specified.
  232. *
  233. * @param string $name Name of the group to modify.
  234. * @param string $groupName Name of security group to revoke access to when operating on a user/group pair.
  235. * @param string $ownerId Owner of security group to revoke access to when operating on a user/group pair.
  236. * @return boolean
  237. */
  238. public function revokeGroup($name, $groupName, $ownerId)
  239. {
  240. $params = array();
  241. $params['Action'] = 'RevokeSecurityGroupIngress';
  242. $params['GroupName'] = $name;
  243. $params['SourceSecurityGroupName'] = $groupName;
  244. $params['SourceSecurityGroupOwnerId'] = $ownerId;
  245. $response = $this->sendRequest($params);
  246. $xpath = $response->getXPath();
  247. $success = $xpath->evaluate('string(//ec2:return/text())');
  248. return ($success === "true");
  249. }
  250. }