Skip to content

Commit 3ca30d0

Browse files
author
Mike Dickey
committed
This PR enables running in unpriviliged mode when run as the splunk
user account. The splunk user is now part of the ansible group and able to run the ansible playbooks itself at startup, but unlike ansible, it has no sudo capabilities at startup time. Note that you currently must explicitly set SPLUNK_HOME_OWNERSHIP_ENFORCEMENT to false when running as the splunk user, otherwise it will fail due to lack of permissions. Note that there are limitations, beyond just being unable to "correct" permission for volume mounts. Any features requiring elevated permissions, such as install JDK or other packages, will not work when running as the splunk user. The defaults are left unchanged. By default, it will still run as the ansible user and the behavior should be the same as before.
1 parent 805f945 commit 3ca30d0

File tree

2 files changed

+57
-47
lines changed

2 files changed

+57
-47
lines changed

splunk/common-files/Dockerfile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,15 @@ RUN sed -i -e 's/%sudo\s\+ALL=(ALL\(:ALL\)\?)\s\+ALL/%sudo ALL=NOPASSWD:ALL/g' /
101101
&& groupadd -r ${ANSIBLE_GROUP} \
102102
&& useradd -r -m -g ${ANSIBLE_GROUP} ${ANSIBLE_USER} \
103103
&& usermod -aG sudo ${ANSIBLE_USER} \
104+
&& usermod -aG ${ANSIBLE_GROUP} ${SPLUNK_USER} \
104105
# Container Artifact Directory is a place for all artifacts and logs that are generated by the provisioning process. The directory is owned by the user "ansible".
105106
&& mkdir ${CONTAINER_ARTIFACT_DIR} \
106-
&& chown -R ${ANSIBLE_USER}:${ANSIBLE_GROUP} $CONTAINER_ARTIFACT_DIR \
107+
&& chown -R ${ANSIBLE_USER}:${ANSIBLE_GROUP} ${CONTAINER_ARTIFACT_DIR} \
108+
&& chmod -R 775 ${CONTAINER_ARTIFACT_DIR} \
107109
&& chmod -R 555 ${SPLUNK_ANSIBLE_HOME} \
108-
&& chmod -R 777 ${CONTAINER_ARTIFACT_DIR} \
110+
&& chgrp ${ANSIBLE_GROUP} ${SPLUNK_ANSIBLE_HOME} ${SPLUNK_ANSIBLE_HOME}/ansible.cfg \
111+
&& chmod 775 ${SPLUNK_ANSIBLE_HOME} \
112+
&& chmod 664 ${SPLUNK_ANSIBLE_HOME}/ansible.cfg \
109113
&& chmod 755 /sbin/entrypoint.sh /sbin/createdefaults.py /sbin/checkstate.sh
110114

111115
USER ${ANSIBLE_USER}

splunk/common-files/entrypoint.sh

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ trap teardown SIGINT SIGTERM
3535

3636
prep_ansible() {
3737
cd ${SPLUNK_ANSIBLE_HOME}
38+
if [ `whoami` == "${SPLUNK_USER}" ]; then
39+
sed -i -e "s,^become\\s*=.*,become = false," ansible.cfg
40+
fi
3841
if [[ "$DEBUG" == "true" ]]; then
3942
ansible-playbook --version
4043
python inventory/environ.py --write-to-file
@@ -54,33 +57,36 @@ watch_for_failure(){
5457
echo
5558
user_permission_change
5659
# Any crashes/errors while Splunk is running should get logged to splunkd_stderr.log and sent to the container's stdout
60+
if [ `whoami` != "${SPLUNK_USER}" ]; then
61+
RUN_AS_SPLUNK="sudo -u ${SPLUNK_USER}"
62+
fi
5763
if [ -z "$SPLUNK_TAIL_FILE" ]; then
58-
sudo -u ${SPLUNK_USER} tail -n 0 -f ${SPLUNK_HOME}/var/log/splunk/splunkd_stderr.log &
64+
${RUN_AS_SPLUNK} tail -n 0 -f ${SPLUNK_HOME}/var/log/splunk/splunkd_stderr.log &
5965
else
60-
sudo -u ${SPLUNK_USER} tail -n 0 -f ${SPLUNK_TAIL_FILE} &
66+
${RUN_AS_SPLUNK} tail -n 0 -f ${SPLUNK_TAIL_FILE} &
6167
fi
6268
wait
6369
}
6470

6571
create_defaults() {
66-
createdefaults.py
72+
createdefaults.py
6773
}
6874

6975
start_and_exit() {
70-
if [ -z "$SPLUNK_PASSWORD" ]
71-
then
72-
echo "WARNING: No password ENV var. Stack may fail to provision if splunk.password is not set in ENV or a default.yml"
73-
fi
76+
if [ -z "$SPLUNK_PASSWORD" ]
77+
then
78+
echo "WARNING: No password ENV var. Stack may fail to provision if splunk.password is not set in ENV or a default.yml"
79+
fi
7480
sh -c "echo 'starting' > ${CONTAINER_ARTIFACT_DIR}/splunk-container.state"
7581
setup
76-
prep_ansible
82+
prep_ansible
7783
ansible-playbook $ANSIBLE_EXTRA_FLAGS -i inventory/environ.py site.yml
7884
}
7985

8086
start() {
81-
trap teardown EXIT
87+
trap teardown EXIT
8288
start_and_exit
83-
watch_for_failure
89+
watch_for_failure
8490
}
8591

8692
configure_multisite() {
@@ -89,58 +95,58 @@ configure_multisite() {
8995
}
9096

9197
restart(){
92-
trap teardown EXIT
98+
trap teardown EXIT
9399
sh -c "echo 'restarting' > ${CONTAINER_ARTIFACT_DIR}/splunk-container.state"
94-
prep_ansible
95-
${SPLUNK_HOME}/bin/splunk stop 2>/dev/null || true
100+
prep_ansible
101+
${SPLUNK_HOME}/bin/splunk stop 2>/dev/null || true
96102
ansible-playbook -i inventory/environ.py start.yml
97103
watch_for_failure
98104
}
99105

100106
user_permission_change(){
101107
if [[ "$STEPDOWN_ANSIBLE_USER" == "true" ]]; then
102-
bash -c "sudo deluser -q ansible sudo"
108+
bash -c "sudo deluser -q ansible sudo"
103109
fi
104110
}
105111

106112
help() {
107113
cat << EOF
108-
____ _ _ __
114+
____ _ _ __
109115
/ ___| _ __ | |_ _ _ __ | | __ \ \\
110116
\___ \| '_ \| | | | | '_ \| |/ / \ \\
111-
___) | |_) | | |_| | | | | < / /
117+
___) | |_) | | |_| | | | | < / /
112118
|____/| .__/|_|\__,_|_| |_|_|\_\ /_/
113-
|_|
119+
|_|
114120
========================================
115121
116122
Environment Variables:
117-
* SPLUNK_USER - user under which to run Splunk (default: splunk)
118-
* SPLUNK_GROUP - group under which to run Splunk (default: splunk)
119-
* SPLUNK_HOME - home directory where Splunk gets installed (default: /opt/splunk)
120-
* SPLUNK_START_ARGS - arguments to pass into the Splunk start command; you must include '--accept-license' to start Splunk (default: none)
121-
* SPLUNK_ROLE - the role of this Splunk instance (default: splunk_standalone)
122-
Acceptable values:
123-
- splunk_standalone
124-
- splunk_search_head
125-
- splunk_indexer
126-
- splunk_deployer
127-
- splunk_license_master
128-
- splunk_cluster_master
129-
- splunk_heavy_forwarder
130-
* SPLUNK_LICENSE_URI - URI or local file path (absolute path in the container) to a Splunk license
131-
* SPLUNK_STANDALONE_URL, SPLUNK_INDEXER_URL, ... - comma-separated list of resolvable aliases to properly bring-up a distributed environment.
132-
This is optional for standalones, but required for multi-node Splunk deployments.
133-
* SPLUNK_BUILD_URL - URL to a Splunk build which will be installed (instead of the image's default build)
134-
* SPLUNK_APPS_URL - comma-separated list of URLs to Splunk apps which will be downloaded and installed
123+
* SPLUNK_USER - user under which to run Splunk (default: splunk)
124+
* SPLUNK_GROUP - group under which to run Splunk (default: splunk)
125+
* SPLUNK_HOME - home directory where Splunk gets installed (default: /opt/splunk)
126+
* SPLUNK_START_ARGS - arguments to pass into the Splunk start command; you must include '--accept-license' to start Splunk (default: none)
127+
* SPLUNK_ROLE - the role of this Splunk instance (default: splunk_standalone)
128+
Acceptable values:
129+
- splunk_standalone
130+
- splunk_search_head
131+
- splunk_indexer
132+
- splunk_deployer
133+
- splunk_license_master
134+
- splunk_cluster_master
135+
- splunk_heavy_forwarder
136+
* SPLUNK_LICENSE_URI - URI or local file path (absolute path in the container) to a Splunk license
137+
* SPLUNK_STANDALONE_URL, SPLUNK_INDEXER_URL, ... - comma-separated list of resolvable aliases to properly bring-up a distributed environment.
138+
This is optional for standalones, but required for multi-node Splunk deployments.
139+
* SPLUNK_BUILD_URL - URL to a Splunk build which will be installed (instead of the image's default build)
140+
* SPLUNK_APPS_URL - comma-separated list of URLs to Splunk apps which will be downloaded and installed
135141
136142
Examples:
137-
* docker run -it -p 8000:8000 splunk/splunk start
138-
* docker run -it -e SPLUNK_START_ARGS=--accept-license -p 8000:8000 -p 8089:8089 splunk/splunk start
139-
* docker run -it -e SPLUNK_START_ARGS=--accept-license -e SPLUNK_LICENSE_URI=http://example.com/splunk.lic -p 8000:8000 splunk/splunk start
140-
* docker run -it -e SPLUNK_START_ARGS=--accept-license -e SPLUNK_INDEXER_URL=idx1,idx2 -e SPLUNK_SEARCH_HEAD_URL=sh1,sh2 -e SPLUNK_ROLE=splunk_search_head --hostname sh1 --network splunknet --network-alias sh1 -e SPLUNK_PASSWORD=helloworld -e SPLUNK_LICENSE_URI=http://example.com/splunk.lic splunk/splunk start
143+
* docker run -it -p 8000:8000 splunk/splunk start
144+
* docker run -it -e SPLUNK_START_ARGS=--accept-license -p 8000:8000 -p 8089:8089 splunk/splunk start
145+
* docker run -it -e SPLUNK_START_ARGS=--accept-license -e SPLUNK_LICENSE_URI=http://example.com/splunk.lic -p 8000:8000 splunk/splunk start
146+
* docker run -it -e SPLUNK_START_ARGS=--accept-license -e SPLUNK_INDEXER_URL=idx1,idx2 -e SPLUNK_SEARCH_HEAD_URL=sh1,sh2 -e SPLUNK_ROLE=splunk_search_head --hostname sh1 --network splunknet --network-alias sh1 -e SPLUNK_PASSWORD=helloworld -e SPLUNK_LICENSE_URI=http://example.com/splunk.lic splunk/splunk start
141147
142148
EOF
143-
exit 1
149+
exit 1
144150
}
145151

146152
case "$1" in
@@ -157,12 +163,12 @@ case "$1" in
157163
configure_multisite $0
158164
;;
159165
create-defaults)
160-
create_defaults
161-
;;
166+
create_defaults
167+
;;
162168
restart)
163-
shift
164-
restart $@
165-
;;
169+
shift
170+
restart $@
171+
;;
166172
no-provision)
167173
user_permission_change
168174
tail -n 0 -f /etc/hosts &

0 commit comments

Comments
 (0)