rename ssh input

pull/12/head
def 2022-07-29 17:21:00 +02:00
parent fb451e90a9
commit 973af08523
2 changed files with 43 additions and 43 deletions

View File

@ -17,7 +17,7 @@ from selfprivacy_api.utils import (
@strawberry.input @strawberry.input
class SshMutationsInput: class SshMutationInput:
"""Input type for ssh mutation""" """Input type for ssh mutation"""
username: str username: str
@ -29,65 +29,65 @@ class SshMutations:
"""Mutations ssh""" """Mutations ssh"""
@strawberry.mutation(permission_classes=[IsAuthenticated]) @strawberry.mutation(permission_classes=[IsAuthenticated])
def create_ssh(self, input: SshMutationsInput) -> UserMutationReturn: def create_ssh(self, ssh_input: SshMutationInput) -> UserMutationReturn:
"""Create a new ssh""" """Create a new ssh"""
if not validate_ssh_public_key(input.ssh_key): if not validate_ssh_public_key(ssh_input.ssh_key):
return UserMutationReturn( return UserMutationReturn(
success=False, success=False,
message="Invalid key type. Only ssh-ed25519 and ssh-rsa are supported", message="Invalid key type. Only ssh-ed25519 and ssh-rsa are supported",
code=400, code=400,
user=get_user_by_username(input.username), user=get_user_by_username(ssh_input.username),
) )
with WriteUserData() as data: with WriteUserData() as data:
if input.username == data["username"]: if ssh_input.username == data["username"]:
if "sshKeys" not in data: if "sshKeys" not in data:
data["sshKeys"] = [] data["sshKeys"] = []
# Return 409 if key already in array # Return 409 if key already in array
for key in data["sshKeys"]: for key in data["sshKeys"]:
if key == input.ssh_key: if key == ssh_input.ssh_key:
return UserMutationReturn( return UserMutationReturn(
success=False, success=False,
message="Key already exists", message="Key already exists",
code=409, code=409,
user=get_user_by_username(input.username), user=get_user_by_username(ssh_input.username),
) )
data["sshKeys"].append(input.ssh_key) data["sshKeys"].append(ssh_input.ssh_key)
return UserMutationReturn( return UserMutationReturn(
success=True, success=True,
message="New SSH key successfully written", message="New SSH key successfully written",
code=201, code=201,
user=get_user_by_username(input.username), user=get_user_by_username(ssh_input.username),
) )
if "users" not in data: if "users" not in data:
data["users"] = [] data["users"] = []
for user in data["users"]: for user in data["users"]:
if user["username"] == input.username: if user["username"] == ssh_input.username:
if "sshKeys" not in user: if "sshKeys" not in user:
user["sshKeys"] = [] user["sshKeys"] = []
# Return 409 if key already in array # Return 409 if key already in array
for key in user["sshKeys"]: for key in user["sshKeys"]:
if key == input.ssh_key: if key == ssh_input.ssh_key:
return UserMutationReturn( return UserMutationReturn(
success=False, success=False,
message="Key already exists", message="Key already exists",
code=409, code=409,
user=get_user_by_username(input.username), user=get_user_by_username(ssh_input.username),
) )
user["sshKeys"].append(input.ssh_key) user["sshKeys"].append(ssh_input.ssh_key)
return UserMutationReturn( return UserMutationReturn(
success=True, success=True,
message="New SSH key successfully written", message="New SSH key successfully written",
code=201, code=201,
user=get_user_by_username(input.username), user=get_user_by_username(ssh_input.username),
) )
return UserMutationReturn( return UserMutationReturn(
@ -98,74 +98,74 @@ class SshMutations:
) )
@strawberry.mutation(permission_classes=[IsAuthenticated]) @strawberry.mutation(permission_classes=[IsAuthenticated])
def delete_ssh(self, input: SshMutationsInput) -> UserMutationReturn: def delete_ssh(self, ssh_input: SshMutationInput) -> UserMutationReturn:
"""Delete ssh key from user""" """Delete ssh key from user"""
with WriteUserData() as data: with WriteUserData() as data:
if input.username == "root": if ssh_input.username == "root":
if "ssh" not in data: if "ssh" not in data:
data["ssh"] = {} data["ssh"] = {}
if "rootKeys" not in data["ssh"]: if "rootKeys" not in data["ssh"]:
data["ssh"]["rootKeys"] = [] data["ssh"]["rootKeys"] = []
# Return 404 if key not in array # Return 404 if key not in array
for key in data["ssh"]["rootKeys"]: for key in data["ssh"]["rootKeys"]:
if key == input.ssh_key: if key == ssh_input.ssh_key:
data["ssh"]["rootKeys"].remove(key) data["ssh"]["rootKeys"].remove(key)
return UserMutationReturn( return UserMutationReturn(
success=True, success=True,
message="SSH key deleted", message="SSH key deleted",
code=200, code=200,
user=get_user_by_username(input.username), user=get_user_by_username(ssh_input.username),
) )
return UserMutationReturn( return UserMutationReturn(
success=False, success=False,
message="Key not found", message="Key not found",
code=404, code=404,
user=get_user_by_username(input.username), user=get_user_by_username(ssh_input.username),
) )
if input.username == data["username"]: if ssh_input.username == data["username"]:
if "sshKeys" not in data: if "sshKeys" not in data:
data["sshKeys"] = [] data["sshKeys"] = []
# Return 404 if key not in array # Return 404 if key not in array
for key in data["sshKeys"]: for key in data["sshKeys"]:
if key == input.ssh_key: if key == ssh_input.ssh_key:
data["sshKeys"].remove(key) data["sshKeys"].remove(key)
return UserMutationReturn( return UserMutationReturn(
success=True, success=True,
message="SSH key deleted", message="SSH key deleted",
code=200, code=200,
user=get_user_by_username(input.username), user=get_user_by_username(ssh_input.username),
) )
return UserMutationReturn( return UserMutationReturn(
success=False, success=False,
message="Key not found", message="Key not found",
code=404, code=404,
user=get_user_by_username(input.username), user=get_user_by_username(ssh_input.username),
) )
if "users" not in data: if "users" not in data:
data["users"] = [] data["users"] = []
for user in data["users"]: for user in data["users"]:
if user["username"] == input.username: if user["username"] == ssh_input.username:
if "sshKeys" not in user: if "sshKeys" not in user:
user["sshKeys"] = [] user["sshKeys"] = []
# Return 404 if key not in array # Return 404 if key not in array
for key in user["sshKeys"]: for key in user["sshKeys"]:
if key == input.ssh_key: if key == ssh_input.ssh_key:
user["sshKeys"].remove(key) user["sshKeys"].remove(key)
return UserMutationReturn( return UserMutationReturn(
success=True, success=True,
message="SSH key deleted", message="SSH key deleted",
code=200, code=200,
user=get_user_by_username(input.username), user=get_user_by_username(ssh_input.username),
) )
return UserMutationReturn( return UserMutationReturn(
success=False, success=False,
message="Key not found", message="Key not found",
code=404, code=404,
user=get_user_by_username(input.username), user=get_user_by_username(ssh_input.username),
) )
return UserMutationReturn( return UserMutationReturn(

View File

@ -45,8 +45,8 @@ def some_users(mocker, datadir):
API_CREATE_SSH_MUTATION = """ API_CREATE_SSH_MUTATION = """
mutation createSsh($input: SshMutationsInput!) { mutation createSsh($sshInput: SshMutationInput!) {
createSsh(input: $input) { createSsh(sshInput: $sshInput) {
success success
message message
code code
@ -65,7 +65,7 @@ def test_graphql_add_ssh_unauthorized(client, some_users, mock_subprocess_popen)
json={ json={
"query": API_CREATE_SSH_MUTATION, "query": API_CREATE_SSH_MUTATION,
"variables": { "variables": {
"input": { "sshInput": {
"username": "user1", "username": "user1",
"sshKey": "ssh-rsa KEY test_key@pc", "sshKey": "ssh-rsa KEY test_key@pc",
}, },
@ -82,7 +82,7 @@ def test_graphql_add_ssh(authorized_client, some_users, mock_subprocess_popen):
json={ json={
"query": API_CREATE_SSH_MUTATION, "query": API_CREATE_SSH_MUTATION,
"variables": { "variables": {
"input": { "sshInput": {
"username": "user1", "username": "user1",
"sshKey": "ssh-rsa KEY test_key@pc", "sshKey": "ssh-rsa KEY test_key@pc",
}, },
@ -109,7 +109,7 @@ def test_graphql_add_root_ssh(authorized_client, some_users, mock_subprocess_pop
json={ json={
"query": API_CREATE_SSH_MUTATION, "query": API_CREATE_SSH_MUTATION,
"variables": { "variables": {
"input": { "sshInput": {
"username": "root", "username": "root",
"sshKey": "ssh-rsa KEY test_key@pc", "sshKey": "ssh-rsa KEY test_key@pc",
}, },
@ -136,7 +136,7 @@ def test_graphql_add_main_ssh(authorized_client, some_users, mock_subprocess_pop
json={ json={
"query": API_CREATE_SSH_MUTATION, "query": API_CREATE_SSH_MUTATION,
"variables": { "variables": {
"input": { "sshInput": {
"username": "tester", "username": "tester",
"sshKey": "ssh-rsa KEY test_key@pc", "sshKey": "ssh-rsa KEY test_key@pc",
}, },
@ -163,7 +163,7 @@ def test_graphql_add_bad_ssh(authorized_client, some_users, mock_subprocess_pope
json={ json={
"query": API_CREATE_SSH_MUTATION, "query": API_CREATE_SSH_MUTATION,
"variables": { "variables": {
"input": { "sshInput": {
"username": "user1", "username": "user1",
"sshKey": "trust me, this is the ssh key", "sshKey": "trust me, this is the ssh key",
}, },
@ -184,7 +184,7 @@ def test_graphql_add_ssh_404user(authorized_client, some_users, mock_subprocess_
json={ json={
"query": API_CREATE_SSH_MUTATION, "query": API_CREATE_SSH_MUTATION,
"variables": { "variables": {
"input": { "sshInput": {
"username": "user666", "username": "user666",
"sshKey": "ssh-rsa KEY test_key@pc", "sshKey": "ssh-rsa KEY test_key@pc",
}, },
@ -200,8 +200,8 @@ def test_graphql_add_ssh_404user(authorized_client, some_users, mock_subprocess_
API_DELETE_SSH_MUTATION = """ API_DELETE_SSH_MUTATION = """
mutation deleteSsh($input: SshMutationsInput!) { mutation deleteSsh($sshInput: SshMutationInput!) {
deleteSsh(input: $input) { deleteSsh(sshInput: $sshInput) {
success success
message message
code code
@ -220,7 +220,7 @@ def test_graphql_dell_ssh_unauthorized(client, some_users, mock_subprocess_popen
json={ json={
"query": API_DELETE_SSH_MUTATION, "query": API_DELETE_SSH_MUTATION,
"variables": { "variables": {
"input": { "sshInput": {
"username": "user1", "username": "user1",
"sshKey": "ssh-rsa KEY test_key@pc", "sshKey": "ssh-rsa KEY test_key@pc",
}, },
@ -237,7 +237,7 @@ def test_graphql_dell_ssh(authorized_client, some_users, mock_subprocess_popen):
json={ json={
"query": API_DELETE_SSH_MUTATION, "query": API_DELETE_SSH_MUTATION,
"variables": { "variables": {
"input": { "sshInput": {
"username": "user1", "username": "user1",
"sshKey": "ssh-rsa KEY user1@pc", "sshKey": "ssh-rsa KEY user1@pc",
}, },
@ -263,7 +263,7 @@ def test_graphql_dell_root_ssh(authorized_client, some_users, mock_subprocess_po
json={ json={
"query": API_DELETE_SSH_MUTATION, "query": API_DELETE_SSH_MUTATION,
"variables": { "variables": {
"input": { "sshInput": {
"username": "root", "username": "root",
"sshKey": "ssh-ed25519 KEY test@pc", "sshKey": "ssh-ed25519 KEY test@pc",
}, },
@ -287,7 +287,7 @@ def test_graphql_dell_main_ssh(authorized_client, some_users, mock_subprocess_po
json={ json={
"query": API_DELETE_SSH_MUTATION, "query": API_DELETE_SSH_MUTATION,
"variables": { "variables": {
"input": { "sshInput": {
"username": "tester", "username": "tester",
"sshKey": "ssh-rsa KEY tester@pc", "sshKey": "ssh-rsa KEY tester@pc",
}, },
@ -311,7 +311,7 @@ def test_graphql_dell_404ssh(authorized_client, some_users, mock_subprocess_pope
json={ json={
"query": API_DELETE_SSH_MUTATION, "query": API_DELETE_SSH_MUTATION,
"variables": { "variables": {
"input": { "sshInput": {
"username": "user1", "username": "user1",
"sshKey": "ssh-rsa KEY test_key@pc", "sshKey": "ssh-rsa KEY test_key@pc",
}, },
@ -332,7 +332,7 @@ def test_graphql_dell_ssh_404user(authorized_client, some_users, mock_subprocess
json={ json={
"query": API_DELETE_SSH_MUTATION, "query": API_DELETE_SSH_MUTATION,
"variables": { "variables": {
"input": { "sshInput": {
"username": "user666", "username": "user666",
"sshKey": "ssh-rsa KEY test_key@pc", "sshKey": "ssh-rsa KEY test_key@pc",
}, },