sshkeys (ciuser) to an VM endpoint (format error)

iamralf

New Member
May 6, 2024
6
0
1
I receive following error from the Proxmox-API (v8.1.10):

{"sshkeys": "invalid format - invalid urlencoded string: ssh-rsa%20AA.................................%20eom\n"}}, "msg": "Status code was 400 and not [200]: HTTP Error 400: Parameter verification failed.", "pragma": "no-cache", "redirected": false, "server": "pve-api-daemon/3.0", "status": 400, "url": "https://proxmox:8006/api2/json/nodes/cluster01/qemu/5000/config"}

The same issue seems to be reported here, but the solution is not applicable. Sorry for posting there, but without response:
https://forum.proxmox.com/threads/sshkeys-invalid-format-invalid-urlencoded-string.126202/

{{ | urlencode }} does its job in the Ansible task (There are no "\n" or "+").
However, "\n" is appended to the content of the sshkey-variable in the error message back from the Proxmox server.
The code runs without the sshkeys line in the Ansible task below. ("ciuser" and "cipassword").

My Ansible task look like:

YAML:
   - name: configure vm
     uri:
       url: "https://{{ server_url }}:8006/api2/json/nodes/{{ node }}/qemu/{{ vmid_next | trim }}/config"
       method: POST
       return_content: yes
       body_format: json
       use_proxy: no
       validate_certs: no
       timeout: 5
       headers:
         Authorization: "PVEAPIToken=..."
         Content-Type: "application/json"
       body:
         [...]
         ciuser: "{{ inventory_settings__pve_vm_username }}"
         sshkeys: "{{ inventory_settings__pve_vm_public_key | trim | urlencode }}"
     [...]

What is the problem here? Thanks for any help!
 
Last edited:
I receive following error from the Proxmox-API (v8.1.10):

{"sshkeys": "invalid format - invalid urlencoded string: ssh-rsa%20AA.................................%20eom\n"}}, "msg": "Status code was 400 and not [200]: HTTP Error 400: Parameter verification failed.", "pragma": "no-cache", "redirected": false, "server": "pve-api-daemon/3.0", "status": 400, "url": "https://proxmox:8006/api2/json/nodes/cluster01/qemu/5000/config"}

The same issue seems to be reported here, but the solution is not applicable. Sorry for posting there, but without response:
https://forum.proxmox.com/threads/sshkeys-invalid-format-invalid-urlencoded-string.126202/

{{ | urlencode }} does its job in the Ansible task (There are no "\n" or "+").
However, "\n" is appended to the content of the sshkey-variable in the error message back from the Proxmox server.
The code runs without the sshkeys line in the Ansible task below. ("ciuser" and "cipassword").

My Ansible task look like:

YAML:
   - name: configure vm
     uri:
       url: "https://{{ server_url }}:8006/api2/json/nodes/{{ node }}/qemu/{{ vmid_next | trim }}/config"
       method: POST
       return_content: yes
       body_format: json
       use_proxy: no
       validate_certs: no
       timeout: 5
       headers:
         Authorization: "PVEAPIToken=..."
         Content-Type: "application/json"
       body:
         [...]
         ciuser: "{{ inventory_settings__pve_vm_username }}"
         sshkeys: "{{ inventory_settings__pve_vm_public_key | trim | urlencode }}"
     [...]

What is the problem here? Thanks for any help!

I had exactly the same troubles as you are, this is a very stupid bug that should be addressed.

I know its been a while since you posted this but I wanted to provide some help for those who have been bashing their brain on this issue like myself for way too long.

This worked for me so far. I dont like it and I dont agree with it at all.

vm_sshkeys is a fact that I have that contains my public ssh key in a string format. I run this step before calling the URI and using encoded_sshkeys_output to populate the sshkeys property.

YAML:
- name: Encode SSH Keys for Proxmox
  ansible.builtin.shell: |
    python3 -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1], safe=''))" "{{ vm_sshkeys }}"
  register: encoded_sshkeys_output
  changed_when: false

FYI, I created an account on this forum just to respond to this for any future head bashers out there.
 
hi!
as a member above i got registered just to share another way to fix this.
Ansible filter plugin to fix the issue seems to me more elegant than shell invokation in ansible.
Python:
import urllib.parse
import sys

def fix_urlencode(input_string):
    return urllib.parse.quote(input_string, safe='')

class FilterModule(object):
    def filters(self):
        return {
            'fix_urlencode': fix_urlencode
        }

and in my playbook:
YAML:
body:
  sshkeys:        "{{ vm_ssh_key_data | trim | fix_urlencode }}"
body_format:      form-urlencoded
status_code:      200