Esempio n. 1
0
 def testGetProxyMap(self):
     cfg = conarycfg.ConaryConfiguration(readConfigFiles=False)
     cfg.configLine("conaryproxy http http://localhost:123")
     cfg.configLine("conaryproxy https http://localhost:123")
     pm = cfg.getProxyMap()
     # getProxyMap() does not return proxyMap in this case - it's a new
     # proxyMap object
     self.assertFalse(pm is cfg.proxyMap)
     got = dict((x[0].protocol, (x[0].address, x[1])) for x in pm.filterList)
     self.assertEquals(got, {
         'http': (networking.HostPort('*'),
             [request.URL('conary://localhost:123')]),
         'https': (networking.HostPort('*'),
             [request.URL('conary://localhost:123')]),
         })
Esempio n. 2
0
 def writeConaryProxies(self, remotes):
     """
     Management nodes should already be written as conaryProxies by the cim
     interface, but add an extra check here to be sure.
     """
     proxies = set()
     for remote in remotes:
         if not remote.strip():
             continue
         # Strip off port in a way that works for IPv4 and IPv6
         # 1.2.3.4:8443 -> conarys://1.2.3.4
         # [fd00::1234]:8443 -> conarys://[fd00::1234]
         try:
             host = networking.HostPort(remote).host
         except ValueError:
             continue
         hostport = networking.HostPort(host, None)
         url = request.URL(
                 scheme='conarys',
                 userpass=(None, None),
                 hostport=hostport,
                 path='',
                 )
         proxies.add(str(url))
     if not proxies:
         return
     with open(self.cfg.conaryProxyFilePath, 'w') as f:
         print >> f, 'proxyMap *', ' '.join(sorted(proxies))
Esempio n. 3
0
    def testChainedConaryProxies(self):
        cProxy1 = cProxy2 = None
        self.openRepository()

        try:
            if self.cfg.conaryProxy:
                proxyCount = 1
            else:
                proxyCount = 0

            cProxy1 = self.getConaryProxy(proxies=self.cfg.conaryProxy)
            cProxy1.start()

            proxy1Addr = str(request.URL(cProxy1.getUrl()).hostport)
            proxyHash = dict(http=('conary://' + proxy1Addr),
                             https=('conarys://' + proxy1Addr))

            cProxy2 = self.getConaryProxy(1, proxies=proxyHash)
            cProxy2.start()

            proxy2Addr = str(request.URL(cProxy2.getUrl()).hostport)

            self.cfg.configLine("conaryProxy http://" + proxy2Addr)
            proxyCount += 2

            repos = conaryclient.ConaryClient(self.cfg).getRepos()
            pv = repos.c['localhost'].getProtocolVersion()
            self.assertEqual(pv, netserver.SERVER_VERSIONS[-1])
            transport = repos.c['localhost']._transport
            self.assertTrue('via' in transport.responseHeaders)
            via = transport.responseHeaders['via']
            via = via.split(',')

            if os.environ.get('CONARY_HTTP_PROXY', None) and \
                              os.environ.get('CONARY_PROXY', None):
                proxyCount += 1
            self.assertEqual(len(via), proxyCount)
            self._assertProxy(via[-2], cProxy1)
            self._assertProxy(via[-1], cProxy2)
        finally:
            if cProxy1: cProxy1.stop()
            if cProxy2: cProxy2.stop()
Esempio n. 4
0
 def setConaryProxy(self, managementNodes):
     proxies = set()
     for remote in managementNodes:
         if not remote.strip():
             continue
         # Strip off port in a way that works for IPv4 and IPv6
         # 1.2.3.4:8443 -> conarys://1.2.3.4
         # [fd00::1234]:8443 -> conarys://[fd00::1234]
         try:
             host = networking.HostPort(remote).host
         except ValueError:
             continue
         hostport = networking.HostPort(host, None)
         url = request.URL(
                 scheme='conarys',
                 userpass=(None, None),
                 hostport=hostport,
                 path='',
                 )
         proxies.add(str(url))
     if not proxies:
         return
     with open(self.cfg.conaryProxyFilePath, 'w') as f:
         print >> f, 'proxyMap *', ' '.join(sorted(proxies))
Esempio n. 5
0
    def testProxyBypassNoProxy(self):
        environ = os.environ.copy()
        proxyPlain = request.URL("http://*****:*****@host.example.com:1234")
        proxyConary = request.URL("conary://*****:*****@host.example.com:1234")
        url = request.URL("http://rpath.com")

        # We should not hit the proxy at all
        self.mock(os, 'environ', environ)

        # All flavors of localhost should be direct
        proxyMap = proxy_map.ProxyMap()
        opener = opmod.URLOpener(proxyMap)

        # Normal hosts will proxy through
        self.assertEqual(opener._shouldBypass(url, proxyPlain), False)
        self.assertEqual(opener._shouldBypass(url, proxyConary), False)
        for h in httputils.LocalHosts:
            self.assertEqual(
                opener._shouldBypass(request.URL("http://%s:33" % h),
                                     proxyPlain), True)
            self.assertEqual(
                opener._shouldBypass(request.URL("http://%s:33" % h),
                                     proxyConary), True)

        # Force direct for everything on HTTP proxies
        environ['no_proxy'] = "*"
        self.assertEqual(opener._shouldBypass(url, proxyPlain), True)
        # environment variable should not affect the selection of conary proxy
        self.assertEqual(opener._shouldBypass(url, proxyConary), False)

        # Test that NO_PROXY is also used, not just no_proxy
        del environ['no_proxy']
        self.assertEqual(opener._shouldBypass(url, proxyPlain), False)
        environ['NO_PROXY'] = "*"
        self.assertEqual(opener._shouldBypass(url, proxyPlain), True)
        self.assertEqual(opener._shouldBypass(url, proxyConary), False)
        # no_proxy takes precedence over NO_PROXY
        environ['no_proxy'] = "host.com"
        self.assertEqual(opener._shouldBypass(url, proxyPlain), False)
        self.assertEqual(
            opener._shouldBypass(request.URL("http://host.com"), proxyPlain),
            True)
        # http://lynx.isc.org/lynx2.8.5/lynx2-8-5/lynx_help/keystrokes/environments.html - comma-separated list of domains (with a trailing, to force empty domain)
        environ['no_proxy'] = "host.domain.dom, domain1.dom, domain2, "
        tests = [
            ('rpath.com:80', False),
            ('domain.com:80', False),
            ('host.domain.dom:80', True),
            ('hosthost.domain.dom:80', True),
            ('sub.host.domain.dom:80', True),
            ('www.domain1.dom:80', True),
            # Hmm. We will assume the domain is qualified up to ., the domain2
            # example probably assumes it matches any domain2 in the domain
            # search path, but we don't handle that.
            ('domain2:80', True),
            ('www.domain2:80', True),
        ]
        for h, expected in tests:
            self.assertEqual(
                opener._shouldBypass(request.URL('http://' + h), proxyPlain),
                expected)
Esempio n. 6
0
 def useServer(self, num=0):
     self.openRepository(num)
     server = self.servers.getCachedServer(num)
     self.server = server.getName()
     self.port = request.URL(server.getUrl()).hostport[1]
Esempio n. 7
0
    def testConaryCfgOptions(self):
        configfile = """
repositoryMap host.somewhere.com http://someotherhost.com:port/loc
proxyMap host.somewhere.com http://someotherhost.com:123/loc
installLabelPath conary.rpath.com@rpl:1 contrib.rpath.org@rpl:1
macros a  a
macros b  b b
macros c
flavor use:ssl,krb,readline,\
       bootstrap is: x86(i486,i586,i686,mmx) 
"""
        dir = tempfile.mkdtemp()
        cwd = os.getcwd()
        os.chdir(dir)
        f = open('foo', 'w')
        f.write(configfile)
        f.close()
        cfg = conarycfg.ConaryConfiguration(readConfigFiles=False)
        cfg.read(dir + '/foo')
        assert (cfg.buildPath)  # make sure defaults are working
        assert (cfg['macros']['a'] == 'a')
        assert (cfg['macros']['b'] == 'b b')
        assert (cfg['macros']['c'] == '')
        assert (cfg['repositoryMap']['host.somewhere.com'] ==
                'http://someotherhost.com:port/loc')

        filter, targets = cfg.proxyMap.filterList[0]
        self.assertEqual(filter.address,
                         networking.HostPort('host.somewhere.com'))
        self.assertEqual(targets,
                         [request.URL('http://someotherhost.com:123/loc')])

        ilp = cfg['installLabelPath']
        l0 = versions.Label('conary.rpath.com@rpl:1')
        l1 = versions.Label('contrib.rpath.org@rpl:1')
        lOther = versions.Label('contrib.rpath.org@rpl:devel')
        assert (ilp == [l0, l1])

        assert (ilp.priority(l0, l1) == -1)
        assert (ilp.priority(l0, l0) == 0)
        assert (ilp.priority(l1, l0) == 1)
        assert (ilp.priority(lOther, l0) is None)
        assert (ilp.priority(lOther, lOther) is None)
        assert (ilp.priority(l0, lOther) is None)

        v0 = versions.VersionFromString('/conary.rpath.com@rpl:1/1.0-1-1')
        v1 = versions.VersionFromString('/contrib.rpath.org@rpl:1/1.0-1-1')
        assert (ilp.versionPriority(v0, v1) == -1)
        assert (ilp.versionPriority(v0, v0) == 0)
        assert (ilp.versionPriority(v1, v0) == 1)

        assert (str(cfg.flavor[0]) ==
                'bootstrap,krb,readline,ssl is: x86(i486,i586,i686,mmx)')
        out = StringIO()
        cfg.displayKey('flavor', out)
        assert (
            out.getvalue().split(None, 1)[1] ==
            'bootstrap, krb, readline, ssl is: x86(i486, i586, i686, mmx)\n')
        out.close()
        out = StringIO()
        cfg.setDisplayOptions(prettyPrint=True)
        cfg.displayKey('flavor', out)
        assert (
            out.getvalue().split(None, 1)[1] ==
            'bootstrap, krb, readline, ssl is: x86(i486,\n                          i586, i686, mmx)\n'
        )
        out.close()
        out = StringIO()
        cfg.displayKey('repositoryMap', out)
        assert (
            out.getvalue() ==
            'repositoryMap             host.somewhere.com        http://someotherhost.com:port/loc\n'
        )
        out.close()
        out = StringIO()
        cfg.displayKey('proxyMap', out)
        self.assertEqual(
            out.getvalue(), "proxyMap                  host.somewhere.com "
            "http://someotherhost.com:123/loc\n")
        out.close()
        cfg.setDisplayOptions(hidePasswords=True)
        out = StringIO()
        cfg.displayKey('repositoryMap', out)
        assert (
            out.getvalue() ==
            'repositoryMap             host.somewhere.com        http://someotherhost.com:port/loc\n'
        )
        out.close()

        keys = cfg.macros.keys()
        keys.sort()
        assert (keys == ['a', 'b', 'c'])
        os.remove('foo')
        foo = open('foo', 'w')
        cfg.display(foo)
        foo.close()
        lines = [x.split() for x in open('foo', 'r')]
        assert (['macros', 'a', 'a'] in lines)
        assert (['macros', 'c'] in lines)
        os.remove('foo')
        os.chdir(cwd)
        os.rmdir(dir)