sts_client.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  4. # except in compliance with the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the
  9. # License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  10. # either express or implied. See the License for the specific language governing permissions
  11. # and limitations under the License.
  12. """
  13. This module provides a client for STS.
  14. """
  15. from future.utils import iteritems
  16. import copy
  17. import http.client
  18. import os
  19. import json
  20. import logging
  21. import shutil
  22. from builtins import str
  23. from builtins import bytes
  24. import baidubce
  25. from baidubce import bce_client_configuration
  26. from baidubce import utils
  27. from baidubce.auth import bce_v1_signer
  28. from baidubce.bce_base_client import BceBaseClient
  29. from baidubce.exception import BceClientError
  30. from baidubce.exception import BceServerError
  31. from baidubce.exception import BceHttpClientError
  32. from baidubce.http import bce_http_client
  33. from baidubce.http import handler
  34. from baidubce.http import http_content_types
  35. from baidubce.http import http_headers
  36. from baidubce.http import http_methods
  37. from baidubce.services import sts
  38. from baidubce.utils import required
  39. _logger = logging.getLogger(__name__)
  40. class StsClient(BceBaseClient):
  41. """
  42. sdk client
  43. """
  44. def __init__(self, config=None):
  45. BceBaseClient.__init__(self, config)
  46. def get_session_token(self, acl, duration_seconds=None):
  47. """
  48. :type duration_seconds: int
  49. :param duration_seconds: None
  50. :token effective period
  51. :type id: string
  52. :param id: None
  53. :acl id
  54. :type acl: dict
  55. :param acl: None
  56. :acl
  57. :return:
  58. **HttpResponse**
  59. """
  60. params = None
  61. if duration_seconds is not None:
  62. if isinstance(duration_seconds, int):
  63. params = {b'durationSeconds': duration_seconds}
  64. if acl is None:
  65. body = None
  66. else:
  67. if not isinstance(acl, dict):
  68. raise TypeError(b'acl should be dict')
  69. if 'id' in acl:
  70. body = json.dumps(acl)
  71. else:
  72. body = json.dumps(acl, default=self._dump_acl_object)
  73. return self._send_request(
  74. http_methods.POST,
  75. body=body,
  76. headers={http_headers.CONTENT_TYPE: http_content_types.JSON},
  77. params=params)
  78. @staticmethod
  79. def _dump_acl_object(acl):
  80. result = {}
  81. for k, v in iteritems(acl.__dict__):
  82. if not k.startswith('_'):
  83. result[k] = v
  84. return result
  85. def _merge_config(self, config):
  86. if config is None:
  87. return self.config
  88. else:
  89. new_config = copy.copy(self.config)
  90. new_config.merge_non_none_values(config)
  91. return new_config
  92. def _send_request(self,
  93. http_method,
  94. body=None,
  95. headers=None,
  96. params=None,
  97. config=None,
  98. body_parser=None):
  99. config = self._merge_config(config)
  100. path = sts.URL_PREFIX + b"sessionToken"
  101. if body_parser is None:
  102. body_parser = handler.parse_json
  103. return bce_http_client.send_request(
  104. config, bce_v1_signer.sign, [handler.parse_error, body_parser],
  105. http_method, path, body, headers, params)