def test_underDomain_matches_subdomain(self): # URI.underDomain should return True when asked whether the url is # under one of its parent domains. uri = URI('http://code.launchpad.dev/foo') self.assertTrue(uri.underDomain('code.launchpad.dev')) self.assertTrue(uri.underDomain('launchpad.dev')) self.assertTrue(uri.underDomain(''))
def test_underDomain_doesnt_match_non_subdomain(self): # URI.underDomain should return False when asked whether the url is # under a domain which isn't one of its parents. uri = URI('http://code.launchpad.dev/foo') self.assertFalse(uri.underDomain('beta.code.launchpad.dev')) self.assertFalse(uri.underDomain('google.com')) self.assertFalse(uri.underDomain('unchpad.dev'))
def test_underDomain_doesnt_match_non_subdomain(self): # URI.underDomain should return False when asked whether the url is # under a domain which isn't one of its parents. uri = URI('http://code.launchpad.dev/foo') self.assertFalse(uri.underDomain('beta.code.launchpad.dev')) self.assertFalse(uri.underDomain('google.com')) self.assertFalse(uri.underDomain('unchpad.dev'))
def test_underDomain_matches_subdomain(self): # URI.underDomain should return True when asked whether the url is # under one of its parent domains. uri = URI('http://code.launchpad.dev/foo') self.assertTrue(uri.underDomain('code.launchpad.dev')) self.assertTrue(uri.underDomain('launchpad.dev')) self.assertTrue(uri.underDomain(''))
def _normalize_stacked_on_url(self, branch): """Normalize and return the stacked-on location of `branch`. In the common case, `branch` will either be unstacked or stacked on a relative path, in which case this is very easy: just return the location. If `branch` is stacked on the absolute URL of another Launchpad branch, we normalize this to a relative path (mutating the branch) and return the relative path. If `branch` is stacked on some other absolute URL we don't recognise, we just return that and rely on the `branchChanged` XML-RPC method recording a complaint in the appropriate place. """ stacked_on_url = get_stacked_on_url(branch) if stacked_on_url is None: return None if "://" not in stacked_on_url: # Assume it's a relative path. return stacked_on_url uri = URI(stacked_on_url) if uri.scheme not in ["http", "bzr+ssh", "sftp"]: return stacked_on_url launchpad_domain = config.vhost.mainsite.hostname if not uri.underDomain(launchpad_domain): return stacked_on_url # We use TransportConfig directly because the branch # is still locked at this point! We're effectively # 'borrowing' the lock that is being released. branch_config = TransportConfig(branch._transport, "branch.conf") branch_config.set_option(uri.path, "stacked_on_location") return uri.path
def _normalize_stacked_on_url(self, branch): """Normalize and return the stacked-on location of `branch`. In the common case, `branch` will either be unstacked or stacked on a relative path, in which case this is very easy: just return the location. If `branch` is stacked on the absolute URL of another Launchpad branch, we normalize this to a relative path (mutating the branch) and return the relative path. If `branch` is stacked on some other absolute URL we don't recognise, we just return that and rely on the `branchChanged` XML-RPC method recording a complaint in the appropriate place. """ stacked_on_url = get_stacked_on_url(branch) if stacked_on_url is None: return None if '://' not in stacked_on_url: # Assume it's a relative path. return stacked_on_url uri = URI(stacked_on_url) if uri.scheme not in ['http', 'bzr+ssh', 'sftp']: return stacked_on_url launchpad_domain = config.vhost.mainsite.hostname if not uri.underDomain(launchpad_domain): return stacked_on_url # We use TransportConfig directly because the branch # is still locked at this point! We're effectively # 'borrowing' the lock that is being released. branch_config = TransportConfig(branch._transport, 'branch.conf') branch_config.set_option(uri.path, 'stacked_on_location') return uri.path
def checkOneURL(self, url): """See `BranchOpenPolicy.checkOneURL`. We refuse to mirror from Launchpad or a ssh-like or file URL. """ try: uri = URI(url) except InvalidURIError: raise BadUrl(url) launchpad_domain = config.vhost.mainsite.hostname if uri.underDomain(launchpad_domain): raise BadUrl(url) for hostname in get_blacklisted_hostnames(): if uri.underDomain(hostname): raise BadUrl(url) if uri.scheme not in self.allowed_schemes: raise BadUrl(url)
def checkOneURL(self, url): """See `BranchOpenPolicy.checkOneURL`. We refuse to mirror from Launchpad or a ssh-like or file URL. """ # Avoid circular import from lp.code.interfaces.branch import get_blacklisted_hostnames uri = URI(url) launchpad_domain = config.vhost.mainsite.hostname if uri.underDomain(launchpad_domain): raise BadUrlLaunchpad(url) for hostname in get_blacklisted_hostnames(): if uri.underDomain(hostname): raise BadUrl(url) if uri.scheme in ['sftp', 'bzr+ssh']: raise BadUrlSsh(url) elif uri.scheme not in ['http', 'https']: raise BadUrlScheme(uri.scheme, url)
def mirror_location(self): """Check the mirror location to see if it is a private one.""" branch = self.branch # If the user has edit permissions, then show the actual location. if branch.url is None or check_permission('launchpad.Edit', branch): return branch.url # XXX: Tim Penhey, 2008-05-30, bug 235916 # Instead of a configuration hack we should support the users # specifying whether or not they want the mirror location # hidden or not. Given that this is a database patch, # it isn't going to happen today. hosts = config.codehosting.private_mirror_hosts.split(',') private_mirror_hosts = [name.strip() for name in hosts] uri = URI(branch.url) for private_host in private_mirror_hosts: if uri.underDomain(private_host): return '<private server>' return branch.url