Ejemplo n.º 1
0
 def _FakeSSHEnvironment(self, use_putty=False):
     env = ssh.Environment(
         ssh.Suite.PUTTY if use_putty else ssh.Suite.OPENSSH, '')
     env.ssh = 'ssh'
     env.ssh_term = 'ssh'
     env.scp = 'scp'
     env.keygen = 'ssh-keygen'
     return env
Ejemplo n.º 2
0
    def SetUp(self):
        self.client = mock.Client(apis.GetClientClass('cloudshell',
                                                      'v1alpha1'))
        self.client.Mock()
        self.addCleanup(self.client.Unmock)

        self.operations_client = mock.Client(
            apis.GetClientClass('cloudshell', 'v1'))
        self.operations_client.Mock()
        self.addCleanup(self.operations_client.Unmock)

        self.messages = apis.GetMessagesModule('cloudshell', 'v1alpha1')
        self.operations_messages = apis.GetMessagesModule('cloudshell', 'v1')

        # TODO(b/72457554): factor out the following to share with compute ssh tests
        self.env = ssh.Environment(ssh.Suite.OPENSSH)
        self.env.ssh = 'ssh'
        self.env.ssh_term = 'ssh'
        self.env.scp = 'scp'
        self.env.keygen = 'ssh-keygen'
        self.StartObjectPatch(ssh.Environment,
                              'Current',
                              return_value=self.env)
        self.home_dir = os.path.realpath(
            self.CreateTempDir(name=os.path.join('home', 'me')))
        self.ssh_dir = os.path.realpath(
            self.CreateTempDir(name=os.path.join(self.home_dir, '.ssh')))
        self.private_key_file = os.path.join(self.ssh_dir, 'id_rsa')
        self.pubkey = ssh.Keys.PublicKey(
            'ssh-rsa',
            'AAAAB3NzaC1yc2EAAAADAQABAAABAQCwFyCpWwERm3r1/snlgt9907rd5FcV2l'
            'vzdUxt04FCr+uNNusfx/9LUmRPVjHyIXZAcOeqRlnM8kKo765msDdyAn0n36M4LjmXBqnj'
            'edI+4OLhYPCDxGaHfnlOLIY3HCup7JSn1/u7iBddE0KnMQ13oBi010BK5iwNRe1Mr8m1ar'
            '06BK9n3UN/0DrbydTGbqcaOfYzKuMK5aeCEgvxu/TAOHsAG3fhJ0eR5orfRRUdIngP8kjZ'
            'rSrS12IRTEptaiR+NXd4/GVDcm1VvLcX8kyugVy3Md1i7kHV883jz9diMbhC/fVxERJK/7'
            'PfiEb/cYLCqWE6pTAFl+G6M4NvO3Bf', 'me@my-computer')
        self.keys = ssh.Keys(self.private_key_file)
        self.StartObjectPatch(ssh.Keys, 'EnsureKeysExist', autospec=True)
        self.StartObjectPatch(ssh.Keys, 'FromFilename', return_value=self.keys)
        self.StartObjectPatch(ssh.Keys,
                              'GetPublicKey',
                              autospec=True,
                              return_value=self.pubkey)
Ejemplo n.º 3
0
    def SetUp(self):
        # A typical Unix environment
        self.env = ssh.Environment(ssh.Suite.OPENSSH)
        self.env.ssh = 'ssh'
        self.env.ssh_term = 'ssh'
        self.env.scp = 'scp'
        self.env.keygen = 'ssh-keygen'
        self.env_mock = self.StartObjectPatch(ssh.Environment,
                                              'Current',
                                              return_value=self.env)

        self.home_dir = os.path.realpath(
            self.CreateTempDir(name=os.path.join('home', 'me')))
        self.ssh_dir = os.path.realpath(
            self.CreateTempDir(name=os.path.join(self.home_dir, '.ssh')))
        self.private_key_file = os.path.join(self.ssh_dir, 'id_rsa')
        self.public_key_file = self.private_key_file + '.pub'
        self.known_hosts_file = os.path.join(self.ssh_dir, 'known_hosts')
        self.StartObjectPatch(ssh.KnownHosts, 'DEFAULT_PATH',
                              self.known_hosts_file)
        self.known_hosts_add = self.StartObjectPatch(ssh.KnownHosts,
                                                     'Add',
                                                     autospec=True)
        self.known_hosts_addmultiple = self.StartObjectPatch(ssh.KnownHosts,
                                                             'AddMultiple',
                                                             autospec=True)
        self.known_hosts_write = self.StartObjectPatch(ssh.KnownHosts,
                                                       'Write',
                                                       autospec=True)

        # Common test vars
        self.remote = ssh.Remote('23.251.133.75', user='******')
        self.options = {
            'UserKnownHostsFile': self.known_hosts_file,
            'IdentitiesOnly': 'yes',
            'CheckHostIP': 'no',
            'StrictHostKeyChecking': 'no',
        }

        # Keys
        self.pubkey = ssh.Keys.PublicKey(
            'ssh-rsa',
            'AAAAB3NzaC1yc2EAAAADAQABAAABAQCwFyCpWwERm3r1/snlgt9907rd5FcV2l'
            'vzdUxt04FCr+uNNusfx/9LUmRPVjHyIXZAcOeqRlnM8kKo765msDdyAn0n36M4LjmXBqnj'
            'edI+4OLhYPCDxGaHfnlOLIY3HCup7JSn1/u7iBddE0KnMQ13oBi010BK5iwNRe1Mr8m1ar'
            '06BK9n3UN/0DrbydTGbqcaOfYzKuMK5aeCEgvxu/TAOHsAG3fhJ0eR5orfRRUdIngP8kjZ'
            'rSrS12IRTEptaiR+NXd4/GVDcm1VvLcX8kyugVy3Md1i7kHV883jz9diMbhC/fVxERJK/7'
            'PfiEb/cYLCqWE6pTAFl+G6M4NvO3Bf', 'me@my-computer')
        self.keys = ssh.Keys(self.private_key_file)

        self.StartObjectPatch(ssh.Keys, 'FromFilename', return_value=self.keys)
        self.get_public_key = self.StartObjectPatch(ssh.Keys,
                                                    'GetPublicKey',
                                                    autospec=True,
                                                    return_value=self.pubkey)
        self.ensure_keys = self.StartObjectPatch(ssh.Keys,
                                                 'EnsureKeysExist',
                                                 autospec=True)

        # User names
        self.StartObjectPatch(ssh,
                              'GetDefaultSshUsername',
                              autospec=True,
                              return_value='me')

        # Commands
        self.keygen_run = self.StartObjectPatch(ssh.KeygenCommand, 'Run')

        self.ssh_init = self.StartObjectPatch(ssh.SSHCommand,
                                              '__init__',
                                              return_value=None,
                                              autospec=True)
        self.ssh_run = self.StartObjectPatch(ssh.SSHCommand,
                                             'Run',
                                             autospec=True,
                                             return_value=0)

        self.scp_init = self.StartObjectPatch(ssh.SCPCommand,
                                              '__init__',
                                              return_value=None,
                                              autospec=True)
        self.scp_run = self.StartObjectPatch(ssh.SCPCommand,
                                             'Run',
                                             autospec=True,
                                             return_value=0)
        self.scp_build = self.StartObjectPatch(ssh.SCPCommand,
                                               'Build',
                                               autospec=True,
                                               return_value='')

        self.poller_init = self.StartObjectPatch(ssh.SSHPoller,
                                                 '__init__',
                                                 return_value=None,
                                                 autospec=True)
        self.poller_poll = self.StartObjectPatch(ssh.SSHPoller,
                                                 'Poll',
                                                 autospec=True,
                                                 return_value=0)

        self.project_resource = self.v1_messages.Project(
            commonInstanceMetadata=self.v1_messages.Metadata(items=[
                self.v1_messages.Metadata.ItemsValueListEntry(key='a',
                                                              value='b'),
                self.v1_messages.Metadata.ItemsValueListEntry(
                    key='sshKeys',
                    value='me:{0}\n'.format(self.public_key_material)),
            ]),
            name='my-project',
        )

        self.project_resource_without_metadata = self.v1_messages.Project(
            name='my-project', )

        self.project_resource_with_oslogin_enabled = self.v1_messages.Project(
            name='my-project',
            commonInstanceMetadata=self.v1_messages.Metadata(items=[
                self.v1_messages.Metadata.ItemsValueListEntry(
                    key='enable-oslogin', value='true'),
            ]),
        )

        self.project_resource_with_guest_attr_enabled = self.v1_messages.Project(
            commonInstanceMetadata=self.v1_messages.Metadata(items=[
                self.v1_messages.Metadata.ItemsValueListEntry(key='a',
                                                              value='b'),
                self.v1_messages.Metadata.ItemsValueListEntry(
                    key='enable-guest-attributes', value='true'),
                self.v1_messages.Metadata.ItemsValueListEntry(
                    key='sshKeys',
                    value='me:{0}\n'.format(self.public_key_material)),
            ]),
            name='my-project',
        )

        self.make_requests.side_effect = iter([
            [self.project_resource],
        ])
Ejemplo n.º 4
0
    def SetupSSHMocks(self):
        make_requests_patcher = base_mock.patch(
            'googlecloudsdk.api_lib.compute.request_helper.MakeRequests',
            autospec=True)
        self.addCleanup(make_requests_patcher.stop)
        self.make_requests = make_requests_patcher.start()

        self.env = ssh.Environment(ssh.Suite.OPENSSH)
        self.env.ssh = 'ssh'
        self.env.ssh_term = 'ssh'

        self.home_dir = os.path.realpath(
            self.CreateTempDir(name=os.path.join('home', 'me')))
        self.ssh_dir = os.path.realpath(
            self.CreateTempDir(name=os.path.join(self.home_dir, '.ssh')))
        # self.private_key_file = os.path.join(self.ssh_dir, 'id_rsa')
        self.private_key_file = ssh.Keys.DEFAULT_KEY_FILE
        self.public_key_file = self.private_key_file + '.pub'
        self.known_hosts_file = os.path.join(self.ssh_dir, 'known_hosts')
        self.StartObjectPatch(ssh.KnownHosts, 'DEFAULT_PATH',
                              self.known_hosts_file)

        # Common test vars
        self.remote = ssh.Remote('randomIP', user='******')
        self.options = {
            'UserKnownHostsFile': self.known_hosts_file,
            'IdentitiesOnly': 'yes',
            'CheckHostIP': 'no',
            'StrictHostKeyChecking': 'no',
        }

        # Keys
        self.pubkey = ssh.Keys.PublicKey(
            'ssh-rsa',
            'AAAAB3NzaC1yc2EAAAADAQABAAABAQCwFyCpWwERm3r1/snlgt9907rd5FcV2l'
            'vzdUxt04FCr+uNNusfx/9LUmRPVjHyIXZAcOeqRlnM8kKo765msDdyAn0n36M4LjmXBqnj'
            'edI+4OLhYPCDxGaHfnlOLIY3HCup7JSn1/u7iBddE0KnMQ13oBi010BK5iwNRe1Mr8m1ar'
            '06BK9n3UN/0DrbydTGbqcaOfYzKuMK5aeCEgvxu/TAOHsAG3fhJ0eR5orfRRUdIngP8kjZ'
            'rSrS12IRTEptaiR+NXd4/GVDcm1VvLcX8kyugVy3Md1i7kHV883jz9diMbhC/fVxERJK/7'
            'PfiEb/cYLCqWE6pTAFl+G6M4NvO3Bf', 'me@my-computer')
        # self.keys = ssh.Keys.FromFilename(ssh.Keys.DEFAULT_KEY_FILE)
        self.keys = ssh.Keys.FromFilename(self.private_key_file)

        self.get_public_key = self.StartObjectPatch(ssh.Keys,
                                                    'GetPublicKey',
                                                    autospec=True,
                                                    return_value=self.pubkey)
        self.ensure_keys = self.StartObjectPatch(ssh.Keys,
                                                 'EnsureKeysExist',
                                                 autospec=True)

        self.ssh_init = self.StartObjectPatch(ssh.SSHCommand,
                                              '__init__',
                                              return_value=None,
                                              autospec=True)
        self.ssh_run = self.StartObjectPatch(ssh.SSHCommand,
                                             'Run',
                                             autospec=True,
                                             return_value=0)

        self.poller_init = self.StartObjectPatch(ssh.SSHPoller,
                                                 '__init__',
                                                 return_value=None,
                                                 autospec=True)
        self.poller_poll = self.StartObjectPatch(ssh.SSHPoller,
                                                 'Poll',
                                                 autospec=True,
                                                 return_value=0)

        self.project_resource = self.instances_messages.Project(
            commonInstanceMetadata=self.instances_messages.Metadata(items=[
                self.instances_messages.Metadata.ItemsValueListEntry(
                    key='a', value='b'),
                self.instances_messages.Metadata.ItemsValueListEntry(
                    key='sshKeys',
                    value='me:{0}\n'.format(self.public_key_material)),
            ]),
            name='my-project',
        )

        self.make_requests.side_effect = iter([
            [self.project_resource],
            [self.project_resource],
            [self.project_resource],
        ])