Exemple #1
0
def proxyserver_add():
  """Get the form for adding new proxy servers."""
  if flask.request.method == 'GET':
    return flask.render_template('proxy_server_form.html',
                                 proxy_server=None)

  server = models.ProxyServer(
      name=flask.request.form.get('name'),
      ip_address=flask.request.form.get('ip_address'))

  #TODO more robust validation here and in proxyserver_edit
  host_public_key_contents = flask.request.form.get('host_public_key')
  if host_public_key_contents is None:
    flask.abort(400)

  ssh_private_key_contents = flask.request.form.get('ssh_private_key')
  if ssh_private_key_contents is None:
    flask.abort(400)

  try:
    server.read_public_key_from_file_contents(host_public_key_contents)
  except:
    flask.abort(400, ufo.get_json_message('publicKeyReadError'))
  try:
    server.read_private_key_from_file_contents(ssh_private_key_contents)
  except:
    flask.abort(400, ufo.get_json_message('privateKeyReadError'))

  try:
    server.save()
  except custom_exceptions.UnableToSaveToDB as e:
    flask.abort(e.code, e.message)

  return flask.redirect(flask.url_for('proxyserver_list'))
    def _CreateAndSaveFakeProxyServer(self, i=0):
        """Create a fake proxy server, and save it into db."""
        proxy_server = models.ProxyServer(
            ip_address=FAKE_PROXY_SERVER_DATA[i]['ip_address'],
            name=FAKE_PROXY_SERVER_DATA[i]['name'],
            ssh_private_key=FAKE_PROXY_SERVER_DATA[i]['ssh_private_key'],
            ssh_private_key_type=FAKE_PROXY_SERVER_DATA[i]
            ['ssh_private_key_type'],
            host_public_key=FAKE_PROXY_SERVER_DATA[i]['host_public_key'],
            host_public_key_type=FAKE_PROXY_SERVER_DATA[i]
            ['host_public_key_type'])

        return proxy_server.save()
Exemple #3
0
    def testProxyServerToDict(self):
        """Whether the necessary fields match in a dictionary representation."""
        server = models.ProxyServer()
        server.ip_address = '127.0.0.0'
        server.name = 'foo test name'
        temp_key = RSA.generate(2048)
        server.read_public_key_from_file_contents(
            temp_key.publickey().exportKey('OpenSSH'))
        server.read_private_key_from_file_contents(temp_key.exportKey('PEM'))
        server_dict = server.to_dict()

        self.assertEqual(server_dict['id'], server.id)
        self.assertEqual(server_dict['ip_address'], server.ip_address)
        self.assertEqual(server_dict['name'], server.name)
        self.assertEqual(server_dict['host_public_key'],
                         server.get_public_key_as_authorization_file_string())
        self.assertIn('ssh_private_key', server_dict)
Exemple #4
0
  def testUserGetInviteCode(self):
    """Test the user get invite code returns a valid invite code."""
    fake_ip = '0.1.2.3'
    proxy_server = models.ProxyServer(ip_address=fake_ip)
    proxy_server.save()
    created_user = self._CreateAndSaveFakeUser()

    get_data = {'user_id': created_user.id}
    resp = self.client.get(flask.url_for('user_get_invite_code'),
                           query_string=get_data)
    fixed_json_obj = json.loads(resp.data[len(ufo.XSSI_PREFIX):])
    invite_url = str(fixed_json_obj['invite_code'])

    self.assertIn(user.INVITE_CODE_URL_PREFIX, invite_url)
    invite_code_base64 = invite_url[len(user.INVITE_CODE_URL_PREFIX):]
    invite_code_json = base64.urlsafe_b64decode(invite_code_base64)
    invite_code = json.loads(invite_code_json)

    self.assertEquals('Cloud', invite_code['networkName'])
    self.assertEquals(fake_ip, invite_code['networkData']['host'])
    self.assertEquals('getter', invite_code['networkData']['user'])
    self.assertEquals(base64.urlsafe_b64encode(created_user.private_key),
                      invite_code['networkData']['key'])