import base64
import binascii
-from ganeti import utils
from ganeti import compat
from ganeti import http
+from ganeti import utils
from cStringIO import StringIO
"""
# today we don't have per-request filtering, but we might want to
# add it in the future
- # pylint: disable-msg=W0613
+ # pylint: disable=W0613
return self.AUTH_REALM
def AuthenticationRequired(self, req):
"""
# Unused argument, method could be a function
- # pylint: disable-msg=W0613,R0201
+ # pylint: disable=W0613,R0201
return False
def PreHandleRequest(self, req):
"""
try:
- creds = base64.b64decode(in_data.encode('ascii')).decode('ascii')
+ creds = base64.b64decode(in_data.encode("ascii")).decode("ascii")
except (TypeError, binascii.Error, UnicodeError):
logging.exception("Error when decoding Basic authentication credentials")
return False
self.options = options
-def ReadPasswordFile(file_name):
- """Reads a password file.
+def ParsePasswordFile(contents):
+ """Parses the contents of a password file.
Lines in the password file are of the following format::
options are optional and separated by comma (','). Empty lines and comments
('#') are ignored.
- @type file_name: str
- @param file_name: Path to password file
+ @type contents: str
+ @param contents: Contents of password file
@rtype: dict
@return: Dictionary containing L{PasswordFileUser} instances
"""
users = {}
- for line in utils.ReadFile(file_name).splitlines():
- line = line.strip()
-
- # Ignore empty lines and comments
- if not line or line.startswith("#"):
- continue
-
+ for line in utils.FilterEmptyLinesAndComments(contents):
parts = line.split(None, 2)
if len(parts) < 2:
# Invalid line
+ # TODO: Return line number from FilterEmptyLinesAndComments
+ logging.warning("Ignoring non-comment line with less than two fields")
continue
name = parts[0]
if len(parts) >= 3:
for part in parts[2].split(","):
options.append(part.strip())
+ else:
+ logging.warning("Ignoring values for user '%s': %s", name, parts[3:])
users[name] = PasswordFileUser(name, password, options)