11#! /bin/bash
22
3+ set -o pipefail
4+ set -o errexit
5+ set -o nounset
6+ set -o errtrace
37set -x
48
59source /usr/local/bin/crc-systemd-common.sh
610export KUBECONFIG=" /opt/kubeconfig"
711
812function gen_htpasswd() {
9- if [ ! -z " ${1} " ] && [ ! -z " ${2} " ]; then
10- podman run --rm -ti xmartlabs/htpasswd $1 $2 >> /tmp/htpasswd.txt
13+ if [ -z " ${1:- } " ] || [ -z " ${2:- } " ]; then
14+ echo " gen_htpasswd needs two arguments: username password" 1>&2
15+ return 1
1116 fi
17+
18+ podman run --rm docker.io/xmartlabs/htpasswd " $1 " " $2 "
1219}
1320
1421wait_for_resource secret
@@ -19,20 +26,24 @@ if [ ! -f /opt/crc/pass_developer ]; then
1926fi
2027
2128if [ ! -f /opt/crc/pass_kubeadmin ]; then
22- echo " developer password does not exist"
29+ echo " kubeadmin password does not exist"
2330 exit 1
2431fi
2532
26- PASS_DEVELOPER=$( cat /opt/crc/pass_developer)
27- PASS_KUBEADMIN=$( cat /opt/crc/pass_kubeadmin)
33+ echo " generating the kubeadmin and developer passwords ..."
2834
29- rm -f /tmp/htpasswd.txt
30- gen_htpasswd developer " ${PASS_DEVELOPER} "
31- gen_htpasswd kubeadmin " ${PASS_KUBEADMIN} "
35+ set +x # /!\ disable the logging to avoid leaking the passwords
3236
33- if [ -f /tmp/htpasswd.txt ] ; then
34- sed -i ' /^\s*$/d ' /tmp/htpasswd.txt
37+ dev_pass= $( gen_htpasswd developer " $( cat /opt/crc/pass_developer ) " )
38+ adm_pass= $( gen_htpasswd kubeadmin " $( cat /opt/crc/pass_kubeadmin ) " )
3539
36- oc create secret generic htpass-secret --from-file=htpasswd=/tmp/htpasswd.txt -n openshift-config --dry-run=client -o yaml > /tmp/htpass-secret.yaml
37- oc replace -f /tmp/htpass-secret.yaml
38- fi
40+ echo " creating the password secret ..."
41+ # use bash <() to use a temporary fd file
42+ # use sed to remove the empty lines
43+ oc create secret generic htpass-secret \
44+ --from-file=htpasswd=<( printf ' %s\n%s\n' " $dev_pass " " $adm_pass " ) \
45+ -n openshift-config \
46+ --dry-run=client -oyaml \
47+ | oc apply -f-
48+
49+ echo " all done"
0 commit comments