Ejemplo n.º 1
0
 def test_chain(self):
     try:
         from OpenSSL import SSL  # noqa
     except ImportError:
         logging.warning(
             "Skipping chain test (install pyOpenSSL to activate test)")
         return
     client = http.Client()
     try:
         client.get_server_certificate_chain(
             uri.URI.from_octets("http://www.pyslet.org/"))
         self.fail("Can't get certificate chain from http URL")
     except ValueError:
         pass
     chain = client.get_server_certificate_chain(
         uri.URI.from_octets("https://code.google.com/p/qtimigration/"))
     fpath = os.path.join(self.d, 'ca_certs.txt')
     with open(fpath, 'wb') as f:
         f.write(chain)
     client = http.Client(ca_certs=fpath)
     request = http.ClientRequest("https://code.google.com/p/qtimigration/")
     try:
         client.process_request(request)
     except messages.HTTPException as err:
         logging.error(err)
     client.close()
Ejemplo n.º 2
0
 def setUp(self):  # noqa
     self.port = random.randint(1111, 9999)
     self.server = server.Server(port=self.port,
                                 app=self.legacy_app,
                                 protocol=params.HTTP_1p0)
     self.server.timeout = 0.5
     self.client = http.Client()
Ejemplo n.º 3
0
 def __init__(self):
     super(MultiTenantTPApp, self).__init__()
     mt_settings = self.settings['MultiTenantTPApp']
     self.google_id = mt_settings['google_client_id']
     self.google_secret = mt_settings['google_client_secret']
     # TODO: add configuration of certificates!
     self.http = http.Client(ca_certs=self.cert_file)
Ejemplo n.º 4
0
 def test_google_insecure(self):
     client = http.Client()
     request = http.ClientRequest("https://code.google.com/p/qtimigration/")
     try:
         client.process_request(request)
     except messages.HTTPException as err:
         logging.error(err)
     client.close()
Ejemplo n.º 5
0
 def setUp(self):  # noqa
     self.port = random.randint(1111, 9999)
     self.server = server.Server(port=self.port,
                                 app=self.legacy_app,
                                 protocol=params.HTTP_1p0)
     # We need to give time to prevent handle_request doing nothing
     self.server.timeout = 10
     self.client = http.Client()
Ejemplo n.º 6
0
 def test_google_secure(self):
     client = http.Client(
         ca_certs=os.path.join(TEST_DATA_DIR, "ca_certs.txt"))
     request = http.ClientRequest("https://code.google.com/p/qtimigration/")
     try:
         client.process_request(request)
     except messages.HTTPException as err:
         logging.error(err)
     client.close()
     client = http.Client(
         ca_certs=os.path.join(TEST_DATA_DIR, "no_certs.txt"))
     request = http.ClientRequest("https://code.google.com/p/qtimigration/")
     try:
         client.process_request(request)
         if request.status != 0:
             self.fail("Expected status=0 after security failure")
         if not request.error:
             self.fail("Expected error after security failure")
         logging.info(str(request.error))
     except messages.HTTPException as err:
         logging.error(str(err))
     client.close()
Ejemplo n.º 7
0
def fetch_url(url, username=None, password=None):
    mgr = http.Client()
    url = uri.URI.from_octets(url)
    # does url end with $metadata?
    if url.get_file_name() != "$metadata":
        url = uri.URI.from_octets("$metadata").resolve(url)
    if username:
        cred = auth.BasicCredentials()
        cred.userid = username
        cred.password = password
        cred.protectionSpace = url.get_canonical_root()
        mgr.add_credentials(cred)
    doc = edmx.Document(baseURI=url, reqManager=mgr)
    doc.Read()
    mgr.close()
    if not doc.root.GetBase():
        doc.root.SetBase(url)
    return doc
Ejemplo n.º 8
0
 def setup(cls, options=None, args=None, **kwargs):
     """Adds multi-tenant initialisation"""
     super(MultiTenantTPApp, cls).setup(options, args, **kwargs)
     mt_settings = cls.settings.setdefault('MultiTenantTPApp', {})
     mt_settings.setdefault('google_client_id', '')
     mt_settings.setdefault('google_client_secret', '')
     cert_url = mt_settings.setdefault('google_certs', 'google_certs.pem')
     cls.cert_file = cls.resolve_setup_path(cert_url)
     if options and options.google_certs:
         # update the certs_file and exit
         c = http.Client()
         certs = []
         for s in ('https://accounts.google.com',
                   'https://www.googleapis.com', ):
             url = URI.from_octets(s)
             certs.append(c.get_server_certificate_chain(url))
         with open(cls.cert_file, 'wb') as f:
             f.write(string.join(certs, ''))
         sys.exit(0)
Ejemplo n.º 9
0
def run_weather_loader(container=None,
                       max_load=30,
                       not_before="19950630T000000"):
    """Monitors the DTG website for new values

    container
        The EntityContainer containing the weather data.

    max_load
        The maximum number of days worth of data to load.  When setting
        up a new server this determines the rate at which the new server
        will catch up.

    This function is designed to be called once per day, it loads
    historical data from the DTG website one day at a time up to a
    maximum of max_load.  If the data can't be loaded, e.g., because the
    DTG site is not reachable, then the method backs off until it has
    waited for approximately 1 hour after which it gives up.  Therefore,
    you should always set max_load greater than 1 to ensure that the
    method catches up with the data after an outage.

    The earliest date it will load is 30th June 1995, the latest date it
    will load is yesterday."""
    if container is None:
        doc = load_metadata()
        container = make_container(doc)
    client = http.Client()
    weather_data = container['DataPoints']
    dtg = "http://www.cl.cam.ac.uk/research/dtg/weather/daily-text.cgi?%s"
    not_before_point = iso.TimePoint.from_str(not_before)
    with weather_data.open() as collection:
        collection.set_orderby(
            core.CommonExpression.orderby_from_str('TimePoint desc'))
        sleep_interval = 60
        collection.set_page(1)
        last_point = list(collection.iterpage())
        if last_point:
            last_point = last_point[0]['TimePoint'].value
            if last_point < not_before_point:
                last_point = not_before_point
        else:
            last_point = not_before_point
        next_day = last_point.date
        n_loaded = 0
        while n_loaded < max_load:
            today = iso.TimePoint.from_now_utc().date
            if next_day < today:
                # Load in next_day
                logging.info("Requesting data for %s", str(next_day))
                century, year, month, day = next_day.get_calendar_day()
                request = http.ClientRequest(dtg % str(next_day))
                client.process_request(request)
                if request.status == 200:
                    # process this file and move on to the next day
                    f = StringIO.StringIO(request.res_body)
                    load_data_from_file(weather_data, f, century * 100 + year,
                                        month, day)
                    n_loaded += 1
                    next_day = next_day.offset(days=1)
                    if sleep_interval > 10:
                        sleep_interval = sleep_interval // 2
                else:
                    # back off and try again
                    sleep_interval = sleep_interval * 2
            else:
                # we're done for today
                client.idle_cleanup(0)
                break
            client.idle_cleanup(0)
            if sleep_interval > 3600:
                # site might be down, postpone
                break
            time.sleep(sleep_interval)