Skip to content

Commit 5e92c9a

Browse files
bin: refactor and add flag to skip sops encrypt validation
1 parent 95388cd commit 5e92c9a

File tree

3 files changed

+56
-26
lines changed

3 files changed

+56
-26
lines changed

bin/apply.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,6 @@ esac
8989
check_node_label "$1" elastisys.io/node-group
9090
update_ips_dryrun "$1" "${environment}"
9191
check_upgrade "$1"
92-
config_load "$1"
92+
config_load "$1" --skip-test-encrypt
9393
[[ -z "${CK8S_CI_SKIP_APPLY:-}" ]] || exit 0 # Improve mockability in the future
9494
apps_apply "$1" "${environment}" "${@:2}"

bin/common.bash

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -416,14 +416,15 @@ validate_config() {
416416
schema_validate() {
417417
merged_config="${1}"
418418
schema_file="${2}"
419+
verbose="${3:-}"
419420

420421
schema_validation_result="$(mktemp --suffix='.txt')"
421422
append_trap "rm ${schema_validation_result}" EXIT
422423

423424
if ! yajsv -s "${schema_file}" "${merged_config}" >"${schema_validation_result}"; then
424425
log_warning "Failed schema validation:"
425426
sed -r 's/^.*_(..-config\.yaml): fail: (.*)/\1: \2/; / failed validation$/q' <"${schema_validation_result}"
426-
if [[ "${3:-}" == "-v" ]]; then
427+
if [[ "${verbose}" == "true" ]]; then
427428
grep -oP '(?<=fail: )[^:]+' "${schema_validation_result}" | sort -u |
428429
while read -r jpath; do
429430
if [[ $jpath != "(root)" ]]; then
@@ -485,34 +486,63 @@ validate_sops_config() {
485486
exit 1
486487
fi
487488

488-
# Compares the keyring with the sops config to see if the config has anything the keyring does not have.
489-
keyring=$(gpg --with-colons --list-keys | awk -F: '/^pub:.*/ { getline; print $10 }')
490-
creation_pgp=$(yq '[.creation_rules[].pgp // "" | split(",") | .[]] | unique | .[]' "${sops_config}")
491-
# Pass keyring fingerprints twice to ensure other keys will not be flagged
492-
fingerprints=$(tr ' ' '\n' <<<"${keyring} ${keyring} ${creation_pgp}" | sort | uniq -u)
493-
494-
# Find rules ending with trailing comma
495-
comma_search=$(yq '.creation_rules[] | select(.pgp == "*,")' "${sops_config}")
496-
497-
if [ -n "${fingerprints// /}" ] || [ "${comma_search: -1}" == "," ]; then
498-
log_error "ERROR: SOPS config contains no or invalid PGP keys."
499-
log_error "SOPS config: ${sops_config}:"
500-
yq 'split(" ") | {"missing or invalid fingerprints": .}' <<<"${fingerprints}" | cat
501-
log_error "Fingerprints must be uppercase and separated by commas."
502-
log_error "Recreate or edit the SOPS config to fix the issue"
503-
exit 1
489+
skip_test_encrypt="${1:-false}"
490+
491+
# should be able to decrypt $CK8S_CONFIG_PATH/secrets.yaml with some private key
492+
if ! sops --decrypt "${secrets["secrets_file"]}" >/dev/null; then
493+
log_fatal "Failed to decrypt ${secrets["secrets_file"]}. Ensure you have a private key that has a matching public key in ${sops_config}"
494+
fi
495+
496+
if [[ "${skip_test_encrypt}" == "false" ]]; then
497+
tmp_secret=$(mktemp --suffix=-secret)
498+
append_trap "rm ${tmp_secret}" EXIT
499+
500+
yq -i '.secret = "value"' "${tmp_secret}"
501+
502+
# this will fail if e.g. not all gpg public keys are imported to keyring
503+
if ! sops --config "${sops_config}" --in-place --encrypt "${tmp_secret}"; then
504+
log_fatal "Failed to encrypt file using fingerprints in ${sops_config}. If you are using GPG fingerprints, ensure they are in your keyring"
505+
fi
506+
507+
# test decrypt to ensure a matching private key is available
508+
if ! sops --in-place --decrypt "${tmp_secret}"; then
509+
log_fatal "Failed to decrypt file. Ensure your private key has a matching public key in ${sops_config}"
510+
fi
504511
fi
505512
}
506513

507514
# Load and validate all configuration options from the config path.
508-
# Usage: config_load <sc|wc> [--skip-validation|-v]
515+
# Usage: config_load <sc|wc> [--skip-validation] [-v] [--skip-test-encrypt]
509516
config_load() {
510-
load_config "$1"
517+
cluster="${1}"
518+
load_config "${cluster}"
519+
shift
520+
521+
skip_validation=false
522+
skip_test_encrypt=false
523+
verbose=false
524+
while [ "${#}" -gt 0 ]; do
525+
case "${1}" in
526+
--skip-validation)
527+
skip_validation=true
528+
;;
529+
--skip-test-encrypt)
530+
skip_test_encrypt=true
531+
;;
532+
-v)
533+
verbose=true
534+
;;
535+
*)
536+
log_error "Usage: config_load <sc|wc> [--skip-validation] [-v] [--skip-test-encrypt]"
537+
;;
538+
esac
539+
shift
540+
done
511541

512-
if [[ "${CK8S_SKIP_VALIDATION}" == "false" ]] && [[ "--skip-validation" != "${2:-''}" ]]; then
513-
validate_version "$1"
514-
validate_config "$1" "${2:-''}"
515-
validate_sops_config
542+
if [[ "${CK8S_SKIP_VALIDATION}" == "false" ]] && [[ "${skip_validation}" == "false" ]]; then
543+
validate_version "${cluster}"
544+
validate_config "${cluster}" "${verbose}"
545+
validate_sops_config "${skip_test_encrypt}"
516546
fi
517547
}
518548

bin/ops.bash

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ ops_helm() {
5454
ops_helmfile() {
5555
# Skip validation when fetching completions
5656
if [ "$2" == "__complete" ]; then
57-
config_load "$1" --skip-validation
57+
config_load "$1" --skip-validation --skip-test-encrypt
5858
else
59-
config_load "$1"
59+
config_load "$1" --skip-test-encrypt
6060
fi
6161

6262
case "${1}" in

0 commit comments

Comments
 (0)