fix(nova,manila): derive SSH public key with a filter (#4485)
The Nova and Manila roles wrote the SSH private key to a temporary file and ran community.crypto.openssh_keypair on it to get the public key. That module copies the .pub file with preserved_copy(), which also copies file attributes and fails with "Operation not supported" when /tmp is a tmpfs (the default on Ubuntu 26.04). Add a small ssh_public_key filter that derives the OpenSSH public key from the private key in memory, and use it in both roles. This removes the duplicated temporary file tasks and stops leaving a stray .pub file behind in /tmp. Signed-off-by: Mohammed Naser <mnaser@vexxhost.com>
This commit is contained in:
1 parent
9b15a8610f
commit
28a442e06f
9 files changed
+221
-120
No files matched your search
@@ -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(<type> <base64>) 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 <mnaser@vexxhost.com>
|
||||
"""
|
||||
|
||||
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}
|
||||
@@ -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``.
|
||||
@@ -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 }}"
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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+sLIobQ4xbPD7nLsv
|
||||
gn0CFbc/zf2dgCT0eqWnsttL8ohOzoeJ9ei4Ojfn9vfSStHZOQ61cvOWUP0ENvsJ
|
||||
TCoJY7ljLD4tibM7h9XFnPccb/gc8ZAhVizY9WDWwfqQDByBAkEA716OmYDRLHB3
|
||||
647lq+0qkLcU+JPiuslYorNnxgsgggFTLRWATLOkQAn4fssNCKyFyjPAHwhSJ1b1
|
||||
VTeTDiaycQJBAN8QEQYF1GzfgRHOh54GhB62zgOESdI8LQeJq8XafO3ostYw6wEv
|
||||
UDMzr9Zp9aosd/yd+9oKXhAIG9cIBBHkiWcCQQDlvOc4RP59nGQ8XSXp4PKdelpS
|
||||
Sf2uMFk1ruLGnR9SJ1thReWhuDjIjclGSDP2Qz8VVp3qKt5Oi5NjerdVOabBAkEA
|
||||
vR13AV8x8TgwynMKYbzuiMe9tBeVKsvf9XMQEgeiQWY3MQd6Rp/hY/efTa66HBPl
|
||||
imQ64WpGw1rMl6txH+ZdYwJANg4uwgo7q0G1w02K+nkjvBbJbJSFLFkFJzqE92gB
|
||||
vBOmX376YUF8YxEEx3LCKs1n5rEq9+6UsGwXf70JXsgMhA==
|
||||
-----END RSA PRIVATE KEY-----
|
||||
"""
|
||||
RSA_PEM_PUBLIC_KEY = (
|
||||
"ssh-rsa "
|
||||
"AAAAB3NzaC1yc2EAAAADAQABAAAAgQDQkmQLifFE058xPDKfCyPHl4whLglwZ5EO5HRabnsJ"
|
||||
"Fl90Z1OS8fvpdz3SNlNDJ5U4VEfKmm42XYMFA570T0KbVfMMz2ungDYAm1+vHGKkUMeZGIpv"
|
||||
"HD7mFvBbeDZhtw/yjpZ1xtKe8IcbhFk9j+JN7iSED5nDl3TJBYzR1LREdw=="
|
||||
)
|
||||
|
||||
ENCRYPTED_PRIVATE_KEY = """\
|
||||
-----BEGIN OPENSSH PRIVATE KEY-----
|
||||
b3BlbnNzaC1rZXktdjEAAAAACmFlczI1Ni1jdHIAAAAGYmNyeXB0AAAAGAAAABCFl38BoX
|
||||
OEE6ldv8FAKWegAAAAGAAAAAEAAAAzAAAAC3NzaC1lZDI1NTE5AAAAIE2GXf1P/yrhNvjI
|
||||
n9atuU1IHTXJA2hkgxq/nMPx/67HAAAAkLr+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)
|
||||
Reference in new issue
Block a user