PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/scalr-2/tags/scalr-2.1.0/app/www/sec_group_edit.php

http://scalr.googlecode.com/
PHP | 327 lines | 281 code | 45 blank | 1 comment | 35 complexity | b8fd78477f714a2ec6cfdcc274801a1e MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, GPL-3.0
  1. <?
  2. require("src/prepend.inc.php");
  3. if (!Scalr_Session::getInstance()->getAuthToken()->hasAccess(Scalr_AuthToken::ACCOUNT_USER))
  4. {
  5. $errmsg = _("You have no permissions for viewing requested page");
  6. UI::Redirect("index.php");
  7. }
  8. if ($req_role_name)
  9. $req_name = CONFIG::$SECGROUP_PREFIX.$req_role_name;
  10. if (!$req_name || (!stristr($req_name, CONFIG::$SECGROUP_PREFIX) && !$_SESSION['sg_show_all']))
  11. {
  12. $errmsg = "Please select security group from list";
  13. UI::Redirect("sec_groups_view.php");
  14. }
  15. $display["title"] = "Security group&nbsp;&raquo;&nbsp;Edit group '{$req_name}'";
  16. $display["group_name"] = $req_name;
  17. $display["platform"] = $req_platform;
  18. $display["location"] = $req_location;
  19. switch($req_platform)
  20. {
  21. case SERVER_PLATFORMS::EC2:
  22. $platformClient = Scalr_Service_Cloud_Aws::newEc2(
  23. $req_location,
  24. Scalr_Session::getInstance()->getEnvironment()->getPlatformConfigValue(Modules_Platforms_Ec2::PRIVATE_KEY),
  25. Scalr_Session::getInstance()->getEnvironment()->getPlatformConfigValue(Modules_Platforms_Ec2::CERTIFICATE)
  26. );
  27. $account_id = Scalr_Session::getInstance()->getEnvironment()->getPlatformConfigValue(Modules_Platforms_Ec2::ACCOUNT_ID);
  28. break;
  29. case SERVER_PLATFORMS::EUCALYPTUS:
  30. $platformClient = Scalr_Service_Cloud_Eucalyptus::newCloud(
  31. Scalr_Session::getInstance()->getEnvironment()->getPlatformConfigValue(Modules_Platforms_Eucalyptus::SECRET_KEY),
  32. Scalr_Session::getInstance()->getEnvironment()->getPlatformConfigValue(Modules_Platforms_Eucalyptus::ACCESS_KEY),
  33. Scalr_Session::getInstance()->getEnvironment()->getPlatformConfigValue(Modules_Platforms_Eucalyptus::EC2_URL)
  34. );
  35. $account_id = Scalr_Session::getInstance()->getEnvironment()->getPlatformConfigValue(Modules_Platforms_Eucalyptus::ACCOUNT_ID);
  36. break;
  37. }
  38. // Rows
  39. try
  40. {
  41. if ($req_platform == SERVER_PLATFORMS::EC2)
  42. $response = $platformClient->DescribeSecurityGroups($req_name);
  43. else
  44. $response = $platformClient->describeSecurityGroups(array($req_name));
  45. $group = $response->securityGroupInfo->item;
  46. if (!($group instanceof stdClass))
  47. $group = $group[0];
  48. if ($group && $group instanceof stdClass)
  49. {
  50. $rules = $group->ipPermissions->item;
  51. if ($rules instanceof stdClass)
  52. $rules = array($rules);
  53. }
  54. }
  55. catch(Exception $e)
  56. {
  57. $errmsg = $e->getMessage();
  58. UI::Redirect("sec_groups_view.php");
  59. }
  60. $group_rules = array();
  61. foreach ($rules as $rule)
  62. {
  63. if ($rule->groups->item && !is_array($rule->groups->item))
  64. $rule->groups->item = array($rule->groups->item);
  65. if (count($rule->groups->item) > 0)
  66. {
  67. foreach ($rule->groups->item as &$group)
  68. {
  69. if ($group)
  70. {
  71. $r = clone $rule;
  72. $r->ip = '0.0.0.0/0';
  73. $r->rule = "user:{$group->userId}:{$group->groupName}:0.0.0.0/0";
  74. $r->userId = $group->userId;
  75. $r->type = 'user';
  76. $r->groupname = $group->groupName;
  77. $r->id = md5($r->rule);
  78. if (!$group_rules[$r->id])
  79. {
  80. $display['ug_rules'][$r->id] = $r;
  81. $group_rules[$r->id] = $r;
  82. }
  83. }
  84. }
  85. }
  86. elseif (count($rule->ipRanges->item) > 1)
  87. {
  88. foreach ($rule->ipRanges->item as &$ipRange)
  89. {
  90. if ($ipRange)
  91. {
  92. $r = clone $rule;
  93. $r->ip = $ipRange->cidrIp;
  94. $r->rule = "{$r->ipProtocol}:{$r->fromPort}:{$r->toPort}:{$ipRange->cidrIp}";
  95. $r->id = md5($r->rule);
  96. if (!$display['rules'][$r->id])
  97. {
  98. $display['rules'][$r->id] = $r;
  99. $group_rules[$r->id] = $r;
  100. }
  101. }
  102. }
  103. }
  104. else
  105. {
  106. $rule->ip = $rule->ipRanges->item->cidrIp;
  107. $rule->rule = "{$rule->ipProtocol}:{$rule->fromPort}:{$rule->toPort}:{$rule->ip}";
  108. $rule->id = md5($rule->rule);
  109. $display['rules'][$rule->id] = $rule;
  110. $group_rules[$rule->id] = $rule;
  111. }
  112. }
  113. if ($_POST)
  114. {
  115. $addRulesSet = array();
  116. $exists_rules = array();
  117. foreach ((array)$post_rules as $rule)
  118. {
  119. if (!$group_rules[md5($rule)] && $rule)
  120. {
  121. $group_rule = explode(":", $rule);
  122. if ($group_rule[0] != 'user')
  123. {
  124. $addRulesSet[] = array(
  125. 'IpProtocol' => $group_rule[0],
  126. 'FromPort' => $group_rule[1],
  127. 'ToPort' => $group_rule[2],
  128. 'CidrIp' => $group_rule[3]
  129. );
  130. }
  131. else
  132. {
  133. $addRulesSet[] = array(
  134. 'IpProtocol' => 'tcp',
  135. 'FromPort' => 1,
  136. 'ToPort' => 65535,
  137. 'GroupName' => $group_rule[2],
  138. 'UserId' => $group_rule[1]
  139. );
  140. $addRulesSet[] = array(
  141. 'IpProtocol' => 'udp',
  142. 'FromPort' => 1,
  143. 'ToPort' => 65535,
  144. 'GroupName' => $group_rule[2],
  145. 'UserId' => $group_rule[1]
  146. );
  147. $addRulesSet[] = array(
  148. 'IpProtocol' => 'icmp',
  149. 'FromPort' => -1,
  150. 'ToPort' => -1,
  151. 'GroupName' => $group_rule[2],
  152. 'UserId' => $group_rule[1]
  153. );
  154. }
  155. $new_rules_added = true;
  156. }
  157. $exists_rules[md5($rule)] = true;
  158. }
  159. $remRulesSet = array();
  160. foreach ($group_rules as $rule_hash=>$rule)
  161. {
  162. if (!$exists_rules[$rule_hash])
  163. {
  164. if ($rule->type != 'user')
  165. {
  166. $remRulesSet[] = array(
  167. 'IpProtocol' => $rule->ipProtocol,
  168. 'FromPort' => $rule->fromPort,
  169. 'ToPort' => $rule->toPort,
  170. 'CidrIp' => $rule->ip
  171. );
  172. }
  173. else
  174. {
  175. $remRulesSet[] = array(
  176. 'IpProtocol' => 'tcp',
  177. 'FromPort' => 1,
  178. 'ToPort' => 65535,
  179. 'GroupName' => $rule->groupname,
  180. 'UserId' => $rule->userId
  181. );
  182. $remRulesSet[] = array(
  183. 'IpProtocol' => 'udp',
  184. 'FromPort' => 1,
  185. 'ToPort' => 65535,
  186. 'GroupName' => $rule->groupname,
  187. 'UserId' => $rule->userId
  188. );
  189. $remRulesSet[] = array(
  190. 'IpProtocol' => 'icmp',
  191. 'FromPort' => -1,
  192. 'ToPort' => -1,
  193. 'GroupName' => $rule->groupname,
  194. 'UserId' => $rule->userId
  195. );
  196. }
  197. $remove_rules = true;
  198. }
  199. }
  200. if ($new_rules_added)
  201. {
  202. if ($req_platform == SERVER_PLATFORMS::EUCALYPTUS)
  203. {
  204. foreach ($addRulesSet as $rule)
  205. $platformClient->authorizeSecurityGroupIngress(
  206. $req_name,
  207. $rule['IpProtocol'],
  208. $rule['FromPort'],
  209. $rule['ToPort'],
  210. $rule['CidrIp'],
  211. $rule['GroupName'],
  212. $rule['UserId']
  213. );
  214. }
  215. else
  216. {
  217. $IpPermissionSet = new IpPermissionSetType();
  218. foreach ($addRulesSet as $rule) {
  219. if ($rule['GroupName'])
  220. $IpPermissionSet->AddItem(
  221. $rule['IpProtocol'],
  222. $rule['FromPort'],
  223. $rule['ToPort'],
  224. array('userId' => $rule['UserId'], 'groupName' => $rule['GroupName']),
  225. null
  226. );
  227. else
  228. $IpPermissionSet->AddItem(
  229. $rule['IpProtocol'],
  230. $rule['FromPort'],
  231. $rule['ToPort'],
  232. null,
  233. array($rule['CidrIp'])
  234. );
  235. }
  236. $platformClient->AuthorizeSecurityGroupIngress($account_id, $req_name, $IpPermissionSet);
  237. }
  238. }
  239. if ($remove_rules)
  240. {
  241. if ($req_platform == SERVER_PLATFORMS::EUCALYPTUS)
  242. {
  243. foreach ($remRulesSet as $rule)
  244. $platformClient->revokeSecurityGroupIngress(
  245. $req_name,
  246. $rule['IpProtocol'],
  247. $rule['FromPort'],
  248. $rule['ToPort'],
  249. $rule['CidrIp'],
  250. $rule['GroupName'],
  251. $rule['UserId']
  252. );
  253. }
  254. else
  255. {
  256. $IpPermissionSet = new IpPermissionSetType();
  257. foreach ($remRulesSet as $rule) {
  258. if ($rule['GroupName'])
  259. $IpPermissionSet->AddItem(
  260. $rule['IpProtocol'],
  261. $rule['FromPort'],
  262. $rule['ToPort'],
  263. array('userId' => $rule['UserId'], 'groupName' => $rule['GroupName']),
  264. null
  265. );
  266. else
  267. $IpPermissionSet->AddItem(
  268. $rule['IpProtocol'],
  269. $rule['FromPort'],
  270. $rule['ToPort'],
  271. null,
  272. array($rule['CidrIp'])
  273. );
  274. }
  275. $platformClient->RevokeSecurityGroupIngress($account_id, $req_name, $IpPermissionSet);
  276. }
  277. }
  278. $okmsg = _("Security group successfully updated");
  279. UI::Redirect("/sec_group_edit.php?name={$req_name}&platform={$req_platform}&location={$req_location}");
  280. }
  281. require("src/append.inc.php");
  282. ?>