|
20 | 20 |
|
21 | 21 | SSH_PERMIT_TARGET_HOST = os.getenv("SSH_PERMIT_TARGET_HOST", "*")
|
22 | 22 | SSH_TARGET_KEY_PATH = os.getenv("SSH_TARGET_KEY_PATH", "~/.ssh/id_ed25519.pub")
|
| 23 | +SSH_TARGET_PUBLICKEY_API_PORT = os.getenv("SSH_TARGET_PUBLICKEY_API_PORT", 8080) |
23 | 24 |
|
24 | 25 | authorized_keys_cache_file = "/etc/ssh/authorized_keys_cache"
|
25 | 26 | authorized_keys_cache_file_lock = "cache_files.lock"
|
@@ -92,7 +93,7 @@ def get_authorized_keys_kubernetes(query_cache: list = []) -> (list, list):
|
92 | 93 |
|
93 | 94 | key = None
|
94 | 95 | # Try to get the public key via an API call first
|
95 |
| - publickey_url = "http://{}:8091/publickey".format(pod_ip) |
| 96 | + publickey_url = "http://{}:{}/publickey".format(pod_ip, str(SSH_TARGET_PUBLICKEY_API_PORT)) |
96 | 97 | timeout_seconds = 10
|
97 | 98 | try:
|
98 | 99 | request = requests.request("GET", publickey_url, timeout=timeout_seconds)
|
@@ -145,9 +146,24 @@ def get_authorized_keys_docker(query_cache: list = []) -> (list, list):
|
145 | 146 | new_query_cache.append(container.id)
|
146 | 147 | continue
|
147 | 148 |
|
148 |
| - exec_result = container.exec_run(PRINT_KEY_COMMAND) |
149 |
| - authorized_keys.append(exec_result[1].decode("utf-8")) |
150 |
| - new_query_cache.append(container.id) |
| 149 | + key = None |
| 150 | + # Try to get the public key via an API call first |
| 151 | + publickey_url = "http://{}:{}/publickey".format(container.id, str(SSH_TARGET_PUBLICKEY_API_PORT)) |
| 152 | + timeout_seconds = 10 |
| 153 | + try: |
| 154 | + request = requests.request("GET", publickey_url, timeout=timeout_seconds) |
| 155 | + if request.status_code == 200: |
| 156 | + key = request.text |
| 157 | + except requests.exceptions.ConnectTimeout: |
| 158 | + print("Connection to {ip} timed out after {timeout} seconds. Will try to exec into the pod to retrieve the key.".format(ip=pod_ip, timeout=str(timeout_seconds))) |
| 159 | + |
| 160 | + if key is None: |
| 161 | + exec_result = container.exec_run(PRINT_KEY_COMMAND) |
| 162 | + key = exec_result[1].decode("utf-8") |
| 163 | + |
| 164 | + if key is not None: |
| 165 | + authorized_keys.append(key) |
| 166 | + new_query_cache.append(container.id) |
151 | 167 |
|
152 | 168 | return authorized_keys, new_query_cache
|
153 | 169 |
|
|
0 commit comments