Skip to content

Commit 2afedc6

Browse files
committed
Merge branch 'release/0.1.10'
2 parents 2374276 + 737a498 commit 2afedc6

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ ENV SSH_PERMIT_TARGET_HOST="*" \
7575
SSH_TARGET_KEY_PATH="~/.ssh/id_ed25519.pub" \
7676
MANUAL_AUTH_FILE="false" \
7777
SSHD_ENVIRONMENT_VARIABLES="${_RESOURCES_PATH}/sshd_environment" \
78-
SSH_TARGET_PUBLICKEY_API_PORT=8080
78+
SSH_TARGET_PUBLICKEY_API_PORT=8080 \
79+
ENV_NAME_SSH_TARGET_LABELS=""
7980

8081
RUN \
8182
chmod -R ug+rwx $_RESOURCES_PATH && \

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ The container can be configured with the following environment variables (`--env
9696
<td>Defines on which port the other containers can be reached via ssh. The ssh connection to the target can only be made via this port then. The default value '*' permits any port.</td>
9797
<td>*</td>
9898
</tr>
99+
<tr>
100+
<td>SSH_TARGET_LABELS</td>
101+
<td>Specify which containers are targeted. Filters containers / pods via these labels. Must be in the form of "label1=value1,label2=value2,label3=value3". Default is empty string which disables filtering.</td>
102+
<td>""</td>
103+
</tr>
99104
<tr>
100105
<td>SSH_TARGET_PUBLICKEY_API_PORT</td>
101106
<td>Port where the target container exposes the /publickey endpoint (if used).</td>

docker-res/ssh/update_authorized_keys.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
SSH_PERMIT_TARGET_HOST = os.getenv("SSH_PERMIT_TARGET_HOST", "*")
2222
SSH_TARGET_KEY_PATH = os.getenv("SSH_TARGET_KEY_PATH", "~/.ssh/id_ed25519.pub")
2323
SSH_TARGET_PUBLICKEY_API_PORT = os.getenv("SSH_TARGET_PUBLICKEY_API_PORT", 8080)
24+
ENV_SSH_TARGET_LABELS = os.getenv("SSH_TARGET_LABELS", "")
2425

2526
authorized_keys_cache_file = "/etc/ssh/authorized_keys_cache"
2627
authorized_keys_cache_file_lock = "cache_files.lock"
@@ -78,7 +79,7 @@ def get_authorized_keys_kubernetes(query_cache: list = []) -> (list, list):
7879
"""
7980

8081
pod_list = kubernetes_client.list_namespaced_pod(
81-
NAMESPACE, field_selector="status.phase=Running")
82+
NAMESPACE, field_selector="status.phase=Running", label_selector=SSH_TARGET_LABELS)
8283
authorized_keys = []
8384
new_query_cache = []
8485
for pod in pod_list.items:
@@ -135,7 +136,12 @@ def get_authorized_keys_docker(query_cache: list = []) -> (list, list):
135136
136137
"""
137138

138-
containers = docker_client.containers.list()
139+
filters = {"status": "running"}
140+
if ENV_SSH_TARGET_LABELS != "":
141+
SSH_TARGET_LABELS = ENV_SSH_TARGET_LABELS.split(",")
142+
filters.update({"label": SSH_TARGET_LABELS})
143+
144+
containers = docker_client.containers.list(filters=filters)
139145
authorized_keys = []
140146
new_query_cache = []
141147
for container in containers:
@@ -154,8 +160,8 @@ def get_authorized_keys_docker(query_cache: list = []) -> (list, list):
154160
request = requests.request("GET", publickey_url, timeout=timeout_seconds)
155161
if request.status_code == 200:
156162
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)))
163+
except (requests.exceptions.ConnectionError, requests.exceptions.ConnectTimeout):
164+
print("Connection to {ip} timed out after {timeout} seconds. Will try to exec into the pod to retrieve the key.".format(ip=container.id, timeout=str(timeout_seconds)))
159165

160166
if key is None:
161167
exec_result = container.exec_run(PRINT_KEY_COMMAND)

docker-res/start_ssh.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
ENV_NAME_MANUAL_AUTH_FILE = "MANUAL_AUTH_FILE"
1515
ENV_MANUAL_AUTH_FILE = os.getenv(ENV_NAME_MANUAL_AUTH_FILE, "false")
1616

17+
ENV_NAME_SSH_TARGET_LABELS = "SSH_TARGET_LABELS"
18+
ENV_SSH_TARGET_LABELS = os.getenv(ENV_NAME_SSH_TARGET_LABELS, "")
19+
1720
if ENV_SSH_PERMIT_TARGET_HOST == "":
1821
print("The environment variable {} must be set.".format(ENV_NAME_PERMIT_TARGET_HOST))
1922
exit(1)
@@ -29,5 +32,6 @@
2932
# export environment variables to a file which sshd can read to preserve their values in the ssh session
3033
call("echo 'export {}={}' >> {}".format(ENV_NAME_PERMIT_TARGET_HOST, ENV_SSH_PERMIT_TARGET_HOST, os.getenv("SSHD_ENVIRONMENT_VARIABLES")), shell=True)
3134
call("echo 'export {}={}' >> {}".format(ENV_NAME_MANUAL_AUTH_FILE, ENV_MANUAL_AUTH_FILE, os.getenv("SSHD_ENVIRONMENT_VARIABLES")), shell=True)
35+
call("echo 'export {}={}' >> {}".format(ENV_NAME_SSH_TARGET_LABELS, ENV_SSH_TARGET_LABELS, os.getenv("SSHD_ENVIRONMENT_VARIABLES")), shell=True)
3236

3337
call("/usr/local/sbin/sshd -D -f " + SSHD_CONFIG, shell=True)

0 commit comments

Comments
 (0)