credentials.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # Copyright 2012-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  2. #
  3. # Modifications made by Cloudera are:
  4. # Copyright (c) 2016 Cloudera, Inc. All rights reserved.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License"). You
  7. # may not use this file except in compliance with the License. A copy of
  8. # the License is located at
  9. #
  10. # http://aws.amazon.com/apache2.0/
  11. #
  12. # or in the "license" file accompanying this file. This file is
  13. # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  14. # ANY KIND, either express or implied. See the License for the specific
  15. # language governing permissions and limitations under the License.
  16. from collections import namedtuple
  17. import six
  18. ReadOnlyCredentials = namedtuple('ReadOnlyCredentials',
  19. ['access_key_id', 'private_key', 'method'])
  20. class Credentials(object):
  21. """
  22. Holds the credentials needed to authenticate requests.
  23. """
  24. def __init__(self, access_key_id, private_key, method):
  25. self.access_key_id = access_key_id
  26. self.private_key = private_key
  27. self.method = method
  28. self._normalize()
  29. def ensure_unicode(self, s, encoding='utf-8', errors='strict'):
  30. if isinstance(s, six.text_type):
  31. return s
  32. return unicode(s, encoding, errors)
  33. def _normalize(self):
  34. self.access_key_id = self.ensure_unicode(self.access_key_id)
  35. self.private_key = self.ensure_unicode(self.private_key)
  36. def get_frozen_credentials(self):
  37. return ReadOnlyCredentials(self.access_key_id,
  38. self.private_key,
  39. self.method)