Beispiel #1
0
class ConfigurationManager(object):
    '''
    Application wide configuration manager
    '''
    def __init__(self):
        '''
        Create a config parser and load config from environment.
        '''
        # create config parser
        self.config = RawConfigParser()
        self.config.read(CONFIG_PATH)

    def get(self, section, name):
        '''
        Load parameter ``name`` from configuration, respecting priority order.
        Most of the time, ``section`` will correspond to the current api
        ``endpoint``. ``default`` section only contains ``endpoint`` and general
        configuration.

        :param str section: configuration section or region name. Ignored when
            looking in environment
        :param str name: configuration parameter to lookup
        '''
        # 1/ try env
        try:
            return os.environ['OVH_' + name.upper()]
        except KeyError:
            pass

        # 2/ try from specified section/endpoint
        try:
            return self.config.get(section, name)
        except (NoSectionError, NoOptionError):
            pass

        # not found, sorry
        return None

    async def read(self, config_file):
        # Read an other config file

        async with aiofile.AIOFile(config_file, 'r') as afp:
            self.config.read_string(await afp.read())
def generate_new_ssl_conf(options, orig_config_string, ca=False):
    """Take the original openssl.conf contents, make it readable to
    RawConfigParser, then edit it to allow for self-signed subjectAltName
    support per http://stackoverflow.com/a/21494483 .

    Returns the RawConfigParser.
    """
    config = RawConfigParser()
    # stop lowercasing key names!!
    config.optionxform = lambda option: option
    # add [default] section at the top to keep configparser from barfing
    config_string = "# Modified per http://stackoverflow.com/a/21494483\n" + \
                    "[ default ]\n{}".format(orig_config_string)
    if PYTHON2:
        with tempfile.TemporaryFile() as fh:
            fh.write(config_string)
            fh.seek(0)
            config.readfp(fh)
    else:
        config.read_string(config_string)

    log.debug(config.sections())

    # The section names barf without the spaces =\
    # Changes for subjectAltName:
    if not ca:
        config.set(' CA_default ', 'copy_extensions', r'copy')
        config.set(' v3_ca ', 'subjectAltName', r'$ENV::ALTNAME')
    # Changes for our own CA (http://stackoverflow.com/a/7770075):
    config.set(' CA_default ', 'dir', options.ca_dir)
    config.set(' CA_default ', 'certs', '$dir')
    config.set(' CA_default ', 'new_certs_dir', '$dir/ca.db.certs')
    config.set(' CA_default ', 'database', '$dir/ca.db.index')
    config.set(' CA_default ', 'serial', '$dir/ca.db.serial')
    config.set(' CA_default ', 'RANDFILE', '$dir/ca.db.rand')
    config.set(' CA_default ', 'certificate', '$dir/ca.crt')
    config.set(' CA_default ', 'private_key', '$dir/ca.key')
    config.set(' CA_default ', 'default_days', '365')
    config.set(' CA_default ', 'default_crl_days', '30')
    config.set(' CA_default ', 'default_md', 'md5')
    config.set(' CA_default ', 'preserve', 'no')
    config.set(' CA_default ', 'policy', 'policy_anything')
    return config
Beispiel #3
0
def generate_new_ssl_conf(options, orig_config_string, ca=False):
    """Take the original openssl.conf contents, make it readable to
    RawConfigParser, then edit it to allow for self-signed subjectAltName
    support per http://stackoverflow.com/a/21494483 .

    Returns the RawConfigParser.
    """
    config = RawConfigParser()
    # stop lowercasing key names!!
    config.optionxform = lambda option: option
    # add [default] section at the top to keep configparser from barfing
    config_string = "# Modified per http://stackoverflow.com/a/21494483\n" + \
                    "[ default ]\n{}".format(orig_config_string)
    if PYTHON2:
        with tempfile.TemporaryFile() as fh:
            fh.write(config_string)
            fh.seek(0)
            config.readfp(fh)
    else:
        config.read_string(config_string)

    log.debug(config.sections())

    # The section names barf without the spaces =\
    # Changes for subjectAltName:
    if not ca:
        config.set(' CA_default ', 'copy_extensions', r'copy')
        config.set(' v3_ca ', 'subjectAltName', r'$ENV::ALTNAME')
    # Changes for our own CA (http://stackoverflow.com/a/7770075):
    config.set(' CA_default ', 'dir', options.ca_dir)
    config.set(' CA_default ', 'certs', '$dir')
    config.set(' CA_default ', 'new_certs_dir', '$dir/ca.db.certs')
    config.set(' CA_default ', 'database', '$dir/ca.db.index')
    config.set(' CA_default ', 'serial', '$dir/ca.db.serial')
    config.set(' CA_default ', 'RANDFILE', '$dir/ca.db.rand')
    config.set(' CA_default ', 'certificate', '$dir/ca.crt')
    config.set(' CA_default ', 'private_key', '$dir/ca.key')
    config.set(' CA_default ', 'default_days', '365')
    config.set(' CA_default ', 'default_crl_days', '30')
    config.set(' CA_default ', 'default_md', 'md5')
    config.set(' CA_default ', 'preserve', 'no')
    config.set(' CA_default ', 'policy', 'policy_anything')
    return config