diff --git a/plugins/filter/ssh_public_key.py b/plugins/filter/ssh_public_key.py new file mode 100644 index 00000000..5c170777 --- /dev/null +++ b/plugins/filter/ssh_public_key.py @@ -0,0 +1,85 @@ +# Copyright (c) 2026 VEXXHOST, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from __future__ import absolute_import, division, print_function + +__metaclass__ = type + +from ansible.errors import AnsibleFilterError +from ansible.module_utils._text import to_bytes, to_text +from ansible.module_utils.common.collections import is_string + +DOCUMENTATION = """ + name: ssh_public_key + short_description: Get the OpenSSH public key of a private key + version_added: 6.0.0 + description: + - Return the public key of an unencrypted SSH private key, in the + C( ) format used by C(authorized_keys), without a comment. + - Accepts private keys in OpenSSH or PEM format. + options: + _input: + type: string + required: true + description: + - The private key contents + author: + - Mohammed Naser +""" + +EXAMPLES = """ + - name: Print the public key of an SSH private key + ansible.builtin.debug: + msg: "{{ lookup('file', 'id_ed25519') | vexxhost.atmosphere.ssh_public_key }}" +""" + +RETURN = """ + _value: + description: The OpenSSH public key + type: string +""" + + +def ssh_public_key(value): + if not is_string(value): + raise AnsibleFilterError( + "Invalid value type (%s) for ssh_public_key" % type(value) + ) + + try: + from cryptography.hazmat.primitives import serialization + except ImportError as e: + raise AnsibleFilterError("ssh_public_key requires cryptography: %s" % e) + + data = to_bytes(value.strip() + "\n") + try: + if b"-----BEGIN OPENSSH PRIVATE KEY-----" in data: + key = serialization.load_ssh_private_key(data, password=None) + else: + key = serialization.load_pem_private_key(data, password=None) + except (ValueError, TypeError) as e: + # NOTE: Never include the input here, it is a private key. + raise AnsibleFilterError("Unable to load SSH private key: %s" % e) + + return to_text( + key.public_key().public_bytes( + encoding=serialization.Encoding.OpenSSH, + format=serialization.PublicFormat.OpenSSH, + ) + ) + + +class FilterModule(object): + def filters(self): + return {"ssh_public_key": ssh_public_key} diff --git a/releasenotes/notes/fix-ssh-public-key-on-tmpfs-9d1ef4c183aa5b04.yaml b/releasenotes/notes/fix-ssh-public-key-on-tmpfs-9d1ef4c183aa5b04.yaml new file mode 100644 index 00000000..20e9db45 --- /dev/null +++ b/releasenotes/notes/fix-ssh-public-key-on-tmpfs-9d1ef4c183aa5b04.yaml @@ -0,0 +1,7 @@ +--- +fixes: + - | + The Nova and Manila roles no longer fail with ``Operation not supported`` + while generating the SSH public key on deployment hosts where ``/tmp`` is + a ``tmpfs``, such as Ubuntu 26.04. The roles also no longer leave a public + key file behind in ``/tmp``. diff --git a/roles/manila/tasks/generate_public_key.yml b/roles/manila/tasks/generate_public_key.yml deleted file mode 100644 index a7765bcd..00000000 --- a/roles/manila/tasks/generate_public_key.yml +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2023 VEXXHOST, Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -- name: Generate public key for SSH private key - become: false - delegate_to: localhost - block: - - name: Generate temporary file for SSH public key - check_mode: false - changed_when: false - ansible.builtin.tempfile: - state: file - prefix: manila_ssh_key_ - register: _manila_ssh_key_tempfile - # NOTE(mnaser): It's important to add a trailing newline at the end of this - # string or else `ssh-keygen` will not be happy. - - name: Write contents of current private SSH key - check_mode: false - changed_when: false - ansible.builtin.copy: - dest: "{{ _manila_ssh_key_tempfile.path }}" - content: "{{ manila_ssh_key }}\n" - mode: "0600" - - name: Generate public key for SSH private key - changed_when: false - community.crypto.openssh_keypair: - path: "{{ _manila_ssh_key_tempfile.path }}" - regenerate: never - register: _manila_ssh_publickey - always: - - name: Delete temporary file for public SSH key - check_mode: false - changed_when: false - ansible.builtin.file: - path: "{{ _manila_ssh_key_tempfile.path }}" - state: absent - -- name: Create secret with the SSH keys - kubernetes.core.k8s: - state: present - definition: - apiVersion: v1 - kind: Secret - metadata: - name: "{{ manila_helm_release_name }}-ssh-keys" - namespace: "{{ manila_helm_release_namespace }}" - type: Opaque - data: - id_rsa: "{{ manila_ssh_key | b64encode }}" - id_rsa.pub: "{{ _manila_ssh_publickey.public_key | b64encode }}" diff --git a/roles/manila/tasks/main.yml b/roles/manila/tasks/main.yml index e437d845..6ee1b8d5 100644 --- a/roles/manila/tasks/main.yml +++ b/roles/manila/tasks/main.yml @@ -16,9 +16,19 @@ ansible.builtin.import_tasks: file: generate_resources.yml -- name: Generate public key for SSH private key - ansible.builtin.import_tasks: - file: generate_public_key.yml +- name: Create secret with the SSH keys + kubernetes.core.k8s: + state: present + definition: + apiVersion: v1 + kind: Secret + metadata: + name: "{{ manila_helm_release_name }}-ssh-keys" + namespace: "{{ manila_helm_release_namespace }}" + type: Opaque + data: + id_rsa: "{{ manila_ssh_key | b64encode }}" + id_rsa.pub: "{{ manila_ssh_key | vexxhost.atmosphere.ssh_public_key | b64encode }}" - name: Deploy Helm chart run_once: true diff --git a/roles/nova/tasks/generate_public_key.yml b/roles/nova/tasks/generate_public_key.yml deleted file mode 100644 index fff00ae6..00000000 --- a/roles/nova/tasks/generate_public_key.yml +++ /dev/null @@ -1,47 +0,0 @@ -# Copyright (c) 2023 VEXXHOST, Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -- name: Generate public key for SSH private key - become: false - delegate_to: localhost - block: - - name: Generate temporary file for SSH public key - check_mode: false - changed_when: false - ansible.builtin.tempfile: - state: file - prefix: nova_ssh_key_ - register: _nova_ssh_key_tempfile - # NOTE(mnaser): It's important to add a trailing newline at the end of this - # string or else `ssh-keygen` will not be happy. - - name: Write contents of current private SSH key - check_mode: false - changed_when: false - ansible.builtin.copy: - dest: "{{ _nova_ssh_key_tempfile.path }}" - content: "{{ nova_ssh_key }}\n" - mode: "0600" - - name: Generate public key for SSH private key - changed_when: false - community.crypto.openssh_keypair: - path: "{{ _nova_ssh_key_tempfile.path }}" - regenerate: never - register: _nova_ssh_publickey - always: - - name: Delete temporary file for public SSH key - check_mode: false - changed_when: false - ansible.builtin.file: - path: "{{ _nova_ssh_key_tempfile.path }}" - state: absent diff --git a/roles/nova/tasks/main.yml b/roles/nova/tasks/main.yml index e3e73748..bd025170 100644 --- a/roles/nova/tasks/main.yml +++ b/roles/nova/tasks/main.yml @@ -21,10 +21,6 @@ for migration instructions. when: atmosphere_ceph_enabled is defined -- name: Generate public key for SSH private key - ansible.builtin.import_tasks: - file: generate_public_key.yml - # NOTE(okozachenko1203): We cannot set helm hook for jobs. # As a workaround, remove them before upgrade nova helm release. - name: Remove nova-bootstrap and nova-cell-setup job diff --git a/roles/nova/tasks/migrate_from_osa.yml b/roles/nova/tasks/migrate_from_osa.yml index d7321183..83a8e970 100644 --- a/roles/nova/tasks/migrate_from_osa.yml +++ b/roles/nova/tasks/migrate_from_osa.yml @@ -12,10 +12,6 @@ # License for the specific language governing permissions and limitations # under the License. -- name: Generate public key for SSH private key - ansible.builtin.import_tasks: - file: generate_public_key.yml - - name: Generate configuration difference ansible.builtin.include_role: name: osa_config_diff diff --git a/roles/nova/vars/main.yml b/roles/nova/vars/main.yml index d8a4f4ee..bd78374c 100644 --- a/roles/nova/vars/main.yml +++ b/roles/nova/vars/main.yml @@ -26,7 +26,7 @@ _nova_helm_values: - "{{ atmosphere_network_backend | default('openvswitch') }}" ssh: enabled: true - public_key: "{{ _nova_ssh_publickey.public_key }}" + public_key: "{{ nova_ssh_key | vexxhost.atmosphere.ssh_public_key }}" private_key: "{{ nova_ssh_key | regex_replace('([^\n])$', '\\1\n') }}" bootstrap: structured: diff --git a/tests/unit/plugins/filter/test_ssh_public_key.py b/tests/unit/plugins/filter/test_ssh_public_key.py new file mode 100644 index 00000000..d913b1dd --- /dev/null +++ b/tests/unit/plugins/filter/test_ssh_public_key.py @@ -0,0 +1,115 @@ +# Copyright (c) 2026 VEXXHOST, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import pytest +from ansible.errors import AnsibleFilterError +from ansible_collections.vexxhost.atmosphere.plugins.filter.ssh_public_key import ( + ssh_public_key, +) + +# NOTE: Throwaway keys generated with `ssh-keygen` for these tests only. The +# expected public keys are the matching `.pub` files without a comment. + +ED25519_PRIVATE_KEY = """\ +-----BEGIN OPENSSH PRIVATE KEY----- +b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW +QyNTUxOQAAACBwzsQgMX1vQUACsDaST8BCrg2dgQW6XGGZBkVVR9xC0AAAAJDNlegEzZXo +BAAAAAtzc2gtZWQyNTUxOQAAACBwzsQgMX1vQUACsDaST8BCrg2dgQW6XGGZBkVVR9xC0A +AAAED2m9ZIo7rvBJ/4Vpz2EpfnDHLIg/8ltTBUVdfy7QZuW3DOxCAxfW9BQAKwNpJPwEKu +DZ2BBbpcYZkGRVVH3ELQAAAAB2ZpeHR1cmUBAgMEBQY= +-----END OPENSSH PRIVATE KEY----- +""" +ED25519_PUBLIC_KEY = ( + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHDOxCAxfW9BQAKwNpJPwEKuDZ2BBbpcYZkGRVVH3ELQ" +) + +ECDSA_PRIVATE_KEY = """\ +-----BEGIN OPENSSH PRIVATE KEY----- +b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAaAAAABNlY2RzYS +1zaGEyLW5pc3RwMjU2AAAACG5pc3RwMjU2AAAAQQTSQvEyHSJcF2nNSe09Gu41nYiFgapB +dPsgSEE1o3wKub/bXw31+X0i62w1Uv3tJ7ixehGUW1H0rpSrpFYmOQEDAAAAoH7yG1p+8h +taAAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBNJC8TIdIlwXac1J +7T0a7jWdiIWBqkF0+yBIQTWjfAq5v9tfDfX5fSLrbDVS/e0nuLF6EZRbUfSulKukViY5AQ +MAAAAgRdslMza2AFg+6apmIudQkoC7WIwAT5XEOoceCLFTU1cAAAAHZml4dHVyZQE= +-----END OPENSSH PRIVATE KEY----- +""" +ECDSA_PUBLIC_KEY = ( + "ecdsa-sha2-nistp256 " + "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBNJC8TIdIlwXac1J" + "7T0a7jWdiIWBqkF0+yBIQTWjfAq5v9tfDfX5fSLrbDVS/e0nuLF6EZRbUfSulKukViY5AQM=" +) + +RSA_PEM_PRIVATE_KEY = """\ +-----BEGIN RSA PRIVATE KEY----- +MIICXgIBAAKBgQDQkmQLifFE058xPDKfCyPHl4whLglwZ5EO5HRabnsJFl90Z1OS +8fvpdz3SNlNDJ5U4VEfKmm42XYMFA570T0KbVfMMz2ungDYAm1+vHGKkUMeZGIpv +HD7mFvBbeDZhtw/yjpZ1xtKe8IcbhFk9j+JN7iSED5nDl3TJBYzR1LREdwIDAQAB +AoGBAI1q9NKuERztr/6eJmSYFQZ7xtOod1XzovNKC2pVhq6+sLIobQ4xb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AkLr+5SzEbjN9np49oyQAH2MAhDOw6/EvYYmE+/ +cUKJt5PYtRIOztJsXNkBFk6aDyMdAB+rjS7m/8d8fcESToNkn2pyvzbRdIofDwMC5WVNPG +sXOB6HcoGf8EqaKbrExb7m0P8VwRBdOOB030w3qBo+yWjgi5IIKfeKHC0t8RaD2uQH6hxQ +4bnYWf1RnZn25gcQ== +-----END OPENSSH PRIVATE KEY----- +""" + + +@pytest.mark.parametrize( + "private_key,expected", + [ + (ED25519_PRIVATE_KEY, ED25519_PUBLIC_KEY), + (ECDSA_PRIVATE_KEY, ECDSA_PUBLIC_KEY), + (RSA_PEM_PRIVATE_KEY, RSA_PEM_PUBLIC_KEY), + # Keys passed in through inventory often lose their trailing newline. + (ED25519_PRIVATE_KEY.rstrip("\n"), ED25519_PUBLIC_KEY), + (RSA_PEM_PRIVATE_KEY.rstrip("\n"), RSA_PEM_PUBLIC_KEY), + ], +) +def test_ssh_public_key(private_key, expected): + assert ssh_public_key(private_key) == expected + + +@pytest.mark.parametrize( + "value", + [ + ENCRYPTED_PRIVATE_KEY, + ED25519_PUBLIC_KEY, + "not a key", + None, + ], +) +def test_ssh_public_key_invalid(value): + with pytest.raises(AnsibleFilterError): + ssh_public_key(value)