first rough version of a complete sfi myslice
authorThierry Parmentelat <thierry.parmentelat@inria.fr>
Wed, 22 May 2013 18:35:30 +0000 (20:35 +0200)
committerThierry Parmentelat <thierry.parmentelat@inria.fr>
Wed, 22 May 2013 18:35:30 +0000 (20:35 +0200)
not thoroughly tested yet, as none of my v2 APIs seem to respond for now

sfa/client/manifolduploader.py
sfa/client/sfi.py
sfa/util/sfalogging.py

index 0c9b1be..9df009a 100755 (executable)
@@ -29,12 +29,12 @@ class ManifoldUploader:
 
     # platform is a name internal to the manifold deployment, 
     # that maps to a testbed, like e.g. 'ple'
-    def __init__ (self, url=None, platform=None, username=None, password=None, debug=False):
+    def __init__ (self, logger, url=None, platform=None, username=None, password=None, ):
         self._url=url
         self._platform=platform
         self._username=username
         self._password=password
-        self.debug=debug
+        self.logger=logger
 
     def username (self):
         if not self._username: 
@@ -60,16 +60,18 @@ class ManifoldUploader:
         return self._url            
 
     # does the job for one credential
-    # expects the credential (string) and an optional filename (for messaging)
+    # expects the credential (string) and an optional message for reporting
     # return True upon success and False otherwise
-    def upload (self, delegated_credential, filename=None):
+    def upload (self, delegated_credential, message=None):
         url=self.url()
         platform=self.platform()
         username=self.username()
         password=self.password()
         auth = {'AuthMethod': 'password', 'Username': username, 'AuthString': password}
+        if not message: message=""
 
         try:
+            self.logger.debug("Connecting manifold url %s"%url)
             manifold = xmlrpclib.Server(url, allow_none = 1)
             # the code for a V2 interface
             query= { 'action':       'update',
@@ -78,36 +80,42 @@ class ManifoldUploader:
                      'params':       {'credential': delegated_credential, },
                      }
             try:
+                self.logger.debug("Trying v2 method Update %s"%message)
                 retcod2=manifold.Update (auth, query)
             except Exception,e:
                 # xxx we need a constant constant for UNKNOWN, how about using 1
                 MANIFOLD_UNKNOWN=1
                 retcod2={'code':MANIFOLD_UNKNOWN,'output':"%s"%e}
             if retcod2['code']==0:
-                if filename: print filename,
-                print 'v2 upload OK'
+                info=""
+                if message: info += message+" "
+                info += 'v2 upload OK'
+                self.logger.info(info)
                 return True
             #print delegated_credential, "upload failed,",retcod['output'], \
             #    "with code",retcod['code']
             # the code for V1
             try:
+                self.logger.debug("Trying v1 method AddCredential %s"%message)
                 retcod1=manifold.AddCredential(auth, delegated_credential, platform)
             except Exception,e:
                 retcod1=e
             if retcod1==1:
-                if filename: print filename,
-                print 'v1 upload OK'
+                info=""
+                if message: info += message+" "
+                info += 'v1 upload OK'
+                self.logger.info(message)
                 return True
             # everything has failed, let's report
-            if filename: print "Could not upload",filename
-            else: print "Could not upload credential"
-            print "  V2 Update returned code",retcod2['code'],"and error",retcod2['output']
-            print "  V1 AddCredential returned code",retcod1,"(expected 1)"
+            if message: self.logger.error("Could not upload %s"%message)
+            else: self.logger.error("Could not upload credential")
+            self.logger.info("  V2 Update returned code %s and error %s"%(retcod2['code'],retcod2['output']))
+            self.logger.info("  V1 AddCredential returned code %s (expected 1)"%retcod1)
             return False
         except Exception, e:
-            if filename: print "Could not upload",filename,e
-            else: print "Could not upload credential",e
-            if self.debug:
+            if message: self.logger.error("Could not upload %s %s"%(message,e))
+            else:        self.logger.error("Could not upload credential %s"%e)
+            if self.logger.debugEnabled():
                 import traceback
                 traceback.print_exc()
 
@@ -125,17 +133,16 @@ def main ():
                          help='the manifold username')
     parser.add_argument ('-P','--password',dest='password',action='store',default=None,
                          help='the manifold password')
-    parser.add_argument ('-d','--debug',dest='debug',action='store_true',default=False,
-                         help='turn on debug mode')
     args = parser.parse_args ()
     
+    from sfa.util.sfalogging import sfi_logger
     uploader = ManifoldUploader (url=args.url, platform=args.platform,
                                  username=args.username, password=args.password,
-                                 debug=args.debug)
+                                 logger=sfi_logger)
     for filename in args.credential_files:
         with file(filename) as f:
             result=uploader.upload (f.read(),filename)
-            if args.debug: print '... result',result
+            sfi_logger.info('... result=%s'%result)
 
 if __name__ == '__main__':
     main()
index 066bd9d..4712e80 100644 (file)
@@ -273,9 +273,11 @@ class Sfi:
         self.authority = None
         self.logger = sfi_logger
         self.logger.enable_console()
+        ### various auxiliary material that we keep at hand 
         self.command=None
         self.config=None
         self.config_file=None
+        self.client_bootstrap=None
 
     ### suitable if no reasonable command has been provided
     def print_commands_help (self, options):
@@ -405,7 +407,7 @@ class Sfi:
                              metavar="slice_hrn", help="delegate cred. for slice HRN")
            parser.add_option("-a", "--auths", dest='delegate_auths',action='append',default=[],
                              metavar='auth_hrn', help="delegate cred for auth HRN")
-           # this primarily is a shorthand for -a my_hrn^
+           # this primarily is a shorthand for -a my_hrn
            parser.add_option("-p", "--pi", dest='delegate_pi', default=None, action='store_true',
                              help="delegate your PI credentials, so s.t. like -a your_hrn^")
            parser.add_option("-A","--to-authority",dest='delegate_to_authority',action='store_true',default=False,
@@ -1393,7 +1395,7 @@ use this if you mean an authority instead""")
         self.logger.info("writing %s gid to %s" % (target_hrn, filename))
         GID(string=gid).save_to_file(filename)
          
-
+    ####################
     @register_command("to_hrn","""$ sfi delegate -u -p -s ple.inria.heartbeat -s ple.inria.omftest ple.upmc.slicebrowser
 
   will locally create a set of delegated credentials for the benefit of ple.upmc.slicebrowser
@@ -1458,21 +1460,26 @@ use this if you mean an authority instead""")
     ####################
     @register_command("","""$ less +/myslice sfi_config
 [myslice]
-backend  = 'http://manifold.pl.sophia.inria.fr:7080'
+backend  = http://manifold.pl.sophia.inria.fr:7080
 # the HRN that myslice uses, so that we are delegating to
-delegate = 'ple.upmc.slicebrowser'
+delegate = ple.upmc.slicebrowser
 # platform - this is a myslice concept
-platform = 'ple'
+platform = ple
 # username - as of this writing (May 2013) a simple login name
-user     = 'thierry'
+username = thierry
 
 $ sfi myslice
-
   will first collect the slices that you are part of, then make sure
   all your credentials are up-to-date (read: refresh expired ones)
   then compute delegated credentials for user 'ple.upmc.slicebrowser'
   and upload them all on myslice backend, using 'platform' and 'user'.
   A password will be prompted for the upload part.
+
+$ sfi -v myslice  -- or sfi -vv myslice
+  same but with more and more verbosity
+
+$ sfi m
+  is synonym to sfi myslice as no other command starts with an 'm'
 """
 ) # register_command
     def myslice (self, options, args):
@@ -1488,10 +1495,68 @@ $ sfi myslice
         if len(args)>0:
             self.print_help()
             sys.exit(1)
-        # ManifoldUploader
-        pass
 
-# Thierry: I'm turning this off, no idea what it's used for
+        ### the rough sketch goes like this
+        # (a) rain check for sufficient config in sfi_config
+        # we don't allow to override these settings for now
+        myslice_dict={}
+        myslice_keys=['backend', 'delegate', 'platform', 'username']
+        for key in myslice_keys:
+            full_key="MYSLICE_" + key.upper()
+            value=getattr(self.config,full_key,None)
+            if value:   myslice_dict[key]=value
+            else:       print "Unsufficient config, missing key %s in [myslice] section of sfi_config"%key
+        if len(myslice_dict) != len(myslice_keys):
+            sys.exit(1)
+
+        # (b) figure whether we are PI for the authority where we belong
+        sfi_logger.info("Resolving our own id")
+        my_records=self.registry().Resolve(self.user,self.my_credential_string)
+        if len(my_records)!=1: print "Cannot Resolve %s -- exiting"%self.user; sys.exit(1)
+        my_record=my_records[0]
+        sfi_logger.info("Checking for authorities that we are PI for")
+        my_auths = my_record['reg-pi-authorities']
+        sfi_logger.debug("Found %d authorities: %s"%(len(my_auths),my_auths))
+
+        # (c) get the set of slices that we are in
+        sfi_logger.info("Checking for slices that we are member of")
+        my_slices=my_record['reg-slices']
+        sfi_logger.debug("Found %d slices: %s"%(len(my_slices),my_slices))
+
+        # (d) make sure we have *valid* credentials for all these
+        hrn_credentials=[]
+        hrn_credentials.append ( (self.user, 'user', self.my_credential_string,) )
+        for auth_hrn in my_auths:
+            hrn_credentials.append ( (auth_hrn, 'auth', self.authority_credential_string(auth_hrn),) )
+        for slice_hrn in my_slices:
+            hrn_credentials.append ( (slice_hrn, 'slice', self.slice_credential_string (slice_hrn),) )
+
+        # (e) check for the delegated version of these
+        # xxx todo add an option -a/-A? like for 'sfi delegate' for when we ever 
+        # switch to myslice using an authority instead of a user
+        delegatee_type='user'
+        delegatee_hrn=myslice_dict['delegate']
+        hrn_delegated_credentials = [
+            (hrn, htype, self.client_bootstrap.delegate_credential_string (credential, delegatee_hrn, delegatee_type),)
+            for (hrn, htype, credential) in hrn_credentials ]
+
+        # (f) and finally upload them to manifold server
+        # xxx todo add an option so the password can be set on the command line
+        # (but *NOT* in the config file) so other apps can leverage this
+        uploader = ManifoldUploader (logger=sfi_logger,
+                                     url=myslice_dict['backend'],
+                                     platform=myslice_dict['platform'],
+                                     username=myslice_dict['username'])
+        for (hrn,htype,delegated_credential) in hrn_delegated_credentials:
+            sfi_logger.info("Uploading delegated credential for %s (%s)"%(hrn,htype))
+            uploader.upload(delegated_credential,message=hrn)
+        # at first I thought we would want to save these,
+        # like 'sfi delegate does' but on second thought
+        # it is probably not helpful as people would not
+        # need to run 'sfi delegate' at all anymore
+        return
+
+# Thierry: I'm turning this off as a command, no idea what it's used for
 #    @register_command("cred","")
     def trusted(self, options, args):
         """
index 495a274..61d76a6 100644 (file)
@@ -83,6 +83,9 @@ class _SfaLogger:
     def setLevelDebug(self):
         self.logger.setLevel(logging.DEBUG)
 
+    def debugEnabled (self):
+        return self.logger.getEffectiveLevel() == logging.DEBUG
+
     # define a verbose option with s/t like
     # parser.add_option("-v", "--verbose", action="count", dest="verbose", default=0)
     # and pass the coresponding options.verbose to this method to adjust level
@@ -96,6 +99,8 @@ class _SfaLogger:
     # in case some other code needs a boolean
     def getBoolVerboseFromOpt(self,verbose):
         return verbose>=1
+    def getBoolDebugFromOpt(self,verbose):
+        return verbose>=2
 
     ####################
     def info(self, msg):