Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Password resolution in connector #1002

Merged
merged 2 commits into from
Jan 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions juju/client/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ async def connect(self, **kwargs):
else:
if self._connection:
await self._connection.close()

account = kwargs.pop('account', {})
# Prioritize the username and password that user provided
# If not enough, try to patch it with info from accounts.yaml
if 'username' not in kwargs and account.get('user'):
kwargs.update(username=account.get('user'))
if 'password' not in kwargs and account.get('password'):
kwargs.update(password=account.get('password'))

if not ({'username', 'password'}.issubset(kwargs)):
required = {'username', 'password'}.difference(kwargs)
raise ValueError(f'Some authentication parameters are required : {",".join(required)}')
self._connection = await Connection.connect(**kwargs)

if not self.controller_name:
Expand All @@ -103,7 +115,7 @@ async def disconnect(self):
await self._log_connection.close()
self._log_connection = None

async def connect_controller(self, controller_name=None, specified_facades=None):
async def connect_controller(self, controller_name=None, specified_facades=None, **kwargs):
"""Connect to a controller by name. If the name is empty, it
connect to the current controller.
"""
Expand All @@ -118,16 +130,15 @@ async def connect_controller(self, controller_name=None, specified_facades=None)

proxy = proxy_from_config(controller.get('proxy-config', None))

await self.connect(
endpoint=endpoints,
uuid=None,
username=accounts.get('user'),
password=accounts.get('password'),
cacert=controller.get('ca-cert'),
bakery_client=self.bakery_client_for_controller(controller_name),
specified_facades=specified_facades,
proxy=proxy,
)
kwargs.update(endpoint=endpoints,
uuid=None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does None need to be provided in uuid, or can it be left out?

account=accounts,
cacert=controller.get('ca-cert'),
bakery_client=self.bakery_client_for_controller(controller_name),
specified_facades=specified_facades,
proxy=proxy,
)
await self.connect(**kwargs)
self.controller_name = controller_name
self.controller_uuid = controller["uuid"]

Expand Down Expand Up @@ -176,8 +187,7 @@ async def connect_model(self, model_name=None, **kwargs):
# JujuData.
kwargs.update(endpoint=endpoints,
uuid=model_uuid,
username=account.get('user'),
password=account.get('password'),
account=account,
cacert=controller.get('ca-cert'),
bakery_client=self.bakery_client_for_controller(controller_name),
proxy=proxy)
Expand Down