Skip to content

Commit 5e19882

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 4172626 commit 5e19882

1 file changed

Lines changed: 20 additions & 13 deletions

File tree

juju/client/connector.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ async def connect(self, **kwargs):
8080
else:
8181
if self._connection:
8282
await self._connection.close()
83+
84+
account = kwargs.pop('account', {})
85+
# Prioritize the username and password that user provided
86+
# If not enough, try to patch it with info from accounts.yaml
87+
if 'username' not in kwargs and account.get('user'):
88+
kwargs.update(username=account.get('user'))
89+
if 'password' not in kwargs and account.get('password'):
90+
kwargs.update(password=account.get('password'))
91+
8392
self._connection = await Connection.connect(**kwargs)
8493

8594
if not self.controller_name:
@@ -103,7 +112,7 @@ async def disconnect(self):
103112
await self._log_connection.close()
104113
self._log_connection = None
105114

106-
async def connect_controller(self, controller_name=None, specified_facades=None):
115+
async def connect_controller(self, controller_name=None, specified_facades=None, **kwargs):
107116
"""Connect to a controller by name. If the name is empty, it
108117
connect to the current controller.
109118
"""
@@ -118,16 +127,15 @@ async def connect_controller(self, controller_name=None, specified_facades=None)
118127

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

121-
await self.connect(
122-
endpoint=endpoints,
123-
uuid=None,
124-
username=accounts.get('user'),
125-
password=accounts.get('password'),
126-
cacert=controller.get('ca-cert'),
127-
bakery_client=self.bakery_client_for_controller(controller_name),
128-
specified_facades=specified_facades,
129-
proxy=proxy,
130-
)
130+
kwargs.update(endpoint=endpoints,
131+
uuid=None,
132+
account=accounts,
133+
cacert=controller.get('ca-cert'),
134+
bakery_client=self.bakery_client_for_controller(controller_name),
135+
specified_facades=specified_facades,
136+
proxy=proxy,
137+
)
138+
await self.connect(**kwargs)
131139
self.controller_name = controller_name
132140
self.controller_uuid = controller["uuid"]
133141

@@ -176,8 +184,7 @@ async def connect_model(self, model_name=None, **kwargs):
176184
# JujuData.
177185
kwargs.update(endpoint=endpoints,
178186
uuid=model_uuid,
179-
username=account.get('user'),
180-
password=account.get('password'),
187+
account=account,
181188
cacert=controller.get('ca-cert'),
182189
bakery_client=self.bakery_client_for_controller(controller_name),
183190
proxy=proxy)

0 commit comments

Comments
 (0)