This repository was archived by the owner on Apr 20, 2022. It is now read-only.
forked from ggrocco/snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatch_wildcard.sh
executable file
·68 lines (62 loc) · 2.16 KB
/
patch_wildcard.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/bash
# Usage
#
# CONTEXT=prod KEY=base_64_encoded_key CRT=base_64_encoded_crt ./patch_wildcard.sh
# or
# ./patch_wildcard.sh prod base_64_encoded_key base_64_encoded_crt
#
#
# For argocd-secret:
#
# CONTEXT=prod KEY=base_64_encoded_key CRT=base_64_encoded_crt SECRET_NAME=argocd-secret ./patch_wildcard.sh
# or
# ./patch_wildcard.sh prod base_64_encoded_key base_64_encoded_crt argocd-secret
if [[ -z $CONTEXT || -z $KEY || -z $CRT ]]
then
CONTEXT=$1
KEY=$2
CRT=$3
else
CONTEXT=$CONTEXT
KEY=$KEY
CRT=$CRT
fi
if [[ -z $4 && -z $SECRET_NAME ]]
then
SECRET_NAME="wildcard-pwnhealth-com-tls"
elif [[ -z $SECRET_NAME ]]
then
SECRET_NAME=$4
fi
if [[ -z $CONTEXT || -z $KEY || -z $CRT ]]
then
echo "Please pass context, base 64 encoded key and base 64 encoded crt"
printf "Example usage:\n CONTEXT=prod KEY=base_64_encoded_key CRT=base_64_encoded_crt ./patch_wildcard.sh\n"
printf "Xor:\n ./patch_wildcard.sh prod base_64_encoded_key base_64_encoded_crt\n"
else
echo "Starting..."
printf "Patching secret: $SECRET_NAME \n"
kubectl get secrets --context=$CONTEXT --all-namespaces | # get all secrets
grep $SECRET_NAME | # filter by secrets that match wildcard-pwnhealth
awk '{ print $1 }' | # print the first column (the namespace)
xargs -n1 echo
read -r -p "Are you sure you want to update tls for the namespaces above in context: ${CONTEXT}? [y/N] " response
case "$response" in
[yY][eE][sS]|[yY])
kubectl get secrets --context=$CONTEXT --all-namespaces | # get all secrets
grep $SECRET_NAME | # filter by secrets that match wildcard-pwnhealth
awk '{ print $1 }' | # print the first column (the namespace)
# Iterate through every namespace and update the tls.key and tls.crt value
# for secret named: "wildcard-pwnhealth-com-tls"
xargs -n1 kubectl patch secret \
--context=$CONTEXT $SECRET_NAME \
--type='json' \
-p="[{'op' : 'replace' ,'path' : '/data/tls.key' ,'value' : '${KEY}'}, {'op' : 'replace' ,'path' : '/data/tls.crt' ,'value' : '${CRT}'}]" \
-n
;;
*)
echo "Action canceled."
;;
esac
echo "Done..."
fi