Login | Register
My pages Projects Community openCollabNet

Discussions > commits > svn commit: r267 - trunk/current: . admin api

Project highlights: Stable Version: 1.6.1, Development Version: 1.7.6

current
Discussion topic

Back to topic list

svn commit: r267 - trunk/current: . admin api

Reply

Author jjneely
Full name Jack Neely
Date 2006-08-28 15:15:48 PDT
Message Author: jjneely
Date: 2006-08-28 15:15:48-0700
New Revision: 267

Added:
   trunk/current/admin/login.py
Modified:
   trunk/current/admin/__init__.py
   trunk/current/admin/add_dir.py
   trunk/current/admin/​create_channel.py
   trunk/current/admin/scan.py
   trunk/current/admin/search.py
   trunk/current/admin/​subscribe.py
   trunk/current/admin/test.py
   trunk/current/admin/​unsubscribe.py
   trunk/current/api/cadmin.py
   trunk/current/api/re​gistration.py
   trunk/current/cadmin.py
   trunk/current/exception.py
   trunk/current/sessions.py
   trunk/current/users.py

Log:
Create a SessionUser object to authenticate users based on their session.
Add authentication to the cadmin commands. Not all the modules in admin/
are ported quite yet.

Would be handy if cadmin could add users for testing here.

Make the sessions code use SHA1.


Modified: trunk/current/admin/__init__.py
Url: http://current.tigri​s.org/source/browse/​current/trunk/curren​t/admin/__init__.py?​view=diff&rev=26​7&p1=trunk/curre​nt/admin/__init__.py​&p2=trunk/curren​t/admin/__init__.py​&r1=266&r2=26​7
====================​====================​====================​==================
--- trunk/current/admin/__init__.py (original)
+++ trunk/current/admin/__init__.py 2006-08-28 15:15:48-0700
@@ -18,7 +18,7 @@
 
     shortHelp = "No help description avaliable."
 
- def run(self, server, argv):
+ def run(self, server, session, argv):
         pass
 
     def name(self):

Modified: trunk/current/admin/add_dir.py
Url: http://current.tigri​s.org/source/browse/​current/trunk/curren​t/admin/add_dir.py?v​iew=diff&rev=267​&p1=trunk/curren​t/admin/add_dir.py​&p2=trunk/current/​admin/add_dir.py​&r1=266&r2=267
====================​====================​====================​==================
--- trunk/current/admin/add_dir.py (original)
+++ trunk/current/admin/add_dir.py 2006-08-28 15:15:48-0700
@@ -8,7 +8,7 @@
 
     shortHelp = "Add a directory of packages to a channel."
 
- def run(self, server, argv):
+ def run(self, server, session, argv):
         u = "usage: %prog add_dir <channel> <dir> [[dir] ...]"
         parser = optparse.OptionParser(u)
         parser.add_option("-l", "--label", action="store",

Modified: trunk/current/admin/​create_channel.py
Url: http://current.tigri​s.org/source/browse/​current/trunk/curren​t/admin/create_chann​el.py?view=diff&​rev=267&p1=trunk​/current/admin/creat​e_channel.py&p2=​trunk/current/admin/​create_channel.py​&r1=266&r2=267
====================​====================​====================​==================
--- trunk/current/admin/​create_channel.py (original)
+++ trunk/current/admin/​create_channel.py 2006-08-28 15:15:48-0700
@@ -12,7 +12,7 @@
 
     shortHelp = "Create a channel or subchannel."
 
- def run(self, server, argv):
+ def run(self, server, session, argv):
         u = "%prog create_channel -l <label> -a <arch> -r " \
                 "<release> -n <name> [options]"
         parser = optparse.OptionParser(u)

Added: trunk/current/admin/login.py
Url: http://current.tigri​s.org/source/browse/​current/trunk/curren​t/admin/login.py?vie​w=auto&rev=267
====================​====================​====================​==================
--- (empty file)
+++ trunk/current/admin/login.py 2006-08-28 15:15:48-0700
@@ -0,0 +1,27 @@
+from current.admin import CadminConfig
+import pprint
+import sys
+import getpass
+
+class Module(CadminConfig):
+
+ shortHelp = "Authenticate to a Current server."
+
+ def run(self, server, session, argv):
+ usage = "usage: %prog login"
+
+ sys.stdout.write("Login: ")
+ user = sys.stdin.readline().strip()
+ password = getpass.getpass("Password:")
+
+ result = self.call(server.cadmin.login, user, password)
+ if result == None:
+ print "Login failed. Check your username and password."
+ else:
+ print "Login successful."
+
+ return result
+
+ def name(self):
+ return "unsubscribe"
+

Modified: trunk/current/admin/scan.py
Url: http://current.tigri​s.org/source/browse/​current/trunk/curren​t/admin/scan.py?view​=diff&rev=267​&p1=trunk/current/a​dmin/scan.py&p2=​trunk/current/admin/​scan.py&r1=266​&r2=267
====================​====================​====================​==================
--- trunk/current/admin/scan.py (original)
+++ trunk/current/admin/scan.py 2006-08-28 15:15:48-0700
@@ -8,7 +8,7 @@
 
     shortHelp = "Update all packages from disk for the given channels."
 
- def run(self, server, argv):
+ def run(self, server, session, argv):
         u = "usage: %prog scan <channel> [[channel] ...]"
         parser = optparse.OptionParser(u)
         parser.add_option("-l", "--label", action="append",
@@ -26,6 +26,6 @@
 
         chan = {}
         chan['channels'] = oChans + leftargs
- result = xmlrpc.doCall(server​.cadmin.scanChannels​, chan)
+ result = xmlrpc.doCall(server​.cadmin.scanChannels​, session, chan)
         pprint.pprint(result)
 

Modified: trunk/current/admin/search.py
Url: http://current.tigri​s.org/source/browse/​current/trunk/curren​t/admin/search.py?vi​ew=diff&rev=267​&p1=trunk/current​/admin/search.py​&p2=trunk/current/ad​min/search.py&r1​=266&r2=267
====================​====================​====================​==================
--- trunk/current/admin/search.py (original)
+++ trunk/current/admin/search.py 2006-08-28 15:15:48-0700
@@ -6,7 +6,7 @@
 
     shortHelp = "Find UUIDs from profile names."
 
- def run(self, server, argv):
+ def run(self, server, session, argv):
         usage = "usage: %prog search"
         if len(argv) is not 0:
             print usage

Modified: trunk/current/admin/​subscribe.py
Url: http://current.tigri​s.org/source/browse/​current/trunk/curren​t/admin/subscribe.py​?view=diff&rev=2​67&p1=trunk/curr​ent/admin/subscribe.​py&p2=trunk/curr​ent/admin/subscribe.​py&r1=266&r2​=267
====================​====================​====================​==================
--- trunk/current/admin/​subscribe.py (original)
+++ trunk/current/admin/​subscribe.py 2006-08-28 15:15:48-0700
@@ -6,7 +6,7 @@
 
     shortHelp = "Subscribe a system to a channel."
 
- def run(self, server, argv):
+ def run(self, server, session, argv):
         usage = "usage: %prog subscribe -u <uuid> -l <label>"
         parser = self.defaultParser(usage)
         (opts, leftargs) = parser.parse_args(argv)

Modified: trunk/current/admin/test.py
Url: http://current.tigri​s.org/source/browse/​current/trunk/curren​t/admin/test.py?view​=diff&rev=267​&p1=trunk/current/a​dmin/test.py&p2=​trunk/current/admin/​test.py&r1=266​&r2=267
====================​====================​====================​==================
--- trunk/current/admin/test.py (original)
+++ trunk/current/admin/test.py 2006-08-28 15:15:48-0700
@@ -6,7 +6,7 @@
 
     shortHelp = "Test connection to Current server."
 
- def run(self, server, argv):
+ def run(self, server, session, argv):
         result = xmlrpc.doCall(server​.cadmin.status)
         pprint.pprint(result)
     

Modified: trunk/current/admin/​unsubscribe.py
Url: http://current.tigri​s.org/source/browse/​current/trunk/curren​t/admin/unsubscribe.​py?view=diff&rev​=267&p1=trunk/cu​rrent/admin/unsubscr​ibe.py&p2=trunk/​current/admin/unsubs​cribe.py&r1=266​&r2=267
====================​====================​====================​==================
--- trunk/current/admin/​unsubscribe.py (original)
+++ trunk/current/admin/​unsubscribe.py 2006-08-28 15:15:48-0700
@@ -6,7 +6,7 @@
 
     shortHelp = "Unsubscribe a system to a channel."
 
- def run(self, server, argv):
+ def run(self, server, session, argv):
         usage = "usage: %prog unsubscribe -u <uuid> -l <label>"
         parser = self.defaultParser(usage)
         (opts, leftargs) = parser.parse_args(argv)

Modified: trunk/current/api/cadmin.py
Url: http://current.tigri​s.org/source/browse/​current/trunk/curren​t/api/cadmin.py?view​=diff&rev=267​&p1=trunk/current/a​pi/cadmin.py&p2=​trunk/current/api/ca​dmin.py&r1=266​&r2=267
====================​====================​====================​==================
--- trunk/current/api/cadmin.py (original)
+++ trunk/current/api/cadmin.py 2006-08-28 15:15:48-0700
@@ -15,15 +15,19 @@
 import string
 import sys
 import pprint
+import xmlrpclib
 
 from current.logger import *
-from current.sessions import Session
 from current.exception import *
+from current.users import SessionUser
 from current import profiles
 from current import channels
 from current import auth
 from current import configure
 
+# Failed Auth error code
+EAUTH = 17
+
 # Special array of exported functionality.
 # Idea stolen from up2date/getMethod.py
 __current_api__ = [
@@ -41,16 +45,20 @@
 def login(username, password):
     # This is over SSL, right? RIGHT?
 
- # la-la-la lookin' user up in DB la-la-la
+ u = SessionUser()
+ sessid = u.login(username, password)
 
- sess = Session()
- sess['userid'] = username
- sess.save()
+ if sessid == None:
+ return ""
+ else:
+ return sessid
 
- return sess.sid
 
+def scanChannels(sess, chanlist):
+ u = SessionUser(sess)
+ if not u.isValid():
+ return xmlrpclib.Fault(EAUTH, "Bad session. Please login.")
 
-def scanChannels(chanlist):
     result = {}
     logfunc(locals())
     chanlib = channels.Channels()
@@ -64,7 +72,11 @@
 
     return result
 
-def createChannel(channel):
+def createChannel(sess, channel):
+ u = SessionUser(sess)
+ if not u.isValid():
+ return xmlrpclib.Fault(EAUTH, "Bad session. Please login.")
+
     result = {}
     logfunc(locals())
     chanlib = channels.Channels()
@@ -78,7 +90,11 @@
 
     return result
 
-def addChannelPath(channel):
+def addChannelPath(sess, channel):
+ u = SessionUser(sess)
+ if not u.isValid():
+ return xmlrpclib.Fault(EAUTH, "Bad session. Please login.")
+
     result = {}
     logfunc(locals())
     chanlib = channels.Channels()
@@ -114,14 +130,22 @@
 
     return status
 
-def deleteSystem(uuid):
+def deleteSystem(sess, uuid):
+ u = SessionUser(sess)
+ if not u.isValid():
+ return xmlrpclib.Fault(EAUTH, "Bad session. Please login.")
+
     # Remove the related system profile
     p = profiles.Profile(uuid)
     p.delete()
     
     return True
 
-def unsubscribe(uuid, channel):
+def unsubscribe(sess, uuid, channel):
+ u = SessionUser(sess)
+ if not u.isValid():
+ return xmlrpclib.Fault(EAUTH, "Bad session. Please login.")
+
     # Subscribe the system identifyed by uuid to the given textual channel
     # label
     p = profiles.Profile(uuid)
@@ -129,7 +153,11 @@
     
     return True
 
-def subscribe(uuid, channel):
+def subscribe(sess, uuid, channel):
+ u = SessionUser(sess)
+ if not u.isValid():
+ return xmlrpclib.Fault(EAUTH, "Bad session. Please login.")
+
     # Subscribe the system identifyed by uuid to the given textual channel
     # label
     p = profiles.Profile(uuid)
@@ -137,7 +165,11 @@
     
     return True
 
-def findProfile():
+def findProfile(sess):
+ u = SessionUser(sess)
+ if not u.isValid():
+ return xmlrpclib.Fault(EAUTH, "Bad session. Please login.")
+
     # Return UUIDs of systems with matching profile name
     # XXX: a regex or something?
     systems = profiles.Systems()

Modified: trunk/current/api/re​gistration.py
Url: http://current.tigri​s.org/source/browse/​current/trunk/curren​t/api/registration.p​y?view=diff&rev=​267&p1=trunk/cur​rent/api/registratio​n.py&p2=trunk/cu​rrent/api/registrati​on.py&r1=266​&r2=267
====================​====================​====================​==================
--- trunk/current/api/re​gistration.py (original)
+++ trunk/current/api/re​gistration.py 2006-08-28 15:15:48-0700
@@ -57,7 +57,7 @@
 def reserve_user(username, password):
 
     try:
- u = users.Users(username)
+ u = users.User(username)
     except CurrentException, e:
         return xmlrpclib.False
 
@@ -79,7 +79,7 @@
 
     # see if the user exits. If so, do nothing if passwd match
     try:
- u = users.Users(username)
+ u = users.User(username)
         if u.isValid(password):
             return 0
         else:
@@ -87,7 +87,7 @@
     except CurrentException, e:
         pass
 
- u = users.Users()
+ u = users.User()
     u.newUser(username, password, email_address)
     return 0
     
@@ -103,7 +103,7 @@
     # password fields OR a "token" field that will contain the activationkey
 
     try:
- u = users.Users(system_d​ict['username'])
+ u = users.User(system_di​ct['username'])
     except CurrentException, e:
         return xmlrpclib.Fault(60,'User unknown')
     if not u.isValid(system_dic​t['password']):
@@ -156,7 +156,7 @@
 
     # locate user
     try:
- u = users.Users(p.user_id)
+ u = users.User(p.user_id)
     except CurrentException, e:
         log("Fault! Profile does not refer to a valid user", VERBOSE)
         log("Error: %s" % str(e), VERBOSE)

Modified: trunk/current/cadmin.py
Url: http://current.tigri​s.org/source/browse/​current/trunk/curren​t/cadmin.py?view=dif​f&rev=267&p1​=trunk/current/cadmi​n.py&p2=trunk/cu​rrent/cadmin.py&​r1=266&r2=267
====================​====================​====================​==================
--- trunk/current/cadmin.py (original)
+++ trunk/current/cadmin.py 2006-08-28 15:15:48-0700
@@ -30,6 +30,7 @@
 # Our modules
 from current import admin
 from current.exception import CurrentRPCError
+from current.configure import Preferences # Does this work on machine w/o C
 
 def getServer(url=""):
     if url == "" and not os.access("/etc/sysc​onfig/rhn/up2date", os.R_OK):
@@ -85,6 +86,8 @@
     log.info("CADMIN - Current Administration Text Interface")
     log.info("Licensed under the GNU GPL version 2.0 or greater.")
 
+ prefs = Preferences()
+
     cadminOpts, command, commandOpts = getArguments()
     usage = "usage: %prog [options] COMMAND [options] [arguments]\n"
     usage = usage + commandSummary()
@@ -113,17 +116,20 @@
         sys.exit()
     
     print
+ session = prefs.getLogin()
     server = getServer(opts.server)
     module = admin.modules[command]
 
     try:
         log.debug("Running module: %s, %s" % (command, module))
- ret = module.run(server, commandOpts)
+ ret = module.run(server, session, commandOpts)
         log.debug("Module returned: %s" % ret)
     except CurrentRPCError, e:
         print "An error occured. The error message is:"
         print str(e)
 
+ if command == "login":
+ prefs.setLogin(ret)
 
 if __name__ == '__main__':
     try:

Modified: trunk/current/exception.py
Url: http://current.tigri​s.org/source/browse/​current/trunk/curren​t/exception.py?view=​diff&rev=267​&p1=trunk/current/ex​ception.py&p2=tr​unk/current/exceptio​n.py&r1=266&​r2=267
====================​====================​====================​==================
--- trunk/current/exception.py (original)
+++ trunk/current/exception.py 2006-08-28 15:15:48-0700
@@ -19,3 +19,6 @@
 
 class ConfigurationError(C​urrentException):
     pass
+
+class CurrentUser(CurrentException):
+ pass

Modified: trunk/current/sessions.py
Url: http://current.tigri​s.org/source/browse/​current/trunk/curren​t/sessions.py?view=d​iff&rev=267&​p1=trunk/current/ses​sions.py&p2=trun​k/current/sessions.p​y&r1=266&r2=​267
====================​====================​====================​==================
--- trunk/current/sessions.py (original)
+++ trunk/current/sessions.py 2006-08-28 15:15:48-0700
@@ -21,7 +21,7 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-import md5
+import sha
 import time
 import random
 import pickle
@@ -65,7 +65,7 @@
         self.createTime = time.time()
         self.timeOut = self.createTime + 3600 # default time out 1 hr
         s = "Current-%s-%s" % (str(random.random()), str(secret))
- self.sid = md5.new(s).hexdigest()
+ self.sid = sha.new(s).hexdigest()
         self.is_new = True
         
                                                         

Modified: trunk/current/users.py
Url: http://current.tigri​s.org/source/browse/​current/trunk/curren​t/users.py?view=diff​&rev=267&p1=​trunk/current/users.​py&p2=trunk/curr​ent/users.py&r1=​266&r2=267
====================​====================​====================​==================
--- trunk/current/users.py (original)
+++ trunk/current/users.py 2006-08-28 15:15:48-0700
@@ -21,15 +21,19 @@
 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
 import sha
+from current.sessions import Session
 from current.exception import *
 from current.logger import *
 from current.db import users
+from current import configure
 
-class Users(object):
+class User(object):
 
     def __init__(self, user=None):
         self.db = users.UserDB()
         self.pid = None
+ self.username = None
+ self.password = None
         
         if user == None:
             # New object
@@ -38,7 +42,7 @@
         if type(user) == str:
             self.pid = self.db.getUserID(user)
             if self.pid == None:
- raise CurrentException("No user found: %s" % user)
+ raise CurrentUser("No user found: %s" % user)
         else:
             self.pid = user
 
@@ -54,26 +58,35 @@
         (self.username, self.password, self.email) = tup
             
     def __sanity(self):
- if self.pid is None:
- raise CurrentExeception("Cannot delete unknown user.")
+ if self.pid == None:
+ raise CurrentUser("User object is not initialized.")
+
+ def _makePasswd(self, clear):
+ s = "Current-%s-%s" % (configure.config['s​erver_secret'], clear)
+ crypt = sha.new(s).hexdigest()
+ del s
+ return crypt
 
     def isValid(self, password):
         """Check if this combo exists in the database."""
 
- return self.password == sha.new(password).hexdigest()
+ self.__sanity()
+ return self.password == self._makePasswd(password)
 
     def newUser(self, username, password, email):
         """Create a new user """
 
         if self.pid != None:
- raise CurrentException("User object already contains user.")
+ raise CurrentUser("User object already contains user.")
         
- self.pid = self.db.addUser(username, sha.new(password).hexdigest(), email)
+ self.pid = self.db.addUser(username, self._makePasswd(password),
+ email)
         self.__load()
        
     def addInfo(self, product_info):
         """Add contact information to the user"""
 
+ self.__sanity()
         self.db.addInfo(self.pid, product_info)
 
     def delete(self):
@@ -83,3 +96,46 @@
         
         self.db.delete(self.pid)
         self.pid = None
+
+
+class SessionUser(User):
+
+ def __init__(self, sess=None):
+ self.session = Session(sess, configure.config['se​rver_secret'])
+
+ if self.isValid():
+ User.__init__(self, self.session['pid'])
+ else:
+ User.__init__(self)
+
+ def __sanity(self):
+ if not self.isValid():
+ raise CurrentUser("Expired user session. Please log in.")
+
+ User.__sanity(self)
+
+ def isValid(self):
+ if self.session.isValid() and not self.session.isNew():
+ return True
+ else:
+ return False
+
+ def login(self, user, password):
+ if not self.session.isNew():
+ log(WARNING, "BUG: Attempt to reuse SessionUser object")
+ raise CurrentUser("BUG: Attempt to reuse SessionUser object")
+
+ self.pid = self.db.getUserID(user)
+ if self.pid == None:
+ log(DEBUG, "Tried to lookup non-existant user id")
+ return None
+
+ self.__load()
+
+ if User.isValid(self, password):
+ self.session['pid'] = self.pid
+ self.session['userid'] = self.username
+ self.session.is_new = False
+ return self.session.sid
+ else:
+ return None

« Previous message in topic | 1 of 1 | Next message in topic »

Messages

Show all messages in topic

svn commit: r267 - trunk/current: . admin api jjneely Jack Neely 2006-08-28 15:15:48 PDT
Messages per page: