def test_uploader(self): ec2_ic_mock = MagicMock() uploader = SshKeyUploader(instance_id='i-1234567890', az='eu-west-1a', ssh_key=self.ssh_key, ec2_ic=ec2_ic_mock) uploader.upload() self.assertTrue(ec2_ic_mock.send_ssh_public_key.called)
def test_uploader(ec2_ic_mock, ssh_key, instance_id): uploader = SshKeyUploader(instance_id=instance_id, az="eu-west-1a", ssh_key=ssh_key, ec2_ic=ec2_ic_mock) uploader.upload() assert ec2_ic_mock.send_ssh_public_key.called
def test_uploader_exception(self): ec2_ic_mock = MagicMock() ec2_ic_mock.configure_mock(**{'send_ssh_public_key.return_value': {'Success': False, 'RequestId': '12345'}}) uploader = SshKeyUploader(instance_id='i-1234567890', az='eu-west-1a', ssh_key=self.ssh_key, ec2_ic=ec2_ic_mock) with self.assertRaises((ValueError)): uploader.upload()
def test_uploader_exception(ec2_ic_mock, ssh_key, instance_id): ec2_ic_mock.configure_mock( **{ "send_ssh_public_key.return_value": { "Success": False, "RequestId": "12345" } }) with pytest.raises(ValueError): uploader = SshKeyUploader( instance_id=instance_id, az="eu-west-1a", ssh_key=ssh_key, ec2_ic=ec2_ic_mock, ) uploader.upload()
def ssh( config, instance_name, user=DEFAULT_OS_USER, port=DEFAULT_SSH_PORT, key_type=DEFAULT_KEY_ALGORITHM, key_size=DEFAULT_KEY_SIZE, profile_name=AWS_DEFAULT_PROFILE, region_name=AWS_DEFAULT_REGION, command=None, forwarding=None, ): instance, profile, region = fetch_instance_details_from_config( config, instance_name, profile_name, region_name) ssm = get_aws_client("ssm", region_name=region, profile_name=profile) ec2 = get_aws_resource("ec2", region_name=region, profile_name=profile) ec2_ic = get_aws_client("ec2-instance-connect", region_name=region, profile_name=profile) instance_id = query_instance(name=instance, ec2=ec2) if instance_id is None: raise ValueError( "No instance could be found for name: {}".format(instance)) az = get_instance_details(instance_id=instance_id, ec2=ec2)["availability_zone"] logger.info( "Opening SSH session on instance %s (%s) via profile %s", instance_id, region, profile, ) with SshKey(key_type=key_type, key_size=key_size) as ssh_key: with SshKeyUploader(instance_id=instance_id, az=az, user=user, ssh_key=ssh_key, ec2_ic=ec2_ic): with SshSession( instance_id, region_name=region, profile_name=profile, ssm=ssm, port=port, user=user, command=command, forwarding=forwarding, ) as ssh_session: ssh_session.open()
def ssh_proxy(config, instance_name, user=DEFAULT_OS_USER, port=DEFAULT_SSH_PORT, key_type=DEFAULT_KEY_ALGORITHM, key_size=DEFAULT_KEY_SIZE, profile_name=AWS_DEFAULT_PROFILE, region_name=AWS_DEFAULT_REGION): instance, profile, region = fetch_instance_details_from_config(config, instance_name, profile_name, region_name) ssm = get_aws_client('ssm', region_name=region, profile_name=profile) ec2 = get_aws_resource('ec2', region_name=region, profile_name=profile) ec2_ic = get_aws_client('ec2-instance-connect', region_name=region, profile_name=profile) instance_id = query_instance(name=instance, ec2=ec2) if instance_id is None: raise ValueError('No instance could be found for name: {}'.format(instance)) az = get_instance_details(instance_id=instance_id, ec2=ec2)['availability_zone'] logger.info('Opening SSH proxy session on instance %s (%s) via profile %s', instance_id, region, profile) with SshKey(key_type=key_type, key_size=key_size) as ssh_key: with SshKeyUploader(instance_id=instance_id, az=az, user=user, ssh_key=ssh_key, ec2_ic=ec2_ic): with SshProxySession(instance_id, region_name=region, profile_name=profile, ssm=ssm, port=port, user=user) as ssh_proxy_session: ssh_proxy_session.open()
def test_uploader_as_context_manager(ec2_ic_mock, ssh_key, instance_id): with SshKeyUploader(instance_id=instance_id, az="eu-west-1a", ssh_key=ssh_key, ec2_ic=ec2_ic_mock): assert ec2_ic_mock.send_ssh_public_key.called
def test_uploader_as_context_manager(self): ec2_ic_mock = MagicMock() with SshKeyUploader(instance_id='i-1234567890', az='eu-west-1a', ssh_key=self.ssh_key, ec2_ic=ec2_ic_mock): self.assertTrue(ec2_ic_mock.send_ssh_public_key.called)