Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the daemon config UI #909

Merged
merged 22 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions fapolicy_analyzer/glade/config_admin_page.glade
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<!--
Copyright Concurrent Technologies Corporation 2022

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
-->
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkPaned" id="configAdminPage">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkFrame">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="hexpand">False</property>
<property name="vexpand">False</property>
<property name="label_xalign">0</property>
<property name="shadow_type">none</property>
<child>
<object class="GtkBox" id="configContentArea">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="hexpand">False</property>
<property name="vexpand">False</property>
<property name="orientation">vertical</property>
<child>
<placeholder/>
</child>
</object>
</child>
<child type="label_item">
<placeholder/>
</child>
</object>
<packing>
<property name="resize">True</property>
<property name="shrink">True</property>
</packing>
</child>
<child>
<object class="GtkFrame" id="configStatusFrame">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label_xalign">0</property>
<property name="shadow_type">none</property>
<child>
<placeholder/>
</child>
<child type="label_item">
<placeholder/>
</child>
</object>
<packing>
<property name="resize">False</property>
<property name="shrink">True</property>
</packing>
</child>
</object>
</interface>
38 changes: 38 additions & 0 deletions fapolicy_analyzer/glade/config_text_view.glade
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<!--
Copyright Concurrent Technologies Corporation 2022

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
-->
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkScrolledWindow" id="configTextView">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="shadow_type">in</property>
<child>
<object class="GtkViewport">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkSourceView" id="configText">
<property name="visible">True</property>
<property name="can_focus">True</property>
</object>
</child>
</object>
</child>
</object>
</interface>
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@
-->
<interface>
<requires lib="gtk+" version="3.22"/>
<object class="GtkScrolledWindow" id="rulesTextView">
<object class="GtkScrolledWindow" id="editableTextView">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="shadow-type">in</property>
<property name="can_focus">True</property>
<property name="shadow_type">in</property>
<child>
<object class="GtkViewport" id="viewport">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="can_focus">False</property>
<child>
<object class="GtkSourceView" id="textView">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="can_focus">True</property>
<property name="editable">True</property>
<property name="monospace">True</property>
<property name="show_line_numbers">True</property>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.2 -->
<!-- Generated with glade 3.38.2 -->
<!--
Copyright Concurrent Technologies Corporation 2022

Expand All @@ -18,7 +18,7 @@
-->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkFrame" id="rulesStatusInfo">
<object class="GtkFrame" id="editorStatusInfo">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label_xalign">0</property>
Expand Down
10 changes: 10 additions & 0 deletions fapolicy_analyzer/glade/main_window.glade
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,16 @@
<signal name="activate" handler="on_fapdStartMenu_activate" swapped="no"/>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="configMenuItem">
<property name="label">Config</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
<signal name="activate" handler="on_configAdminMenu_activate" swapped="no"/>
</object>
</child>
</object>
</child>
</object>
Expand Down
4 changes: 4 additions & 0 deletions fapolicy_analyzer/tests/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ def mock_rule():
return MagicMock(id=1, text="Mock Rule Number 1", is_valid=True, info=[])


def mock_config():
return MagicMock(config_text="Config Text", is_valid=True)


class mock_System:
def ancillary_trust(self):
return [mock_trust()]
Expand Down
2 changes: 1 addition & 1 deletion fapolicy_analyzer/tests/rules/test_rules_admin_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def test_handles_rules_exception(mock_dispatch, mock_system_feature):
def test_populates_text_editor(mock_system_feature, mocker):
mock_text_renderer = MagicMock()
mocker.patch(
"fapolicy_analyzer.ui.rules.rules_admin_page.RulesTextView.render_rules",
"fapolicy_analyzer.ui.rules.rules_admin_page.RulesTextView.render_text",
mock_text_renderer,
)
mock_system_feature.on_next(
Expand Down
6 changes: 3 additions & 3 deletions fapolicy_analyzer/tests/rules/test_rules_text_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_creates_widget(widget):


def test_renders_rules(widget):
widget.render_rules("foo/")
widget.render_text("foo/")

textView = widget.get_object("textView")
textBuffer = textView.get_buffer()
Expand All @@ -50,10 +50,10 @@ def test_renders_rules(widget):

def test_handles_bad_language_file(mocker):
mocker.patch(
"fapolicy_analyzer.ui.rules.rules_text_view.resources.path",
"fapolicy_analyzer.ui.editable_text_view.resources.path",
return_value="bad path",
)
mock_logger = mocker.patch("fapolicy_analyzer.ui.rules.rules_text_view.logging")
mock_logger = mocker.patch("fapolicy_analyzer.ui.editable_text_view.logging")
widget = RulesTextView()
assert widget is not None
mock_logger.warning.assert_has_calls(
Expand Down
123 changes: 123 additions & 0 deletions fapolicy_analyzer/tests/test_config_admin_page.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# Copyright Concurrent Technologies Corporation 2023
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

import context # noqa: F401 # isort: skip
from unittest.mock import MagicMock

import gi
import pytest

from callee import Attrs, InstanceOf
from mocks import mock_System
from rx.subject import Subject

from fapolicy_analyzer.redux import Action
from fapolicy_analyzer.ui.actions import (
ADD_NOTIFICATION,
APPLY_CHANGESETS,
MODIFY_CONFIG_TEXT,
)
from fapolicy_analyzer.ui.config_admin_page import ConfigAdminPage
from fapolicy_analyzer.ui.store import init_store
from fapolicy_analyzer.ui.strings import (
CONFIG_TEXT_LOAD_ERROR,
)


gi.require_version("Gtk", "3.0")
from gi.repository import Gtk # isort: skip

initial_system = {
"config_text": MagicMock(error=None, loading=False, config_text=""),
"changesets": MagicMock(),
"system": MagicMock(),
}


@pytest.fixture()
def mock_dispatch(mocker):
return mocker.patch("fapolicy_analyzer.ui.config_admin_page.dispatch")


@pytest.fixture()
def mock_system_feature(mocker):
mockSystemFeature = Subject()
mocker.patch(
"fapolicy_analyzer.ui.config_admin_page.get_system_feature",
return_value=mockSystemFeature,
)
yield mockSystemFeature
mockSystemFeature.on_completed()


@pytest.fixture
def widget(mock_dispatch, mock_system_feature):
init_store(mock_System())
return ConfigAdminPage()


def test_creates_widget(widget):
assert type(widget.get_ref()) is Gtk.Paned


@pytest.mark.usefixtures("widget")
def test_populates_text_editor(mock_system_feature, mocker):
mock_text_renderer = MagicMock()
mocker.patch(
"fapolicy_analyzer.ui.config_admin_page.ConfigTextView.render_text",
mock_text_renderer,
)
mock_system_feature.on_next(
{
**initial_system,
"config_text": MagicMock(
error=None,
config_text="foo",
loading=False,
),
}
)
mock_text_renderer.assert_called_once_with("foo")


@pytest.mark.usefixtures("widget")
def test_handles_config_text_exception(mock_dispatch, mock_system_feature):
mock_dispatch.reset_mock()
mock_system_feature.on_next(
{
**initial_system,
"config_text": MagicMock(error="foo", loading=False),
}
)
mock_dispatch.assert_called_with(
InstanceOf(Action)
& Attrs(
type=ADD_NOTIFICATION,
payload=Attrs(text=CONFIG_TEXT_LOAD_ERROR),
)
)


def test_handles_config_text_change(widget, mock_dispatch):
widget._text_view.config_changed("new config")
mock_dispatch.assert_called_with(
InstanceOf(Action) & Attrs(type=MODIFY_CONFIG_TEXT, payload="new config")
)


def test_save_click_valid(widget, mock_dispatch):
widget._text_view.config_changed("allow perm=any all : all")
widget.on_save_clicked()
mock_dispatch.assert_called_with(InstanceOf(Action) & Attrs(type=APPLY_CHANGESETS))
22 changes: 22 additions & 0 deletions fapolicy_analyzer/ui/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@
MODIFY_RULES_TEXT = "MODIFY_RULES_TEXT"
ERROR_RULES_TEXT = "ERROR_RULES_TEXT"

REQUEST_CONFIG_TEXT = "REQUEST_CONFIG_TEXT"
RECEIVED_CONFIG_TEXT = "RECEIVED_CONFIG_TEXT"
MODIFY_CONFIG_TEXT = "MODIFY_CONFIG_TEXT"
ERROR_CONFIG_TEXT = "ERROR_CONFIG_TEXT"


PROFILING_INIT_EVENT = "PROFILING_INIT"
START_PROFILING_REQUEST = "START_PROFILING_REQUEST"
START_PROFILING_RESPONSE = "START_PROFILING_RESPONSE"
Expand Down Expand Up @@ -274,6 +280,22 @@ def error_rules_text(error: str) -> Action:
return _create_action(ERROR_RULES_TEXT, error)


def request_config_text() -> Action:
return _create_action(REQUEST_CONFIG_TEXT)


def received_config_text(config_text: str) -> Action:
return _create_action(RECEIVED_CONFIG_TEXT, config_text)


def modify_config_text(config_text: str) -> Action:
return _create_action(MODIFY_CONFIG_TEXT, config_text)


def error_config_text(error: str) -> Action:
return _create_action(ERROR_CONFIG_TEXT, error)


def profiler_init() -> Action:
return _create_action(PROFILING_INIT_EVENT)

Expand Down
Loading