Securitygroups.php 12 KB

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