Skip to content

Commit 2154a9e

Browse files
committed
User accounts.yaml as a backup to user provided credentials
For both model and controller connections, accounts.yaml is required to have both username and password for connection to be established. This is not always true, for example, juju change-user-password actually removes the password from the accounts.yaml file.
1 parent 7b7579e commit 2154a9e

1 file changed

Lines changed: 26 additions & 20 deletions

File tree

juju/client/connector.py

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ async def connect(self, **kwargs):
8585
# connected to.
8686
if self._connection:
8787
await self._connection.close()
88+
89+
account = kwargs.pop('account', {})
90+
# Prioritize the username and password that user provided
91+
# If not enough, try to patch it with info from accounts.yaml
92+
if 'username' not in kwargs and account.get('user'):
93+
kwargs.update(username=account.get('user'))
94+
if 'password' not in kwargs and account.get('password'):
95+
kwargs.update(password=account.get('password'))
96+
8897
self._connection = await Connection.connect(**kwargs)
8998

9099
# Check if we support the target controller
@@ -117,7 +126,7 @@ async def disconnect(self, entity):
117126
await self._log_connection.close()
118127
self._log_connection = None
119128

120-
async def connect_controller(self, controller_name=None, specified_facades=None):
129+
async def connect_controller(self, controller_name=None, specified_facades=None, **kwargs):
121130
"""Connect to a controller by name. If the name is empty, it
122131
connect to the current controller.
123132
"""
@@ -130,16 +139,16 @@ async def connect_controller(self, controller_name=None, specified_facades=None)
130139

131140
proxy = proxy_from_config(controller.get("proxy-config", None))
132141

133-
await self.connect(
134-
endpoint=endpoints,
135-
uuid=None,
136-
username=accounts.get("user"),
137-
password=accounts.get("password"),
138-
cacert=controller.get("ca-cert"),
139-
bakery_client=self.bakery_client_for_controller(controller_name),
140-
specified_facades=specified_facades,
141-
proxy=proxy,
142-
)
142+
kwargs.update(endpoint=endpoints,
143+
uuid=None,
144+
account=accounts,
145+
cacert=controller.get('ca-cert'),
146+
bakery_client=self.bakery_client_for_controller(controller_name),
147+
specified_facades=specified_facades,
148+
proxy=proxy,
149+
)
150+
await self.connect(**kwargs)
151+
self.controller_name = controller_name
143152
self.controller_uuid = controller["uuid"]
144153

145154
async def connect_model(self, _model_name=None, **kwargs):
@@ -184,15 +193,12 @@ async def connect_model(self, _model_name=None, **kwargs):
184193

185194
# TODO remove the need for base.CleanModel to subclass
186195
# JujuData.
187-
kwargs.update(
188-
endpoint=endpoints,
189-
uuid=model_uuid,
190-
username=account.get("user"),
191-
password=account.get("password"),
192-
cacert=controller.get("ca-cert"),
193-
bakery_client=self.bakery_client_for_controller(controller_name),
194-
proxy=proxy,
195-
)
196+
kwargs.update(endpoint=endpoints,
197+
uuid=model_uuid,
198+
account=account,
199+
cacert=controller.get('ca-cert'),
200+
bakery_client=self.bakery_client_for_controller(controller_name),
201+
proxy=proxy)
196202
await self.connect(**kwargs)
197203
# TODO this might be a good spot to trigger refreshing the
198204
# local cache (the connection to the model might help)

0 commit comments

Comments
 (0)