selfprivacy-rest-api/selfprivacy_api/graphql/mutations/ssh_mutations.py

176 lines
6.2 KiB
Python
Raw Normal View History

2022-07-21 00:23:59 +03:00
#!/usr/bin/env python3
"""Users management module"""
# pylint: disable=too-few-public-methods
2022-07-22 13:33:32 +03:00
2022-07-21 00:23:59 +03:00
import strawberry
from selfprivacy_api.graphql import IsAuthenticated
from selfprivacy_api.graphql.common_types.user import User, UserMutationReturn, UserType
2022-07-22 13:33:32 +03:00
2022-07-21 00:23:59 +03:00
from selfprivacy_api.utils import (
WriteUserData,
ReadUserData,
get_user_by_username,
2022-07-21 00:23:59 +03:00
validate_ssh_public_key,
)
@strawberry.input
class SshMutationsInput:
"""Input type for ssh mutation"""
username: str
ssh_key: str
2022-07-21 00:23:59 +03:00
@strawberry.type
class SshMutations:
2022-07-21 00:23:59 +03:00
"""Mutations ssh"""
@strawberry.mutation(permission_classes=[IsAuthenticated])
2022-07-22 13:33:32 +03:00
def create_ssh(self, input: SshMutationsInput) -> UserMutationReturn:
2022-07-21 00:23:59 +03:00
"""Create a new ssh"""
2022-07-22 13:33:32 +03:00
if not validate_ssh_public_key(input.ssh_key):
return UserMutationReturn(
success=False,
message="Invalid key type. Only ssh-ed25519 and ssh-rsa are supported",
code=400,
user=get_user_by_username(input.username),
)
2022-07-22 13:33:32 +03:00
with WriteUserData() as data:
2022-07-21 00:23:59 +03:00
2022-07-22 13:33:32 +03:00
if input.username == data["username"]:
2022-07-21 00:23:59 +03:00
if "sshKeys" not in data:
data["sshKeys"] = []
# Return 409 if key already in array
for key in data["sshKeys"]:
if key == input.ssh_key:
2022-07-21 00:23:59 +03:00
return UserMutationReturn(
success=False,
2022-07-22 13:33:32 +03:00
message="Key already exists",
2022-07-21 00:23:59 +03:00
code=409,
user=get_user_by_username(input.username),
2022-07-21 00:23:59 +03:00
)
data["sshKeys"].append(input.ssh_key)
2022-07-21 00:23:59 +03:00
return UserMutationReturn(
success=True,
message="New SSH key successfully written",
code=201,
user=get_user_by_username(input.username),
2022-07-21 00:23:59 +03:00
)
if "users" not in data:
data["users"] = []
for user in data["users"]:
2022-07-22 13:33:32 +03:00
if user["username"] == input.username:
2022-07-21 00:23:59 +03:00
if "sshKeys" not in user:
user["sshKeys"] = []
# Return 409 if key already in array
for key in user["sshKeys"]:
if key == input.ssh_key:
2022-07-21 00:23:59 +03:00
return UserMutationReturn(
success=False,
2022-07-22 13:33:32 +03:00
message="Key already exists",
2022-07-21 00:23:59 +03:00
code=409,
user=get_user_by_username(input.username),
2022-07-21 00:23:59 +03:00
)
user["sshKeys"].append(input.ssh_key)
2022-07-21 00:23:59 +03:00
return UserMutationReturn(
success=True,
message="New SSH key successfully written",
code=201,
user=get_user_by_username(input.username),
2022-07-21 00:23:59 +03:00
)
return UserMutationReturn(
success=False,
2022-07-22 13:33:32 +03:00
message="User not found",
2022-07-21 00:23:59 +03:00
code=404,
user=None,
)
@strawberry.mutation(permission_classes=[IsAuthenticated])
2022-07-22 13:33:32 +03:00
def delete_ssh(self, input: SshMutationsInput) -> UserMutationReturn:
"""Delete ssh key from user"""
2022-07-21 00:23:59 +03:00
with WriteUserData() as data:
2022-07-22 13:33:32 +03:00
if input.username == "root":
2022-07-21 00:23:59 +03:00
if "ssh" not in data:
data["ssh"] = {}
if "rootKeys" not in data["ssh"]:
data["ssh"]["rootKeys"] = []
# Return 404 if key not in array
for key in data["ssh"]["rootKeys"]:
if key == input.ssh_key:
2022-07-21 00:23:59 +03:00
data["ssh"]["rootKeys"].remove(key)
2022-07-22 13:36:11 +03:00
2022-07-21 00:23:59 +03:00
return UserMutationReturn(
success=True,
message="SSH key deleted",
code=200,
user=get_user_by_username(input.username),
2022-07-21 00:23:59 +03:00
)
return UserMutationReturn(
success=False,
2022-07-22 13:33:32 +03:00
message="Key not found",
2022-07-21 00:23:59 +03:00
code=404,
user=get_user_by_username(input.username),
2022-07-21 00:23:59 +03:00
)
2022-07-22 13:33:32 +03:00
if input.username == data["username"]:
2022-07-21 00:23:59 +03:00
if "sshKeys" not in data:
data["sshKeys"] = []
# Return 404 if key not in array
for key in data["sshKeys"]:
if key == input.ssh_key:
2022-07-21 00:23:59 +03:00
data["sshKeys"].remove(key)
return UserMutationReturn(
success=True,
message="SSH key deleted",
code=200,
user=get_user_by_username(input.username),
2022-07-21 00:23:59 +03:00
)
return UserMutationReturn(
success=False,
2022-07-22 13:33:32 +03:00
message="Key not found",
2022-07-21 00:23:59 +03:00
code=404,
user=get_user_by_username(input.username),
2022-07-21 00:23:59 +03:00
)
if "users" not in data:
data["users"] = []
for user in data["users"]:
2022-07-22 13:33:32 +03:00
if user["username"] == input.username:
2022-07-21 00:23:59 +03:00
if "sshKeys" not in user:
user["sshKeys"] = []
# Return 404 if key not in array
for key in user["sshKeys"]:
if key == input.ssh_key:
2022-07-21 00:23:59 +03:00
user["sshKeys"].remove(key)
return UserMutationReturn(
success=True,
message="SSH key deleted",
code=200,
user=get_user_by_username(input.username),
2022-07-21 00:23:59 +03:00
)
return UserMutationReturn(
success=False,
2022-07-22 13:33:32 +03:00
message="Key not found",
2022-07-21 00:23:59 +03:00
code=404,
user=get_user_by_username(input.username),
2022-07-21 00:23:59 +03:00
)
return UserMutationReturn(
success=False,
2022-07-22 13:33:32 +03:00
message="User not found",
2022-07-21 00:23:59 +03:00
code=404,
user=None,
)