Skip to content

Commit 8e9940c

Browse files
authored
Merge pull request #92 from dotconfig404/feat/nonroot
feat: openshift compatible container support
2 parents 026d588 + 1415b0b commit 8e9940c

11 files changed

Lines changed: 81 additions & 33 deletions

openvoxserver/Containerfile.alpine

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ ENV AUTOSIGN=true \
129129
OPENVOXSERVER_MAX_REQUESTS_PER_INSTANCE=0 \
130130
OPENVOXSERVER_PORT=8140 \
131131
PATH=$PATH:/opt/puppetlabs/server/bin:/opt/puppetlabs/puppet/bin:/opt/puppetlabs/bin \
132-
SSLDIR=/etc/puppetlabs/puppet/ssl \
133132
USE_OPENVOXDB=true \
134133
### build variables
135134
apps_dir=/opt/puppetlabs/server/apps \
@@ -192,6 +191,43 @@ RUN apk update \
192191
# install puppet gem as library into jruby loadpath
193192
&& puppetserver gem install --no-document openvox
194193

194+
# explicitly set HOME: random UIDs will cause HOME to be "/" generally
195+
ENV HOME=${data_dir}/puppetserver
196+
197+
# use system/root paths instead of non-root paths to make permission management
198+
# and volume mounting simpler. for this we link the appropiate paths and explicitly
199+
# set the base paths used for interpolation, i.e confdir, codedir, vardir, rundir and
200+
# logdir via the template and/or via `30-ensure-config.sh` in `puppet.conf`
201+
RUN mkdir -p ${HOME}/.puppetlabs/var \
202+
&& ln -sf /etc/puppetlabs ${HOME}/.puppetlabs/etc \
203+
&& ln -sf /opt/puppetlabs ${HOME}/.puppetlabs/opt \
204+
&& ln -sf /var/log/puppetlabs ${HOME}/.puppetlabs/var/log \
205+
&& ln -sf /var/run/puppetlabs ${HOME}/.puppetlabs/var/run
206+
207+
# mirror user permissions to group, set group to root, and set gid bit on dirs
208+
RUN for d in \
209+
/etc/puppetlabs \
210+
/var/log/puppetlabs \
211+
/var/run/puppetlabs \
212+
/opt/puppetlabs/ \
213+
; do \
214+
mkdir -p "$d"; \
215+
chgrp -R 0 "$d"; \
216+
chmod -R g=u "$d"; \
217+
find "$d" -type d -exec chmod g+s {} +; \
218+
done
219+
220+
# the foreground starting script has this check before running the server:
221+
# [ "$EUID" = "$(id -u ${USER})" ]
222+
# simply calling `id -u` results in the UID of the current user and the check will pass
223+
RUN sed -i 's/^ *USER="puppet"/USER=""/' /etc/default/puppetserver
224+
# `puppetserver setup` forces symlinking the "old" cadir to the "new" one for puppet 6 compatibility
225+
# reasons. this won't work because after creating a link ruby tries to call chown
226+
RUN sed -i '/Puppetserver::Ca::Utils::Config\.symlink_to_old_cadir/ s/^/# /' \
227+
/usr/lib/ruby/gems/3.4.0/gems/openvoxserver-ca-3.0.0/lib/puppetserver/ca/action/setup.rb
228+
229+
USER puppet:0
230+
195231
# k8s uses livenessProbe, startupProbe, readinessProbe and ignores HEALTHCHECK
196232
HEALTHCHECK --interval=20s --timeout=15s --retries=12 --start-period=3m CMD ["/healthcheck.sh"]
197233

openvoxserver/container-entrypoint.d/20-use-templates-initially.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ set -e
77
# default
88
TEMPLATES=/var/tmp/puppet
99

10-
cd /etc/puppetlabs/puppet
10+
cd "$(puppet config print confdir)"
1111
for f in auth.conf hiera.yaml puppet.conf puppetdb.conf; do
1212
[ -f "$TEMPLATES/$f" ] && [ ! -f "$f" ] && {
1313
echo "Copying template $f from $TEMPLATES"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
puppet config set confdir /etc/puppetlabs/puppet
6+
puppet config set vardir /opt/puppetlabs/puppet/cache
7+
puppet config set logdir /var/log/puppetlabs/puppet
8+
puppet config set codedir /etc/puppetlabs/code
9+
puppet config set rundir /var/run/puppetlabs
10+
puppet config set manage_internal_file_permissions false

openvoxserver/container-entrypoint.d/30-set-permissions.sh

Lines changed: 0 additions & 8 deletions
This file was deleted.

openvoxserver/container-entrypoint.d/40-update-puppetdb-conf.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
set -e
44

55
if test -n "${OPENVOXDB_SERVER_URLS}" ; then
6-
sed -i "s@^server_urls.*@server_urls = ${OPENVOXDB_SERVER_URLS}@" /etc/puppetlabs/puppet/puppetdb.conf
6+
sed -i "s@^server_urls.*@server_urls = ${OPENVOXDB_SERVER_URLS}@" $(puppet config print confdir)/puppetdb.conf
77
fi

openvoxserver/container-entrypoint.d/70-set-dns-alt-names.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ config_section=main
99
# an existing certificate on the /etc/puppetlabs/puppet volume
1010
if [ -n "${DNS_ALT_NAMES}" ]; then
1111
certname=$(puppet config print certname)
12-
if test ! -f "${SSLDIR}/certs/$certname.pem" ; then
12+
if test ! -f "$(puppet config print ssldir)/certs/$certname.pem" ; then
1313
puppet config set dns_alt_names "${DNS_ALT_NAMES}" --section "${config_section}"
1414
else
1515
actual=$(puppet config print dns_alt_names --section "${config_section}")

openvoxserver/container-entrypoint.d/89-csr_attributes.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
require 'json'
44
require 'yaml'
55

6+
target_path = ARGV[0] || '/etc/puppetlabs/puppet/csr_attributes.yaml'
67
begin
78
csr_yaml = YAML.dump(JSON.load(ENV['CSR_ATTRIBUTES']))
8-
File.write('/etc/puppetlabs/puppet/csr_attributes.yaml', csr_yaml)
9+
File.write(target_path, csr_yaml)
910
rescue => error
1011
puts "Error on reading JSON env. Terminating"
1112
puts "Malformed JSON: #{ENV['CSR_ATTRIBUTES']}"

openvoxserver/container-entrypoint.d/89-csr_attributes.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ set -e
55
# determine script location
66
readonly SCRIPT_FILENAME=$(readlink -f "${BASH_SOURCE[0]}")
77
readonly SCRIPT_PATH=$(dirname "$SCRIPT_FILENAME")
8+
readonly CSR_PATH=$(puppet config print csr_attributes)
89

910
if [ -n "${CSR_ATTRIBUTES}" ]; then
1011
echo "CSR Attributes: ${CSR_ATTRIBUTES}"
11-
/opt/puppetlabs/puppet/bin/ruby "$SCRIPT_PATH/89-csr_attributes.rb"
12+
/opt/puppetlabs/puppet/bin/ruby "$SCRIPT_PATH/89-csr_attributes.rb" "$CSR_PATH"
1213
fi

openvoxserver/container-entrypoint.d/90-ca.sh

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ else
5959
exit 99
6060
fi
6161

62-
if [[ -f /etc/puppetlabs/puppetserver/ca/ca_crt.pem ]]; then
62+
ca_cert=$(puppet config print cacert)
63+
if [[ -f "$ca_cert" ]]; then
6364
echo "CA already imported."
6465
else
6566
puppetserver ca import \
@@ -68,9 +69,10 @@ else
6869
--private-key $INTERMEDIATE_CA_KEY
6970
fi
7071
else
71-
new_cadir=/etc/puppetlabs/puppetserver/ca
72+
new_cadir=$(puppet config print cadir)
73+
ssl_dir=$(puppet config print ssldir)
7274

73-
if [ ! -f "$new_cadir/ca_crt.pem" ] && [ ! -f "$SSLDIR/ca/ca_crt.pem" ]; then
75+
if [ ! -f "$new_cadir/ca_crt.pem" ] && [ ! -f "$ssl_dir/ca/ca_crt.pem" ]; then
7476
# There is no existing CA
7577

7678
# Append user-supplied DNS Alt Names
@@ -87,13 +89,11 @@ else
8789

8890
# See puppet.conf file for relevant settings
8991
puppetserver ca setup \
90-
--ca-name "$ca_name" \
91-
--config /etc/puppetlabs/puppet/puppet.conf
92+
--ca-name "$ca_name"
9293

93-
elif [ ! -f "$new_cadir/ca_crt.pem" ] && [ -f "$SSLDIR/ca/ca_crt.pem" ]; then
94+
elif [ ! -f "$new_cadir/ca_crt.pem" ] && [ -f "$ssl_dir/ca/ca_crt.pem" ]; then
9495
# Legacy CA upgrade
95-
puppetserver ca migrate \
96-
--config /etc/puppetlabs/puppet/puppet.conf
97-
fi
96+
puppetserver ca migrate
97+
fi
9898
fi
9999
fi

openvoxserver/container-entrypoint.d/99-log-config.sh

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,43 @@ echo "System configuration values:"
88
echo "* HOSTNAME: '${HOSTNAME}'"
99
echo "* hostname -f: '$(hostname -f)'"
1010

11+
ssl_dir=$(puppet config print ssldir)
12+
1113
if [ -n "${CERTNAME}" ]; then
1214
echo "* CERTNAME: '${CERTNAME}'"
1315
certname=${CERTNAME}.pem
1416
else
1517
echo "* CERTNAME: unset, try to use the oldest certificate in the certs directory, because this might be the one that was used initially."
16-
if [ ! -d "${SSLDIR}/certs" ]; then
18+
if [ ! -d "${ssl_dir}/certs" ]; then
1719
certname="Not-Found"
18-
echo "WARNING: No certificates directory found in ${SSLDIR}!"
20+
echo "WARNING: No certificates directory found in ${ssl_dir}!"
1921
else
20-
certname=$(cd "${SSLDIR}/certs" && find * -type f -name '*.pem' ! -name ca.pem -print0 | xargs -0 ls -1tr | head -n 1)
22+
certname=$(cd "${ssl_dir}/certs" && find * -type f -name '*.pem' ! -name ca.pem -print0 | xargs -0 ls -1tr | head -n 1)
2123
if [ -z "${certname}" ]; then
22-
echo "WARNING: No certificates found in ${SSLDIR}/certs! Please set CERTNAME!"
24+
echo "WARNING: No certificates found in ${ssl_dir}/certs! Please set CERTNAME!"
2325
fi
2426
fi
2527
fi
2628

2729
echo "* OPENVOXSERVER_PORT: '${OPENVOXSERVER_PORT:-8140}'"
2830
echo "* Certname: '${certname}'"
2931
echo "* DNS_ALT_NAMES: '${DNS_ALT_NAMES}'"
30-
echo "* SSLDIR: '${SSLDIR}'"
32+
echo "* SSLDIR: '${ssl_dir}'"
3133

3234
altnames="-certopt no_subject,no_header,no_version,no_serial,no_signame,no_validity,no_issuer,no_pubkey,no_sigdump,no_aux"
3335

34-
if [ -f "${SSLDIR}/certs/ca.pem" ]; then
36+
if [ -f "${ssl_dir}/certs/ca.pem" ]; then
3537
echo "CA Certificate:"
3638
# shellcheck disable=SC2086 # $altnames shouldn't be quoted
37-
openssl x509 -subject -issuer -text -noout -in "${SSLDIR}/certs/ca.pem" $altnames
39+
openssl x509 -subject -issuer -text -noout -in "${ssl_dir}/certs/ca.pem" $altnames
3840
fi
3941

4042
if [ -n "${certname}" ]; then
41-
if [ -f "${SSLDIR}/certs/${certname}" ]; then
43+
if [ -f "${ssl_dir}/certs/${certname}" ]; then
4244
echo "Certificate ${certname}:"
4345
# shellcheck disable=SC2086 # $altnames shouldn't be quoted
44-
openssl x509 -subject -issuer -text -noout -in "${SSLDIR}/certs/${certname}" $altnames
46+
openssl x509 -subject -issuer -text -noout -in "${ssl_dir}/certs/${certname}" $altnames
4547
else
46-
echo "WARNING: Certificate ${certname} not found in ${SSLDIR}/certs!"
48+
echo "WARNING: Certificate ${certname} not found in ${ssl_dir}/certs!"
4749
fi
4850
fi

0 commit comments

Comments
 (0)