Search code examples
kuberneteskubernetes-secrets

How to pass user credentials to (user-restricted) mounted volume inside Kubernetes Pod?


I am trying to pass user credentials via Kubernetes secret to a mounted, password protected directory inside a Kubernetes Pod. The NFS folder /mount/protected has user access restrictions, i.e. only certain users can access this folder.

This is my Pod configuration:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  volumes:
  - name: my-volume
    hostPath:
      path: /mount/protected
      type: Directory
    secret:
      secretName: my-secret
  containers:
  - name: my-container
    image: <...>
    command: ["/bin/sh"]
    args: ["-c", "python /my-volume/test.py"]
    volumeMounts:
    - name: my-volume
      mountPath: /my-volume

When applying it, I get the following error:

The Pod "my-pod" is invalid:
* spec.volumes[0].secret: Forbidden: may not specify more than 1 volume type
* spec.containers[0].volumeMounts[0].name: Not found: "my-volume"

I created my-secret according to the following guide:
https://kubernetes.io/docs/tasks/inject-data-application/distribute-credentials-secure/#create-a-secret
So basically:

apiVersion: v1
kind: Secret
metadata:
  name: my-secret
data:
  username: bXktYXBw
  password: PHJlZGFjdGVkPg==

But when I mount the folder /mount/protected with:

spec:
  volumes:
  - name: my-volume
    hostPath:
      path: /mount/protected
      type: Directory

I get a permission denied error python: can't open file '/my-volume/test.py': [Errno 13] Permission denied when running a Pod that mounts this volume path.

My question is how can I tell my Pod that it should use specific user credentials to gain access to this mounted folder?


Solution

  • I eventually figured out how to pass user credentials to a mounted directory within a Pod by using CIFS Flexvolume Plugin for Kubernetes (https://github.com/fstab/cifs). With this Plugin, every user can pass her/his credentials to the Pod. The user only needs to create a Kubernetes secret (cifs-secret), storing the username/password and use this secret for the mount within the Pod. The volume is then mounted as follows:

      (...)
      volumes:
      - name: test
        flexVolume:
          driver: "fstab/cifs"
          fsType: "cifs"
          secretRef:
            name: "cifs-secret"
          options:
            networkPath: "//server/share"
            mountOptions: "dir_mode=0755,file_mode=0644,noperm"