Revision 2287b920

b/daemons/ganeti-rapi
45 45
from ganeti import luxi
46 46
from ganeti import serializer
47 47
from ganeti import compat
48
from ganeti import utils
48 49
from ganeti.rapi import connector
49 50

  
50 51
import ganeti.http.auth   # pylint: disable-msg=W0611
......
102 103
    @param filename: Path to file
103 104

  
104 105
    """
105
    if not os.path.isfile(constants.RAPI_USERS_FILE):
106
      logging.warning("Users file %s not found", filename)
106
    try:
107
      contents = utils.ReadFile(filename)
108
    except EnvironmentError, err:
109
      logging.warning("Error while reading %s: %s", filename, err)
107 110
      return False
108 111

  
109 112
    try:
110
      users = http.auth.ReadPasswordFile(filename)
113
      users = http.auth.ParsePasswordFile(contents)
111 114
    except Exception, err: # pylint: disable-msg=W0703
112 115
      # We don't care about the type of exception
113
      logging.error("Error while reading %s: %s", filename, err)
116
      logging.error("Error while parsing %s: %s", filename, err)
114 117
      return False
115 118

  
116 119
    self._users = users
b/lib/http/auth.py
27 27
import base64
28 28
import binascii
29 29

  
30
from ganeti import utils
31 30
from ganeti import compat
32 31
from ganeti import http
33 32

  
......
287 286
    self.options = options
288 287

  
289 288

  
290
def ReadPasswordFile(file_name):
291
  """Reads a password file.
289
def ParsePasswordFile(contents):
290
  """Parses the contents of a password file.
292 291

  
293 292
  Lines in the password file are of the following format::
294 293

  
......
306 305
  """
307 306
  users = {}
308 307

  
309
  for line in utils.ReadFile(file_name).splitlines():
308
  for line in contents.splitlines():
310 309
    line = line.strip()
311 310

  
312 311
    # Ignore empty lines and comments
b/test/ganeti.http_unittest.py
26 26
import unittest
27 27
import time
28 28
import tempfile
29
from cStringIO import StringIO
29 30

  
30 31
from ganeti import http
31 32

  
......
290 291
          self.assert_(ac.called)
291 292

  
292 293

  
293
class TestReadPasswordFile(testutils.GanetiTestCase):
294
  def setUp(self):
295
    testutils.GanetiTestCase.setUp(self)
296

  
297
    self.tmpfile = tempfile.NamedTemporaryFile()
298

  
294
class TestReadPasswordFile(unittest.TestCase):
299 295
  def testSimple(self):
300
    self.tmpfile.write("user1 password")
301
    self.tmpfile.flush()
302

  
303
    users = http.auth.ReadPasswordFile(self.tmpfile.name)
296
    users = http.auth.ParsePasswordFile("user1 password")
304 297
    self.assertEqual(len(users), 1)
305 298
    self.assertEqual(users["user1"].password, "password")
306 299
    self.assertEqual(len(users["user1"].options), 0)
307 300

  
308 301
  def testOptions(self):
309
    self.tmpfile.write("# Passwords\n")
310
    self.tmpfile.write("user1 password\n")
311
    self.tmpfile.write("\n")
312
    self.tmpfile.write("# Comment\n")
313
    self.tmpfile.write("user2 pw write,read\n")
314
    self.tmpfile.write("   \t# Another comment\n")
315
    self.tmpfile.write("invalidline\n")
316
    self.tmpfile.flush()
317

  
318
    users = http.auth.ReadPasswordFile(self.tmpfile.name)
302
    buf = StringIO()
303
    buf.write("# Passwords\n")
304
    buf.write("user1 password\n")
305
    buf.write("\n")
306
    buf.write("# Comment\n")
307
    buf.write("user2 pw write,read\n")
308
    buf.write("   \t# Another comment\n")
309
    buf.write("invalidline\n")
310

  
311
    users = http.auth.ParsePasswordFile(buf.getvalue())
319 312
    self.assertEqual(len(users), 2)
320 313
    self.assertEqual(users["user1"].password, "password")
321 314
    self.assertEqual(len(users["user1"].options), 0)

Also available in: Unified diff