-
Notifications
You must be signed in to change notification settings - Fork 288
Expand file tree
/
Copy pathpattern.sh
More file actions
executable file
·184 lines (164 loc) · 6.56 KB
/
Copy pathpattern.sh
File metadata and controls
executable file
·184 lines (164 loc) · 6.56 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/bin/bash
set -euo pipefail
function is_available {
command -v "$1" >/dev/null 2>&1 || { echo >&2 "$1 is required but it's not installed. Aborting."; exit 1; }
}
function version {
echo "$1" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }'
}
# We need this check mostly for CI testing, we do not want to run container in container
function is_container() {
[ -n "${KUBERNETES_SERVICE_HOST:-}" ] && return 0
[ -f /.dockerenv ] && return 0
[ -f /run/.containerenv ] && return 0
return 1
}
function verify_image() {
local image="$1"
case "${image}" in
quay.io/validatedpatterns/*|quay.io/hybridcloudpatterns/*)
;;
*)
echo "Skipping image verification for third-party registry"
return 0
;;
esac
if ! command -v cosign >/dev/null 2>&1; then
echo "WARNING: cosign is not installed, cannot verify image signature"
echo "Install cosign to enable image verification: https://docs.sigstore.dev/cosign/system_config/installation/"
return 0
fi
echo "Verifying image signature for ${image}..."
local output rc
local oidc_issuer="${VP_COSIGN_OIDC_ISSUER:-https://token.actions.githubusercontent.com}"
local cert_identity="${VP_COSIGN_CERT_IDENTITY:-https://github.com/validatedpatterns/utility-container/.*}"
output=$(cosign verify \
--certificate-oidc-issuer "${oidc_issuer}" \
--certificate-identity-regexp "${cert_identity}" \
"${image}" 2>&1) && rc=$? || rc=$?
if [ "${rc}" -eq 0 ]; then
echo "Image signature verified successfully"
elif [ "${rc}" -ge 10 ] && [ "${rc}" -le 13 ]; then
echo "ERROR: Image signature verification failed for ${image} (exit code ${rc})"
echo "${output}"
echo "Set VP_VERIFY_IMAGE=false to skip this check"
exit 1
else
echo "WARNING: Could not verify image signature for ${image} (likely a network issue)"
echo "Set VP_VERIFY_IMAGE=false to skip this check"
fi
}
if is_container; then
echo "Already running in a container"
exec "$@"
fi
if [ -z "${PATTERN_UTILITY_CONTAINER:-}" ]; then
PATTERN_UTILITY_CONTAINER="quay.io/validatedpatterns/utility-container"
fi
# If PATTERN_DISCONNECTED_HOME is set it will be used to populate both PATTERN_UTILITY_CONTAINER
# and PATTERN_INSTALL_CHART automatically
if [ -n "${PATTERN_DISCONNECTED_HOME:-}" ]; then
PATTERN_UTILITY_CONTAINER="${PATTERN_DISCONNECTED_HOME}/utility-container"
PATTERN_INSTALL_CHART="oci://${PATTERN_DISCONNECTED_HOME}/pattern-install"
echo "PATTERN_DISCONNECTED_HOME is set to ${PATTERN_DISCONNECTED_HOME}"
echo "Setting the following variables:"
echo " PATTERN_UTILITY_CONTAINER: ${PATTERN_UTILITY_CONTAINER}"
echo " PATTERN_INSTALL_CHART: ${PATTERN_INSTALL_CHART}"
fi
readonly commands=(podman)
for cmd in "${commands[@]}"; do is_available "$cmd"; done
UNSUPPORTED_PODMAN_VERSIONS="1.6 1.5"
PODMAN_VERSION_STR=$(podman --version) || { echo "Failed to get podman version"; exit 1; }
for i in ${UNSUPPORTED_PODMAN_VERSIONS}; do
# We add a space
if echo "${PODMAN_VERSION_STR}" | grep -q -E "\b${i}"; then
echo "Unsupported podman version. We recommend > 4.3.0"
podman --version
exit 1
fi
done
# podman --version outputs:
# podman version 4.8.2
PODMAN_VERSION=$(echo "${PODMAN_VERSION_STR}" | awk '{ print $NF }')
# podman < 4.3.0 do not support keep-id:uid=...
PODMAN_ARGS=()
if [ "$(version "${PODMAN_VERSION}")" -lt "$(version "4.3.0")" ]; then
PODMAN_ARGS=(-v "${HOME}:/root")
else
# We do not rely on bash's $UID and $GID because on MacOSX $GID is not set
MYNAME=$(id -n -u)
MYUID=$(id -u)
MYGID=$(id -g)
PODMAN_ARGS=(--passwd-entry "${MYNAME}:x:${MYUID}:${MYGID}::/pattern-home:/bin/bash" --user "${MYUID}:${MYGID}" --userns "keep-id:uid=${MYUID},gid=${MYGID}")
fi
if [ -n "${KUBECONFIG:-}" ]; then
# Check if KUBECONFIG path starts with HOME directory
if [[ ! "${KUBECONFIG}" =~ ^"${HOME}" ]]; then
echo "${KUBECONFIG} is pointing outside of the HOME folder, this will make it unavailable from the container."
echo "Please move it somewhere inside your $HOME folder, as that is what gets bind-mounted inside the container"
exit 1
fi
fi
# Detect if we use podman machine. If we do not then we bind mount local host ssl folders
# if we are using podman machine then we do not bind mount anything (for now!)
REMOTE_PODMAN=$(podman system connection list | tail -n +2 | wc -l) || REMOTE_PODMAN=0
PKI_HOST_MOUNT_ARGS=()
if [ "${REMOTE_PODMAN}" -eq 0 ]; then # If we are not using podman machine we check the hosts folders
# We check /etc/pki/tls because on ubuntu /etc/pki/fwupd sometimes
# exists but not /etc/pki/tls and we do not want to bind mount in such a case
# as it would find no certificates at all.
if [ -d /etc/pki/tls ]; then
PKI_HOST_MOUNT_ARGS=(-v /etc/pki:/etc/pki:ro)
elif [ -d /etc/ssl ]; then
PKI_HOST_MOUNT_ARGS=(-v /etc/ssl:/etc/ssl:ro)
else
PKI_HOST_MOUNT_ARGS=(-v /usr/share/ca-certificates:/usr/share/ca-certificates:ro)
fi
fi
# Parse EXTRA_ARGS into an array if set
EXTRA_ARGS_ARRAY=()
if [ -n "${EXTRA_ARGS:-}" ]; then
# shellcheck disable=SC2206
EXTRA_ARGS_ARRAY=(${EXTRA_ARGS})
fi
if [ "${VP_VERIFY_IMAGE:-true}" != "false" ]; then
verify_image "$PATTERN_UTILITY_CONTAINER"
fi
# Copy Kubeconfig from current environment. The utilities will pick up ~/.kube/config if set so it's not mandatory
# $HOME is mounted as itself for any files that are referenced with absolute paths
# $HOME is mounted to /root because the UID in the container is 0 and that's where SSH looks for credentials
podman run -it --rm --pull=newer \
--security-opt label=disable \
-e ANSIBLE_STDOUT_CALLBACK \
-e DISABLE_VALIDATE_ORIGIN \
-e EXTRA_HELM_OPTS \
-e EXTRA_PLAYBOOK_OPTS \
-e K8S_AUTH_HOST \
-e K8S_AUTH_PASSWORD \
-e K8S_AUTH_SSL_CA_CERT \
-e K8S_AUTH_TOKEN \
-e K8S_AUTH_USERNAME \
-e K8S_AUTH_VERIFY_SSL \
-e KUBECONFIG \
-e PATTERN_DIR \
-e PATTERN_DISCONNECTED_HOME \
-e PATTERN_INSTALL_CHART \
-e PATTERN_NAME \
-e TARGET_BRANCH \
-e TARGET_CLUSTERGROUP \
-e TARGET_VARIANT \
-e TARGET_ORIGIN \
-e TOKEN_NAMESPACE \
-e TOKEN_SECRET \
-e UUID_FILE \
-e VALUES_SECRET \
-e 'VP_*' \
${PKI_HOST_MOUNT_ARGS[@]+"${PKI_HOST_MOUNT_ARGS[@]}"} \
-v "$(pwd -P)":"$(pwd -P)" \
-v "${HOME}":"${HOME}" \
-v "${HOME}":/pattern-home \
"${PODMAN_ARGS[@]}" \
${EXTRA_ARGS_ARRAY[@]+"${EXTRA_ARGS_ARRAY[@]}"} \
-w "$(pwd -P)" \
"$PATTERN_UTILITY_CONTAINER" \
"$@"