Ejemplo n.º 1
0
    def test_get_policy_resource_filename(self, mock_resource_get):
        mock_resource_get.return_value = "test-file"
        self.assertEqual(policyd.get_policy_resource_filename(), "test-file")
        mock_resource_get.assert_called_once_with(
            policyd.POLICYD_RESOURCE_NAME)

        # check that if an error is raised, that None is returned.
        def go_bang(*args):
            raise Exception("bang")

        mock_resource_get.side_effect = go_bang
        self.assertEqual(policyd.get_policy_resource_filename(), None)
Ejemplo n.º 2
0
def maybe_handle_policyd_override(openstack_release, hook):
    """Handle the use-policy-override config flag and resource file.

    This function checks that policy overrides are supported on this release,
    that the config flag is enabled, and then processes the resources, copies
    the package policies to the config area, loads the override files.  In the
    case where the config flag is false, it removes the policy overrides by
    deleting the config area policys.  Note that the template for
    `local_settings.py` controls where the horizon service actually reads the
    policies from.

    Note that for the 'config-changed' hook, the function is only interested in
    whether the config value of `use-policy-override` matches the current
    status of the policy overrides success file.  If it doesn't, either the
    config area policies are removed (i.e. False) or the policy overrides file
    is processed.

    :param openstack_release: The release of OpenStack installed.
    :type openstack_release: str
    :param hook: The hook name
    :type hook: str
    """
    log("Seeing if policyd overrides need doing", level=INFO)
    if not policyd.is_policyd_override_valid_on_this_release(
            openstack_release):
        log("... policy overrides not valid on this release: {}".format(
            openstack_release),
            level=INFO)
        return
    # if policy config is not set, then remove the entire directory
    _config = config()
    if not _config.get(policyd.POLICYD_CONFIG_NAME, False):
        _dir = policyd.policyd_dir_for('openstack-dashboard')
        if os.path.exists(_dir):
            log("... config is cleared, and removing {}".format(_dir), INFO)
            shutil.rmtree(_dir)
        else:
            log("... nothing to do", INFO)
        policyd.remove_policy_success_file()
        return
    # config-change and the policyd overrides have been performed just return
    if hook == "config-changed" and policyd.is_policy_success_file_set():
        log("... already setup, so skipping.", level=INFO)
        return
    # from now on it should succeed; if it doesn't then status line will show
    # broken.
    resource_filename = policyd.get_policy_resource_filename()
    restart = policyd.process_policy_resource_file(
        resource_filename,
        'openstack-dashboard',
        blacklist_paths=blacklist_policyd_paths(),
        preserve_topdir=True,
        preprocess_filename=policyd_preprocess_name,
        user='******',
        group='horizon')
    copy_conf_to_policyd()
    if restart:
        service('stop', 'apache2')
        service('start', 'apache2')
    log("Policy override processing complete.", level=INFO)