From 8e325b5086df6e045f92dac68d31037da3fbbcdb Mon Sep 17 00:00:00 2001 From: r-vdp Date: Mon, 4 Dec 2023 16:34:08 +0100 Subject: [PATCH] ec2: shellcheck fixes --- nixos/modules/virtualisation/ec2-data.nix | 12 +++++++----- nixos/modules/virtualisation/ec2-metadata-fetcher.sh | 5 +++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/nixos/modules/virtualisation/ec2-data.nix b/nixos/modules/virtualisation/ec2-data.nix index 0b9d098dbab76..036c8c593369a 100644 --- a/nixos/modules/virtualisation/ec2-data.nix +++ b/nixos/modules/virtualisation/ec2-data.nix @@ -33,7 +33,8 @@ with lib; if ! [ -e /root/.ssh/authorized_keys ]; then echo "obtaining SSH key..." - mkdir -m 0700 -p /root/.ssh + mkdir -p /root/.ssh + chown 0700 /root/.ssh if [ -s /etc/ec2-metadata/public-keys-0-openssh-key ]; then (umask 177; cat /etc/ec2-metadata/public-keys-0-openssh-key >> /root/.ssh/authorized_keys) echo "new key added to authorized_keys" @@ -45,19 +46,20 @@ with lib; # generate one normally. userData=/etc/ec2-metadata/user-data - mkdir -m 0755 -p /etc/ssh + mkdir -p /etc/ssh + chown 0755 /etc/ssh if [ -s "$userData" ]; then key="$(sed 's/|/\n/g; s/SSH_HOST_DSA_KEY://; t; d' $userData)" key_pub="$(sed 's/SSH_HOST_DSA_KEY_PUB://; t; d' $userData)" - if [ -n "$key" -a -n "$key_pub" -a ! -e /etc/ssh/ssh_host_dsa_key ]; then + if [ -n "$key" ] && [ -n "$key_pub" ] && [ ! -e /etc/ssh/ssh_host_dsa_key ]; then (umask 077; echo "$key" > /etc/ssh/ssh_host_dsa_key) echo "$key_pub" > /etc/ssh/ssh_host_dsa_key.pub fi key="$(sed 's/|/\n/g; s/SSH_HOST_ED25519_KEY://; t; d' $userData)" key_pub="$(sed 's/SSH_HOST_ED25519_KEY_PUB://; t; d' $userData)" - if [ -n "$key" -a -n "$key_pub" -a ! -e /etc/ssh/ssh_host_ed25519_key ]; then + if [ -n "$key" ] && [ -n "$key_pub" ] && [ ! -e /etc/ssh/ssh_host_ed25519_key ]; then (umask 077; echo "$key" > /etc/ssh/ssh_host_ed25519_key) echo "$key_pub" > /etc/ssh/ssh_host_ed25519_key.pub fi @@ -79,7 +81,7 @@ with lib; # ec2-get-console-output. echo "-----BEGIN SSH HOST KEY FINGERPRINTS-----" > /dev/console for i in /etc/ssh/ssh_host_*_key.pub; do - ${config.programs.ssh.package}/bin/ssh-keygen -l -f $i || true > /dev/console + ${config.programs.ssh.package}/bin/ssh-keygen -l -f "$i" || true > /dev/console done echo "-----END SSH HOST KEY FINGERPRINTS-----" > /dev/console ''; diff --git a/nixos/modules/virtualisation/ec2-metadata-fetcher.sh b/nixos/modules/virtualisation/ec2-metadata-fetcher.sh index 716aff7c22fba..5e0a908ee6c33 100644 --- a/nixos/modules/virtualisation/ec2-metadata-fetcher.sh +++ b/nixos/modules/virtualisation/ec2-metadata-fetcher.sh @@ -1,5 +1,6 @@ metaDir=/etc/ec2-metadata -mkdir -m 0755 -p "$metaDir" +mkdir -p "$metaDir" +chown 0755 "$metaDir" rm -f "$metaDir/*" get_imds_token() { @@ -40,7 +41,7 @@ while [ $try -le 3 ]; do sleep 1 done -if [ "x$IMDS_TOKEN" == "x" ]; then +if [ "$IMDS_TOKEN" == "" ]; then echo "failed to fetch an IMDS2v token." fi