Merge branch 'master' of ssh://git.onelab.eu/git/sfa
[sfa.git] / sfa / client / manifolduploader.py
1 #!/usr/bin/env python
2 #
3 # inspired from tophat/bin/uploadcredential.py
4 #
5 # the purpose here is to let people upload their delegated credentials
6 # to a manifold/myslice infrastructure, without the need for having to
7 # install a separate tool; so duplicating this code is suboptimal in
8 # terms of code sharing but acceptable for hopefully easier use
9 #
10 # As of April 2013, manifold is moving from old-fashioned API known as
11 # v1, that offers an AddCredential API call, towards a new API v2 that
12 # manages credentials with the same set of Get/Update calls as other
13 # objects
14
15 # Since this code targets the future we favour v2, however in case
16 # this won't work the v1 way is attempted too
17 #
18
19 ## this for now points at demo.myslice.info, but sounds like a
20 ## better default for the long run
21 DEFAULT_URL = "http://myslice.onelab.eu:7080"
22 DEFAULT_PLATFORM = 'ple'
23
24 import xmlrpclib
25 import getpass
26
27 class ManifoldUploader:
28     """A utility class for uploading delegated credentials to a manifold/MySlice infrastructure"""
29
30     # platform is a name internal to the manifold deployment, 
31     # that maps to a testbed, like e.g. 'ple'
32     def __init__ (self, url=None, platform=None, username=None, password=None, debug=False):
33         self._url=url
34         self._platform=platform
35         self._username=username
36         self._password=password
37         self.debug=debug
38
39     def username (self):
40         if not self._username: 
41             self._username=raw_input("Enter your manifold username: ")
42         return self._username            
43
44     def password (self):
45         if not self._password: 
46             username=self.username()
47             self._password=getpass.getpass("Enter password for manifold user %s: "%username)
48         return self._password            
49
50     def platform (self):
51         if not self._platform: 
52             self._platform=raw_input("Enter your manifold platform [%s]: "%DEFAULT_PLATFORM)
53             if self._platform.strip()=="": self._platform = DEFAULT_PLATFORM
54         return self._platform            
55
56     def url (self):
57         if not self._url: 
58             self._url=raw_input("Enter the URL for your manifold API [%s]: "%DEFAULT_URL)
59             if self._url.strip()=="": self._url = DEFAULT_URL
60         return self._url            
61
62     # does the job for one credential
63     # expects the credential (string) and an optional filename (for messaging)
64     # return True upon success and False otherwise
65     def upload (self, delegated_credential, filename=None):
66         url=self.url()
67         platform=self.platform()
68         username=self.username()
69         password=self.password()
70         auth = {'AuthMethod': 'password', 'Username': username, 'AuthString': password}
71
72         try:
73             manifold = xmlrpclib.Server(url, allow_none = 1)
74             # the code for a V2 interface
75             query= { 'action':       'update',
76                      'fact_table':   'local:account',
77                      'filters':      [ ['platform', '=', platform] ] ,
78                      'params':       {'credential': delegated_credential, },
79                      }
80             try:
81                 retcod2=manifold.Update (auth, query)
82             except Exception,e:
83                 # xxx we need a constant constant for UNKNOWN, how about using 1
84                 MANIFOLD_UNKNOWN=1
85                 retcod2={'code':MANIFOLD_UNKNOWN,'output':"%s"%e}
86             if retcod2['code']==0:
87                 if filename: print filename,
88                 print 'v2 upload OK'
89                 return True
90             #print delegated_credential, "upload failed,",retcod['output'], \
91             #    "with code",retcod['code']
92             # the code for V1
93             try:
94                 retcod1=manifold.AddCredential(auth, delegated_credential, platform)
95             except Exception,e:
96                 retcod1=e
97             if retcod1==1:
98                 if filename: print filename,
99                 print 'v1 upload OK'
100                 return True
101             # everything has failed, let's report
102             if filename: print "Could not upload",filename
103             else: print "Could not upload credential"
104             print "  V2 Update returned code",retcod2['code'],"and error",retcod2['output']
105             print "  V1 AddCredential returned code",retcod1,"(expected 1)"
106             return False
107         except Exception, e:
108             if filename: print "Could not upload",filename,e
109             else: print "Could not upload credential",e
110             if self.debug:
111                 import traceback
112                 traceback.print_exc()
113
114 ### this is mainly for unit testing this class but can come in handy as well
115 def main ():
116     from argparse import ArgumentParser
117     parser = ArgumentParser (description="manifoldupoader simple tester.")
118     parser.add_argument ('credential_files',metavar='FILE',type=str,nargs='+',
119                          help="the filenames to upload")
120     parser.add_argument ('-u','--url',dest='url', action='store',default=None,
121                          help='the URL of the manifold API')
122     parser.add_argument ('-p','--platform',dest='platform',action='store',default=None,
123                          help='the manifold platform name')
124     parser.add_argument ('-U','--user',dest='username',action='store',default=None,
125                          help='the manifold username')
126     parser.add_argument ('-P','--password',dest='password',action='store',default=None,
127                          help='the manifold password')
128     parser.add_argument ('-d','--debug',dest='debug',action='store_true',default=False,
129                          help='turn on debug mode')
130     args = parser.parse_args ()
131     
132     uploader = ManifoldUploader (url=args.url, platform=args.platform,
133                                  username=args.username, password=args.password,
134                                  debug=args.debug)
135     for filename in args.credential_files:
136         with file(filename) as f:
137             result=uploader.upload (f.read(),filename)
138             if args.debug: print '... result',result
139
140 if __name__ == '__main__':
141     main()