Package ganeti :: Package rapi :: Package auth :: Module users_file
[hide private]
[frames] | no frames]

Source Code for Module ganeti.rapi.auth.users_file

  1  # 
  2  # 
  3   
  4  # Copyright (C) 2006, 2007, 2008, 2009, 2010, 2012, 2013, 2015 Google Inc. 
  5  # All rights reserved. 
  6  # 
  7  # Redistribution and use in source and binary forms, with or without 
  8  # modification, are permitted provided that the following conditions are 
  9  # met: 
 10  # 
 11  # 1. Redistributions of source code must retain the above copyright notice, 
 12  # this list of conditions and the following disclaimer. 
 13  # 
 14  # 2. Redistributions in binary form must reproduce the above copyright 
 15  # notice, this list of conditions and the following disclaimer in the 
 16  # documentation and/or other materials provided with the distribution. 
 17  # 
 18  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 
 19  # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
 20  # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 21  # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
 22  # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 23  # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 24  # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 25  # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 26  # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 27  # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 28  # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 29   
 30  """RAPI users config file parser. 
 31   
 32  """ 
 33   
 34  import errno 
 35  import logging 
 36   
 37  from ganeti import utils 
 38   
 39   
40 -class PasswordFileUser(object):
41 """Data structure for users from password file. 42 43 """
44 - def __init__(self, name, password, options):
45 self.name = name 46 self.password = password 47 self.options = options
48 49
50 -def ParsePasswordFile(contents):
51 """Parses the contents of a password file. 52 53 Lines in the password file are of the following format:: 54 55 <username> <password> [options] 56 57 Fields are separated by whitespace. Username and password are mandatory, 58 options are optional and separated by comma (','). Empty lines and comments 59 ('#') are ignored. 60 61 @type contents: str 62 @param contents: Contents of password file 63 @rtype: dict 64 @return: Dictionary containing L{PasswordFileUser} instances 65 66 """ 67 users = {} 68 69 for line in utils.FilterEmptyLinesAndComments(contents): 70 parts = line.split(None, 2) 71 if len(parts) < 2: 72 # Invalid line 73 # TODO: Return line number from FilterEmptyLinesAndComments 74 logging.warning("Ignoring non-comment line with less than two fields") 75 continue 76 77 name = parts[0] 78 password = parts[1] 79 80 # Extract options 81 options = [] 82 if len(parts) >= 3: 83 for part in parts[2].split(","): 84 options.append(part.strip()) 85 else: 86 logging.warning("Ignoring values for user '%s': %s", name, parts[3:]) 87 88 users[name] = PasswordFileUser(name, password, options) 89 90 return users
91 92
93 -class RapiUsers(object):
94 - def __init__(self):
95 """Initializes this class. 96 97 """ 98 self._users = None
99
100 - def Get(self, username):
101 """Checks whether a user exists. 102 103 """ 104 if self._users: 105 return self._users.get(username, None) 106 else: 107 return None
108
109 - def Load(self, filename):
110 """Loads a file containing users and passwords. 111 112 @type filename: string 113 @param filename: Path to file 114 115 """ 116 logging.info("Reading users file at %s", filename) 117 try: 118 try: 119 contents = utils.ReadFile(filename) 120 except EnvironmentError, err: 121 self._users = None 122 if err.errno == errno.ENOENT: 123 logging.warning("No users file at %s", filename) 124 else: 125 logging.warning("Error while reading %s: %s", filename, err) 126 return False 127 128 users = ParsePasswordFile(contents) 129 130 except Exception, err: # pylint: disable=W0703 131 # We don't care about the type of exception 132 logging.error("Error while parsing %s: %s", filename, err) 133 return False 134 135 self._users = users 136 137 return True
138