Skip to content

Commit b781db2

Browse files
authored
Merge pull request #24 from zerodha/user-ssh-keys
feat: add support for adding ssh public keys to user
2 parents bb48b39 + 21ebddf commit b781db2

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

modules/nomad-clients/locals.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ locals {
77
nomad_join_tag_value = var.nomad_join_tag_value
88
nomad_file_limit = var.nomad_file_limit
99
nomad_client_exec_host_volumes = var.nomad_client_exec_host_volumes
10+
ssh_user = var.ssh_user
11+
ssh_public_keys = var.ssh_public_keys
1012
nomad_client_cfg = templatefile("${path.module}/templates/nomad.tftpl", {
1113
nomad_dc = var.cluster_name
1214
nomad_acl_enable = var.nomad_acl_enable

modules/nomad-clients/scripts/setup_client.tftpl.sh

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# - Prepares DNS configuration for exec tasks
77
# - Renders the Nomad client configuration
88
# - Optionally, adds Docker configuration to Nomad if the 'enable_docker_plugin' variable is set to true
9+
# - Adds SSH public keys to authorized_keys if provided
910
# - Starts the Nomad service
1011

1112
set -Eeuo pipefail
@@ -108,6 +109,60 @@ load_bridge() {
108109
fi
109110
}
110111

112+
# Add SSH public keys to authorized_keys file
113+
add_ssh_keys() {
114+
# Use the configured SSH user or default to "ubuntu"
115+
SSH_USER="${ssh_user}"
116+
117+
# Check if the user exists
118+
if ! id "$SSH_USER" &>/dev/null; then
119+
log "ERROR" "User $SSH_USER does not exist. Cannot add SSH keys."
120+
return 1
121+
fi
122+
123+
# Get user's home directory from /etc/passwd
124+
USER_HOME=$(getent passwd "$SSH_USER" | cut -d: -f6)
125+
if [ -z "$USER_HOME" ]; then
126+
log "ERROR" "Could not determine home directory for user $SSH_USER"
127+
return 1
128+
fi
129+
130+
SSH_DIR="$USER_HOME/.ssh"
131+
AUTH_KEYS_FILE="$SSH_DIR/authorized_keys"
132+
133+
log "INFO" "Adding SSH keys for user $SSH_USER (home: $USER_HOME)"
134+
135+
# Ensure .ssh directory exists and has correct permissions
136+
if [ ! -d "$SSH_DIR" ]; then
137+
mkdir -p "$SSH_DIR"
138+
log "INFO" "Created $SSH_DIR directory"
139+
fi
140+
chmod 700 "$SSH_DIR"
141+
chown "$SSH_USER:$SSH_USER" "$SSH_DIR"
142+
143+
# Create authorized_keys file if it doesn't exist
144+
if [ ! -f "$AUTH_KEYS_FILE" ]; then
145+
touch "$AUTH_KEYS_FILE"
146+
log "INFO" "Created $AUTH_KEYS_FILE file"
147+
fi
148+
chmod 600 "$AUTH_KEYS_FILE"
149+
chown "$SSH_USER:$SSH_USER" "$AUTH_KEYS_FILE"
150+
151+
# Add each provided SSH public key
152+
%{ for key in ssh_public_keys }
153+
if ! grep -q "${key}" "$AUTH_KEYS_FILE"; then
154+
echo "${key}" >> "$AUTH_KEYS_FILE"
155+
log "INFO" "Added SSH public key to authorized_keys for $SSH_USER"
156+
else
157+
log "INFO" "SSH public key already exists in authorized_keys for $SSH_USER"
158+
fi
159+
%{ endfor }
160+
161+
# Ensure the authorized_keys file has the correct ownership and permissions
162+
# This is crucial since the script runs as root
163+
chmod 600 "$AUTH_KEYS_FILE"
164+
chown "$SSH_USER:$SSH_USER" "$AUTH_KEYS_FILE"
165+
}
111166

112167
# Enables nomad systemd service
113168
start_nomad() {
@@ -203,8 +258,6 @@ plugin "docker" {
203258
EOF
204259
}
205260

206-
207-
208261
add_host_volumes_to_nomad() {
209262
cat <<EOF >>/etc/nomad.d/nomad.hcl
210263
client {
@@ -252,6 +305,12 @@ add_host_volumes_to_nomad
252305
log "INFO" "No host volumes configured for Nomad client"
253306
%{ endif }
254307

308+
%{ if length(ssh_public_keys) > 0 }
309+
log "INFO" "Adding SSH public keys to authorized_keys"
310+
add_ssh_keys || log "WARN" "Failed to add SSH keys"
311+
%{ else }
312+
log "INFO" "No SSH public keys provided to add"
313+
%{ endif }
255314

256315
log "INFO" "Modify Nomad systemd config"
257316
modify_nomad_systemd_config

modules/nomad-clients/variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,18 @@ variable "nomad_client_exec_host_volumes" {
221221
default = {}
222222
}
223223

224+
variable "ssh_public_keys" {
225+
description = "List of SSH public keys to add to authorized_keys"
226+
type = list(string)
227+
default = []
228+
}
229+
230+
variable "ssh_user" {
231+
description = "The system user to add SSH keys for"
232+
type = string
233+
default = "ubuntu"
234+
}
235+
224236
variable "extra_script" {
225237
description = "Path to custom script to be run as part of cloud-init"
226238
type = string

0 commit comments

Comments
 (0)