b27d99d7351417591a06e84757f875836503a086
[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, logger, url=None, platform=None, username=None, password=None, ):
33         self._url=url
34         self._platform=platform
35         self._username=username
36         self._password=password
37         self.logger=logger
38         self._proxy=None
39
40     def username (self):
41         if not self._username:
42             self._username=raw_input("Enter your manifold username: ")
43         return self._username
44
45     def password (self):
46         if not self._password:
47             username=self.username()
48             self._password=getpass.getpass("Enter password for manifold user %s: "%username)
49         return self._password
50
51     def platform (self):
52         if not self._platform:
53             self._platform=raw_input("Enter your manifold platform [%s]: "%DEFAULT_PLATFORM)
54             if self._platform.strip()=="": self._platform = DEFAULT_PLATFORM
55         return self._platform
56
57     def url (self):
58         if not self._url:
59             self._url=raw_input("Enter the URL for your manifold API [%s]: "%DEFAULT_URL)
60             if self._url.strip()=="": self._url = DEFAULT_URL
61         return self._url
62
63     def prompt_all(self):
64         self.username(); self.password(); self.platform(); self.url()
65
66     def proxy (self):
67         if not self._proxy:
68             url=self.url()
69             self.logger.debug("Connecting manifold url %s"%url)
70             self._proxy = xmlrpclib.ServerProxy(url, allow_none = True)
71         return self._proxy
72
73     # does the job for one credential
74     # expects the credential (string) and an optional message for reporting
75     # return True upon success and False otherwise
76     def upload (self, delegated_credential, message=None):
77         platform=self.platform()
78         username=self.username()
79         password=self.password()
80         auth = {'AuthMethod': 'password', 'Username': username, 'AuthString': password}
81         if not message: message=""
82
83         try:
84             # looks like the current implementation of manifold server
85             # won't be happy with several calls issued in the same session
86 #            manifold=self.proxy()
87             url=self.url()
88             self.logger.debug("Connecting manifold url %s"%url)
89             manifold = xmlrpclib.ServerProxy(url, allow_none = True)
90             # the code for a V2 interface
91             query= { 'action':     'update',
92                      'object':     'local:account',
93                      'filters':    [ ['platform', '=', platform] ] ,
94                      'params':     {'credential': delegated_credential, },
95                      }
96             try:
97                 self.logger.debug("Trying v2 method Update@%s %s"%(platform,message))
98                 retcod2=manifold.Update (auth, query)
99             except Exception,e:
100                 # xxx we need a constant constant for UNKNOWN, how about using 1
101                 MANIFOLD_UNKNOWN=1
102                 retcod2={'code':MANIFOLD_UNKNOWN,'description':"%s"%e}
103             if retcod2['code']==0:
104                 info=""
105                 if message: info += message+" "
106                 info += 'v2 upload OK'
107                 self.logger.info(info)
108                 return True
109             #print delegated_credential, "upload failed,",retcod['description'], \
110             #    "with code",retcod['code']
111             # the code for V1
112             try:
113                 self.logger.debug("Trying v1 method AddCredential@%s %s"%(platform,message))
114                 retcod1=manifold.AddCredential(auth, delegated_credential, platform)
115             except Exception,e:
116                 retcod1=e
117             if retcod1==1:
118                 info=""
119                 if message: info += message+" "
120                 info += 'v1 upload OK'
121                 self.logger.info(message)
122                 return True
123             # everything has failed, let's report
124             if message: self.logger.error("Could not upload %s"%message)
125             else: self.logger.error("Could not upload credential")
126             if 'code' in retcod2 and 'description' in retcod2:
127                 self.logger.info("  V2 Update returned code %s and error >>%s<<"%(retcod2['code'],retcod2['description']))
128                 self.logger.debug("****** full retcod2")
129                 for (k,v) in retcod2.items(): self.logger.debug("**** %s: %s"%(k,v))
130             else:
131                 self.logger.info("  V2 Update returned %s"%retcod2)
132             self.logger.info("  V1 AddCredential returned code %s (expected 1)"%retcod1)
133             return False
134         except Exception, e:
135             if message: self.logger.error("Could not upload %s %s"%(message,e))
136             else:        self.logger.error("Could not upload credential %s"%e)
137             if self.logger.debugEnabled():
138                 import traceback
139                 traceback.print_exc()
140             return False
141
142 ### this is mainly for unit testing this class but can come in handy as well
143 def main ():
144     from argparse import ArgumentParser
145     parser = ArgumentParser (description="manifoldupoader simple tester.")
146     parser.add_argument ('credential_files',metavar='FILE',type=str,nargs='+',
147                          help="the filenames to upload")
148     parser.add_argument ('-u','--url',dest='url', action='store',default=None,
149                          help='the URL of the manifold API')
150     parser.add_argument ('-p','--platform',dest='platform',action='store',default=None,
151                          help='the manifold platform name')
152     parser.add_argument ('-U','--user',dest='username',action='store',default=None,
153                          help='the manifold username')
154     parser.add_argument ('-P','--password',dest='password',action='store',default=None,
155                          help='the manifold password')
156     parser.add_argument ('-v','--verbose',dest='verbose',action='count',default=0,
157                          help='more and more verbose')
158     args = parser.parse_args ()
159     
160     from sfa.util.sfalogging import sfi_logger
161     sfi_logger.enable_console()
162     sfi_logger.setLevelFromOptVerbose(args.verbose)
163     uploader = ManifoldUploader (url=args.url, platform=args.platform,
164                                  username=args.username, password=args.password,
165                                  logger=sfi_logger)
166
167     for filename in args.credential_files:
168         with file(filename) as f:
169             result=uploader.upload (f.read(),filename)
170             sfi_logger.info('... result=%s'%result)
171
172 if __name__ == '__main__':
173     main()