Skip to content

Commit 654005a

Browse files
committed
[#518] updated rback tests
1 parent 2ef01d8 commit 654005a

File tree

1 file changed

+83
-18
lines changed

1 file changed

+83
-18
lines changed

tests/ca/test_rbac.py

Lines changed: 83 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,50 @@
1818
from src import srv_msg
1919
from src import srv_control
2020
from src.forge_cfg import world
21+
from src.protosupport.multi_protocol_functions import fabric_sudo_command
2122

2223
# note: a lot of reconfigures in all those tests could be done by config-set, this would make tests cleaner
2324
# but due to discovered bug https://gitlab.isc.org/isc-projects/kea/-/issues/2475 I choose to go with reconfigure
2425

2526

27+
def create_user_and_passowrd_file(user, password):
28+
"""create_user_and_passowrd_file Create a user and password file for the RBAC tests that are using basic authentication.
29+
Usulally basic authentication uses one file with username and password. Let's use separate files for each user
30+
in those tests.
31+
32+
:param user: The user to create in the file
33+
:type user: str
34+
:param password: The password to use
35+
:type password: str
36+
"""
37+
fabric_sudo_command(f'echo "{user}" > {os.path.join(world.f_cfg.get_share_path(), "kea-creds-tmp", user)}',
38+
hide_all=not world.f_cfg.forge_verbose)
39+
fabric_sudo_command(f'echo "{password}" > {os.path.join(world.f_cfg.get_share_path(), "kea-creds-tmp", f"{user}_password")}',
40+
hide_all=not world.f_cfg.forge_verbose)
41+
42+
if world.f_cfg.install_method != 'make':
43+
if world.server_system in ['alpine', 'redhat', 'fedora']:
44+
fabric_sudo_command(f'chown -R kea:kea {os.path.join(world.f_cfg.get_share_path(), "kea-creds-tmp")}',
45+
hide_all=not world.f_cfg.forge_verbose)
46+
else:
47+
fabric_sudo_command(f'chown -R _kea:_kea {os.path.join(world.f_cfg.get_share_path(), "kea-creds-tmp")}',
48+
hide_all=not world.f_cfg.forge_verbose)
49+
50+
51+
# fixture to remove authentication files after the test
52+
@pytest.fixture()
53+
def remove_authentication_files():
54+
"""remove_authentication_files Remove authentication files after the test.
55+
This fixture is used to create authentiaction directory and files before the test.
56+
It is used to avoid conflicts with other tests.
57+
"""
58+
fabric_sudo_command(f'mkdir -m 750 -p {os.path.join(world.f_cfg.get_share_path(), "kea-creds-tmp")}',
59+
hide_all=not world.f_cfg.forge_verbose)
60+
yield
61+
fabric_sudo_command(f'rm -rf {os.path.join(world.f_cfg.get_share_path(), "kea-creds-tmp")}',
62+
hide_all=not world.f_cfg.forge_verbose)
63+
64+
2665
@pytest.mark.v4
2766
@pytest.mark.v6
2867
@pytest.mark.ca
@@ -359,6 +398,7 @@ def test_rbac_remote_address(dhcp_version, tls):
359398
assert resp['text'] == 'Forbidden', f"text message from response should be 'Forbidden' it is {resp} instead."
360399

361400

401+
@pytest.mark.usefixtures('remove_authentication_files')
362402
@pytest.mark.v4
363403
@pytest.mark.v6
364404
@pytest.mark.ca
@@ -382,17 +422,22 @@ def test_rbac_basic_authentication(dhcp_version, tls):
382422

383423
misc.test_setup()
384424
srv_control.add_unix_socket()
425+
426+
create_user_and_passowrd_file("admin", "p@ssw0rd")
427+
create_user_and_passowrd_file("admin2", "p@ssw0rd")
428+
385429
auth = {"authentication": {
386430
"type": "basic",
431+
"directory": os.path.join(world.f_cfg.get_share_path(), "kea-creds-tmp"),
387432
"clients":
388433
[
389434
{
390-
"user": "admin",
391-
"password": "p@ssw0rd"
435+
"user-file": "admin",
436+
"password-file": "admin_password"
392437
},
393438
{
394-
"user": "admin2",
395-
"password": "p@ssw0rd"
439+
"user-file": "admin2",
440+
"password-file": "admin2_password"
396441
}
397442
]
398443
}}
@@ -523,22 +568,26 @@ def _preconfigure_test():
523568
misc.test_setup()
524569
srv_control.add_unix_socket()
525570

571+
create_user_and_passowrd_file("admin", "p@ssw0rd")
572+
create_user_and_passowrd_file("admin2", "p@ssw0rd")
573+
create_user_and_passowrd_file("admin3", "p@ssw0rd")
526574
auth = {
527575
"authentication": {
528576
"type": "basic",
577+
"directory": os.path.join(world.f_cfg.get_share_path(), "kea-creds-tmp"),
529578
"clients":
530579
[
531580
{
532-
"user": "admin",
533-
"password": "p@ssw0rd"
581+
"user-file": "admin",
582+
"password-file": "admin_password"
534583
},
535584
{
536-
"user": "admin2",
537-
"password": "p@ssw0rd"
585+
"user-file": "admin2",
586+
"password-file": "admin2_password"
538587
},
539588
{
540-
"user": "admin3",
541-
"password": "p@ssw0rd"
589+
"user-file": "admin3",
590+
"password-file": "admin3_password"
542591
}
543592
]}}
544593

@@ -586,6 +635,7 @@ def make_sure_file_is_correct():
586635
srv_msg.execute_shell_cmd(f"cp ~/dhcp-disable.json.bk {f}") # restore backup after the test
587636

588637

638+
@pytest.mark.usefixtures('remove_authentication_files')
589639
@pytest.mark.v4
590640
@pytest.mark.ca
591641
@pytest.mark.v6
@@ -655,6 +705,7 @@ def test_rbac_access_by_read_write(dhcp_version, make_sure_file_is_correct):
655705
_send_cmd({"command": "dhcp-disable", "arguments": {}}, result=403)
656706

657707

708+
@pytest.mark.usefixtures('remove_authentication_files')
658709
@pytest.mark.v4
659710
@pytest.mark.v6
660711
@pytest.mark.ca
@@ -707,6 +758,7 @@ def test_rbac_access_by_name_removed_file(dhcp_version, make_sure_file_is_correc
707758
srv_control.start_srv('CA' if world.f_cfg.control_agent else 'DHCP', 'started', should_succeed=False)
708759

709760

761+
@pytest.mark.usefixtures('remove_authentication_files')
710762
@pytest.mark.v4
711763
@pytest.mark.v6
712764
@pytest.mark.ca
@@ -792,6 +844,7 @@ def test_rbac_access_by_name_removed_file_2(dhcp_version, make_sure_file_is_corr
792844
_send_cmd({"command": "dhcp-disable", "arguments": {}}, user='admin3')
793845

794846

847+
@pytest.mark.usefixtures('remove_authentication_files')
795848
@pytest.mark.v4
796849
@pytest.mark.v6
797850
@pytest.mark.ca
@@ -915,6 +968,7 @@ def test_rbac_access_by_all_none(dhcp_version):
915968
_send_cmd({"command": i, "arguments": {}}, result=403)
916969

917970

971+
@pytest.mark.usefixtures('remove_authentication_files')
918972
@pytest.mark.v4
919973
@pytest.mark.v6
920974
@pytest.mark.ca
@@ -1037,6 +1091,7 @@ def test_rbac_access_by_commands_with_other_list(dhcp_version):
10371091
_send_cmd({"command": "lease4-get", "arguments": {}}, result=2)
10381092

10391093

1094+
@pytest.mark.usefixtures('remove_authentication_files')
10401095
@pytest.mark.v4
10411096
@pytest.mark.v6
10421097
@pytest.mark.ca
@@ -1102,6 +1157,7 @@ def test_rbac_filter_responses(dhcp_version):
11021157
"We should get smaller number of commands back in second response"
11031158

11041159

1160+
@pytest.mark.usefixtures('remove_authentication_files')
11051161
@pytest.mark.v4
11061162
@pytest.mark.v6
11071163
@pytest.mark.ca
@@ -1134,6 +1190,7 @@ def test_default_role(dhcp_version):
11341190
_send_cmd({"command": i, "arguments": {}}, result=403)
11351191

11361192

1193+
@pytest.mark.usefixtures('remove_authentication_files')
11371194
@pytest.mark.v4
11381195
@pytest.mark.v6
11391196
@pytest.mark.ca
@@ -1184,6 +1241,7 @@ def test_unknown_role(dhcp_version):
11841241
_send_cmd({"command": i, "arguments": {}}, result=403)
11851242

11861243

1244+
@pytest.mark.usefixtures('remove_authentication_files')
11871245
@pytest.mark.v4
11881246
@pytest.mark.v6
11891247
@pytest.mark.ca
@@ -1195,21 +1253,25 @@ def test_creating_access_list_for_multiple_use_cases(dhcp_version):
11951253
"""
11961254
misc.test_setup()
11971255
srv_control.add_unix_socket()
1256+
create_user_and_passowrd_file("admin", "p@ssw0rd")
1257+
create_user_and_passowrd_file("admin2", "p@ssw0rd")
1258+
create_user_and_passowrd_file("admin3", "p@ssw0rd")
11981259
auth = {"authentication": {
11991260
"type": "basic",
1261+
"directory": os.path.join(world.f_cfg.get_share_path(), "kea-creds-tmp"),
12001262
"clients":
12011263
[
12021264
{
1203-
"user": "admin",
1204-
"password": "p@ssw0rd"
1265+
"user-file": "admin",
1266+
"password-file": "admin_password"
12051267
},
12061268
{
1207-
"user": "admin2",
1208-
"password": "p@ssw0rd"
1269+
"user-file": "admin2",
1270+
"password-file": "admin2_password"
12091271
},
12101272
{
1211-
"user": "admin3",
1212-
"password": "p@ssw0rd"
1273+
"user-file": "admin3",
1274+
"password-file": "admin3_password"
12131275
}
12141276
]
12151277
}}
@@ -1291,6 +1353,7 @@ def test_creating_access_list_for_multiple_use_cases(dhcp_version):
12911353
_send_cmd({"command": i, "arguments": {}}, user='admin3', result=403)
12921354

12931355

1356+
@pytest.mark.usefixtures('remove_authentication_files')
12941357
@pytest.mark.v4
12951358
@pytest.mark.v6
12961359
@pytest.mark.ca
@@ -1302,13 +1365,15 @@ def test_mixed_roles(dhcp_version):
13021365
"""
13031366
misc.test_setup()
13041367
srv_control.add_unix_socket()
1368+
create_user_and_passowrd_file("admin", "p@ssw0rd")
13051369
auth = {"authentication": {
13061370
"type": "basic",
1371+
"directory": os.path.join(world.f_cfg.get_share_path(), "kea-creds-tmp"),
13071372
"clients":
13081373
[
13091374
{
1310-
"user": "admin",
1311-
"password": "p@ssw0rd"
1375+
"user-file": "admin",
1376+
"password-file": "admin_password"
13121377
}
13131378
]
13141379
}}

0 commit comments

Comments
 (0)