kubernetes: Secrets with specific permissions (defaultMode or mode) not being applied in Kubernetes 1.4.0
BUG REPORT
Kubernetes version: client version: 1.4.0 server version 1.4.0
Environment:
- Cloud Provider: GCP/GKE (Google Container Engine)
- OS in container: CentOS 7
- Kernel in container: 3.16.7-ckt25-2
What happened: Attempting to use the “defaultMode” or “Mode” permissions option for Secrets recently added in Kubernetes 1.4.0. does not actually apply requested permissions on files in mount point. Followed documentation here: http://kubernetes.io/docs/user-guide/secrets/#
I’ve tried it two ways:
- First, define the secret:
{
"kind": "Secret",
"apiVersion": "v1",
"metadata": {
"name": "db-secret",
"namespace": "default"
},
"data": {
"dbcert": "<base64 encoded - snip>",
"dbkey": "<base64 encoded - snip>",
"dbchain": "<base64 encoded - snip>",
"dhparam": "<base64 encoded - snip>"
}
}
- Then, call the secret to be mounted either with defaultMode for entire mount, or with Mode for each ‘file’:
- Permissions specified per file:
"volumes": [{
"name": "secrets",
"secret": {
"secretName": "db-secret",
"items": [{
"key": "dbcert",
"path": "dbcert",
"mode": 420
},{
"key": "dbkey",
"path": "dbkey",
"mode": 256
},{
"key": "dbchain",
"path": "dbchain",
"mode": 420
}]
}
}]
- Permissions specified for entire mounted db-secret volume:
"volumes": [
<snip other volumes>
{
"name": "secrets",
"secret": {
"secretName": "db-secret",
"defaultMode": 256
}
}
]
- Results: Either way, the files remain with the default Secrets permissions of 644:
bash-4.2$ pwd
/etc/secrets/..data
bash-4.2$ ls -la
total 12
drwxr-xr-x 2 root root 100 Oct 17 20:48 .
drwxrwxrwt 3 root root 140 Oct 17 20:48 ..
-rw-r--r-- 1 root root 2533 Oct 17 20:48 dbcert
-rw-r--r-- 1 root root 2106 Oct 17 20:48 dbchain
-rw-r--r-- 1 root root 1708 Oct 17 20:48 dbkey
I see nothing in release notes for any Kubernetes version post-1.4.0 that would indicate a discovered bug that’s been fixed. I haven’t been able to find any filed bugs about this. Am I doing something wrong? I’ve read the docs over and over, it seems pretty simple.
What you expected to happen: When the container starts, the files in the Secrets (db-secret) volume mount should either all be chmod 400 (when using defaultMode with Decimal 256), or at least the dbkey file should be chmod 400 (when using Mode per secret value with Decimal 256).
How to reproduce it Create a secret bundle, upload to the GKE cluster, then define a Pod to mount that secret as a volume with defaultMode or Mode options specified to chmod the secret files to a more restrictive ACL.
Anything else do we need to know: I originally attempted to use this with client/server 1.3.7, not realizing the feature(s) were added in 1.4.0, and was getting a deployment error that defaultMode was an invalid option. Found that it was added in 1.4.0, upgraded my GKE cluster and my local client , and re-deployed. No errors now, but the permissions requested aren’t actually applied.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 33 (9 by maintainers)
Many others referenced in a wall of text. I think a common confusion is that the files are linked, so
ls
will show the link permissions by default.Simply add -L:
ls -laL /path/to/directory/
to dereference the linkI was beating my head on this to understand what happening. @thockin Explaned its correctly. TLDR; But if you, @petrokashlikov can set the permission to the configMap or secret file make use of subPath which wil create the file with the permission specified in defaultMode
Lets take look eg:-
now if you get into the pod and check the permission
drwxr-xr-x 2 root root 4096 Feb 27 07:22 . ‘.’ is current folder; which is no_existing_folder created by k8s have the default permission 0755
-r-------- 1 root root 3243 Feb 27 07:22 secret_key_1_new_name -r-------- 1 root root 422 Feb 27 07:22 secret_key_2_new_name The no_existing_folder created by k8s have the default permission 0755 The secrete file(renamed scret_key_1 -> secret_key_1_new_name) by k8s is having the defaultMode as specified in config Note:- If the no defaultMode is specifed, secret key file permission will be 0644 by default
Now comming back to @thockin explantion. You may have notice that …data, …2019_01_21_03_26_48.920981748 folders are missing when you use the subPath config If we remove the subPath config
eg:
See the different by getting into the pod
Notice that when we removed the subPath config from yaml,
If we check the file permission
Checking permission for actual data
So as i understand if k8s created file or folder automatically(like no_existng_folder in mountPath, no the end file) they will use the default Permission(folder 0755, file 0644). For the end file like the last part of mountPath, will give 0777(It wont respect defaultMode value unless subPath config is specified), though am not sure why 0777 ??
We’re using a workaround as well:
Joshperry–you’re getting the files mounted with octal permissions “0620”…0620 is 400 in decimal. The mode/defaultMode field is treating “400” as a decimal number…and you’re getting the expected behavior for that.
In my case the lifecycle approach ran into race condition issues. If it doesn’t work for you, consider using an init container. This solved my problem with a secret
git-secret
and keyssh
I also faced similar issue and seems it is issue with representation, but actual permissions are fine. I’ve set defaultMode: 256 for volume with ssh keys. When I look at /etc/ssh/keys folder which I mounted, permissions doesn’t look right
lrwxrwxrwx 1 root root 27 Feb 15 23:29 ssh_host_rsa_key.pub -> ..data/ssh_host_rsa_key.pub lrwxrwxrwx 1 root root 23 Feb 15 23:29 ssh_host_rsa_key -> ..data/ssh_host_rsa_key lrwxrwxrwx 1 root root 31 Feb 15 23:29 ssh_host_ed25519_key.pub -> ..data/ssh_host_ed25519_key.pub lrwxrwxrwx 1 root root 27 Feb 15 23:29 ssh_host_ed25519_key -> ..data/ssh_host_ed25519_key
but you see that it is actually not files, but links to actual files and if you go to that directory, permissions are read only and from original /etc/ssh/keys dir you in fact can’t delete files, so there is no write permissions as displayed
root@sftp-5f5cb76dd4-mljww:/etc/ssh/keys/…data# ls -lrt total 16 -r-------- 1 root root 754 Feb 15 23:29 ssh_host_rsa_key.pub -r-------- 1 root root 3401 Feb 15 23:29 ssh_host_rsa_key -r-------- 1 root root 110 Feb 15 23:29 ssh_host_ed25519_key.pub -r-------- 1 root root 419 Feb 15 23:29 ssh_host_ed25519_key
I flagged this for followup, and finally had some time. My test shows it working – someone tell me otherwise?
kubectl apply
this file:What I see in the logs is:
0400 and 0700 are correct. The extra g+r is because I tested the fsGroup at the same time.
This appears fixed (by my repro test)
I’ve changed my config to use octal 0400 (also tried 256 decimal) and it is now working properly. So perhaps not an upgrade issue, at least for me, from ~1.3.