import repository from arizona
[raven.git] / lib / ravenlib / client / repoclient.py
1 import xmlrpclib
2 import base64
3 import sys
4
5 from ravenlib.modpythonapi.AuthenticatedClient import AuthenticatedClient
6
7 # for test_request_hash()
8 from sfa.trust.credential import Credential
9 from sfa.trust.gid import GID
10
11 class RepoClient(AuthenticatedClient):
12     ##
13     # Create a new GeniClient object.
14     #
15     # @param url is the url of the server
16     # @param key_file = private key file of client
17     # @param cert_file = x.509 cert containing the client's public key. This
18     #      could be a GID certificate, or any x.509 cert.
19
20     def __init__(self, url, key_file):
21         AuthenticatedClient.__init__(self, url, key_file)
22
23     def test_request_hash(self, cred_str, arglist, requestHash):
24         # Check to make sure the requestHash will work. This should never fail,
25         # assuming 'raven config' verified the credential and key went together.
26         cred = Credential(string = cred_str)
27         gid = cred.get_gid_caller()
28         key = gid.get_pubkey()
29         if not key.verify_string(str(arglist), requestHash):
30             print >> sys.stderr, "WARNING: Problem detected when testing the request hash; request is probably about to fail."
31             file("/tmp/repoclient_bad_payload", "w").write(str(arglist))
32             print >> sys.stderr, "Bad payload saved to /tmp/repoclient_bad_payload"
33
34     def upload_file(self, cred, file_name, file_bytes):
35         cred_str = self.credToStr(cred)
36         file_bytes = base64.b64encode(file_bytes)
37         requestHash = self.computeRequestHash([cred_str, file_name, file_bytes])
38         self.test_request_hash(cred_str, [cred_str, file_name, file_bytes], requestHash)
39         result = self.server.upload_file(cred_str, file_name, file_bytes, requestHash)
40         return result
41
42     def upload_legacy(self, cred, file_name, file_bytes):
43         cred_str = self.credToStr(cred)
44         file_bytes = base64.b64encode(file_bytes)
45         requestHash = self.computeRequestHash([cred_str, file_name, file_bytes])
46         result = self.server.upload_file(cred_str, file_name, file_bytes, requestHash)
47         return result
48
49