Skip to content

Instantly share code, notes, and snippets.

@PatrLind
Created April 26, 2021 08:43
Show Gist options
  • Save PatrLind/e651d3cbc3bf68e4bd9fcc9568cbd3fb to your computer and use it in GitHub Desktop.
Save PatrLind/e651d3cbc3bf68e4bd9fcc9568cbd3fb to your computer and use it in GitHub Desktop.
How to protect your ~/.kube/ configuration

How to protect your ~/.kube/ configuration

I had a need to protect my Kubernetes config file on my computer against accidental or malicious change or reading, so I came up with this way of protecting the config files.

How it works

The ~/.kube folder is mounted using encfs. By using the --ondemand flag it will automatically ask for the encryption key/password when accessed and keep the encrypted filesystem mounted for a period of time. When the filesystem hasn't been accessed for a while the .kube folder will be automatically unmounted.

My environment

I am running Ubuntu 18.04.5 LTS and connecting to it with SSH. My client PC is running Windows 10 and I also have a XServer (Xming server) set up on Windows. The way encfs gets the password is by calling the --extpass application. This application cannot be executed in the shell since there is no way for the shell to spawn another "window"/application at the same time. Instead it is executed using X11 (X11 forwarding over SSH in my setup). If you are running this directly on your Linux Desktop I assume it will just spawn another window with the password prompt.

How to set up

Prerequisites

Make sure you are in your home directory:

cd ~

Create a shell script called ask-pass.sh, this will be used to ask for the password:

#!/bin/sh
zenity --timeout=60 --password --title="Mount password for '$encfs_root'"

Create another shell script called mount-kube.sh, this will be used to start encfs in the background:

#!/bin/sh
# Idle time is 15 minutes by default, change according to your needs
encfs --ondemand --idle=15 --extpass=~/ask-pass.sh --delaymount ~/.kuberaw ~/.kube

Make the scripts runnable:

chmod u+x ask-pass.sh
chmod u+x mount-kube.sh

Rename your existing .kube directory for backup (if you have one):

mv .kube kube-old

Start encfs by running mount-kube.sh:

./mount-kube.sh

The first time you run the command you will get some questions on how to create the new encrypted filesystem:

The directory "/home/YOUR-HOME-FOLDER/.kuberaw/" does not exist. Should it be created? (y,N) y
The directory "/home/YOUR-HOME-FOLDER/.kube/" does not exist. Should it be created? (y,N) y
Creating new encrypted volume.
Please choose from one of the following options:
 enter "x" for expert configuration mode,
 enter "p" for pre-configured paranoia mode,
 anything else, or an empty line will select standard mode.
?> p

Paranoia configuration selected.

Configuration finished.  The filesystem to be created has
the following properties:
Filesystem cipher: "ssl/aes", version 3:0:2
Filename encoding: "nameio/block", version 4:0:2
Key Size: 256 bits
Block Size: 1024 bytes, including 8 byte MAC header
Each file contains 8 byte header with unique IV data.
Filenames encoded using IV chaining mode.
File data IV is chained to filename IV.
File holes passed through to ciphertext.

-------------------------- WARNING --------------------------
The external initialization-vector chaining option has been
enabled.  This option disables the use of hard links on the
filesystem. Without hard links, some programs may not work.
The programs 'mutt' and 'procmail' are known to fail.  For
more information, please see the encfs mailing list.
If you would like to choose another configuration setting,
please press CTRL-C now to abort and start over.

Now you will need to enter a password for your filesystem.
You will need to remember this password, as there is absolutely
no recovery mechanism.  However, the password can be changed
later using encfsctl.

You should get a password prompt dialog window showing. Enter your desired password that is used to encrypt the .kube folder.

Now, copy the old configuration (if you have any) into the newly created folder:

cp -ar kube-old/* .kube/

If you have exiting configuration you can now make sure that kubectl works as it did before:

kubectl get pods

That's it! If you haven't accessed the config folder in a while (by running kubectl for example) the filesystem will be automatically unmounted (after 15 minutes by default). By accessing the files again (by running kubectl or ls or similar) you will get a password prompt before the command can complete.

If you experience problems mounting, or if the encfs has crashed, try running unmount with fusermount and then re-mount:

fusermount -u .kube
./mount-kube.sh
@y0zg
Copy link

y0zg commented May 18, 2021

Shouldn't chattr standard linux tool do the same?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment