rename ssh input
parent
fb451e90a9
commit
973af08523
|
@ -17,7 +17,7 @@ from selfprivacy_api.utils import (
|
|||
|
||||
|
||||
@strawberry.input
|
||||
class SshMutationsInput:
|
||||
class SshMutationInput:
|
||||
"""Input type for ssh mutation"""
|
||||
|
||||
username: str
|
||||
|
@ -29,65 +29,65 @@ class SshMutations:
|
|||
"""Mutations ssh"""
|
||||
|
||||
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
||||
def create_ssh(self, input: SshMutationsInput) -> UserMutationReturn:
|
||||
def create_ssh(self, ssh_input: SshMutationInput) -> UserMutationReturn:
|
||||
"""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(
|
||||
success=False,
|
||||
message="Invalid key type. Only ssh-ed25519 and ssh-rsa are supported",
|
||||
code=400,
|
||||
user=get_user_by_username(input.username),
|
||||
user=get_user_by_username(ssh_input.username),
|
||||
)
|
||||
|
||||
with WriteUserData() as data:
|
||||
|
||||
if input.username == data["username"]:
|
||||
if ssh_input.username == data["username"]:
|
||||
if "sshKeys" not in data:
|
||||
data["sshKeys"] = []
|
||||
# Return 409 if key already in array
|
||||
for key in data["sshKeys"]:
|
||||
if key == input.ssh_key:
|
||||
if key == ssh_input.ssh_key:
|
||||
|
||||
return UserMutationReturn(
|
||||
success=False,
|
||||
message="Key already exists",
|
||||
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(
|
||||
success=True,
|
||||
message="New SSH key successfully written",
|
||||
code=201,
|
||||
user=get_user_by_username(input.username),
|
||||
user=get_user_by_username(ssh_input.username),
|
||||
)
|
||||
|
||||
if "users" not in data:
|
||||
data["users"] = []
|
||||
for user in data["users"]:
|
||||
if user["username"] == input.username:
|
||||
if user["username"] == ssh_input.username:
|
||||
if "sshKeys" not in user:
|
||||
user["sshKeys"] = []
|
||||
# Return 409 if key already in array
|
||||
for key in user["sshKeys"]:
|
||||
if key == input.ssh_key:
|
||||
if key == ssh_input.ssh_key:
|
||||
|
||||
return UserMutationReturn(
|
||||
success=False,
|
||||
message="Key already exists",
|
||||
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(
|
||||
success=True,
|
||||
message="New SSH key successfully written",
|
||||
code=201,
|
||||
user=get_user_by_username(input.username),
|
||||
user=get_user_by_username(ssh_input.username),
|
||||
)
|
||||
|
||||
return UserMutationReturn(
|
||||
|
@ -98,74 +98,74 @@ class SshMutations:
|
|||
)
|
||||
|
||||
@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"""
|
||||
|
||||
with WriteUserData() as data:
|
||||
if input.username == "root":
|
||||
if ssh_input.username == "root":
|
||||
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:
|
||||
if key == ssh_input.ssh_key:
|
||||
data["ssh"]["rootKeys"].remove(key)
|
||||
|
||||
return UserMutationReturn(
|
||||
success=True,
|
||||
message="SSH key deleted",
|
||||
code=200,
|
||||
user=get_user_by_username(input.username),
|
||||
user=get_user_by_username(ssh_input.username),
|
||||
)
|
||||
return UserMutationReturn(
|
||||
success=False,
|
||||
message="Key not found",
|
||||
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:
|
||||
data["sshKeys"] = []
|
||||
# Return 404 if key not in array
|
||||
for key in data["sshKeys"]:
|
||||
if key == input.ssh_key:
|
||||
if key == ssh_input.ssh_key:
|
||||
data["sshKeys"].remove(key)
|
||||
return UserMutationReturn(
|
||||
success=True,
|
||||
message="SSH key deleted",
|
||||
code=200,
|
||||
user=get_user_by_username(input.username),
|
||||
user=get_user_by_username(ssh_input.username),
|
||||
)
|
||||
|
||||
return UserMutationReturn(
|
||||
success=False,
|
||||
message="Key not found",
|
||||
code=404,
|
||||
user=get_user_by_username(input.username),
|
||||
user=get_user_by_username(ssh_input.username),
|
||||
)
|
||||
if "users" not in data:
|
||||
data["users"] = []
|
||||
for user in data["users"]:
|
||||
if user["username"] == input.username:
|
||||
if user["username"] == ssh_input.username:
|
||||
if "sshKeys" not in user:
|
||||
user["sshKeys"] = []
|
||||
# Return 404 if key not in array
|
||||
for key in user["sshKeys"]:
|
||||
if key == input.ssh_key:
|
||||
if key == ssh_input.ssh_key:
|
||||
user["sshKeys"].remove(key)
|
||||
return UserMutationReturn(
|
||||
success=True,
|
||||
message="SSH key deleted",
|
||||
code=200,
|
||||
user=get_user_by_username(input.username),
|
||||
user=get_user_by_username(ssh_input.username),
|
||||
)
|
||||
|
||||
return UserMutationReturn(
|
||||
success=False,
|
||||
message="Key not found",
|
||||
code=404,
|
||||
user=get_user_by_username(input.username),
|
||||
user=get_user_by_username(ssh_input.username),
|
||||
)
|
||||
|
||||
return UserMutationReturn(
|
||||
|
|
|
@ -45,8 +45,8 @@ def some_users(mocker, datadir):
|
|||
|
||||
|
||||
API_CREATE_SSH_MUTATION = """
|
||||
mutation createSsh($input: SshMutationsInput!) {
|
||||
createSsh(input: $input) {
|
||||
mutation createSsh($sshInput: SshMutationInput!) {
|
||||
createSsh(sshInput: $sshInput) {
|
||||
success
|
||||
message
|
||||
code
|
||||
|
@ -65,7 +65,7 @@ def test_graphql_add_ssh_unauthorized(client, some_users, mock_subprocess_popen)
|
|||
json={
|
||||
"query": API_CREATE_SSH_MUTATION,
|
||||
"variables": {
|
||||
"input": {
|
||||
"sshInput": {
|
||||
"username": "user1",
|
||||
"sshKey": "ssh-rsa KEY test_key@pc",
|
||||
},
|
||||
|
@ -82,7 +82,7 @@ def test_graphql_add_ssh(authorized_client, some_users, mock_subprocess_popen):
|
|||
json={
|
||||
"query": API_CREATE_SSH_MUTATION,
|
||||
"variables": {
|
||||
"input": {
|
||||
"sshInput": {
|
||||
"username": "user1",
|
||||
"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={
|
||||
"query": API_CREATE_SSH_MUTATION,
|
||||
"variables": {
|
||||
"input": {
|
||||
"sshInput": {
|
||||
"username": "root",
|
||||
"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={
|
||||
"query": API_CREATE_SSH_MUTATION,
|
||||
"variables": {
|
||||
"input": {
|
||||
"sshInput": {
|
||||
"username": "tester",
|
||||
"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={
|
||||
"query": API_CREATE_SSH_MUTATION,
|
||||
"variables": {
|
||||
"input": {
|
||||
"sshInput": {
|
||||
"username": "user1",
|
||||
"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={
|
||||
"query": API_CREATE_SSH_MUTATION,
|
||||
"variables": {
|
||||
"input": {
|
||||
"sshInput": {
|
||||
"username": "user666",
|
||||
"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 = """
|
||||
mutation deleteSsh($input: SshMutationsInput!) {
|
||||
deleteSsh(input: $input) {
|
||||
mutation deleteSsh($sshInput: SshMutationInput!) {
|
||||
deleteSsh(sshInput: $sshInput) {
|
||||
success
|
||||
message
|
||||
code
|
||||
|
@ -220,7 +220,7 @@ def test_graphql_dell_ssh_unauthorized(client, some_users, mock_subprocess_popen
|
|||
json={
|
||||
"query": API_DELETE_SSH_MUTATION,
|
||||
"variables": {
|
||||
"input": {
|
||||
"sshInput": {
|
||||
"username": "user1",
|
||||
"sshKey": "ssh-rsa KEY test_key@pc",
|
||||
},
|
||||
|
@ -237,7 +237,7 @@ def test_graphql_dell_ssh(authorized_client, some_users, mock_subprocess_popen):
|
|||
json={
|
||||
"query": API_DELETE_SSH_MUTATION,
|
||||
"variables": {
|
||||
"input": {
|
||||
"sshInput": {
|
||||
"username": "user1",
|
||||
"sshKey": "ssh-rsa KEY user1@pc",
|
||||
},
|
||||
|
@ -263,7 +263,7 @@ def test_graphql_dell_root_ssh(authorized_client, some_users, mock_subprocess_po
|
|||
json={
|
||||
"query": API_DELETE_SSH_MUTATION,
|
||||
"variables": {
|
||||
"input": {
|
||||
"sshInput": {
|
||||
"username": "root",
|
||||
"sshKey": "ssh-ed25519 KEY test@pc",
|
||||
},
|
||||
|
@ -287,7 +287,7 @@ def test_graphql_dell_main_ssh(authorized_client, some_users, mock_subprocess_po
|
|||
json={
|
||||
"query": API_DELETE_SSH_MUTATION,
|
||||
"variables": {
|
||||
"input": {
|
||||
"sshInput": {
|
||||
"username": "tester",
|
||||
"sshKey": "ssh-rsa KEY tester@pc",
|
||||
},
|
||||
|
@ -311,7 +311,7 @@ def test_graphql_dell_404ssh(authorized_client, some_users, mock_subprocess_pope
|
|||
json={
|
||||
"query": API_DELETE_SSH_MUTATION,
|
||||
"variables": {
|
||||
"input": {
|
||||
"sshInput": {
|
||||
"username": "user1",
|
||||
"sshKey": "ssh-rsa KEY test_key@pc",
|
||||
},
|
||||
|
@ -332,7 +332,7 @@ def test_graphql_dell_ssh_404user(authorized_client, some_users, mock_subprocess
|
|||
json={
|
||||
"query": API_DELETE_SSH_MUTATION,
|
||||
"variables": {
|
||||
"input": {
|
||||
"sshInput": {
|
||||
"username": "user666",
|
||||
"sshKey": "ssh-rsa KEY test_key@pc",
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue