예제 #1
0
파일: ssl_test.py 프로젝트: qsdj/grr
  def setUp(self):
    super(ApiSslServerTestBase, self).setUp()

    key = rdf_crypto.RSAPrivateKey.GenerateKey()
    key_path = os.path.join(self.temp_dir, "key.pem")
    with open(key_path, "wb") as f:
      f.write(key.AsPEM())

    subject = issuer = x509.Name([
        x509.NameAttribute(oid.NameOID.COMMON_NAME, u"localhost"),
    ])

    cert = x509.CertificateBuilder().subject_name(subject).issuer_name(
        issuer).public_key(key.GetPublicKey().GetRawPublicKey()).serial_number(
            x509.random_serial_number()).not_valid_before(
                datetime.datetime.utcnow()).not_valid_after(
                    datetime.datetime.utcnow() +
                    datetime.timedelta(days=1)).add_extension(
                        x509.SubjectAlternativeName(
                            [x509.DNSName(u"localhost")]),
                        critical=False,
                    ).sign(key.GetRawPrivateKey(), hashes.SHA256(),
                           backends.default_backend())

    self.cert_path = os.path.join(self.temp_dir, "certificate.pem")
    with open(self.cert_path, "wb") as f:
      f.write(cert.public_bytes(serialization.Encoding.PEM))

    self.config_overrider = test_lib.ConfigOverrider({
        "AdminUI.enable_ssl": True,
        "AdminUI.ssl_key_file": key_path,
        "AdminUI.ssl_cert_file": self.cert_path,
    })
    self.config_overrider.Start()

    self.port = portpicker.PickUnusedPort()
    self.thread = wsgiapp_testlib.ServerThread(self.port)
    self.thread.StartAndWaitUntilServing()

    api_auth_manager.APIACLInit.InitApiAuthManager()
    self.token.username = "******"
    webauth.WEBAUTH_MANAGER.SetUserName(self.token.username)

    self.endpoint = "https://localhost:%s" % self.port
예제 #2
0
    def GetConnector(api_version):
        if api_version not in [1, 2]:
            raise ValueError("api_version may be 1 or 2 only")

        with _HTTP_ENDPOINTS_LOCK:
            if api_version not in _HTTP_ENDPOINTS:
                port = portpicker.PickUnusedPort()
                logging.info("Picked free AdminUI port %d.", port)

                # Force creation of new APIAuthorizationManager.
                api_auth_manager.APIACLInit.InitApiAuthManager()

                trd = wsgiapp_testlib.ServerThread(port)
                trd.StartAndWaitUntilServing()

                _HTTP_ENDPOINTS[api_version] = "http://localhost:%d" % port

            return http_connector.HttpConnector(
                api_endpoint=_HTTP_ENDPOINTS[api_version])