auth.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from __future__ import annotations
  2. import os
  3. from typing import Any
  4. from wandb import env
  5. from wandb.sdk import wandb_setup
  6. from wandb.sdk.lib import wbauth
  7. def sagemaker_auth(
  8. overrides: dict[str, Any] | None = None,
  9. path: str = ".",
  10. api_key: str | None = None,
  11. ) -> None:
  12. """Write a secrets.env file with the W&B ApiKey and any additional secrets passed.
  13. Args:
  14. overrides: Additional environment variables to write to secrets.env
  15. path: The path to write the secrets file.
  16. """
  17. overrides = overrides or dict()
  18. api_key = (
  19. overrides.get(env.API_KEY, None)
  20. or api_key
  21. or wandb_setup.singleton().settings.api_key
  22. or wbauth.read_netrc_auth(host=wandb_setup.singleton().settings.base_url)
  23. )
  24. if api_key is None:
  25. raise ValueError(
  26. "Can't find W&B API key, set the WANDB_API_KEY env variable"
  27. + " or run `wandb login`"
  28. )
  29. overrides[env.API_KEY] = api_key
  30. with open(os.path.join(path, "secrets.env"), "w") as file:
  31. for k, v in overrides.items():
  32. file.write(f"{k}={v}\n")