12 from http
.client
import HTTPSConnection
18 from juju
.errors
import JujuError
, JujuAPIError
, JujuConnectionError
19 from juju
.utils
import IdQueue
21 log
= logging
.getLogger("websocket")
28 # Connect to an arbitrary api server
29 client = await Connection.connect(
30 api_endpoint, model_uuid, username, password, cacert)
32 # Connect using a controller/model name
33 client = await Connection.connect_model('local.local:default')
35 # Connect to the currently active model
36 client = await Connection.connect_current()
38 Note: Any connection method or constructor can accept an optional `loop`
39 argument to override the default event loop from `asyncio.get_event_loop`.
42 self
, endpoint
, uuid
, username
, password
, cacert
=None,
43 macaroons
=None, loop
=None):
44 self
.endpoint
= endpoint
46 self
.username
= username
47 self
.password
= password
48 self
.macaroons
= macaroons
50 self
.loop
= loop
or asyncio
.get_event_loop()
52 self
.__request
_id
__ = 0
56 self
.messages
= IdQueue(loop
=self
.loop
)
64 def _get_ssl(self
, cert
=None):
65 return ssl
.create_default_context(
66 purpose
=ssl
.Purpose
.CLIENT_AUTH
, cadata
=cert
)
70 url
= "wss://{}/model/{}/api".format(self
.endpoint
, self
.uuid
)
72 url
= "wss://{}/api".format(self
.endpoint
)
75 kw
['ssl'] = self
._get
_ssl
(self
.cacert
)
76 kw
['loop'] = self
.loop
78 self
.ws
= await websockets
.connect(url
, **kw
)
79 self
.loop
.create_task(self
.receiver())
80 log
.info("Driver connected to juju %s", url
)
83 async def close(self
):
86 async def recv(self
, request_id
):
88 raise websockets
.exceptions
.ConnectionClosed(0, 'websocket closed')
89 return await self
.messages
.get(request_id
)
91 async def receiver(self
):
94 result
= await self
.ws
.recv()
95 if result
is not None:
96 result
= json
.loads(result
)
97 await self
.messages
.put(result
['request-id'], result
)
98 except Exception as e
:
99 await self
.messages
.put_all(e
)
100 if isinstance(e
, websockets
.ConnectionClosed
):
101 # ConnectionClosed is not really exceptional for us,
102 # but it may be for any pending message listeners
106 async def rpc(self
, msg
, encoder
=None):
107 self
.__request
_id
__ += 1
108 msg
['request-id'] = self
.__request
_id
__
109 if'params' not in msg
:
111 if "version" not in msg
:
112 msg
['version'] = self
.facades
[msg
['type']]
113 outgoing
= json
.dumps(msg
, indent
=2, cls
=encoder
)
114 await self
.ws
.send(outgoing
)
115 result
= await self
.recv(msg
['request-id'])
120 if 'error' in result
:
122 raise JujuAPIError(result
)
124 if 'response' not in result
:
125 # This may never happen
128 if 'results' in result
['response']:
129 # Check for errors in a result list.
131 for res
in result
['response']['results']:
132 if res
.get('error', {}).get('message'):
133 errors
.append(res
['error']['message'])
135 raise JujuError(errors
)
137 elif result
['response'].get('error', {}).get('message'):
138 raise JujuError(result
['response']['error']['message'])
142 def http_headers(self
):
143 """Return dictionary of http headers necessary for making an http
144 connection to the endpoint of this Connection.
146 :return: Dictionary of headers
149 if not self
.username
:
152 creds
= u
'{}:{}'.format(
153 tag
.user(self
.username
),
156 token
= base64
.b64encode(creds
.encode())
158 'Authorization': 'Basic {}'.format(token
.decode())
161 def https_connection(self
):
162 """Return an https connection to this Connection's endpoint.
164 Returns a 3-tuple containing::
166 1. The :class:`HTTPSConnection` instance
167 2. Dictionary of auth headers to be used with the connection
168 3. The root url path (str) to be used for requests.
171 endpoint
= self
.endpoint
172 host
, remainder
= endpoint
.split(':', 1)
175 port
, _
= remainder
.split('/', 1)
177 conn
= HTTPSConnection(
179 context
=self
._get
_ssl
(self
.cacert
),
183 "/model/{}".format(self
.uuid
)
186 return conn
, self
.http_headers(), path
188 async def clone(self
):
189 """Return a new Connection, connected to the same websocket endpoint
193 return await Connection
.connect(
203 async def controller(self
):
204 """Return a Connection to the controller at self.endpoint
207 return await Connection
.connect(
219 cls
, endpoint
, uuid
, username
, password
, cacert
=None,
220 macaroons
=None, loop
=None):
221 """Connect to the websocket.
223 If uuid is None, the connection will be to the controller. Otherwise it
224 will be to the model.
227 client
= cls(endpoint
, uuid
, username
, password
, cacert
, macaroons
,
231 redirect_info
= await client
.redirect_info()
232 if not redirect_info
:
233 await client
.login(username
, password
, macaroons
)
238 s
for servers
in redirect_info
['servers']
239 for s
in servers
if s
["scope"] == 'public'
241 for server
in servers
:
243 "{value}:{port}".format(**server
), uuid
, username
,
244 password
, redirect_info
['ca-cert'], macaroons
)
247 result
= await client
.login(username
, password
, macaroons
)
248 if 'discharge-required-error' in result
:
251 except Exception as e
:
256 "Couldn't authenticate to %s", endpoint
)
259 async def connect_current(cls
, loop
=None):
260 """Connect to the currently active model.
263 jujudata
= JujuData()
264 controller_name
= jujudata
.current_controller()
265 model_name
= jujudata
.current_model()
267 return await cls
.connect_model(
268 '{}:{}'.format(controller_name
, model_name
), loop
)
271 async def connect_current_controller(cls
, loop
=None):
272 """Connect to the currently active controller.
275 jujudata
= JujuData()
276 controller_name
= jujudata
.current_controller()
277 if not controller_name
:
278 raise JujuConnectionError('No current controller')
280 return await cls
.connect_controller(controller_name
, loop
)
283 async def connect_controller(cls
, controller_name
, loop
=None):
284 """Connect to a controller by name.
287 jujudata
= JujuData()
288 controller
= jujudata
.controllers()[controller_name
]
289 endpoint
= controller
['api-endpoints'][0]
290 cacert
= controller
.get('ca-cert')
291 accounts
= jujudata
.accounts()[controller_name
]
292 username
= accounts
['user']
293 password
= accounts
.get('password')
294 macaroons
= get_macaroons() if not password
else None
296 return await cls
.connect(
297 endpoint
, None, username
, password
, cacert
, macaroons
, loop
)
300 async def connect_model(cls
, model
, loop
=None):
301 """Connect to a model by name.
303 :param str model: [<controller>:]<model>
306 jujudata
= JujuData()
309 # explicit controller given
310 controller_name
, model_name
= model
.split(':')
312 # use the current controller if one isn't explicitly given
313 controller_name
= jujudata
.current_controller()
316 accounts
= jujudata
.accounts()[controller_name
]
317 username
= accounts
['user']
318 # model name must include a user prefix, so add it if it doesn't
319 if '/' not in model_name
:
320 model_name
= '{}/{}'.format(username
, model_name
)
322 controller
= jujudata
.controllers()[controller_name
]
323 endpoint
= controller
['api-endpoints'][0]
324 cacert
= controller
.get('ca-cert')
325 password
= accounts
.get('password')
326 models
= jujudata
.models()[controller_name
]
327 model_uuid
= models
['models'][model_name
]['uuid']
328 macaroons
= get_macaroons() if not password
else None
330 return await cls
.connect(
331 endpoint
, model_uuid
, username
, password
, cacert
, macaroons
, loop
)
333 def build_facades(self
, info
):
336 self
.facades
[facade
['name']] = facade
['versions'][-1]
338 async def login(self
, username
, password
, macaroons
=None):
343 if username
and not username
.startswith('user-'):
344 username
= 'user-{}'.format(username
)
346 result
= await self
.rpc({
351 "auth-tag": username
,
352 "credentials": password
,
353 "nonce": "".join(random
.sample(string
.printable
, 12)),
354 "macaroons": macaroons
or []
356 response
= result
['response']
357 self
.build_facades(response
.get('facades', {}))
358 self
.info
= response
.copy()
361 async def redirect_info(self
):
363 result
= await self
.rpc({
365 "request": "RedirectInfo",
368 except JujuAPIError
as e
:
369 if e
.message
== 'not redirected':
372 return result
['response']
377 self
.path
= os
.environ
.get('JUJU_DATA') or '~/.local/share/juju'
378 self
.path
= os
.path
.abspath(os
.path
.expanduser(self
.path
))
380 def current_controller(self
):
381 cmd
= shlex
.split('juju list-controllers --format yaml')
382 output
= subprocess
.check_output(cmd
)
383 output
= yaml
.safe_load(output
)
384 return output
.get('current-controller', '')
386 def current_model(self
, controller_name
=None):
387 if not controller_name
:
388 controller_name
= self
.current_controller()
389 models
= self
.models()[controller_name
]
390 if 'current-model' not in models
:
391 raise JujuError('No current model')
392 return models
['current-model']
394 def controllers(self
):
395 return self
._load
_yaml
('controllers.yaml', 'controllers')
398 return self
._load
_yaml
('models.yaml', 'controllers')
401 return self
._load
_yaml
('accounts.yaml', 'controllers')
403 def _load_yaml(self
, filename
, key
):
404 filepath
= os
.path
.join(self
.path
, filename
)
405 with io
.open(filepath
, 'rt') as f
:
406 return yaml
.safe_load(f
)[key
]
410 """Decode and return macaroons from default ~/.go-cookies
414 cookie_file
= os
.path
.expanduser('~/.go-cookies')
415 with
open(cookie_file
, 'r') as f
:
416 cookies
= json
.load(f
)
417 except (OSError, ValueError):
418 log
.warn("Couldn't load macaroons from %s", cookie_file
)
422 c
['Value'] for c
in cookies
423 if c
['Name'].startswith('macaroon-') and c
['Value']
427 json
.loads(base64
.b64decode(value
).decode('utf-8'))
428 for value
in base64_macaroons