Created
February 1, 2022 19:52
-
-
Save dwilliams782/fa177f05b6dea7df0a798cae8299c823 to your computer and use it in GitHub Desktop.
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 | |
| set -eou pipefail | |
| if [[ -z ${1+x} || ${1} == "all" ]]; then | |
| echo "Checking all namespaces" | |
| namespaces=$(kubectl get namespaces -o json | jq -r '.items[].metadata.name') | |
| else | |
| namespaces=${1} | |
| fi | |
| IGNORE_NAMESPACES=( | |
| "kube-system" | |
| ) | |
| FILENAME="secrets.csv" | |
| rm -f $FILENAME | |
| printf '%s\n' "namespace" "secret" | paste -sd ',' >> ${FILENAME} | |
| for namespace in $namespaces; do | |
| if [[ "${IGNORE_NAMESPACES[*]}" =~ ${namespace} ]]; then | |
| echo "Ignoring namespace: ${namespace}" | |
| continue | |
| fi | |
| echo "Checking namespace: ${namespace}" | |
| STS=$(kubectl get statefulset -n "${namespace}" -o json) | |
| DEPLOYMENT=$(kubectl get deployment -n "${namespace}" -o json) | |
| CRONJOB=$(kubectl get cronjob -n "${namespace}" -o json) | |
| SECRETS=() | |
| SECRETS+=$(echo "${STS}" | jq -r '.items[].spec.template.spec.volumes[]?.secret?.secretName? | . // empty') | |
| SECRETS+=$(echo "${STS}" | jq -r '.items[].spec.template.spec.containers[].env[]?.valueFrom?.secretKeyRef?.name | . // empty') | |
| SECRETS+=$(echo "${DEPLOYMENT}" | jq -r '.items[].spec.template.spec.volumes[]?.secret?.secretName? | . // empty') | |
| SECRETS+=$(echo "${DEPLOYMENT}" | jq -r '.items[].spec.template.spec.containers[].env[]?.valueFrom?.secretKeyRef?.name | . // empty') | |
| SECRETS+=$(echo "${CRONJOB}" | jq -r '.items[].spec.jobTemplate.spec.template.spec.containers[].env[]?.valueFrom?.secretKeyRef?.name | . // empty') | |
| SECRETS+=$(echo "${CRONJOB}" | jq -r '.items[].spec.jobTemplate.spec.template.spec.volumes[]?.secret?.secretName? | . // empty') | |
| UNIQUE=$(printf "%s\n" "${SECRETS[@]}" | sort -u) | |
| VAULTSECRETS=$(kubectl get vaultsecrets -n "${namespace}" -o json | jq -r '.items[].metadata.name') | |
| for secret in $UNIQUE; do | |
| if [[ ! "${VAULTSECRETS[*]}" =~ ${secret} ]]; then | |
| echo "Vaultsecret not found for: Namespace: ${namespace} secret: ${secret}" | |
| printf '%s\n' "${namespace}" "${secret}" | paste -sd ',' >> ${FILENAME} | |
| fi | |
| done | |
| done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment