|
6 | 6 | # - Prepares DNS configuration for exec tasks
|
7 | 7 | # - Renders the Nomad client configuration
|
8 | 8 | # - 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 |
9 | 10 | # - Starts the Nomad service
|
10 | 11 |
|
11 | 12 | set -Eeuo pipefail
|
@@ -108,6 +109,60 @@ load_bridge() {
|
108 | 109 | fi
|
109 | 110 | }
|
110 | 111 |
|
| 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 | +} |
111 | 166 |
|
112 | 167 | # Enables nomad systemd service
|
113 | 168 | start_nomad() {
|
@@ -203,8 +258,6 @@ plugin "docker" {
|
203 | 258 | EOF
|
204 | 259 | }
|
205 | 260 |
|
206 |
| - |
207 |
| - |
208 | 261 | add_host_volumes_to_nomad() {
|
209 | 262 | cat <<EOF >>/etc/nomad.d/nomad.hcl
|
210 | 263 | client {
|
@@ -252,6 +305,12 @@ add_host_volumes_to_nomad
|
252 | 305 | log "INFO" "No host volumes configured for Nomad client"
|
253 | 306 | %{ endif }
|
254 | 307 |
|
| 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 } |
255 | 314 |
|
256 | 315 | log "INFO" "Modify Nomad systemd config"
|
257 | 316 | modify_nomad_systemd_config
|
|
0 commit comments