Skip to content

Commit 73dd58d

Browse files
committed
Alternative secret generation.
Not yet working. Signed-off-by: Kurt Garloff <kurt@garloff.de>
1 parent 1408970 commit 73dd58d

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

_04-clouds-yaml.sh

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/bin/bash
2+
# Create cloud secret -- alternative. Not yet complete.
3+
set -e
4+
# We need settings
5+
unset KUBECONFIG
6+
if test -n "$1"; then
7+
SET="$1"
8+
else
9+
if test -e cluster-settings.env; then SET=cluster-settings.env;
10+
else echo "You need to pass a cluster-settings.env file as parameter"; exit 1
11+
fi
12+
fi
13+
# Read settings -- make sure you can trust it
14+
source "$SET"
15+
# Read helper
16+
THISDIR=$(dirname 0)
17+
source "$THISDIR/yaml_parse.sh"
18+
19+
# Create namespace
20+
test -n "$CS_NAMESPACE"
21+
kubectl create namespace "$CS_NAMESPACE" || true
22+
# Default clouds.yaml location
23+
CLOUDS_YAML=${CLOUDS_YAML:-~/.config/openstack/clouds.yaml}
24+
# Use csp helper chart to create cloud secret
25+
# Notes on expected clouds.yaml:
26+
# - It should have the secrets (which you often keep in secure.yaml instead) merged into it
27+
# NOTE: This will only work if the auth section is the last entry for this cloud in clouds.yaml
28+
# - The cloud should be called openstack
29+
# - We will detect a cacert in there and pass it to the helper chart
30+
if ! test -r "$CLOUDS_YAML"; then echo "clouds.yaml $CLOUDS_YAML not readable"; exit 2; fi
31+
CA=$(RMVTREE=1 extract_yaml clouds.$OS_CLOUD.cacert <$CLOUDS_YAML | sed 's/^\s*cacert: //' || true)
32+
OS_CACERT="${OS_CACERT:-$CA}"
33+
# Extract auth parts from secure.yaml if existent, assume same indentation
34+
SEC_YAML="${CLOUDS_YAML%clouds.yaml}secure.yaml"
35+
if test -r "$SEC_YAML"; then SECRETS=$(RMVTREE=1 RMVCOMMENT=1 extract_yaml clouds.$OS_CLOUD.auth < $SEC_YAML || true); fi
36+
if test -n "$SECRETS"; then
37+
echo "# Appending secrets from secure.yaml to clouds.yaml"
38+
fi
39+
# Determine whether we need to add project ID
40+
RAW_CLOUD=$(extract_yaml clouds.$OS_CLOUD <$CLOUDS_YAML)
41+
if ! echo "$RAW_CLOUD" | grep -q '^\s*project_id:' && echo "$RAW_CLOUD" | grep -q '^\s*project_name:'; then
42+
# Need openstack CLI for this
43+
PROJECT_NAME=$(echo "$RAW_CLOUD" | grep '^\s*project_name:' | sed 's/^\s*project_name: //')
44+
PROJECT_ID=$(openstack project show $PROJECT_NAME -c id -f value | tr -d '\r')
45+
INDENT=$(echo "$RAW_CLOUD" | grep '^\s*project_name:' | sed 's/^\(\s*\)project_name:.*$/\1/')
46+
SECRETS=$(echo -en "${INDENT}project_id: $PROJECT_ID\n$SECRETS")
47+
echo "# Appending project_id: $PROJECT_ID to clouds.yaml"
48+
fi
49+
# We need a region_name, add it in
50+
if ! echo "$RAW_CLOUD" | grep -q '^\s*region_name:'; then
51+
# Need openstack CLI for this
52+
REGION=$(openstack region list -c Region -f value | head -n1 | tr -d '\r')
53+
INDENT=$(echo "$RAW_CLOUD" | grep '^\s*auth:' | sed 's/^\(\s*\)auth:.*$/\1/')
54+
export INSERT="${INDENT}region_name: $REGION"
55+
echo "# Inserting region_name: $REGION to clouds.yaml"
56+
fi
57+
# Construct a clouds.yaml (mode 0600):
58+
# - Only extracting one cloud addressed by $OS_CLOUD
59+
# - By merging secrets in from secure.yaml
60+
# - By renaming it to openstack (current CS limitation)
61+
# - By removing cacert setting
62+
# Store it securely in ~/tmp/clouds-$OS_CLOUD.yaml
63+
echo "# Generating ~/tmp/clouds-$OS_CLOUD.yaml ..."
64+
OLD_UMASK=$(umask)
65+
umask 0177
66+
INJECTSUB="$SECRETS" INJECTSUBKWD="auth" RMVCOMMENT=1 extract_yaml clouds.$OS_CLOUD < $CLOUDS_YAML | sed "s/^\\(\\s*\\)\\($OS_CLOUD\\):/\\1openstack:/" > ~/tmp/clouds-$OS_CLOUD.yaml
67+
sed -i 's@^\(\s*cacert:\).*@\1 /etc/openstack/cacert.pem@' ~/tmp/clouds-$OS_CLOUD.yaml
68+
CL_YAML=$(ls ~/tmp/clouds-$OS_CLOUD.yaml)
69+
kubectl create secret -n kube-system generic clouds-yaml --from-file=$CL_YAML --dry-run=client -oyaml > ~/tmp/clouds-$OS_CLOUD-yaml-secret
70+
umask $OLD_UMASK
71+
if test -n "$OS_CACERT"; then
72+
OS_CACERT=${OS_CACERT/\~/$HOME}
73+
kubectl create secret -n kube-system generic cacert-pem --from-file=$OS_CACERT --dry-run=client -oyaml > ~/tmp/cacert-secret
74+
fi
75+
# FIXME: We will provide more settings in cluster-settings.env later, hardcode it for now
76+
#if test "$CS_CCMLB=octavia-ovn"; then OCTOVN="--set octavia_ovn=true"; else unset OCTOVN; fi
77+
# FIXME: How to pass the information that we want OVN loadbalancers???
78+

0 commit comments

Comments
 (0)