Sfoglia il codice sorgente

add new script del/newSSHKey

Virgil Ollivier 5 mesi fa
parent
commit
6a960f5a8a
2 ha cambiato i file con 43 aggiunte e 0 eliminazioni
  1. 7 0
      scripts/bash/delSSHKey
  2. 36 0
      scripts/bash/newSSHKey

+ 7 - 0
scripts/bash/delSSHKey

@@ -0,0 +1,7 @@
+#!/bin/bash
+
+KEY_NAME="${1:?need a SSHKey name from /me}"
+
+./ovh-bash delete "me/sshKey/${KEY_NAME}" | jq -r 'select(. != null)'
+
+exit $?

+ 36 - 0
scripts/bash/newSSHKey

@@ -0,0 +1,36 @@
+#!/bin/bash
+
+KEY_NAME="${1:?need a SSHKey name from /me}"
+KEY_DIR="${HOME}/.ssh/keys.d"
+
+# check if sshkey exists locally
+if [ -e "${KEY_DIR}/${KEY_NAME}" ] ; then
+  echo "SSH Key ${KEY_DIR}/${KEY_NAME} already exists locally"
+else
+  echo "SSH Key ${KEY_DIR}/${KEY_NAME} not found locally"
+  echo ">>> Creating it ..."
+  ssh-keygen -t ed25519 -f "${KEY_DIR}/${KEY_NAME}" -C "OVH_${KEY_NAME}" -q -N ""
+  ./delSSHKey "${KEY_NAME}"
+fi
+PUBKEY="$( cat "${KEY_DIR}/${KEY_NAME}.pub" )"
+
+# check if KEY_NAME exists into OVH User Account /me
+# remote_keyname = "string or empty"
+remote_keyname=$( ./ovh-bash get "me/sshKey" | jq --arg v "${KEY_NAME}" -r '.|to_entries[]|select(.value == $v ).value' )
+
+# if remote_keyname exists check if pubkey is matching.
+remote_pubkey=$( ./ovh-bash get "me/sshKey/${KEY_NAME}" | jq -r '.key' )
+  
+# if remote_keyname does not match KEY_NAME = this KEY_NAME is not already uploaded into OVH Account
+if [ -z "${remote_keyname}" ] || [ "${remote_pubkey}" != "${PUBKEY}" ] ; then
+  echo "SSH Keys does not exists remotely"
+  echo ">>> Uploading ${KEY_DIR}/${KEY_NAME} to OVH account"
+  HTTP_PAYLOAD='{ "key": "'${PUBKEY}'", "keyName": "'${KEY_NAME}'" }'
+
+  ./ovh-bash post "me/sshKey" "${HTTP_PAYLOAD}" | jq -r 'select(. != null)'
+
+else
+  echo "SSH Keys are up-to-date"
+fi
+
+exit $?