Example #1
0
    def _apply_env_config(self):
        """
        This method checks the environment variables for any OKTA
        configuration parameters and applies them if available.
        """
        # Flatten current config and join with underscores
        # (for environment variable format)
        flattened_config = FlatDict(self._config, delimiter='_')
        flattened_keys = flattened_config.keys()

        # Create empty result config and populate
        updated_config = FlatDict({}, delimiter='_')

        # Go through keys and search for it in the environment vars
        # using the format described in the README
        for key in flattened_keys:
            env_key = ConfigSetter._OKTA + "_" + key.upper()
            env_value = os.environ.get(env_key, None)

            if env_value is not None:
                # If value is found, add to config
                if "scopes" in env_key.lower():
                    updated_config[key] = env_value.split(',')
                else:
                    updated_config[key] = env_value
            # apply to current configuration
        self._apply_config(updated_config.as_dict())
def parse(config_file):
    config = {'config_modified_time': os.path.getmtime(config_file)}
    stream = open(config_file, 'r')
    config.update(yaml.load(stream))

    # Change the value of the $ref tags in the dictionary.
    config = FlatDict(config)
    ref = []

    for key in config:
        if '$ref' in key:
            config[key.replace(':$ref', '')] = config[config[key].replace(
                '#/', '').replace('/', ':')]
    config = config.as_dict()
    # print(config)
    stream.close()

    # Setup logging,
    level = logging.getLevelName(config['logging']['level'])

    logging.basicConfig(level=level)

    # Setup backoff logging for when we get URL errors.
    logging.getLogger('backoff').addHandler(logging.StreamHandler())
    logging.getLogger('backoff').setLevel(level)

    return config
    def _apply_config(self, new_config: dict):
        """This method applies a config dictionary to the current config,
           overwriting values and adding new entries (if present).

        Arguments:
            config {dict} -- A dictionary of client configuration details
        """
        # Update current configuration with new configuration
        # Flatten both dictionaries to account for nested dictionary values
        flat_current_client = FlatDict(self._config['client'], delimiter='_')
        flat_current_testing = FlatDict(self._config['testing'], delimiter='_')

        flat_new_client = FlatDict(new_config.get('client', {}), delimiter='_')
        flat_new_testing = FlatDict(
            new_config.get('testing', {}), delimiter='_')
        flat_current_client.update(flat_new_client)
        flat_current_testing.update(flat_new_testing)
        # Update values in current config and unflatten
        self._config = {'client': flat_current_client.as_dict(),
                        'testing': flat_current_testing.as_dict()}
Example #4
0
    def _prune_config(self, config):
        """
        This method cleans up the configuration object by removing fields
        with no value
        """
        # Flatten dictionary to account for nested dictionary
        flat_current_config = FlatDict(config, delimiter='_')
        # Iterate through keys and remove if value is still empty string
        for key in flat_current_config.keys():
            if flat_current_config.get(key) == '':
                del flat_current_config[key]

        return flat_current_config.as_dict()
    def process(self, config=None):
        if config is None:
            config = {}

        config = FlatDict(config, delimiter='_')
        environ_config = {}

        for key in config.keys():
            env_key = '_'.join([self.prefix, key.upper()])
            env_key = self.aliases.get(env_key, env_key)
            value = environ.get(env_key)

            if value:
                if isinstance(config[key], int):
                    value = int(value)

                environ_config[key] = value

        _extend_dict(config, environ_config)
        config = config.as_dict()

        return config