jwt_key.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. import logging
  10. from textwrap import dedent
  11. import requests
  12. from materialize.cloudtest.util.common import retry
  13. LOGGER = logging.getLogger(__name__)
  14. def fetch_jwt(
  15. email: str, password: str, host: str, scheme: str, max_tries: int = 10
  16. ) -> str:
  17. def fetch():
  18. res = requests.post(
  19. f"{scheme}://{host}/identity/resources/auth/v1/user",
  20. json={"email": email, "password": password},
  21. timeout=10,
  22. )
  23. res.raise_for_status()
  24. return res
  25. try:
  26. res = retry(fetch, max_tries, [requests.exceptions.HTTPError])
  27. except requests.exceptions.HTTPError as e:
  28. res = e.response
  29. LOGGER.error(
  30. dedent(
  31. f"""
  32. e: {e}
  33. res: {res}
  34. res.text: {res.text}
  35. """
  36. )
  37. )
  38. raise
  39. access_token: str = res.json()["accessToken"]
  40. return access_token