Skip to content

Commit 3214258

Browse files
authored
fix: modify the way to get credentials (#318)
1 parent 7758ea5 commit 3214258

File tree

1 file changed

+42
-25
lines changed

1 file changed

+42
-25
lines changed

veadk/integrations/ve_identity/identity_client.py

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -66,38 +66,55 @@ def _refresh_creds(self: IdentityClient):
6666
"VOLCENGINE_SESSION_TOKEN", ""
6767
)
6868

69-
# If credentials are not available, try to get from VeFaaS IAM
70-
if not (ak and sk):
69+
# Helper function to attempt VeFaaS IAM credential retrieval
70+
def try_get_vefaas_credentials():
71+
"""Attempt to retrieve credentials from VeFaaS IAM."""
7172
try:
72-
logger.info(
73-
"Credentials not found in environment, attempting to fetch from VeFaaS IAM..."
74-
)
73+
logger.info("Attempting to fetch credentials from VeFaaS IAM...")
7574
ve_iam_cred = get_credential_from_vefaas_iam()
76-
ak = ve_iam_cred.access_key_id
77-
sk = ve_iam_cred.secret_access_key
78-
session_token = ve_iam_cred.session_token
79-
logger.info("Successfully retrieved credentials from VeFaaS IAM")
75+
return (
76+
ve_iam_cred.access_key_id,
77+
ve_iam_cred.secret_access_key,
78+
ve_iam_cred.session_token,
79+
)
8080
except FileNotFoundError as e:
8181
logger.warning(f"VeFaaS IAM credentials not available: {e}")
8282
except Exception as e:
8383
logger.warning(f"Failed to retrieve credentials from VeFaaS IAM: {e}")
84+
return None
8485

85-
if not session_token and ak and sk:
86-
role_trn = self._get_iam_role_trn_from_vefaas_iam()
87-
if not role_trn:
88-
role_trn = os.getenv("RUNTIME_IAM_ROLE_TRN", "")
89-
# If there is no session_token and role_trn is configured, execute AssumeRole
90-
if role_trn:
91-
try:
92-
logger.info(
93-
f"No session token found, attempting AssumeRole with role: {role_trn}"
94-
)
95-
sts_credentials = self._assume_role(ak, sk, role_trn)
96-
ak = sts_credentials.access_key_id
97-
sk = sts_credentials.secret_access_key
98-
session_token = sts_credentials.session_token
99-
except Exception as e:
100-
logger.warning(f"Failed to assume role: {e}")
86+
# If no AK/SK, try to get from VeFaaS IAM
87+
if not (ak and sk):
88+
logger.info(
89+
"Credentials not found in environment, attempting to fetch from VeFaaS IAM..."
90+
)
91+
credentials = try_get_vefaas_credentials()
92+
if credentials:
93+
ak, sk, session_token = credentials
94+
95+
# If we have AK/SK but no session token, try to get complete credentials
96+
if ak and sk and not session_token:
97+
# First attempt: try VeFaaS IAM
98+
credentials = try_get_vefaas_credentials()
99+
if credentials:
100+
ak, sk, session_token = credentials
101+
102+
# Second attempt: if still no session token, try AssumeRole
103+
if not session_token:
104+
role_trn = self._get_iam_role_trn_from_vefaas_iam() or os.getenv(
105+
"RUNTIME_IAM_ROLE_TRN", ""
106+
)
107+
108+
if role_trn:
109+
try:
110+
logger.info(f"Attempting AssumeRole with role: {role_trn}")
111+
sts_credentials = self._assume_role(ak, sk, role_trn)
112+
ak = sts_credentials.access_key_id
113+
sk = sts_credentials.secret_access_key
114+
session_token = sts_credentials.session_token
115+
logger.info("Successfully obtained credentials via AssumeRole")
116+
except Exception as e:
117+
logger.warning(f"Failed to assume role: {e}")
101118

102119
# Update configuration with the credentials
103120
self._api_client.api_client.configuration.ak = ak

0 commit comments

Comments
 (0)