Securitygroups.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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-2015 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-2015 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. $ips = $xpath->query('ec2:ipRanges/ec2:item', $ip_node);
  100. $sItem['ipRanges'] = array();
  101. foreach($ips as $ip) {
  102. $sItem['ipRanges'][] = $xpath->evaluate('string(ec2:cidrIp/text())', $ip);
  103. }
  104. if(count($sItem['ipRanges']) == 1) {
  105. $sItem['ipRanges'] = $sItem['ipRanges'][0];
  106. }
  107. $item['ipPermissions'][] = $sItem;
  108. unset($ip_node, $sItem);
  109. }
  110. $return[] = $item;
  111. unset($item, $node);
  112. }
  113. return $return;
  114. }
  115. /**
  116. * Deletes a security group.
  117. *
  118. * If you attempt to delete a security group that contains instances, a fault is returned.
  119. * If you attempt to delete a security group that is referenced by another security group,
  120. * a fault is returned. For example, if security group B has a rule that allows access
  121. * from security group A, security group A cannot be deleted until the allow rule is removed.
  122. *
  123. * @param string $name Name of the security group to delete.
  124. * @return boolean
  125. */
  126. public function delete($name)
  127. {
  128. $params = array();
  129. $params['Action'] = 'DeleteSecurityGroup';
  130. $params['GroupName'] = $name;
  131. $response = $this->sendRequest($params);
  132. $xpath = $response->getXPath();
  133. $success = $xpath->evaluate('string(//ec2:return/text())');
  134. return ($success === "true");
  135. }
  136. /**
  137. * Adds permissions to a security group
  138. *
  139. * Permissions are specified by the IP protocol (TCP, UDP or ICMP), the source of the request
  140. * (by IP range or an Amazon EC2 user-group pair), the source and destination port ranges
  141. * (for TCP and UDP), and the ICMP codes and types (for ICMP). When authorizing ICMP, -1
  142. * can be used as a wildcard in the type and code fields.
  143. *
  144. * Permission changes are propagated to instances within the security group as quickly as
  145. * possible. However, depending on the number of instances, a small delay might occur.
  146. *
  147. *
  148. * @param string $name Name of the group to modify.
  149. * @param string $ipProtocol IP protocol to authorize access to when operating on a CIDR IP.
  150. * @param integer $fromPort Bottom of port range to authorize access to when operating on a CIDR IP.
  151. * This contains the ICMP type if ICMP is being authorized.
  152. * @param integer $toPort Top of port range to authorize access to when operating on a CIDR IP.
  153. * This contains the ICMP code if ICMP is being authorized.
  154. * @param string $cidrIp CIDR IP range to authorize access to when operating on a CIDR IP.
  155. * @return boolean
  156. */
  157. public function authorizeIp($name, $ipProtocol, $fromPort, $toPort, $cidrIp)
  158. {
  159. $params = array();
  160. $params['Action'] = 'AuthorizeSecurityGroupIngress';
  161. $params['GroupName'] = $name;
  162. $params['IpProtocol'] = $ipProtocol;
  163. $params['FromPort'] = $fromPort;
  164. $params['ToPort'] = $toPort;
  165. $params['CidrIp'] = $cidrIp;
  166. $response = $this->sendRequest($params);
  167. $xpath = $response->getXPath();
  168. $success = $xpath->evaluate('string(//ec2:return/text())');
  169. return ($success === "true");
  170. }
  171. /**
  172. * Adds permissions to a security group
  173. *
  174. * When authorizing a user/group pair permission, GroupName, SourceSecurityGroupName and
  175. * SourceSecurityGroupOwnerId must be specified.
  176. *
  177. * Permission changes are propagated to instances within the security group as quickly as
  178. * possible. However, depending on the number of instances, a small delay might occur.
  179. *
  180. * @param string $name Name of the group to modify.
  181. * @param string $groupName Name of security group to authorize access to when operating on a user/group pair.
  182. * @param string $ownerId Owner of security group to authorize access to when operating on a user/group pair.
  183. * @return boolean
  184. */
  185. public function authorizeGroup($name, $groupName, $ownerId)
  186. {
  187. $params = array();
  188. $params['Action'] = 'AuthorizeSecurityGroupIngress';
  189. $params['GroupName'] = $name;
  190. $params['SourceSecurityGroupName'] = $groupName;
  191. $params['SourceSecurityGroupOwnerId'] = $ownerId;
  192. $response = $this->sendRequest($params);
  193. $xpath = $response->getXPath();
  194. $success = $xpath->evaluate('string(//ec2:return/text())');
  195. return ($success === "true");
  196. }
  197. /**
  198. * Revokes permissions from a security group. The permissions used to revoke must be specified
  199. * using the same values used to grant the permissions.
  200. *
  201. * Permissions are specified by the IP protocol (TCP, UDP or ICMP), the source of the request
  202. * (by IP range or an Amazon EC2 user-group pair), the source and destination port ranges
  203. * (for TCP and UDP), and the ICMP codes and types (for ICMP). When authorizing ICMP, -1
  204. * can be used as a wildcard in the type and code fields.
  205. *
  206. * Permission changes are propagated to instances within the security group as quickly as
  207. * possible. However, depending on the number of instances, a small delay might occur.
  208. *
  209. *
  210. * @param string $name Name of the group to modify.
  211. * @param string $ipProtocol IP protocol to revoke access to when operating on a CIDR IP.
  212. * @param integer $fromPort Bottom of port range to revoke access to when operating on a CIDR IP.
  213. * This contains the ICMP type if ICMP is being revoked.
  214. * @param integer $toPort Top of port range to revoked access to when operating on a CIDR IP.
  215. * This contains the ICMP code if ICMP is being revoked.
  216. * @param string $cidrIp CIDR IP range to revoke access to when operating on a CIDR IP.
  217. * @return boolean
  218. */
  219. public function revokeIp($name, $ipProtocol, $fromPort, $toPort, $cidrIp)
  220. {
  221. $params = array();
  222. $params['Action'] = 'RevokeSecurityGroupIngress';
  223. $params['GroupName'] = $name;
  224. $params['IpProtocol'] = $ipProtocol;
  225. $params['FromPort'] = $fromPort;
  226. $params['ToPort'] = $toPort;
  227. $params['CidrIp'] = $cidrIp;
  228. $response = $this->sendRequest($params);
  229. $xpath = $response->getXPath();
  230. $success = $xpath->evaluate('string(//ec2:return/text())');
  231. return ($success === "true");
  232. }
  233. /**
  234. * Revokes permissions from a security group. The permissions used to revoke must be specified
  235. * using the same values used to grant the permissions.
  236. *
  237. * Permission changes are propagated to instances within the security group as quickly as
  238. * possible. However, depending on the number of instances, a small delay might occur.
  239. *
  240. * When revoking a user/group pair permission, GroupName, SourceSecurityGroupName and
  241. * SourceSecurityGroupOwnerId must be specified.
  242. *
  243. * @param string $name Name of the group to modify.
  244. * @param string $groupName Name of security group to revoke access to when operating on a user/group pair.
  245. * @param string $ownerId Owner of security group to revoke access to when operating on a user/group pair.
  246. * @return boolean
  247. */
  248. public function revokeGroup($name, $groupName, $ownerId)
  249. {
  250. $params = array();
  251. $params['Action'] = 'RevokeSecurityGroupIngress';
  252. $params['GroupName'] = $name;
  253. $params['SourceSecurityGroupName'] = $groupName;
  254. $params['SourceSecurityGroupOwnerId'] = $ownerId;
  255. $response = $this->sendRequest($params);
  256. $xpath = $response->getXPath();
  257. $success = $xpath->evaluate('string(//ec2:return/text())');
  258. return ($success === "true");
  259. }
  260. }