Skip to content

Commit

Permalink
Allow the Docker image to be run with a random user id (#12905) (#18873
Browse files Browse the repository at this point in the history
…) (#19555)

Prepare docker images to be run with arbitrary user ids. Following common practices
and recommendations, files that need to be read by Beats have now read permissions
for the group and belong to the root group. Also, the user included in the docker image
is added to the root group so it can read these files when run on docker with default
user and privileges.

Some changes are also added to Kubernetes reference manifests to help running beats
with arbitrary user ids, though this is not completely supported and it requires additional
setup.

(cherry picked from commit 3ff02cb)

Co-authored-by: Michael Morello <michael.morello@elastic.co>
  • Loading branch information
jsoriano and barkbay authored Jul 2, 2020
1 parent 930b7ba commit 6d4bedb
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ field. You can revert this change by configuring tags for the module and omittin
- Add support for fixed length extraction in `dissect` processor. {pull}17191[17191]
- Update RPM packages contained in Beat Docker images. {issue}17035[17035]
- Add TLS support to Kerberos authentication in Elasticsearch. {pull}18607[18607]
- Change ownership of files in docker images so they can be used in secured environments. {pull}12905[12905]
- Upgrade k8s.io/client-go and k8s keystore tests. {pull}18817[18817]
- Add support for multiple sets of hints on autodiscover {pull}18883[18883]

Expand Down
5 changes: 3 additions & 2 deletions deploy/kubernetes/auditbeat-kubernetes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,15 @@ spec:
path: /etc
- name: config
configMap:
defaultMode: 0600
defaultMode: 0640
name: auditbeat-config
- name: modules
configMap:
defaultMode: 0600
defaultMode: 0640
name: auditbeat-daemonset-modules
- name: data
hostPath:
# When auditbeat runs as non-root user, this directory needs to be writable by group (g+w).
path: /var/lib/auditbeat-data
type: DirectoryOrCreate
- name: run-containerd
Expand Down
5 changes: 3 additions & 2 deletions deploy/kubernetes/auditbeat/auditbeat-daemonset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,15 @@ spec:
path: /etc
- name: config
configMap:
defaultMode: 0600
defaultMode: 0640
name: auditbeat-config
- name: modules
configMap:
defaultMode: 0600
defaultMode: 0640
name: auditbeat-daemonset-modules
- name: data
hostPath:
# When auditbeat runs as non-root user, this directory needs to be writable by group (g+w).
path: /var/lib/auditbeat-data
type: DirectoryOrCreate
- name: run-containerd
Expand Down
3 changes: 2 additions & 1 deletion deploy/kubernetes/filebeat-kubernetes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ spec:
volumes:
- name: config
configMap:
defaultMode: 0600
defaultMode: 0640
name: filebeat-config
- name: varlibdockercontainers
hostPath:
Expand All @@ -123,6 +123,7 @@ spec:
# data folder stores a registry of read status for all files, so we don't send everything again on a Filebeat pod restart
- name: data
hostPath:
# When filebeat runs as non-root user, this directory needs to be writable by group (g+w).
path: /var/lib/filebeat-data
type: DirectoryOrCreate
---
Expand Down
3 changes: 2 additions & 1 deletion deploy/kubernetes/filebeat/filebeat-daemonset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ spec:
volumes:
- name: config
configMap:
defaultMode: 0600
defaultMode: 0640
name: filebeat-config
- name: varlibdockercontainers
hostPath:
Expand All @@ -79,5 +79,6 @@ spec:
# data folder stores a registry of read status for all files, so we don't send everything again on a Filebeat pod restart
- name: data
hostPath:
# When filebeat runs as non-root user, this directory needs to be writable by group (g+w).
path: /var/lib/filebeat-data
type: DirectoryOrCreate
9 changes: 5 additions & 4 deletions deploy/kubernetes/metricbeat-kubernetes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,15 @@ spec:
path: /var/run/docker.sock
- name: config
configMap:
defaultMode: 0600
defaultMode: 0640
name: metricbeat-daemonset-config
- name: modules
configMap:
defaultMode: 0600
defaultMode: 0640
name: metricbeat-daemonset-modules
- name: data
hostPath:
# When metricbeat runs as non-root user, this directory needs to be writable by group (g+w)
path: /var/lib/metricbeat-data
type: DirectoryOrCreate
---
Expand Down Expand Up @@ -302,11 +303,11 @@ spec:
volumes:
- name: config
configMap:
defaultMode: 0600
defaultMode: 0640
name: metricbeat-deployment-config
- name: modules
configMap:
defaultMode: 0600
defaultMode: 0640
name: metricbeat-deployment-modules
---
apiVersion: rbac.authorization.k8s.io/v1
Expand Down
5 changes: 3 additions & 2 deletions deploy/kubernetes/metricbeat/metricbeat-daemonset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,14 @@ spec:
path: /var/run/docker.sock
- name: config
configMap:
defaultMode: 0600
defaultMode: 0640
name: metricbeat-daemonset-config
- name: modules
configMap:
defaultMode: 0600
defaultMode: 0640
name: metricbeat-daemonset-modules
- name: data
hostPath:
# When metricbeat runs as non-root user, this directory needs to be writable by group (g+w)
path: /var/lib/metricbeat-data
type: DirectoryOrCreate
4 changes: 2 additions & 2 deletions deploy/kubernetes/metricbeat/metricbeat-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ spec:
volumes:
- name: config
configMap:
defaultMode: 0600
defaultMode: 0640
name: metricbeat-deployment-config
- name: modules
configMap:
defaultMode: 0600
defaultMode: 0640
name: metricbeat-deployment-modules
4 changes: 2 additions & 2 deletions dev-tools/packaging/templates/docker/Dockerfile.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ RUN chmod 755 /usr/local/bin/docker-entrypoint
RUN groupadd --gid 1000 {{ .BeatName }}

RUN mkdir {{ $beatHome }}/data {{ $beatHome }}/logs && \
chown -R root:{{ .BeatName }} {{ $beatHome }} && \
chown -R root:root {{ $beatHome }} && \
find {{ $beatHome }} -type d -exec chmod 0750 {} \; && \
find {{ $beatHome }} -type f -exec chmod 0640 {} \; && \
chmod 0750 {{ $beatBinary }} && \
Expand All @@ -43,7 +43,7 @@ RUN mkdir {{ $beatHome }}/data {{ $beatHome }}/logs && \
chmod 0770 {{ $beatHome }}/data {{ $beatHome }}/logs

{{- if ne .user "root" }}
RUN useradd -M --uid 1000 --gid 1000 --home {{ $beatHome }} {{ .user }}
RUN useradd -M --uid 1000 --gid 1000 --groups 0 --home {{ $beatHome }} {{ .user }}
{{- end }}
USER {{ .user }}

Expand Down

0 comments on commit 6d4bedb

Please sign in to comment.