Exemple #1
0
def GetCookies(host, path, cookie_paths=None):
  """Returns cookies that should be set on a request.

  Used by CreateHttpConn for any requests that do not already specify a Cookie
  header. All requests made by this library are HTTPS.

  Args:
    host: The hostname of the Gerrit service.
    path: The path on the Gerrit service, already including /a/ if applicable.
    cookie_paths: Files to look in for cookies. Defaults to looking in the
      standard places where GoB places cookies.

  Returns:
    A dict of cookie name to value, with no URL encoding applied.
  """
  cookies = {}
  if cookie_paths is None:
    cookie_paths = (constants.GOB_COOKIE_PATH, constants.GITCOOKIES_PATH)
  for cookie_path in cookie_paths:
    if os.path.isfile(cookie_path):
      with open(cookie_path) as f:
        for line in f:
          fields = line.strip().split('\t')
          if line.strip().startswith('#') or len(fields) != 7:
            continue
          domain, xpath, key, value = fields[0], fields[2], fields[5], fields[6]
          if cookielib.domain_match(host, domain) and path.startswith(xpath):
            cookies[key] = value
  return cookies
Exemple #2
0
def GetCookies(host, path, cookie_paths=None):
  """Returns cookies that should be set on a request.

  Used by CreateHttpConn for any requests that do not already specify a Cookie
  header. All requests made by this library are HTTPS.

  Args:
    host: The hostname of the Gerrit service.
    path: The path on the Gerrit service, already including /a/ if applicable.
    cookie_paths: Files to look in for cookies. Defaults to looking in the
      standard places where GoB places cookies.

  Returns:
    A dict of cookie name to value, with no URL encoding applied.
  """
  cookies = {}
  if cookie_paths is None:
    cookie_paths = (constants.GOB_COOKIE_PATH, constants.GITCOOKIES_PATH)
  for cookie_path in cookie_paths:
    if os.path.isfile(cookie_path):
      with open(cookie_path) as f:
        for line in f:
          fields = line.strip().split('\t')
          if line.strip().startswith('#') or len(fields) != 7:
            continue
          domain, xpath, key, value = fields[0], fields[2], fields[5], fields[6]
          if cookielib.domain_match(host, domain) and path.startswith(xpath):
            cookies[key] = value
  return cookies
def get_cookie_domain(request_domain):
    """Return a string suitable for use as the domain parameter of a cookie.

    The returned domain value should allow the cookie to be seen by
    all virtual hosts of the Launchpad instance.  If no matching
    domain is known, None is returned.
    """
    cookie_domains = [v.strip()
                      for v in config.launchpad.cookie_domains.split(',')]
    for domain in cookie_domains:
        assert not domain.startswith('.'), \
               "domain should not start with '.'"
        dotted_domain = '.' + domain
        if (domain_match(request_domain, domain)
            or domain_match(request_domain, dotted_domain)):
            return dotted_domain
    return None
Exemple #4
0
 def handle_response(self, f):
     for i in f.response.headers.get("set-cookie", []):
         # FIXME: We now know that Cookie.py screws up some cookies with
         # valid RFC 822/1123 datetime specifications for expiry. Sigh.
         c = Cookie.SimpleCookie(i)
         m = c.values()[0]
         k = self.ckey(m, f)
         if cookielib.domain_match(f.request.host, k[0]):
             self.jar[self.ckey(m, f)] = m
Exemple #5
0
def get_cookie_domain(request_domain):
    """Return a string suitable for use as the domain parameter of a cookie.

    The returned domain value should allow the cookie to be seen by
    all virtual hosts of the Launchpad instance.  If no matching
    domain is known, None is returned.
    """
    cookie_domains = [
        v.strip() for v in config.launchpad.cookie_domains.split(',')
    ]
    for domain in cookie_domains:
        assert not domain.startswith('.'), \
               "domain should not start with '.'"
        dotted_domain = '.' + domain
        if (domain_match(request_domain, domain)
                or domain_match(request_domain, dotted_domain)):
            return dotted_domain
    return None
Exemple #6
0
  def get_auth_header(self, host):
    auth = None
    for domain, creds in self.gitcookies.iteritems():
      if cookielib.domain_match(host, domain):
        auth = (creds[0], None, creds[1])
        break

    if not auth:
      auth = self.netrc.authenticators(host)
    if auth:
      return 'Basic %s' % (base64.b64encode('%s:%s' % (auth[0], auth[2])))
    return None
Exemple #7
0
  def get_auth_header(self, host):
    auth = None
    for domain, creds in self.gitcookies.iteritems():
      if cookielib.domain_match(host, domain):
        auth = (creds[0], None, creds[1])
        break

    if not auth:
      auth = self.netrc.authenticators(host)
    if auth:
      return 'Basic %s' % (base64.b64encode('%s:%s' % (auth[0], auth[2])))
    return None
Exemple #8
0
 def handle_request(self, f):
     if f.match(self.flt):
         cs = []
         for i in self.jar.keys():
             match = [
                 cookielib.domain_match(i[0], f.request.host),
                 f.request.port == i[1],
                 f.request.path.startswith(i[2])
             ]
             if all(match):
                 l = f.request.headers.setdefault("cookie", [])
                 f.request.stickycookie = True
                 l.append(self.jar[i].output(header="").strip())
Exemple #9
0
    def __getitem__(self, host):
        """Returns the authentication tuple for the given host.

    Args:
      host: The host for which a matching auth tuple should be returned.

    Returns:
      (login, secret_token) tuple.

    Raises:
      KeyError: if no matching credentials were found.
    """
        if self.netrc:
            auth = self.netrc.authenticators(host)
            # Get rid of the account in the tuple, it is always None, anyway.
            if auth:
                return (auth[0], auth[2])

        for domain, creds in self.gitcookies.iteritems():
            if cookielib.domain_match(host, domain):
                return creds
        raise KeyError
Exemple #10
0
  def __getitem__(self, host):
    """Returns the authentication tuple for the given host.

    Args:
      host: The host for which a matching auth tuple should be returned.

    Returns:
      (login, secret_token) tuple.

    Raises:
      KeyError: if no matching credentials were found.
    """
    if self.auth:
      return self.auth
    if self.netrc:
      auth = self.netrc.authenticators(host)
      # Get rid of the account in the tuple, it is always None, anyway.
      if auth:
        return (auth[0], auth[2])

    for domain, creds in self.gitcookies.iteritems():
      if cookielib.domain_match(host, domain):
        return creds
    raise KeyError
Exemple #11
0
 def _get_auth_for_host(self, host):
     for domain, creds in self.gitcookies.iteritems():
         if cookielib.domain_match(host, domain):
             return (creds[0], None, creds[1])
     return self.netrc.authenticators(host)
Exemple #12
0
 def domain_match(self, a, b):
     if cookielib.domain_match(a, b):
         return True
     elif cookielib.domain_match(a, b.strip(".")):
         return True
     return False
Exemple #13
0
 def domain_match(self, a, b):
     if cookielib.domain_match(a, b):
         return True
     elif cookielib.domain_match(a, b.strip(".")):
         return True
     return False
 def _get_auth_for_host(self, host):
   for domain, creds in self.gitcookies.iteritems():
     if cookielib.domain_match(host, domain):
       return (creds[0], None, creds[1])
   return self.netrc.authenticators(host)