Пример #1
0
    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))

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

        self.port = portpicker.pick_unused_port()
        thread = wsgiapp_testlib.ServerThread(self.port,
                                              name="ApiSslServerTest")
        thread.StartAndWaitUntilServing()
        self.addCleanup(thread.Stop)

        api_auth_manager.InitializeApiAuthManager(
            api_call_router_without_checks.ApiCallRouterWithoutChecks)
        self.token.username = "******"
        webauth.WEBAUTH_MANAGER.SetUserName(self.token.username)

        self.endpoint = "https://localhost:%s" % self.port
Пример #2
0
    def setUpClass(cls):
        super(ApiE2ETest, cls).setUpClass()
        with ApiE2ETest._api_set_up_lock:
            if not ApiE2ETest._api_set_up_done:

                # Set up HTTP server
                port = portpicker.PickUnusedPort()
                ApiE2ETest.server_port = port
                logging.info("Picked free AdminUI port for HTTP %d.", port)

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

                ApiE2ETest._api_set_up_done = True
Пример #3
0
  def setUpClass(cls):
    super(GRRSeleniumTest, cls).setUpClass()
    with GRRSeleniumTest._selenium_set_up_lock:
      if not GRRSeleniumTest._selenium_set_up_done:

        port = portpicker.PickUnusedPort()
        logging.info("Picked free AdminUI port %d.", port)

        # Start up a server in another thread
        GRRSeleniumTest._server_trd = wsgiapp_testlib.ServerThread(port)
        GRRSeleniumTest._server_trd.StartAndWaitUntilServing()
        GRRSeleniumTest._SetUpSelenium(port)

        GRRSeleniumTest._selenium_set_up_done = True
Пример #4
0
    def setUpClass(cls):
        super(ApiSslServerTestBase, cls).setUpClass()

        if ApiSslServerTestBase._api_set_up_done:
            return

        key = rdf_crypto.RSAPrivateKey.GenerateKey()
        key_path = temp.TempFilePath("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())

        ApiSslServerTestBase.ssl_cert_path = temp.TempFilePath(
            "certificate.pem")
        with open(ApiSslServerTestBase.ssl_cert_path, "wb") as f:
            f.write(cert.public_bytes(serialization.Encoding.PEM))

        ApiSslServerTestBase.ssl_port = portpicker.pick_unused_port()
        with test_lib.ConfigOverrider({
                "AdminUI.enable_ssl":
                True,
                "AdminUI.ssl_key_file":
                key_path,
                "AdminUI.ssl_cert_file":
                ApiSslServerTestBase.ssl_cert_path,
        }):
            ApiSslServerTestBase._ssl_trd = wsgiapp_testlib.ServerThread(
                ApiSslServerTestBase.ssl_port, name="ApiSslServerTest")
            ApiSslServerTestBase._ssl_trd.StartAndWaitUntilServing()

        ApiSslServerTestBase.ssl_endpoint = ("https://localhost:%s" %
                                             ApiSslServerTestBase.ssl_port)

        ApiSslServerTestBase._api_set_up_done = True
Пример #5
0
  def setUpClass(cls):
    super(ApiIntegrationTest, cls).setUpClass()
    if ApiIntegrationTest._api_set_up_done:
      return

    # Set up HTTP server
    port = portpicker.pick_unused_port()
    ApiIntegrationTest.server_port = port
    logging.info("Picked free AdminUI port for HTTP %d.", port)

    ApiIntegrationTest._server_trd = wsgiapp_testlib.ServerThread(
        port, name="api_integration_server")
    ApiIntegrationTest._server_trd.StartAndWaitUntilServing()

    ApiIntegrationTest._api_set_up_done = True
Пример #6
0
  def setUpClass(cls) -> None:
    """Performs all initialization needed to interface with GRR's API."""
    # This is a mixin class intended to be used with `absltest.TestCase`.
    super(ColabTestMixin, cls).setUpClass()  # pytype: disable=attribute-error

    # TODO(hanuszczak): `TestInit` is awful, does a lot of unnecessary stuff and
    # should be avoided. However, because of all the global state that GRR has
    # currently, it is extremely hard figure out which parts need initialization
    # and which do not. Once AFF4 is gone, hopefully this should become much
    # simpler and `TestInit` will no longer be necessary.
    testing_startup.TestInit()

    port = portpicker.pick_unused_port()

    cls._server_thread = wsgiapp_testlib.ServerThread(port, name="ServerThread")
    cls._server_thread.StartAndWaitUntilServing()

    _api._API = api.InitHttp(api_endpoint="http://localhost:{}".format(port))  # pylint: disable=protected-access
Пример #7
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])
Пример #8
0
    def setUpClass(cls):
        super(GRRSeleniumTest, cls).setUpClass()
        with GRRSeleniumTest._selenium_set_up_lock:
            if not GRRSeleniumTest._selenium_set_up_done:

                port = portpicker.pick_unused_port()
                logging.info("Picked free AdminUI port %d.", port)

                # Start up a server in another thread
                GRRSeleniumTest._server_trd = wsgiapp_testlib.ServerThread(
                    port, name="SeleniumServerThread")
                GRRSeleniumTest._server_trd.StartAndWaitUntilServing()
                GRRSeleniumTest._SetUpSelenium(port)

                jquery_path = package.ResourcePath(
                    "grr-response-test",
                    "grr_response_test/test_data/jquery_3.1.0.min.js")
                with open(jquery_path, mode="r", encoding="utf-8") as fd:
                    GRRSeleniumTest._jquery_source = fd.read()

                GRRSeleniumTest._selenium_set_up_done = True