| 1 |
|
| 2 |
|
| 3 |
import os |
| 4 |
import socket |
| 5 |
import struct |
| 6 |
import threading |
| 7 |
import unittest |
| 8 |
|
| 9 |
from cryptography.hazmat.primitives import serialization |
| 10 |
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey |
| 11 |
|
| 12 |
from tests.integration.test_utils import RunSubprocessMixin |
| 13 |
from tests.integration.test_utils import podman_compose_path |
| 14 |
from tests.integration.test_utils import test_path |
| 15 |
|
| 16 |
expected_lines = [ |
| 17 |
"default: ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFYQvN9a+toIB6jSs4zY7FMapZnHt80EKCUr/WhLwUum", |
| 18 |
"id1: ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFYQvN9a+toIB6jSs4zY7FMapZnHt80EKCUr/WhLwUum", |
| 19 |
"id2: ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFYQvN9a+toIB6jSs4zY7FMapZnHt80EKCUr/WhLwUum", |
| 20 |
] |
| 21 |
|
| 22 |
|
| 23 |
class TestBuildSsh(unittest.TestCase, RunSubprocessMixin): |
| 24 |
def test_build_ssh(self): |
| 25 |
"""The build context can contain the ssh authentications that the image builder should |
| 26 |
use during image build. They can be either an array or a map. |
| 27 |
""" |
| 28 |
|
| 29 |
compose_path = os.path.join(test_path(), "build_ssh/docker-compose.yml") |
| 30 |
sock_path = os.path.join(test_path(), "build_ssh/agent_dummy.sock") |
| 31 |
private_key_file = os.path.join(test_path(), "build_ssh/id_ed25519_dummy") |
| 32 |
|
| 33 |
agent = MockSSHAgent(private_key_file) |
| 34 |
|
| 35 |
try: |
| 36 |
|
| 37 |
os.environ['SSH_AUTH_SOCK'] = sock_path |
| 38 |
|
| 39 |
|
| 40 |
agent.start_agent(sock_path) |
| 41 |
|
| 42 |
self.run_subprocess_assert_returncode([ |
| 43 |
podman_compose_path(), |
| 44 |
"-f", |
| 45 |
compose_path, |
| 46 |
"build", |
| 47 |
"test_build_ssh_map", |
| 48 |
"test_build_ssh_array", |
| 49 |
]) |
| 50 |
|
| 51 |
for test_image in [ |
| 52 |
"test_build_ssh_map", |
| 53 |
"test_build_ssh_array", |
| 54 |
]: |
| 55 |
out, _ = self.run_subprocess_assert_returncode([ |
| 56 |
podman_compose_path(), |
| 57 |
"-f", |
| 58 |
compose_path, |
| 59 |
"run", |
| 60 |
"--rm", |
| 61 |
test_image, |
| 62 |
]) |
| 63 |
|
| 64 |
out = out.decode('utf-8') |
| 65 |
|
| 66 |
|
| 67 |
self.assertTrue( |
| 68 |
all(line in out for line in expected_lines), |
| 69 |
f"Incorrect output for image {test_image}", |
| 70 |
) |
| 71 |
|
| 72 |
finally: |
| 73 |
|
| 74 |
agent.stop_agent() |
| 75 |
|
| 76 |
if os.path.exists(sock_path): |
| 77 |
os.remove(sock_path) |
| 78 |
|
| 79 |
self.run_subprocess_assert_returncode([ |
| 80 |
"podman", |
| 81 |
"rmi", |
| 82 |
"my-alpine-build-ssh-map", |
| 83 |
"my-alpine-build-ssh-array", |
| 84 |
]) |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
SSH_AGENTC_REQUEST_IDENTITIES = 11 |
| 89 |
SSH_AGENT_IDENTITIES_ANSWER = 12 |
| 90 |
SSH_AGENT_FAILURE = 5 |
| 91 |
STOP_REQUEST = 0xFF |
| 92 |
|
| 93 |
|
| 94 |
class MockSSHAgent: |
| 95 |
def __init__(self, private_key_path): |
| 96 |
self.sock_path = None |
| 97 |
self.server_sock = None |
| 98 |
self.running = threading.Event() |
| 99 |
self.keys = [self._load_ed25519_private_key(private_key_path)] |
| 100 |
self.agent_thread = None |
| 101 |
|
| 102 |
def _load_ed25519_private_key(self, private_key_path): |
| 103 |
"""Load ED25519 private key from an OpenSSH private key file.""" |
| 104 |
with open(private_key_path, 'rb') as key_file: |
| 105 |
private_key = serialization.load_ssh_private_key(key_file.read(), password=None) |
| 106 |
|
| 107 |
|
| 108 |
if not isinstance(private_key, Ed25519PrivateKey): |
| 109 |
raise ValueError("Invalid key type, expected ED25519 private key.") |
| 110 |
|
| 111 |
|
| 112 |
public_key = private_key.public_key() |
| 113 |
|
| 114 |
|
| 115 |
public_key_blob = public_key.public_bytes( |
| 116 |
encoding=serialization.Encoding.Raw, format=serialization.PublicFormat.Raw |
| 117 |
) |
| 118 |
|
| 119 |
|
| 120 |
key_type = b"ssh-ed25519" |
| 121 |
|
| 122 |
|
| 123 |
key_blob_full = ( |
| 124 |
struct.pack(">I", len(key_type)) |
| 125 |
+ key_type |
| 126 |
+ struct.pack(">I", len(public_key_blob)) |
| 127 |
+ public_key_blob |
| 128 |
) |
| 129 |
|
| 130 |
|
| 131 |
comment = "" |
| 132 |
|
| 133 |
return ("ssh-ed25519", key_blob_full, comment) |
| 134 |
|
| 135 |
def start_agent(self, sock_path): |
| 136 |
"""Start the mock SSH agent and create a Unix domain socket.""" |
| 137 |
self.sock_path = sock_path |
| 138 |
if os.path.exists(self.sock_path): |
| 139 |
os.remove(self.sock_path) |
| 140 |
|
| 141 |
self.server_sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 142 |
self.server_sock.bind(self.sock_path) |
| 143 |
self.server_sock.listen(5) |
| 144 |
|
| 145 |
os.environ['SSH_AUTH_SOCK'] = self.sock_path |
| 146 |
|
| 147 |
self.running.set() |
| 148 |
|
| 149 |
|
| 150 |
self.agent_thread = threading.Thread(target=self._accept_connections, daemon=True) |
| 151 |
self.agent_thread.start() |
| 152 |
|
| 153 |
def _accept_connections(self): |
| 154 |
"""Accept and handle incoming connections.""" |
| 155 |
while self.running.is_set(): |
| 156 |
try: |
| 157 |
client_sock, _ = self.server_sock.accept() |
| 158 |
self._handle_client(client_sock) |
| 159 |
except Exception as e: |
| 160 |
print(f"Error accepting connection: {e}") |
| 161 |
|
| 162 |
def _handle_client(self, client_sock): |
| 163 |
"""Handle a single client request (like ssh-add).""" |
| 164 |
try: |
| 165 |
|
| 166 |
length_message = client_sock.recv(4) |
| 167 |
if not length_message: |
| 168 |
raise "no length message received" |
| 169 |
|
| 170 |
msg_len = struct.unpack(">I", length_message)[0] |
| 171 |
|
| 172 |
request_message = client_sock.recv(msg_len) |
| 173 |
|
| 174 |
|
| 175 |
if request_message[0] == STOP_REQUEST: |
| 176 |
client_sock.close() |
| 177 |
self.running.clear() |
| 178 |
return |
| 179 |
|
| 180 |
|
| 181 |
if request_message[0] == SSH_AGENTC_REQUEST_IDENTITIES: |
| 182 |
response = self._mock_list_keys_response() |
| 183 |
client_sock.sendall(response) |
| 184 |
else: |
| 185 |
print("Message not recognized") |
| 186 |
|
| 187 |
response = struct.pack(">I", 1) + struct.pack(">B", SSH_AGENT_FAILURE) |
| 188 |
client_sock.sendall(response) |
| 189 |
|
| 190 |
except OSError: |
| 191 |
print("Client socket error.") |
| 192 |
pass |
| 193 |
finally: |
| 194 |
client_sock.close() |
| 195 |
|
| 196 |
def _mock_list_keys_response(self): |
| 197 |
"""Create a mock response for ssh-add -l, listing keys.""" |
| 198 |
|
| 199 |
|
| 200 |
response = struct.pack(">B", SSH_AGENT_IDENTITIES_ANSWER) |
| 201 |
|
| 202 |
|
| 203 |
response += struct.pack(">I", len(self.keys)) |
| 204 |
|
| 205 |
|
| 206 |
for key_type, key_blob, comment in self.keys: |
| 207 |
|
| 208 |
response += struct.pack(">I", len(key_blob)) + key_blob |
| 209 |
|
| 210 |
|
| 211 |
comment_encoded = comment.encode() |
| 212 |
response += struct.pack(">I", len(comment_encoded)) + comment_encoded |
| 213 |
|
| 214 |
|
| 215 |
response = struct.pack(">I", len(response)) + response |
| 216 |
|
| 217 |
return response |
| 218 |
|
| 219 |
def stop_agent(self): |
| 220 |
"""Stop the mock SSH agent.""" |
| 221 |
if self.running.is_set(): |
| 222 |
|
| 223 |
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as client_sock: |
| 224 |
client_sock.connect(self.sock_path) |
| 225 |
|
| 226 |
stop_command = struct.pack( |
| 227 |
">B", STOP_REQUEST |
| 228 |
) |
| 229 |
|
| 230 |
|
| 231 |
message_length = struct.pack(">I", len(stop_command)) |
| 232 |
client_sock.sendall(message_length) |
| 233 |
|
| 234 |
client_sock.sendall(stop_command) |
| 235 |
|
| 236 |
self.running.clear() |
| 237 |
|
| 238 |
|
| 239 |
if self.agent_thread: |
| 240 |
self.agent_thread.join() |
| 241 |
self.agent_thread = None |
| 242 |
|
| 243 |
|
| 244 |
if self.server_sock: |
| 245 |
self.server_sock.close() |
| 246 |
os.remove(self.sock_path) |