Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot run elasticsearch with security best practice readOnlyRootFilesystem: true #6126

Closed
ebuildy opened this issue Oct 30, 2022 · 4 comments · Fixed by #6703
Closed

Cannot run elasticsearch with security best practice readOnlyRootFilesystem: true #6126

ebuildy opened this issue Oct 30, 2022 · 4 comments · Fixed by #6703
Assignees
Labels
>enhancement Enhancement of existing functionality

Comments

@ebuildy
Copy link

ebuildy commented Oct 30, 2022

Bug Report

What did you do?

As a good practice, we configure security context (here readOnlyRootFilesystem: true):

apiVersion: elasticsearch.k8s.elastic.co/v1
kind: Elasticsearch
spec:
  nodeSets:
    - 
      count: 3
      name: master
      podTemplate:
        spec:
          containers:
            - 
              securityContext: &securityContext
                allowPrivilegeEscalation: false
                capabilities:
                  drop:
                    - ALL
                privileged: false
                readOnlyRootFilesystem: true
                runAsNonRoot: true
                runAsUser: 1000
          initContainers:
            - name: elastic-internal-init-filesystem
              securityContext: 
                <<: *securityContext
            - name: elastic-internal-suspend
              securityContext:
                <<: *securityContext
            - name: elastic-internal-init-keystore
              securityContext:
                <<: *securityContext

What did you expect to see?

Operator create the STS, create pods, create container and init container can run fine.

What did you see instead? Under which circumstances?

The first initContainer cannot run because a folder is not writable. Diging the source code, we can see:

prepare-fs.sh script:

...
echo "Linking /mnt/elastic-internal/xpack-file-realm/users to /usr/share/elasticsearch/config/users"
		ln -sf /mnt/elastic-internal/xpack-file-realm/users /usr/share/elasticsearch/config/users
...

where the directory /usr/share/elasticsearch/config is not a volume, hence read-only.

We cant find any workaround, except changing polaris or OPA rules.

As a clean solution, the prepare-fs.sh script should use a temporary directory, mounted as emptyDir.

References:

@barkbay
Copy link
Contributor

barkbay commented Nov 17, 2022

The way Elasticsearch starts requires the /tmp directory to be writable, other than that I think it can be started with readOnlyRootFilesystem: true:

apiVersion: elasticsearch.k8s.elastic.co/v1
kind: Elasticsearch
metadata:
  name: elasticsearch-sample
spec:
  version: 8.5.0
  nodeSets:
  - name: default
    config:
      node.store.allow_mmap: false
    podTemplate:
      spec:
        volumes:
          - name: tmp-volume
            emptyDir: { }
        containers:
        - name: elasticsearch
          securityContext:
            readOnlyRootFilesystem: true
          volumeMounts:
            - mountPath: /tmp
              name: tmp-volume
    count: 3
---
apiVersion: kibana.k8s.elastic.co/v1
kind: Kibana
metadata:
  name: kibana-sample
spec:
  version: 8.5.0
  count: 1
  elasticsearchRef:
    name: "elasticsearch-sample"
  podTemplate:
    spec:
      containers:
        - name: kibana
          securityContext:
            readOnlyRootFilesystem: true

@ebuildy
Copy link
Author

ebuildy commented Nov 17, 2022

Thanks you,

The issue happens on initContainers, to copy the configuration, the directory /usr/share/elasticsearch/config is not writable.

We cant specify a volume, because the copy operation need files from /usr/share/elasticsearch/config.

(We run elastic stack with Helm charts, so we were able to create another initContainer to do the trick)

@pebrc
Copy link
Collaborator

pebrc commented Nov 29, 2022

I think we should adapt the example configuration from #6126 (comment) as the default in ECK:

  • the ECK operator should always mount an emptyDir volume in /tmp
  • the ECK operator should set readOnlyRootFilesystem: true

@RichTowle

This comment was marked as off-topic.

@elastic elastic locked as resolved and limited conversation to collaborators May 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
>enhancement Enhancement of existing functionality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants