|
35 | 35 | # TRUSTSTORE_IMPORTS="myRootCA:/mountedpath/enterprise_root_cert.pem myBizPartnerCA:/mountedpath/mybiz_partner_cert.pem" |
36 | 36 | # Description: |
37 | 37 | # This will import the specified certificates into the truststore JKS with the provided alias name(s) during the container startup process. |
| 38 | +# - KNOX_ALIAS_LABEL - (optional) label selector used to discover Knox alias |
| 39 | +# Secrets in the pod's namespace, default 'knox.apache.org/alias=true'. |
38 | 40 |
|
39 | 41 |
|
40 | 42 | set -e |
@@ -92,6 +94,49 @@ saveAlias() { |
92 | 94 | fi |
93 | 95 | } |
94 | 96 |
|
| 97 | +## Helper function to load Knox aliases from labeled Kubernetes Secrets. |
| 98 | +loadAliasesFromK8sSecrets() { |
| 99 | + local sa_token_file="/var/run/secrets/kubernetes.io/serviceaccount/token" |
| 100 | + local sa_ca_file="/var/run/secrets/kubernetes.io/serviceaccount/ca.crt" |
| 101 | + local sa_ns_file="/var/run/secrets/kubernetes.io/serviceaccount/namespace" |
| 102 | + local label="${KNOX_ALIAS_LABEL:-knox.apache.org/alias=true}" |
| 103 | + |
| 104 | + if [[ ! -r ${sa_token_file} || ! -r ${sa_ca_file} || ! -r ${sa_ns_file} ]]; then |
| 105 | + echo "ServiceAccount token not mounted; skipping k8s-sourced Knox aliases" |
| 106 | + return 0 |
| 107 | + fi |
| 108 | + |
| 109 | + echo "Loading Knox aliases from labeled k8s Secrets (label: ${label}) ..." |
| 110 | + local namespace token resp_file http_code secret_names |
| 111 | + namespace=$(/bin/cat "${sa_ns_file}") |
| 112 | + token=$(/bin/cat "${sa_token_file}") |
| 113 | + resp_file=$(mktemp) |
| 114 | + |
| 115 | + http_code=$(curl -sS \ |
| 116 | + --cacert "${sa_ca_file}" \ |
| 117 | + -H "Authorization: Bearer ${token}" \ |
| 118 | + -o "${resp_file}" -w "%{http_code}" \ |
| 119 | + "https://kubernetes.default.svc/api/v1/namespaces/${namespace}/secrets?labelSelector=${label}") || http_code="000" |
| 120 | + |
| 121 | + if [[ ${http_code} != "200" ]]; then |
| 122 | + echo "WARN: failed to list Knox alias Secrets from k8s API (HTTP ${http_code}); continuing without k8s aliases" |
| 123 | + /bin/rm -f "${resp_file}" |
| 124 | + return 0 |
| 125 | + fi |
| 126 | + |
| 127 | + secret_names=$(jq -r '.items[].metadata.name' "${resp_file}" | tr '\n' ' ') || secret_names="" |
| 128 | + /bin/rm -f "${resp_file}" |
| 129 | + |
| 130 | + if [[ -z ${secret_names} ]]; then |
| 131 | + echo "No labeled Knox alias Secrets found" |
| 132 | + return 0 |
| 133 | + fi |
| 134 | + |
| 135 | + # shellcheck disable=SC2086 |
| 136 | + /home/knox/knox/bin/knoxcli.sh create-k8s-alias ${secret_names} --namespace "${namespace}" |
| 137 | + echo "Knox aliases loaded from k8s Secrets: ${secret_names}" |
| 138 | +} |
| 139 | + |
95 | 140 | export GATEWAY_SERVER_RUN_IN_FOREGROUND=true |
96 | 141 |
|
97 | 142 |
|
|
131 | 176 | /home/knox/knox/bin/knoxcli.sh generate-jwk --saveAlias knox.token.hash.key |
132 | 177 | fi |
133 | 178 |
|
| 179 | +loadAliasesFromK8sSecrets |
| 180 | + |
134 | 181 | # If keystore dir is empty use default one |
135 | 182 | if [[ -z ${KEYSTORE_DIR} ]] |
136 | 183 | then |
|
0 commit comments