static_sts.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. from alibabacloud_credentials.provider.refreshable import Credentials
  2. from alibabacloud_credentials_api import ICredentialsProvider
  3. from alibabacloud_credentials.utils import auth_util
  4. class StaticSTSCredentialsProvider(ICredentialsProvider):
  5. def __init__(self, *,
  6. access_key_id: str = None,
  7. access_key_secret: str = None,
  8. security_token: str = None):
  9. self.access_key_id = access_key_id or auth_util.environment_access_key_id
  10. self.access_key_secret = access_key_secret or auth_util.environment_access_key_secret
  11. self.security_token = security_token or auth_util.environment_security_token
  12. if self.access_key_id is None or self.access_key_id == '':
  13. raise ValueError('the access key id is empty')
  14. if self.access_key_secret is None or self.access_key_secret == '':
  15. raise ValueError('the access key secret is empty')
  16. if self.security_token is None or self.security_token == '':
  17. raise ValueError('the security token is empty')
  18. def get_credentials(self) -> Credentials:
  19. return Credentials(
  20. access_key_id=self.access_key_id,
  21. access_key_secret=self.access_key_secret,
  22. security_token=self.security_token,
  23. provider_name=self.get_provider_name()
  24. )
  25. async def get_credentials_async(self) -> Credentials:
  26. return self.get_credentials()
  27. def get_provider_name(self) -> str:
  28. return 'static_sts'