Last active
August 11, 2023 05:53
-
-
Save sloppycoder/959c90ecb54d570bfc34f9a207c8b5f1 to your computer and use it in GitHub Desktop.
create an k8s secret from a ssh private key
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| balk() { | |
| echo $1 | |
| echo key2secret.sh "<private_key> <namespace> <secret_name> <ssh_host>" | |
| exit 1 | |
| } | |
| if [ "$1" = "" ]; then | |
| balk "please specify private key" | |
| fi | |
| if [ "$2" = "" ]; then | |
| balk "please specify namespace" | |
| fi | |
| SECRET_NAME=git-secret | |
| if [ "$3" != "" ]; then | |
| SECRET_NAME=$3 | |
| fi | |
| SSH_HOST=gitlab.com | |
| if [ "$4" != "" ]; then | |
| SSH_HOST=$4 | |
| fi | |
| PRIVATE_KEY=$1 | |
| PUBLIC_KEY=$1.pub | |
| NAMESPACE=$2 | |
| if [ ! -f "$PRIVATE_KEY" ]; then | |
| echo private key file $PRIVATE_KEY does not exist | |
| exit 1 | |
| fi | |
| ssh-keygen -y -f $PRIVATE_KEY > $PUBLIC_KEY | |
| ssh-keyscan $SSH_HOST > known_hosts | |
| kubectl delete secret $SECRET_NAME -n $NAMESPACE | |
| kubectl create secret generic $SECRET_NAME --from-file=$PRIVATE_KEY=$PRIVATE_KEY --from-file=$PUBLIC_KEY=$PUBLIC_KEY --from-file=known_hosts=known_hosts -n $NAMESPACE | |
| rm -f known_hosts $PUBLIC_KEY |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment