diff --git a/.gitignore b/.gitignore index 7ba14e0..bd03efb 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ __pycache__/ dqm2m_production.db_test *.env* *.pkl -*.log.* \ No newline at end of file +*.log.* +*.yaml \ No newline at end of file diff --git a/k8_encrypt_secret.sh b/k8_encrypt_secret.sh new file mode 100644 index 0000000..943f919 --- /dev/null +++ b/k8_encrypt_secret.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +# Script that encodes k8s secrets with base64. +# Might have many bugs!!! + +# Unencoded file to read secrets from +IN_FILE=k8_secret.yaml + +# File to write output to +OUT_FILE=k8_secret_encrypted.yaml + +# Files can also be passed as arguments to the script +if [ ! -z $1 ]; +then + IN_FILE=$1 +fi + +if [ ! -z $2 ]; +then + OUT_FILE=$2 +fi + +# Do not split with spaces in for loop +IFS=$'\n' + +# Copy input file to output, so that we can start replacing it in-place. +cat $IN_FILE > $OUT_FILE +# Use awk to get the secrets out of k8_secret, found under the "data" section in the yaml. +for i in $(awk '/^[^ ]/{ f=/^data:/; next } f{ if (match($0, /^\s+[a-zA-Z0-9_]+\s*:.+/)) { print $0 }}' $OUT_FILE); do + # For each line containing a secret, encode its value in the OUT_FILE in place. + # Set base64's wrap to zero to have it all in one line. + # Use commas in the sed regexp, as we may have '/' in the values (e.g. CMSWEB_FRONTEND_PROXY_URL). + # Leading spaces are not preserved in the replacement string, so we're adding them manually. + sed -r "s,^$i$, $(echo $i | awk '{print $1}') $(echo $i | awk '{printf $2}' | base64 --wrap 0) # $(echo $i | awk '{printf $2}'),g" -i $OUT_FILE +done diff --git a/k8_secret.yaml b/k8_secret.yaml index 0dff80b..02d2c9d 100644 --- a/k8_secret.yaml +++ b/k8_secret.yaml @@ -7,9 +7,9 @@ metadata: namespace: dqm type: Opaque data: - # Remember that, before applying this file with kubectl, + # Remember that, before applying this file with kubectl, # you will need to base64-encode the values and replace them - # with the encoded ones. E.g, "ENV: production" must + # with the encoded ones. E.g, "ENV: production" must # be replaced with "ENV: cHJvZHVjdGlvbg==" # To encode it: # @@ -28,4 +28,4 @@ data: SERVER_FFF_MACHINE: bu-c2f11-13-01 CMSWEB_FRONTEND_PROXY_URL: https://cmsweb.cern.ch/dqm/dqm-square-origin FFF_PRODUCTION_MACHINES: bu-c2f11-09-01;fu-c2f11-11-01;fu-c2f11-11-02;fu-c2f11-11-03;fu-c2f11-11-04 - FFF_PLAYBACK_MACHINES: bu-c2f11-13-01;fu-c2f11-15-01;fu-c2f11-15-02;fu-c2f11-15-03;fu-c2f11-15-04 \ No newline at end of file + FFF_PLAYBACK_MACHINES: bu-c2f11-13-01;fu-c2f11-15-01;fu-c2f11-15-02;fu-c2f11-15-03;fu-c2f11-15-04