Skip to content

Commit 2ce044c

Browse files
committed
Build Docker images (#194)
* Assemble tar packages * Add files to generate Docker images First working version * Fix certs path * clean up * Working indexer in Docker * Add documentation to build Docker images Simplify names of Docker build args * Remove unused Docker dependencies --------- Signed-off-by: Álex Ruiz <[email protected]>
1 parent 5469b9e commit 2ce044c

File tree

7 files changed

+296
-0
lines changed

7 files changed

+296
-0
lines changed

docker/README.md

+14
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,17 @@ Refer to [scripts/README.md](../scripts/README.md) for details about how to buil
7878
[docker-variant]: https://docs.docker.com/desktop/install/linux-install/#differences-between-docker-desktop-for-linux-and-docker-engine
7979
[docker-context]: https://docs.docker.com/desktop/install/linux-install/#context
8080
[wi-repo]: https://github.com/wazuh/wazuh-indexer
81+
82+
## Building Docker images
83+
84+
The [prod](./prod) folder contains the code to build Docker images. A tarball of `wazuh-indexer` needs to be located at the same level that the Dockerfile. Below there is example of the command needed to build the image. Set the build arguments and the image tag accordingly.
85+
86+
```console
87+
docker build --build-arg="VERSION=4.9.0" --build-arg="INDEXER_TAR_NAME=wazuh-indexer-4.9.0-1_linux-x64_cfca84f.tar.gz" --tag=wazuh-indexer:4.9.0 --progress=plain --no-cache .
88+
```
89+
90+
Then, start a container with:
91+
92+
```console
93+
docker run -it --rm wazuh-indexer:4.9.0
94+
```

docker/prod/Dockerfile

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Wazuh Docker Copyright (C) 2017, Wazuh Inc. (License GPLv2)
2+
FROM amazonlinux:2023.3.20240219.0 AS builder
3+
4+
ARG VERSION
5+
ARG INDEXER_TAR_NAME
6+
7+
RUN yum install openssl tar findutils shadow-utils -y
8+
9+
COPY ${INDEXER_TAR_NAME} /
10+
11+
COPY config/opensearch.yml /
12+
13+
COPY config/config.yml /
14+
15+
COPY config/config.sh /
16+
17+
RUN bash config.sh
18+
19+
################################################################################
20+
# Build stage 1 (the actual Wazuh indexer image):
21+
#
22+
# Copy wazuh-indexer from stage 0
23+
# Add entrypoint
24+
25+
################################################################################
26+
FROM amazonlinux:2023.3.20240219.0
27+
28+
ENV USER="wazuh-indexer" \
29+
GROUP="wazuh-indexer" \
30+
NAME="wazuh-indexer" \
31+
INSTALL_DIR="/usr/share/wazuh-indexer"
32+
33+
RUN yum install curl-minimal shadow-utils findutils hostname -y
34+
35+
RUN getent group $GROUP || groupadd -r -g 1000 $GROUP
36+
37+
RUN useradd --system \
38+
--uid 1000 \
39+
--no-create-home \
40+
--home-dir $INSTALL_DIR \
41+
--gid $GROUP \
42+
--shell /sbin/nologin \
43+
--comment "$USER user" \
44+
$USER
45+
46+
WORKDIR $INSTALL_DIR
47+
48+
COPY entrypoint.sh /
49+
50+
COPY config/securityadmin.sh /
51+
52+
RUN chmod 700 /entrypoint.sh && chmod 700 /securityadmin.sh
53+
54+
RUN chown 1000:1000 /*.sh
55+
56+
COPY --from=builder --chown=1000:1000 /debian/wazuh-indexer/usr/share/wazuh-indexer /usr/share/wazuh-indexer
57+
COPY --from=builder --chown=0:0 /debian/wazuh-indexer/usr/lib/systemd /usr/lib/systemd
58+
COPY --from=builder --chown=0:0 /debian/wazuh-indexer/usr/lib/sysctl.d /usr/lib/sysctl.d
59+
COPY --from=builder --chown=0:0 /debian/wazuh-indexer/usr/lib/tmpfiles.d /usr/lib/tmpfiles.d
60+
61+
RUN chown -R 1000:1000 /usr/share/wazuh-indexer
62+
63+
RUN mkdir -p /var/lib/wazuh-indexer && chown 1000:1000 /var/lib/wazuh-indexer && \
64+
mkdir -p /usr/share/wazuh-indexer/logs && chown 1000:1000 /usr/share/wazuh-indexer/logs && \
65+
mkdir -p /run/wazuh-indexer && chown 1000:1000 /run/wazuh-indexer && \
66+
mkdir -p /var/log/wazuh-indexer && chown 1000:1000 /var/log/wazuh-indexer && \
67+
chmod 700 /usr/share/wazuh-indexer && \
68+
chmod 600 /usr/share/wazuh-indexer/config/jvm.options && \
69+
chmod 600 /usr/share/wazuh-indexer/config/opensearch.yml
70+
71+
USER wazuh-indexer
72+
73+
# Services ports
74+
EXPOSE 9200
75+
76+
ENTRYPOINT ["/entrypoint.sh"]
77+
# Dummy overridable parameter parsed by entrypoint
78+
CMD ["opensearchwrapper"]

docker/prod/config/config.sh

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/bin/bash
2+
3+
# Wazuh Docker Copyright (C) 2017, Wazuh Inc. (License GPLv2)
4+
# This has to be exported to make some magic below work.
5+
export DH_OPTIONS
6+
7+
export NAME=wazuh-indexer
8+
export TARGET_DIR=${CURDIR}/debian/${NAME}
9+
10+
# Package build options
11+
export LOG_DIR=/var/log/${NAME}
12+
export LIB_DIR=/var/lib/${NAME}
13+
export PID_DIR=/run/${NAME}
14+
export INDEXER_HOME=/usr/share/${NAME}
15+
export CONFIG_DIR=${INDEXER_HOME}/config
16+
export BASE_DIR=${NAME}-*
17+
18+
rm -rf ${INDEXER_HOME:?}/
19+
tar -xf "${INDEXER_TAR_NAME}"
20+
21+
## TOOLS
22+
23+
## Variables
24+
TOOLS_PATH=${NAME}-${VERSION}/plugins/opensearch-security/tools
25+
CERT_TOOL=${TOOLS_PATH}/wazuh-certs-tool.sh
26+
27+
# generate certificates
28+
cp $CERT_TOOL .
29+
chmod 755 wazuh-certs-tool.sh && bash wazuh-certs-tool.sh -A
30+
31+
# copy to target
32+
mkdir -p ${TARGET_DIR}${INDEXER_HOME}
33+
# mkdir -p ${TARGET_DIR}${INDEXER_HOME}/opensearch-security/ <-- empty dir
34+
mkdir -p ${TARGET_DIR}${CONFIG_DIR}
35+
mkdir -p ${TARGET_DIR}${LIB_DIR}
36+
mkdir -p ${TARGET_DIR}${LOG_DIR}
37+
mkdir -p ${TARGET_DIR}/etc/init.d
38+
mkdir -p ${TARGET_DIR}/etc/default
39+
mkdir -p ${TARGET_DIR}/usr/lib/tmpfiles.d
40+
mkdir -p ${TARGET_DIR}/usr/lib/sysctl.d
41+
mkdir -p ${TARGET_DIR}/usr/lib/systemd/system
42+
mkdir -p ${TARGET_DIR}${CONFIG_DIR}/certs
43+
# Copy installation files to final location
44+
cp -pr ${BASE_DIR}/* ${TARGET_DIR}${INDEXER_HOME}
45+
cp -pr /opensearch.yml ${TARGET_DIR}${CONFIG_DIR}
46+
# Copy Wazuh indexer's certificates
47+
cp -pr /wazuh-certificates/demo.indexer.pem ${TARGET_DIR}${CONFIG_DIR}/certs/indexer.pem
48+
cp -pr /wazuh-certificates/demo.indexer-key.pem ${TARGET_DIR}${CONFIG_DIR}/certs/indexer-key.pem
49+
cp -pr /wazuh-certificates/root-ca.key ${TARGET_DIR}${CONFIG_DIR}/certs/root-ca.key
50+
cp -pr /wazuh-certificates/root-ca.pem ${TARGET_DIR}${CONFIG_DIR}/certs/root-ca.pem
51+
cp -pr /wazuh-certificates/admin.pem ${TARGET_DIR}${CONFIG_DIR}/certs/admin.pem
52+
cp -pr /wazuh-certificates/admin-key.pem ${TARGET_DIR}${CONFIG_DIR}/certs/admin-key.pem
53+
54+
# Set path to indexer home directory
55+
sed -i 's/-Djava.security.policy=file:\/\/\/etc\/wazuh-indexer\/opensearch-performance-analyzer\/opensearch_security.policy/-Djava.security.policy=file:\/\/\/usr\/share\/wazuh-indexer\/opensearch-performance-analyzer\/opensearch_security.policy/g' ${TARGET_DIR}${CONFIG_DIR}/jvm.options
56+
57+
chmod -R 500 ${TARGET_DIR}${CONFIG_DIR}/certs
58+
chmod -R 400 ${TARGET_DIR}${CONFIG_DIR}/certs/*
59+
60+
find ${TARGET_DIR} -type d -exec chmod 750 {} \;
61+
find ${TARGET_DIR} -type f -perm 644 -exec chmod 640 {} \;
62+
find ${TARGET_DIR} -type f -perm 664 -exec chmod 660 {} \;
63+
find ${TARGET_DIR} -type f -perm 755 -exec chmod 750 {} \;
64+
find ${TARGET_DIR} -type f -perm 744 -exec chmod 740 {} \;

docker/prod/config/config.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
nodes:
2+
# Wazuh indexer server nodes
3+
indexer:
4+
- name: demo.indexer
5+
ip: demo.indexer

docker/prod/config/opensearch.yml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
network.host: "0.0.0.0"
2+
node.name: "wazuh.indexer"
3+
path.data: /var/lib/wazuh-indexer
4+
path.logs: /var/log/wazuh-indexer
5+
discovery.type: single-node
6+
compatibility.override_main_response_version: true
7+
plugins.security.ssl.http.pemcert_filepath: /usr/share/wazuh-indexer/config/certs/indexer.pem
8+
plugins.security.ssl.http.pemkey_filepath: /usr/share/wazuh-indexer/config/certs/indexer-key.pem
9+
plugins.security.ssl.http.pemtrustedcas_filepath: /usr/share/wazuh-indexer/config/certs/root-ca.pem
10+
plugins.security.ssl.transport.pemcert_filepath: /usr/share/wazuh-indexer/config/certs/indexer.pem
11+
plugins.security.ssl.transport.pemkey_filepath: /usr/share/wazuh-indexer/config/certs/indexer-key.pem
12+
plugins.security.ssl.transport.pemtrustedcas_filepath: /usr/share/wazuh-indexer/config/certs/root-ca.pem
13+
plugins.security.ssl.http.enabled: true
14+
plugins.security.ssl.transport.enforce_hostname_verification: false
15+
plugins.security.ssl.transport.resolve_hostname: false
16+
plugins.security.authcz.admin_dn:
17+
- "CN=admin,OU=Wazuh,O=Wazuh,L=California,C=US"
18+
plugins.security.check_snapshot_restore_write_privileges: true
19+
plugins.security.enable_snapshot_restore_privilege: true
20+
plugins.security.nodes_dn:
21+
- "CN=demo.indexer,OU=Wazuh,O=Wazuh,L=California,C=US"
22+
plugins.security.restapi.roles_enabled:
23+
- "all_access"
24+
- "security_rest_api_access"
25+
plugins.security.system_indices.enabled: true
26+
plugins.security.system_indices.indices: [".opendistro-alerting-config", ".opendistro-alerting-alert*", ".opendistro-anomaly-results*", ".opendistro-anomaly-detector*", ".opendistro-anomaly-checkpoints", ".opendistro-anomaly-detection-state", ".opendistro-reports-*", ".opendistro-notifications-*", ".opendistro-notebooks", ".opensearch-observability", ".opendistro-asynchronous-search-response*", ".replication-metadata-store"]

docker/prod/config/securityadmin.sh

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
# Initialize the `.opendistro_security` index.
4+
sleep 30
5+
bash "$INDEXER_HOME"/plugins/opensearch-security/tools/securityadmin.sh \
6+
-cacert "$INDEXER_HOME"/config/certs/root-ca.pem \
7+
-cert "$INDEXER_HOME"/config/certs/admin.pem \
8+
-key "$INDEXER_HOME"/config/certs/admin-key.pem \
9+
-cd "$INDEXER_HOME"/config/opensearch-security/ \
10+
-nhnv \
11+
-icl

docker/prod/entrypoint.sh

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/usr/bin/env bash
2+
# Wazuh Docker Copyright (C) 2017, Wazuh Inc. (License GPLv2)
3+
set -e
4+
5+
umask 0002
6+
7+
# Constants
8+
INDEXER_HOME=/usr/share/wazuh-indexer
9+
OPENSEARCH_PATH_CONF=${INDEXER_HOME}/config
10+
JAVA_HOME=${INDEXER_HOME}/jdk
11+
12+
# DISCOVERY=$(grep -oP "(?<=discovery.type: ).*" ${OPENSEARCH_PATH_CONF}/opensearch.yml)
13+
14+
# Export variables to environment
15+
export INDEXER_HOME
16+
export OPENSEARCH_PATH_CONF
17+
export JAVA_HOME
18+
19+
run_as_other_user_if_needed() {
20+
if [[ "$(id -u)" == "0" ]]; then
21+
# If running as root, drop to specified UID and run command
22+
exec chroot --userspec=1000:0 / "${@}"
23+
else
24+
# Either we are running in Openshift with random uid and are a member of the root group
25+
# or with a custom --user
26+
exec "${@}"
27+
fi
28+
}
29+
30+
# Allow user specify custom CMD, maybe bin/opensearch itself
31+
# for example to directly specify `-E` style parameters for opensearch on k8s
32+
# or simply to run /bin/bash to check the image
33+
if [[ "$1" != "opensearchwrapper" ]]; then
34+
if [[ "$(id -u)" == "0" && $(basename "$1") == "opensearch" ]]; then
35+
# Rewrite CMD args to replace $1 with `opensearch` explicitly,
36+
# Without this, user could specify `opensearch -E x.y=z` but
37+
# `bin/opensearch -E x.y=z` would not work.
38+
set -- "opensearch" "${@:2}"
39+
# Use chroot to switch to UID 1000 / GID 0
40+
exec chroot --userspec=1000:0 / "$@"
41+
else
42+
# User probably wants to run something else, like /bin/bash, with another uid forced (Openshift?)
43+
exec "$@"
44+
fi
45+
fi
46+
47+
# Allow environment variables to be set by creating a file with the
48+
# contents, and setting an environment variable with the suffix _FILE to
49+
# point to it. This can be used to provide secrets to a container, without
50+
# the values being specified explicitly when running the container.
51+
#
52+
# This is also sourced in opensearch-env, and is only needed here
53+
# as well because we use INDEXER_PASSWORD below. Sourcing this script
54+
# is idempotent.
55+
source /usr/share/wazuh-indexer/bin/opensearch-env-from-file
56+
57+
if [[ -f bin/opensearch-users ]]; then
58+
# Check for the INDEXER_PASSWORD environment variable to set the
59+
# bootstrap password for Security.
60+
#
61+
# This is only required for the first node in a cluster with Security
62+
# enabled, but we have no way of knowing which node we are yet. We'll just
63+
# honor the variable if it's present.
64+
if [[ -n "$INDEXER_PASSWORD" ]]; then
65+
[[ -f /usr/share/wazuh-indexer/opensearch.keystore ]] || (run_as_other_user_if_needed opensearch-keystore create)
66+
if ! (run_as_other_user_if_needed opensearch-keystore has-passwd --silent); then
67+
# keystore is unencrypted
68+
if ! (run_as_other_user_if_needed opensearch-keystore list | grep -q '^bootstrap.password$'); then
69+
(run_as_other_user_if_needed echo "$INDEXER_PASSWORD" | opensearch-keystore add -x 'bootstrap.password')
70+
fi
71+
else
72+
# keystore requires password
73+
if ! (run_as_other_user_if_needed echo "$KEYSTORE_PASSWORD" |
74+
opensearch-keystore list | grep -q '^bootstrap.password$'); then
75+
COMMANDS="$(printf "%s\n%s" "$KEYSTORE_PASSWORD" "$INDEXER_PASSWORD")"
76+
(run_as_other_user_if_needed echo "$COMMANDS" | opensearch-keystore add -x 'bootstrap.password')
77+
fi
78+
fi
79+
fi
80+
fi
81+
82+
if [[ "$(id -u)" == "0" ]]; then
83+
# If requested and running as root, mutate the ownership of bind-mounts
84+
if [[ -n "$TAKE_FILE_OWNERSHIP" ]]; then
85+
chown -R 1000:0 /usr/share/wazuh-indexer/{data,logs}
86+
fi
87+
fi
88+
89+
# Initialize security
90+
nohup /securityadmin.sh &
91+
92+
#if [[ "$DISCOVERY" == "single-node" ]] && [[ ! -f "/var/lib/wazuh-indexer/.flag" ]]; then
93+
# run securityadmin.sh for single node with CACERT, CERT and KEY parameter
94+
# nohup /securityadmin.sh &
95+
# touch "/var/lib/wazuh-indexer/.flag"
96+
#fi
97+
98+
run_as_other_user_if_needed /usr/share/wazuh-indexer/bin/opensearch <<<"$KEYSTORE_PASSWORD"

0 commit comments

Comments
 (0)