Skip to content

Commit

Permalink
move generate-ssh-key-fingerprint into initdb of api-db
Browse files Browse the repository at this point in the history
  • Loading branch information
Schnitzel committed Mar 19, 2019
1 parent b3997e3 commit cbd7e68
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 22 deletions.
18 changes: 0 additions & 18 deletions helpers/577-api-db-generate-ssh-key-fingerprints.sh

This file was deleted.

2 changes: 2 additions & 0 deletions services/api-db/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
ARG IMAGE_REPO
FROM ${IMAGE_REPO:-lagoon}/mariadb

RUN apk add --no-cache openssh-keygen

ENV MARIADB_DATABASE=infrastructure \
MARIADB_USER=api \
MARIADB_PASSWORD=api \
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

set -eu -o pipefail

# disable globbing
set -f;
# set field separator to NL (only)
IFS=$'\n';

DUPLICATE_SSHKEY_RECORDS=( $(mysql infrastructure --batch -sse "SELECT count(*) count, key_value FROM ssh_key GROUP BY key_value HAVING count > 1") );

if [ ${#DUPLICATE_SSHKEY_RECORDS[@]} -ne 0 ]; then
echo "====== FOUND DUPLICATE SSH KEYS IN LAGOON API DATABASE!"
for DUPLICATE_SSHKEY_RECORD in "${DUPLICATE_SSHKEY_RECORDS[@]}";
do
echo ""
echo $(awk '{print $2}' <<< "$DUPLICATE_SSHKEY_RECORD");
done;
echo ""
echo "====== PLEASE REMOVE DUPLICATED SSH KEYS AND RUN INITIALIZATION OF DB AGAIN"
#exit 1
fi

echo "=== Starting SSH KEY Fingerprint generation"

# get all ssh keys which have no fingerprint yet from api-db into a bash array
SSHKEY_RECORDS=( $(mysql infrastructure --batch -sse "SELECT id, key_type, key_value FROM ssh_key WHERE key_fingerprint is NULL") );

for SSHKEY_RECORD in "${SSHKEY_RECORDS[@]}";
do
RECORD_ID=$(awk '{print $1}' <<< "$SSHKEY_RECORD");
SSHKEY=$(awk '{print $2, $3}' <<< "$SSHKEY_RECORD");
FINGERPRINT=$(ssh-keygen -lE sha256 -f - <<< "$SSHKEY" | awk '{print $2}');
echo "Adding SSH Key Fingerprint for SSH KEY '$RECORD_ID': $FINGERPRINT"
mysql infrastructure -e "UPDATE ssh_key SET key_fingerprint = '$FINGERPRINT' WHERE id = $RECORD_ID";
done;

echo "=== Finished SSH KEY Fingerprint generation"
13 changes: 9 additions & 4 deletions services/api-db/rerun_initdb.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#!/bin/sh
#!/bin/bash

INITDB_DIR="/docker-entrypoint-initdb.d"

for sql_file in `ls $INITDB_DIR`; do mysql --verbose < "$INITDB_DIR/$sql_file" ; done
for f in `ls /docker-entrypoint-initdb.d/*`; do
case "$f" in
*.sh) echo "$0: running $f"; . "$f" ;;
*.sql) echo "$0: running $f"; cat $f| tee | mysql --verbose; echo ;;
*) echo "$0: ignoring $f" ;;
esac
echo
done

0 comments on commit cbd7e68

Please sign in to comment.