Skip to content

Commit

Permalink
assert credentials and check against all contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
asaiacai committed Sep 29, 2024
1 parent 0f8a53b commit 2881508
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions sky/provision/do/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,33 @@ def _init_client():
global _client, CREDENTIALS_PATH
if _client is None:
CREDENTIALS_PATH = None
credentials_found = 0
for path in POSSIBLE_CREDENTIALS_PATHS:
if os.path.exists(path):
CREDENTIALS_PATH = path
credentials_found += 1
logger.debug(f'Digital Ocean credential path found at {path}')
assert credentials_found == 1, 'more than 1 credential file found'
if CREDENTIALS_PATH is None:
raise DigitalOceanError(
'no credentials file found from '
f'the following paths {POSSIBLE_CREDENTIALS_PATHS}')

try:
api_token = common_utils.read_yaml(
CREDENTIALS_PATH)['auth-contexts']['skypilot']
except KeyError as e:
raise KeyError('No valid token found for skypilot.'
'Try setting your auth token'
'to the skypilot context,'
'`doctl auth init --context skypilot`') from e

_client = do.pydo.Client(token=api_token)
auth_contexts = common_utils.read_yaml(
CREDENTIALS_PATH)['auth-contexts']
for context, api_token in auth_contexts.items():
try:
test_client = do.pydo.Client(token=api_token)
test_client.droplets.list()
logger.debug(f'using {context} context')
_client = test_client
break
except do.exceptions().HttpResponseError:
continue
else:
raise DigitalOceanError(
'no valid api tokens found try '
'setting a new API token with `doctl auth init`')
return _client


Expand Down

0 comments on commit 2881508

Please sign in to comment.