signers.py 2.0 KB

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