source: fedd/fedd_util.py @ 0b123ff

axis_examplecompt_changesinfo-opsversion-1.30version-2.00version-3.01version-3.02
Last change on this file since 0b123ff was 51cc9df, checked in by Ted Faber <faber@…>, 16 years ago

split fedid out

  • Property mode set to 100644
File size: 2.4 KB
Line 
1#!/usr/local/bin/python
2
3import logging
4
5from M2Crypto import SSL
6from fedid import fedid
7
8class fedd_ssl_context(SSL.Context):
9    """
10    Simple wrapper around an M2Crypto.SSL.Context to initialize it for fedd.
11    """
12    def __init__(self, my_cert, trusted_certs=None, password=None):
13        """
14        construct a fedd_ssl_context
15
16        @param my_cert: PEM file with my certificate in it
17        @param trusted_certs: PEM file with trusted certs in it (optional)
18        """
19        SSL.Context.__init__(self)
20
21        # load_cert takes a callback to get a password, not a password, so if
22        # the caller provided a password, this creates a nonce callback using a
23        # lambda form.
24        if password != None and not callable(password):
25            # This is cute.  password = lambda *args: password produces a
26            # function object that returns itself rather than one that returns
27            # the object itself.  This is because password is an object
28            # reference and after the assignment it's a lambda.  So we assign
29            # to a temp.
30            pwd = password
31            password =lambda *args: pwd
32
33        if password != None:
34            self.load_cert(my_cert, callback=password)
35        else:
36            self.load_cert(my_cert)
37        if trusted_certs != None: self.load_verify_locations(trusted_certs)
38        self.set_verify(SSL.verify_peer, 10)
39
40def pack_id(id):
41    """
42    Return a dictionary with the field name set by the id type.  Handy for
43    creating dictionaries to be converted to messages.
44    """
45    if isinstance(id, fedid): return { 'fedid': id }
46    elif id.startswith("http:") or id.startswith("https:"): return { 'uri': id }
47    else: return { 'localname': id}
48
49def unpack_id(id):
50    """return id as a type determined by the key"""
51    for k in ("localname", "fedid", "uri", "kerberosUsername"):
52        if id.has_key(k): return id[k]
53    return None
54
55def set_log_level(config, sect, log):
56    """ Set the logging level to the value passed in sect of config."""
57    # The getattr sleight of hand finds the logging level constant
58    # corrersponding to the string.  We're a little paranoid to avoid user
59    # mayhem.
60    if config.has_option(sect, "log_level"):
61        level_str = config.get(sect, "log_level")
62        try:
63            level = int(getattr(logging, level_str.upper(), -1))
64
65            if  logging.DEBUG <= level <= logging.CRITICAL:
66                log.setLevel(level)
67            else:
68                log.error("Bad experiment_log value: %s" % level_str)
69
70        except ValueError:
71            log.error("Bad experiment_log value: %s" % level_str)
72
Note: See TracBrowser for help on using the repository browser.