signers.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # Copyright 2014 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 ccscli import UNSIGNED
  17. import ccscli.auth
  18. from ccscli.exceptions import UnknownSignatureVersionError
  19. class RequestSigner(object):
  20. """
  21. An object to sign requests before they go out over the wire using
  22. one of the authentication mechanisms defined in ``auth.py``.
  23. """
  24. def __init__(self, signature_version, credentials):
  25. self._signature_version = signature_version
  26. self._credentials = credentials
  27. @property
  28. def signature_version(self):
  29. return self._signature_version
  30. def sign(self, request):
  31. """
  32. Sign a request before it goes out over the wire.
  33. """
  34. if self._signature_version != UNSIGNED:
  35. signer = self.get_auth_instance(self._signature_version)
  36. signer.add_auth(request)
  37. def get_auth_instance(self, signature_version, **kwargs):
  38. """
  39. Get an auth instance which can be used to sign a request
  40. using the given signature version.
  41. """
  42. cls = ccscli.auth.AUTH_TYPE_MAPS.get(signature_version)
  43. if cls is None:
  44. raise UnknownSignatureVersionError(
  45. signature_version=signature_version)
  46. # If there's no credentials provided (i.e credentials is None),
  47. # then we'll pass a value of "None" over to the auth classes,
  48. # which already handle the cases where no credentials have
  49. # been provided.
  50. frozen_credentials = self._credentials.get_frozen_credentials()
  51. kwargs['credentials'] = frozen_credentials
  52. auth = cls(**kwargs)
  53. return auth