acl_client.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. # Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing,
  10. # software distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions
  13. # and limitations under the License.
  14. """
  15. This module provides a client class for ACL.
  16. """
  17. import copy
  18. import json
  19. import logging
  20. import uuid
  21. import sys
  22. from baidubce import bce_base_client
  23. from baidubce.auth import bce_v1_signer
  24. from baidubce.http import bce_http_client
  25. from baidubce.http import handler
  26. from baidubce.http import http_methods
  27. from baidubce import utils
  28. from baidubce.utils import required
  29. from baidubce import compat
  30. if sys.version < '3':
  31. reload(sys)
  32. sys.setdefaultencoding('utf-8')
  33. _logger = logging.getLogger(__name__)
  34. class AclClient(bce_base_client.BceBaseClient):
  35. """
  36. ACL base sdk client
  37. """
  38. prefix = b'/v1'
  39. def __init__(self, config=None):
  40. bce_base_client.BceBaseClient.__init__(self, config)
  41. def _merge_config(self, config=None):
  42. """
  43. :param config:
  44. :type config: baidubce.BceClientConfiguration
  45. :return:
  46. """
  47. if config is None:
  48. return self.config
  49. else:
  50. new_config = copy.copy(self.config)
  51. new_config.merge_non_none_values(config)
  52. return new_config
  53. def _send_request(self, http_method, path,
  54. body=None, headers=None, params=None,
  55. config=None, body_parser=None):
  56. config = self._merge_config(config)
  57. if body_parser is None:
  58. body_parser = handler.parse_json
  59. if headers is None:
  60. headers = {b'Accept': b'*/*', b'Content-Type':
  61. b'application/json;charset=utf-8'}
  62. return bce_http_client.send_request(
  63. config, bce_v1_signer.sign, [handler.parse_error, body_parser],
  64. http_method, path, body, headers, params)
  65. @required(vpc_id=(bytes, str))
  66. def list_acl_entrys(self, vpc_id, config=None):
  67. """
  68. Get the detail information of acl for specific vpc.
  69. :param vpc_id:
  70. the vpc id
  71. :type vpc_id: string
  72. :param config:
  73. :type config: baidubce.BceClientConfiguration
  74. :return:
  75. :rtype baidubce.bce_response.BceResponse
  76. """
  77. path = utils.append_uri(self.prefix, 'acl')
  78. params = {}
  79. params[b'vpcId'] = vpc_id
  80. return self._send_request(http_methods.GET, path,
  81. params=params, config=config)
  82. @required(rule_list=list)
  83. def create_acl(self, rule_list, client_token=None, config=None):
  84. """
  85. Create acl rules with the specified options.
  86. :param rule_list:
  87. a list contains acl rules.
  88. https://cloud.baidu.com/doc/VPC/API.html#AclRuleRequest
  89. The elements of the list are AclRuleRequest
  90. :type rule_list: list
  91. AclRuleRequest{
  92. :param subnetId:
  93. The subnet id which the acl rule applied to
  94. :type subnetId: string
  95. :param protocol:
  96. The parameter specify which protocol will the acl rule work on
  97. :value: "all" or ""tcp" or "udp" or "icmp"
  98. :type protocol: string
  99. :param sourceIpAddress:
  100. Source ip address which the rule applied to
  101. :type sourceIpAddress: string
  102. :param destinationIpAddress:
  103. Destination ip address which the rule applied to
  104. :type destinationIpAddress: string
  105. :param sourcePort:
  106. Port used by source ip address
  107. :value 1-65535
  108. :type sourcePort: string
  109. :param destinationPort:
  110. Port used by destination ip address
  111. :value 1-65535
  112. :type destinationPort:string
  113. :param position:
  114. Priority of the rule
  115. :value 1-5000,unique,The smaller the value, the higher the priority
  116. :type:position:Integer
  117. :param direction:
  118. The rule is a ingress or a egress rule
  119. :value: "ingress" or "egress"
  120. :type direction:string
  121. :param action:
  122. The rule is allowed or denied
  123. :value "allow" or "deny"
  124. :type action:string
  125. :param description(Optional):
  126. The option param to describe the acl rule.
  127. :type description: string
  128. }
  129. :param client_token:
  130. If the clientToken is not specified by the user,
  131. a random Stringgenerated by default algorithm will be used.
  132. :type client_token: string
  133. :param config:
  134. :type config: baidubce.BceClientConfiguration
  135. :return:
  136. :rtype baidubce.bce_response.BceResponse
  137. """
  138. path = utils.append_uri(self.prefix, 'acl', 'rule')
  139. params = {}
  140. if client_token is None:
  141. params[b'clientToken'] = generate_client_token()
  142. else:
  143. params[b'clientToken'] = client_token
  144. body = {
  145. 'aclRules': rule_list
  146. }
  147. return self._send_request(http_methods.POST, path,
  148. body=json.dumps(body), params=params,
  149. config=config)
  150. @required(subnet_id=(bytes, str))
  151. def list_subnet_acl(self, subnet_id, marker=None, max_keys=None, config=None):
  152. """
  153. Return a list of acl rules of specify subnet.
  154. :param subnet_id
  155. the id of subnet whhich the acl applied
  156. :type subnet_id: string
  157. :param marker
  158. The optional parameter marker specified in the original
  159. request to specify where in the results to begin listing.
  160. Together with the marker, specifies the list result
  161. which listing should begin. If the marker is not specified,
  162. the list result will listing from the first one.
  163. :type marker: string
  164. :param max_keys
  165. The optional parameter to specifies the max number of
  166. list result to return.
  167. The default value is 1000.
  168. :type max_keys: int
  169. :param config:
  170. :type config: baidubce.BceClientConfiguration
  171. :return:
  172. :rtype baidubce.bce_response.BceResponse
  173. """
  174. path = utils.append_uri(self.prefix, 'acl', 'rule')
  175. params = {}
  176. if marker is not None:
  177. params[b'marker'] = marker
  178. if max_keys is not None:
  179. params[b'maxKeys'] = max_keys
  180. params[b'subnetId'] = subnet_id
  181. return self._send_request(http_methods.GET, path,
  182. params=params, config=config)
  183. @required(acl_rule_id=(bytes, str))
  184. def delete_acl(self, acl_rule_id, client_token=None, config=None):
  185. """
  186. Delete the specific acl rule.
  187. :param acl_rule_id:
  188. The id of the specified acl.
  189. :type acl_rule_id: string
  190. :param client_token:
  191. If the clientToken is not specified by the user, a random String
  192. generated by default algorithm will be used.
  193. :type client_token: string
  194. :param config:
  195. :type config: baidubce.BceClientConfiguration
  196. :return:
  197. :rtype baidubce.bce_response.BceResponse
  198. """
  199. path = utils.append_uri(self.prefix, 'acl', 'rule', acl_rule_id)
  200. params = {}
  201. if client_token is None:
  202. params[b'clientToken'] = generate_client_token()
  203. else:
  204. params[b'clientToken'] = client_token
  205. return self._send_request(http_methods.DELETE, path,
  206. params=params, config=config)
  207. @required(acl_rule_id=(bytes, str))
  208. def update_acl(self, acl_rule_id, description=None,
  209. protocol=None, source_ip_address=None,
  210. destination_ip_address=None, source_port=None,
  211. destination_port=None,
  212. position=None, action=None,
  213. client_token=None, config=None):
  214. """
  215. Modify the special attribute to new value of the acl owned by the user.
  216. :param acl_rule_id
  217. id of the acl to be modified
  218. :type acl_rule_id:string
  219. :param description:
  220. The option param to describe the acl rule.
  221. :type description: string
  222. :param protocol:
  223. The parameter specify which protocol will the acl rule work on
  224. :value: "all" or ""tcp" or "udp" or "icmp"
  225. :type protocol: string
  226. :param source_ip_address:
  227. source ip address which the rule applied to
  228. :type source_ip_address: string
  229. :param destination_ip_address:
  230. destination ip address which the rule applied to
  231. :type destination_ip_address: string
  232. :param source_port:
  233. port used by source ip address
  234. :value 1-65535
  235. :type source_port: string
  236. :param destination_port:
  237. port used by destination ip address
  238. :value 1-65535
  239. :type destination_port:string
  240. :param position:
  241. priority of the rule
  242. :value 1-5000,unique,The smaller the value, the higher the priority
  243. :type:position:Integer
  244. :param action:
  245. the rule is allowed or denied
  246. :value "allow" or "deny"
  247. :type action:string
  248. :param client_token:
  249. If the clientToken is not specified by the user, a random
  250. String generated by default algorithm will be used.
  251. :type client_token: string
  252. :param config:
  253. :type config: baidubce.BceClientConfiguration
  254. :return:
  255. :rtype baidubce.bce_response.BceResponse
  256. """
  257. path = utils.append_uri(self.prefix, 'acl', 'rule', acl_rule_id)
  258. params = {}
  259. if client_token is None:
  260. params[b'clientToken'] = generate_client_token()
  261. else:
  262. params[b'clientToken'] = client_token
  263. body = {}
  264. if description is not None:
  265. body['description'] = compat.convert_to_string(description)
  266. if protocol is not None:
  267. body['protocol'] = compat.convert_to_string(protocol)
  268. if source_ip_address is not None:
  269. body['sourceIpAddress'] = \
  270. compat.convert_to_string(source_ip_address)
  271. if destination_ip_address is not None:
  272. body['destinationIpAddress'] = \
  273. compat.convert_to_string(destination_ip_address)
  274. if source_port is not None:
  275. body['sourcePort'] = compat.convert_to_string(source_port)
  276. if destination_port is not None:
  277. body['destinationPort'] = \
  278. compat.convert_to_string(destination_port)
  279. if position is not None:
  280. body['position'] = position
  281. if action is not None:
  282. body['action'] = compat.convert_to_string(action)
  283. return self._send_request(http_methods.PUT, path, json.dumps(body),
  284. params=params, config=config)
  285. def generate_client_token_by_uuid():
  286. """
  287. The default method to generate the random string for client_token
  288. if the optional parameter client_token is not specified by the user.
  289. :return:
  290. :rtype string
  291. """
  292. return str(uuid.uuid4())
  293. generate_client_token = generate_client_token_by_uuid