async def _reset_claimer(self): async with backend_invited_cmds_factory(addr=self.invitation_addr) as cmds: claimer_initial_ctx = await claimer_retrieve_info(cmds) async with trio.open_nursery() as nursery: nursery.start_soon(claimer_initial_ctx.do_wait_peer) yield nursery.cancel_scope.cancel()
async def _run_claimer(): nonlocal greeter_sas nonlocal claimer_sas async with backend_invited_cmds_factory( addr=gdi_w.invite_addr) as cmds: await start_claimer.wait() initial_ctx = await claimer_retrieve_info(cmds) in_progress_ctx = await initial_ctx.do_wait_peer() greeter_sas = in_progress_ctx.greeter_sas greeter_sas_available.set() await start_claimer_trust.wait() in_progress_ctx = await in_progress_ctx.do_signify_trust() claimer_sas = in_progress_ctx.claimer_sas claimer_sas_available.set() in_progress_ctx = await in_progress_ctx.do_wait_peer_trust() await start_claimer_claim_user.wait() await in_progress_ctx.do_claim_device( requested_device_label=requested_device_label) claimer_done.set()
async def _claim_invitation(config, addr, password): async with backend_invited_cmds_factory( addr=addr, keepalive=config.backend_connection_keepalive) as cmds: try: async with spinner("Retrieving invitation info"): initial_ctx = await claimer_retrieve_info(cmds) except BackendConnectionRefused: raise RuntimeError("Invitation not found") if initial_ctx.greeter_human_handle: display_greeter = click.style(str( initial_ctx.greeter_human_handle), fg="yellow") else: display_greeter = click.style(initial_ctx.greeter_user_id, fg="yellow") click.echo(f"Invitation greeter: {display_greeter}") while True: try: if isinstance(initial_ctx, DeviceClaimInitialCtx): new_device = await _do_claim_device(initial_ctx) else: assert isinstance(initial_ctx, UserClaimInitialCtx) new_device = await _do_claim_user(initial_ctx) if new_device: break except InviteError as exc: click.secho(str(exc), fg="red") click.secho("Restarting the invitation process", fg="red") device_display = click.style(new_device.slughash, fg="yellow") with operation(f"Saving device {device_display}"): save_device_with_password(config.config_dir, new_device, password)
async def test_handshake_unknown_organization(running_backend, coolorg): invitation_addr = BackendInvitationAddr.build( backend_addr=running_backend.addr, organization_id=coolorg.organization_id, invitation_type=InvitationType.DEVICE, token=uuid4(), ) with pytest.raises(BackendConnectionRefused) as exc: async with backend_invited_cmds_factory(invitation_addr) as cmds: await cmds.ping() assert str(exc.value) == "Invalid handshake information"
async def run(self): await self.bootstrap() async with trio.open_nursery() as self.nursery: async with backend_invited_cmds_factory(addr=self.invitation_addr) as self.cmds: next_step = "step_1_start_greet" while True: current_step = next_step next_step = await getattr(self, current_step)() self.steps_done.append(current_step) if next_step is None: break if self.claimer_claim_task: await self.claimer_claim_task.cancel_and_join()
async def test_handshake_organization_expired(running_backend, expiredorg, expiredorgalice): invitation = await running_backend.backend.invite.new_for_device( organization_id=expiredorgalice.organization_id, greeter_user_id=expiredorgalice.user_id) invitation_addr = BackendInvitationAddr.build( backend_addr=running_backend.addr, organization_id=expiredorgalice.organization_id, invitation_type=InvitationType.DEVICE, token=invitation.token, ) with pytest.raises(BackendConnectionRefused) as exc: async with backend_invited_cmds_factory(invitation_addr) as cmds: await cmds.ping() assert str(exc.value) == "Trial organization has expired"
async def run(self, addr, config): try: async with backend_invited_cmds_factory( addr=addr, keepalive=config.backend_connection_keepalive) as cmds: r = await self.main_oob_recv.receive() assert r == self.Step.RetrieveInfo try: initial_ctx = await claimer_retrieve_info(cmds) await self.job_oob_send.send((True, None)) except Exception as exc: await self.job_oob_send.send((False, exc)) r = await self.main_oob_recv.receive() assert r == self.Step.WaitPeer try: in_progress_ctx = await initial_ctx.do_wait_peer() await self.job_oob_send.send((True, None)) except Exception as exc: await self.job_oob_send.send((False, exc)) r = await self.main_oob_recv.receive() assert r == self.Step.GetGreeterSas try: choices = in_progress_ctx.generate_greeter_sas_choices( size=4) await self.job_oob_send.send( (True, None, in_progress_ctx.greeter_sas, choices)) except Exception as exc: await self.job_oob_send.send((False, exc, None, None)) r = await self.main_oob_recv.receive() assert r == self.Step.SignifyTrust try: in_progress_ctx = await in_progress_ctx.do_signify_trust() await self.job_oob_send.send((True, None)) except Exception as exc: await self.job_oob_send.send((False, exc)) r = await self.main_oob_recv.receive() assert r == self.Step.GetClaimerSas await self.job_oob_send.send(in_progress_ctx.claimer_sas) r = await self.main_oob_recv.receive() assert r == self.Step.WaitPeerTrust try: in_progress_ctx = await in_progress_ctx.do_wait_peer_trust( ) await self.job_oob_send.send((True, None)) except Exception as exc: await self.job_oob_send.send((False, exc)) r = await self.main_oob_recv.receive() assert r == self.Step.ClaimDevice try: device_label = await self.main_oob_recv.receive() new_device = await in_progress_ctx.do_claim_device( requested_device_label=device_label) await self.job_oob_send.send((True, None, new_device)) except Exception as exc: await self.job_oob_send.send((False, exc, None)) except BackendNotAvailable as exc: raise JobResultError(status="backend-not-available", origin=exc) except BackendConnectionRefused as exc: raise JobResultError(status="invitation-not-found", origin=exc)
async def test_invited_cmds_has_right_methods(running_backend, coolorg): async with backend_invited_cmds_factory(coolorg.addr) as cmds: for method_name in INVITED_CMDS: assert hasattr(cmds, method_name) for method_name in ALL_CMDS - INVITED_CMDS: assert not hasattr(cmds, method_name)
async def test_ping(running_backend, invitation_addr): async with backend_invited_cmds_factory(invitation_addr) as cmds: rep = await cmds.ping("Hello World !") assert rep == {"status": "ok", "pong": "Hello World !"}
async def test_backend_closed_cmds(running_backend, invitation_addr): async with backend_invited_cmds_factory(invitation_addr) as cmds: pass with pytest.raises(trio.ClosedResourceError): await cmds.ping()
async def test_backend_switch_offline(running_backend, invitation_addr): async with backend_invited_cmds_factory(invitation_addr) as cmds: await cmds.ping() with running_backend.offline(): with pytest.raises(BackendNotAvailable): await cmds.ping()
def _cmds_factory(keepalive): return backend_invited_cmds_factory(invitation_addr, keepalive=keepalive)