def before_each(self): self.paramiko_stub = create_paramiko_client_stub(command_stdout_rtn=[ 'total 132', 'drwxr-xr-x 2 root root 4096 Sep 2 13:36 bin' ]) self.ssh_credentials = create_key_creds() self.ssh_client = SSHClient(client=self.paramiko_stub, host='bam', port=21, credentials=self.ssh_credentials)
def on_get(self, req, resp, tenant_id, target_id): target = Target.get_target(tenant_id, target_id) if target: address = target.address # Nova if 'nova' in address.as_dict().keys(): nova_address = address.address_child auth = target.authentication try: if 'rackspace' in auth: cls = get_driver(Provider.RACKSPACE) cls(auth['rackspace']['username'], auth['rackspace']['api_key'], region=nova_address.region.lower()) resp.status = falcon.HTTP_200 else: raise Exception("No supported providers in target: {0}" .format(target.as_dict())) except Exception: resp.status = falcon.HTTP_404 # SSH else: ip = address.address_child ssh = target.authentication.get('ssh') creds = SSHKeyCredentials( username=ssh.get('username'), key_contents=ssh.get('private_key') ) client = SSHClient( host=ip.address, port=ip.port, credentials=creds ) try: client.connect() resp.status = falcon.HTTP_200 client.close() except SSHException: resp.status = falcon.HTTP_404 else: msg = 'Cannot find target: {target_id}'.format(target_id=target_id) resp.status = falcon.HTTP_404 resp.body = json.dumps({'description': msg})
class SSHClientVerification(Spec): def before_each(self): self.paramiko_stub = create_paramiko_client_stub( command_stdout_rtn=[ 'total 132', 'drwxr-xr-x 2 root root 4096 Sep 2 13:36 bin' ] ) self.ssh_credentials = create_key_creds() self.ssh_client = SSHClient(client=self.paramiko_stub, host='bam', port=21, credentials=self.ssh_credentials) def can_create_an_instance(self): expect(self.ssh_client).not_to.be_none() def unassigned_client_auto_creates_a_paramiko_client(self): inst = SSHClient(host='bam', port=21, credentials=self.ssh_credentials) expect(inst).not_to.be_none() expect(type(inst.client)).to.equal(paramiko.SSHClient) def sets_key_policy_on_client(self): self.ssh_client._set_key_policy() set_host_key_method = self.paramiko_stub.set_missing_host_key_policy expect(len(set_host_key_method.calls)).to.equal(1) def can_connect(self): self.ssh_client.connect() expect(len(self.paramiko_stub.connect.calls)).to.equal(1) def can_connect_with_args(self): self.ssh_client.connect( host='sample.host', port=80, credentials=self.ssh_credentials) expect(self.ssh_client.host).to.equal('sample.host') expect(self.ssh_client.port).to.equal(80) def can_close(self): self.ssh_client.close() expect(len(self.paramiko_stub.close.calls)).to.equal(1) def can_execute_a_command(self): result = self.ssh_client.execute('ls -l /') expect(len(result[0])).to.be_greater_than(1)
class SSHClientVerification(Spec): def before_each(self): self.paramiko_stub = create_paramiko_client_stub(command_stdout_rtn=[ 'total 132', 'drwxr-xr-x 2 root root 4096 Sep 2 13:36 bin' ]) self.ssh_credentials = create_key_creds() self.ssh_client = SSHClient(client=self.paramiko_stub, host='bam', port=21, credentials=self.ssh_credentials) def can_create_an_instance(self): expect(self.ssh_client).not_to.be_none() def unassigned_client_auto_creates_a_paramiko_client(self): inst = SSHClient(host='bam', port=21, credentials=self.ssh_credentials) expect(inst).not_to.be_none() expect(type(inst.client)).to.equal(paramiko.SSHClient) def sets_key_policy_on_client(self): self.ssh_client._set_key_policy() set_host_key_method = self.paramiko_stub.set_missing_host_key_policy expect(len(set_host_key_method.calls)).to.equal(1) def can_connect(self): self.ssh_client.connect() expect(len(self.paramiko_stub.connect.calls)).to.equal(1) def can_connect_with_args(self): self.ssh_client.connect(host='sample.host', port=80, credentials=self.ssh_credentials) expect(self.ssh_client.host).to.equal('sample.host') expect(self.ssh_client.port).to.equal(80) def can_close(self): self.ssh_client.close() expect(len(self.paramiko_stub.close.calls)).to.equal(1) def can_execute_a_command(self): result = self.ssh_client.execute('ls -l /') expect(len(result[0])).to.be_greater_than(1)
def before_each(self): self.paramiko_stub = create_paramiko_client_stub( command_stdout_rtn=[ 'total 132', 'drwxr-xr-x 2 root root 4096 Sep 2 13:36 bin' ] ) self.ssh_credentials = create_key_creds() self.ssh_client = SSHClient(client=self.paramiko_stub, host='bam', port=21, credentials=self.ssh_credentials)
def execute_action(self, job, action): cmd = action.parameters.get("command") for target_id in action.targets: target = Target.get_target(job.tenant_id, target_id) ip = target.address.address_child ssh = target.authentication.get("ssh") creds = SSHKeyCredentials(username=ssh.get("username"), key_contents=ssh.get("private_key")) client = SSHClient(host=ip.address, port=ip.port, credentials=creds) client.connect() LOG.info("Remote command plugin executing: %s", cmd) resp = client.execute(command=cmd) client.close() LOG.info("Remote command plugin execution response: %s", resp)
def on_get(self, req, resp, tenant_id, target_id): target = Target.get_target(tenant_id, target_id) if target: address = target.address # Nova if 'nova' in address.as_dict().keys(): nova_address = address.address_child auth = target.authentication try: if 'rackspace' in auth: cls = get_driver(Provider.RACKSPACE) cls(auth['rackspace']['username'], auth['rackspace']['api_key'], region=nova_address.region.lower()) resp.status = falcon.HTTP_200 else: raise Exception( "No supported providers in target: {0}".format( target.as_dict())) except Exception: resp.status = falcon.HTTP_404 # SSH else: ip = address.address_child ssh = target.authentication.get('ssh') creds = SSHKeyCredentials(username=ssh.get('username'), key_contents=ssh.get('private_key')) client = SSHClient(host=ip.address, port=ip.port, credentials=creds) try: client.connect() resp.status = falcon.HTTP_200 client.close() except SSHException: resp.status = falcon.HTTP_404 else: msg = 'Cannot find target: {target_id}'.format(target_id=target_id) resp.status = falcon.HTTP_404 resp.body = json.dumps({'description': msg})
def unassigned_client_auto_creates_a_paramiko_client(self): inst = SSHClient(host='bam', port=21, credentials=self.ssh_credentials) expect(inst).not_to.be_none() expect(type(inst.client)).to.equal(paramiko.SSHClient)