Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete SSH key fails #22

Open
felinira opened this issue Jan 20, 2024 · 1 comment
Open

Delete SSH key fails #22

felinira opened this issue Jan 20, 2024 · 1 comment

Comments

@felinira
Copy link

Deleting an SSH key seems to always fail with HTTP error 400. Even when the key is not used by any repo.

Example:

- hosts: localhost
  name: Reproduce borgbase API error 400
  tasks:
  - name: add SSH key
    adhawkins.borgbase.borgbase_ssh:
      state: present
      apikey: "{{ borgbase_apikey }}"
      name: "test_key"
      key: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDw0WtinOTMXNNkk1Qu7hyaVjwz10vYv2GyeWFnzgp7m"
  - name: try to remove ssh key
    adhawkins.borgbase.borgbase_ssh:
      state: absent
      apikey: "{{ borgbase_apikey }}"
      name: "test_key"

Throws this error and the key will not be removed:

TASK [try to remove ssh key] *************************************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: urllib.error.HTTPError: HTTP Error 400: Bad Request
fatal: [localhost]: FAILED! => changed=false 
  module_stderr: |-
    Traceback (most recent call last):
      File "<stdin>", line 107, in <module>
      File "<stdin>", line 99, in _ansiballz_main
      File "<stdin>", line 47, in invoke_module
      File "<frozen runpy>", line 226, in run_module
      File "<frozen runpy>", line 98, in _run_module_code
      File "<frozen runpy>", line 88, in _run_code
      File "/tmp/ansible_adhawkins.borgbase.borgbase_ssh_payload_41mupboi/ansible_adhawkins.borgbase.borgbase_ssh_payload.zip/ansible_collections/adhawkins/borgbase/plugins/modules/borgbase_ssh.py", line 299, in <module>
      File "/tmp/ansible_adhawkins.borgbase.borgbase_ssh_payload_41mupboi/ansible_adhawkins.borgbase.borgbase_ssh_payload.zip/ansible_collections/adhawkins/borgbase/plugins/modules/borgbase_ssh.py", line 295, in main
      File "/tmp/ansible_adhawkins.borgbase.borgbase_ssh_payload_41mupboi/ansible_adhawkins.borgbase.borgbase_ssh_payload.zip/ansible_collections/adhawkins/borgbase/plugins/modules/borgbase_ssh.py", line 256, in runModule
      File "/tmp/ansible_adhawkins.borgbase.borgbase_ssh_payload_41mupboi/ansible_adhawkins.borgbase.borgbase_ssh_payload.zip/ansible_collections/adhawkins/borgbase/plugins/modules/borgbase_ssh.py", line 158, in deleteKey
      File "/tmp/ansible_adhawkins.borgbase.borgbase_ssh_payload_41mupboi/ansible_adhawkins.borgbase.borgbase_ssh_payload.zip/ansible_collections/adhawkins/borgbase/plugins/module_utils/borgbase_client.py", line 162, in execute
      File "/tmp/ansible_adhawkins.borgbase.borgbase_ssh_payload_41mupboi/ansible_adhawkins.borgbase.borgbase_ssh_payload.zip/ansible_collections/adhawkins/borgbase/plugins/module_utils/borgbase_client.py", line 176, in _send
      File "/tmp/ansible_adhawkins.borgbase.borgbase_ssh_payload_41mupboi/ansible_adhawkins.borgbase.borgbase_ssh_payload.zip/ansible/module_utils/urls.py", line 1561, in open
      File "/usr/lib/python3.11/urllib/request.py", line 216, in urlopen
        return opener.open(url, data, timeout)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3.11/urllib/request.py", line 525, in open
        response = meth(req, response)
                   ^^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3.11/urllib/request.py", line 634, in http_response
        response = self.parent.error(
                   ^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3.11/urllib/request.py", line 563, in error
        return self._call_chain(*args)
               ^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3.11/urllib/request.py", line 496, in _call_chain
        result = func(*args)
                 ^^^^^^^^^^^
      File "/usr/lib/python3.11/urllib/request.py", line 643, in http_error_default
        raise HTTPError(req.full_url, code, msg, hdrs, fp)
    urllib.error.HTTPError: HTTP Error 400: Bad Request
  module_stdout: ''
  msg: |-
    MODULE FAILURE
    See stdout/stderr for the exact error
  rc: 1

It works fine when using GraphQL directly:

mutation {
  sshAdd(keyData: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDw0WtinOTMXNNkk1Qu7hyaVjwz10vYv2GyeWFnzgp7m", name: "test_key") {
    keyAdded {
      id
    }
  }
}

returns:

{
  "data": {
    "sshAdd": {
      "keyAdded": {
        "id": "81446"
      }
    }
  }
}

Find the key again:

{
  sshList(name: "test_key") {
    id,
  }
}

returns:

{
  "data": {
    "sshList": [
      {
        "id": "81446"
      }
    ]
  }
}

And then:

mutation {
  sshDelete(id: "81446") {
    ok
  }
}

returns:

{
  "data": {
    "sshDelete": {
      "ok": true
    }
  }
}
@Mireiawen
Copy link

I was wondering the same issue, and it seems that the SSH delete uses wrong data type.

In the borgbase/plugins/module_utils/borgbase_client.py it is defined as Int:

    SSH_DELETE = '''
mutation sshDelete($id: Int!) {
  sshDelete(id: $id) {
    ok
  }
}
'''

However, the BorgBase API explorer describes the parameter type as string:

Mutation sshDelete
Type SSHDelete
Arguments
id: String!

I tried to change the type to String, there is also instance of calling the deleteKey that by default turns the parameter to int around line 256 in borgbase/plugins/modules/borgbase_ssh.py:
deleteResult = deleteKey(int(foundKey['id']), apiKey=module.params['apikey'])

After removing the integer conversion, it seems to work;
deleteResult = deleteKey(foundKey['id'], apiKey=module.params['apikey'])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants