Skip to content

Commit 1d6a66c

Browse files
committed
2 parents 494571e + 0102469 commit 1d6a66c

8 files changed

Lines changed: 781 additions & 405 deletions

File tree

eval_protocol/auth.py

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,56 @@ def _get_credential_from_config_file(key_name: str) -> Optional[str]:
136136
return None
137137

138138

139+
def _get_credentials_from_config_file() -> Dict[str, Optional[str]]:
140+
"""
141+
Retrieve both api_key and account_id from auth.ini with a single read/parse.
142+
Tries simple parsing first for both keys, then falls back to configparser for any missing ones.
143+
Returns a dict with up to two keys: 'api_key' and 'account_id'.
144+
"""
145+
results: Dict[str, Optional[str]] = {}
146+
auth_ini_path = _get_auth_ini_file()
147+
if not auth_ini_path.exists():
148+
return results
149+
150+
# 1) Simple key=value parsing
151+
try:
152+
simple_creds = _parse_simple_auth_file(auth_ini_path)
153+
if "api_key" in simple_creds and simple_creds["api_key"]:
154+
results["api_key"] = simple_creds["api_key"]
155+
if "account_id" in simple_creds and simple_creds["account_id"]:
156+
results["account_id"] = simple_creds["account_id"]
157+
if "api_key" in results and "account_id" in results:
158+
return results
159+
except Exception as e:
160+
logger.warning("Error during simple parsing of %s: %s", str(auth_ini_path), e)
161+
162+
# 2) ConfigParser for any missing keys
163+
try:
164+
config = configparser.ConfigParser()
165+
config.read(auth_ini_path)
166+
for key_name in ("api_key", "account_id"):
167+
if key_name in results and results[key_name]:
168+
continue
169+
if "fireworks" in config and config.has_option("fireworks", key_name):
170+
value_from_file = config.get("fireworks", key_name)
171+
if value_from_file:
172+
results[key_name] = value_from_file
173+
continue
174+
if config.has_option(config.default_section, key_name):
175+
value_from_default = config.get(config.default_section, key_name)
176+
if value_from_default:
177+
results[key_name] = value_from_default
178+
except configparser.MissingSectionHeaderError:
179+
# Purely key=value file without section headers; simple parsing should have handled it already.
180+
logger.debug("%s has no section headers; falling back to simple parsing results.", str(auth_ini_path))
181+
except configparser.Error as e_config:
182+
logger.warning("Configparser error reading %s: %s", str(auth_ini_path), e_config)
183+
except Exception as e_general:
184+
logger.warning("Unexpected error reading %s: %s", str(auth_ini_path), e_general)
185+
186+
return results
187+
188+
139189
def get_fireworks_api_key() -> Optional[str]:
140190
"""
141191
Retrieves the Fireworks API key.
@@ -177,13 +227,15 @@ def get_fireworks_account_id() -> Optional[str]:
177227
The Account ID is sourced in the following order:
178228
1. FIREWORKS_ACCOUNT_ID environment variable.
179229
2. 'account_id' from the [fireworks] section of ~/.fireworks/auth.ini.
230+
3. If an API key is available (env or auth.ini), resolve via verifyApiKey.
180231
181232
Returns:
182233
The Account ID if found, otherwise None.
183234
"""
184235
# If a profile is active, prefer profile file first, then env
185236
if _is_profile_active():
186-
account_id_from_file = _get_credential_from_config_file("account_id")
237+
creds = _get_credentials_from_config_file()
238+
account_id_from_file = creds.get("account_id")
187239
if account_id_from_file:
188240
return account_id_from_file
189241
account_id = os.environ.get("FIREWORKS_ACCOUNT_ID")
@@ -196,11 +248,24 @@ def get_fireworks_account_id() -> Optional[str]:
196248
if account_id:
197249
logger.debug("Using FIREWORKS_ACCOUNT_ID from environment variable.")
198250
return account_id
199-
account_id_from_file = _get_credential_from_config_file("account_id")
251+
creds = _get_credentials_from_config_file()
252+
account_id_from_file = creds.get("account_id")
200253
if account_id_from_file:
201254
return account_id_from_file
202255

203-
logger.debug("Fireworks Account ID not found in environment variables or auth.ini.")
256+
# 3) Fallback: if API key is present, attempt to resolve via verifyApiKey (env or auth.ini)
257+
try:
258+
# Intentionally use get_fireworks_api_key to centralize precedence (env vs file)
259+
api_key_for_verify = get_fireworks_api_key()
260+
if api_key_for_verify:
261+
resolved = verify_api_key_and_get_account_id(api_key=api_key_for_verify, api_base=get_fireworks_api_base())
262+
if resolved:
263+
logger.debug("Using FIREWORKS_ACCOUNT_ID resolved via verifyApiKey: %s", resolved)
264+
return resolved
265+
except Exception as e:
266+
logger.debug("Failed to resolve FIREWORKS_ACCOUNT_ID via verifyApiKey: %s", e)
267+
268+
logger.debug("Fireworks Account ID not found in environment variables, auth.ini, or via verifyApiKey.")
204269
return None
205270

206271

0 commit comments

Comments
 (0)