source: fedd/fedd/util.py @ f069052

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

Two changes. Get allow_any_CA checking to work (i.e., self signed certs or
certs signed by an unknown entity) and put more of the ZSI-dependent stuff into
the hidden parts or remote_services. Now those routines will find all the
relevant classes and part names from the naming conventions.

  • Property mode set to 100644
File size: 2.5 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
38        # If no trusted certificates are specified, allow unknown CAs.
39        if trusted_certs: 
40            self.load_verify_locations(trusted_certs)
41            self.set_verify(SSL.verify_peer, 10)
42        else:
43            self.set_verify(SSL.verify_peer, 10, 
44                callback=SSL.cb.ssl_verify_callback_allow_unknown_ca)
45
46def pack_id(id):
47    """
48    Return a dictionary with the field name set by the id type.  Handy for
49    creating dictionaries to be converted to messages.
50    """
51    if isinstance(id, fedid): return { 'fedid': id }
52    elif id.startswith("http:") or id.startswith("https:"): return { 'uri': id }
53    else: return { 'localname': id}
54
55def unpack_id(id):
56    """return id as a type determined by the key"""
57    for k in ("localname", "fedid", "uri", "kerberosUsername"):
58        if id.has_key(k): return id[k]
59    return None
60
61def set_log_level(config, sect, log):
62    """ Set the logging level to the value passed in sect of config."""
63    # The getattr sleight of hand finds the logging level constant
64    # corrersponding to the string.  We're a little paranoid to avoid user
65    # mayhem.
66    if config.has_option(sect, "log_level"):
67        level_str = config.get(sect, "log_level")
68        try:
69            level = int(getattr(logging, level_str.upper(), -1))
70
71            if  logging.DEBUG <= level <= logging.CRITICAL:
72                log.setLevel(level)
73            else:
74                log.error("Bad experiment_log value: %s" % level_str)
75
76        except ValueError:
77            log.error("Bad experiment_log value: %s" % level_str)
78
Note: See TracBrowser for help on using the repository browser.