setup_credentials.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. """
  2. This script is used to set up credentials for some services in the
  3. CI environment. For instance, it can fetch WandB API tokens and write
  4. the WandB configuration file so test scripts can use the service.
  5. """
  6. import os
  7. import sys
  8. import boto3
  9. AWS_WANDB_SECRET_ARN = (
  10. "arn:aws:secretsmanager:us-west-2:029272617770:secret:oss-ci/wandb-key-V8UeE5"
  11. )
  12. AWS_COMET_SECRET_ARN = (
  13. "arn:aws:secretsmanager:us-west-2:029272617770:secret:oss-ci/comet-ml-token-vw81C3"
  14. )
  15. def get_and_write_wandb_api_key(client):
  16. api_key = client.get_secret_value(SecretId=AWS_WANDB_SECRET_ARN)["SecretString"]
  17. with open(os.path.expanduser("~/.netrc"), "w") as fp:
  18. fp.write(f"machine api.wandb.ai\n" f" login user\n" f" password {api_key}\n")
  19. def get_and_write_comet_ml_api_key(client):
  20. api_key = client.get_secret_value(SecretId=AWS_COMET_SECRET_ARN)["SecretString"]
  21. with open(os.path.expanduser("~/.comet.config"), "w") as fp:
  22. fp.write(f"[comet]\napi_key={api_key}\n")
  23. SERVICES = {
  24. "wandb": get_and_write_wandb_api_key,
  25. "comet_ml": get_and_write_comet_ml_api_key,
  26. }
  27. if __name__ == "__main__":
  28. if len(sys.argv) < 2:
  29. print(f"Usage: python {sys.argv[0]} <service1> [service2] ...")
  30. sys.exit(0)
  31. services = sys.argv[1:]
  32. if any(service not in SERVICES for service in services):
  33. raise RuntimeError(
  34. f"All services must be included in {list(SERVICES.keys())}. "
  35. f"Got: {services}"
  36. )
  37. client = boto3.client("secretsmanager", region_name="us-west-2")
  38. for service in services:
  39. SERVICES[service](client)