feature(ssh): change ssh settings from graphql

This commit is contained in:
Houkime 2023-12-15 09:46:59 +00:00
parent 980d3622e8
commit 1b520a8093
2 changed files with 70 additions and 0 deletions

View file

@ -9,6 +9,7 @@ from selfprivacy_api.graphql.mutations.mutation_interface import (
) )
import selfprivacy_api.actions.system as system_actions import selfprivacy_api.actions.system as system_actions
import selfprivacy_api.actions.ssh as ssh_actions
@strawberry.type @strawberry.type
@ -26,6 +27,22 @@ class AutoUpgradeSettingsMutationReturn(MutationReturnInterface):
allowReboot: bool allowReboot: bool
@strawberry.type
class SSHSettingsMutationReturn(MutationReturnInterface):
"""A return type for after changing SSH settings"""
enable: bool
password_authentication: bool
@strawberry.input
class SSHSettingsInput:
"""Input type for SSH settings"""
enable: bool
password_authentication: bool
@strawberry.input @strawberry.input
class AutoUpgradeSettingsInput: class AutoUpgradeSettingsInput:
"""Input type for auto upgrade settings""" """Input type for auto upgrade settings"""
@ -76,6 +93,26 @@ class SystemMutations:
allowReboot=new_settings.allowReboot, allowReboot=new_settings.allowReboot,
) )
@strawberry.mutation(permission_classes=[IsAuthenticated])
def change_ssh_settings(
self, settings: SSHSettingsInput
) -> SSHSettingsMutationReturn:
"""Change ssh settings of the server."""
ssh_actions.set_ssh_settings(
enable=settings.enable,
password_authentication=settings.password_authentication,
)
new_settings = ssh_actions.get_ssh_settings()
return SSHSettingsMutationReturn(
success=True,
message="SSH settings changed",
code=200,
enable=new_settings.enable,
password_authentication=new_settings.passwordAuthentication,
)
@strawberry.mutation(permission_classes=[IsAuthenticated]) @strawberry.mutation(permission_classes=[IsAuthenticated])
def run_system_rebuild(self) -> GenericMutationReturn: def run_system_rebuild(self) -> GenericMutationReturn:
system_actions.rebuild_system() system_actions.rebuild_system()

View file

@ -4,6 +4,7 @@ import pytest
from tests.common import read_json from tests.common import read_json
from tests.test_graphql.common import assert_empty from tests.test_graphql.common import assert_empty
from selfprivacy_api.graphql.mutations.system_mutations import SystemMutations
class ProcessMock: class ProcessMock:
@ -59,6 +60,38 @@ mutation addSshKey($sshInput: SshMutationInput!) {
} }
""" """
API_SET_SSH_SETTINGS = """
mutation enableSsh($sshInput: SSHSettingsInput!) {
system {
changeSshSettings(sshInput: $sshInput) {
success
message
code
enable
password_authentication
}
}
}
"""
def test_graphql_change_ssh_settings_unauthorized(
client, some_users, mock_subprocess_popen
):
response = client.post(
"/graphql",
json={
"query": API_SET_SSH_SETTINGS,
"variables": {
"sshInput": {
"enable": True,
"passwordAuthentication": True,
},
},
},
)
assert_empty(response)
def test_graphql_add_ssh_key_unauthorized(client, some_users, mock_subprocess_popen): def test_graphql_add_ssh_key_unauthorized(client, some_users, mock_subprocess_popen):
response = client.post( response = client.post(