Hide Secrets in Your Dot Files on Public Repository
I am using dotdrop to manage my dot files. If you don't use it, it's fine, but you may need to do a some extra work to make things done. For those who manually copy and paste their dot files, I highly recommend you to try it out. It can automate your copy-paste flow, and it is highly customizable. It can save you a lot of time dealing with chores. (Nix OS users should ignore what I said)
The conditions can be divided into two:
- Secrets are only some small pieces of code in your dot files. For example, you write a web spider to collect your personal information on a website, but the script file contains your cookie. It can be annoying that you want to share your code and protect your privacy at the same time while using the current dot file automatic workflow. You are in an awkward position.
- Secrets are individual files.
The problem can be solved with encryption
technology. We will use gpg
to encrypt secrets. For those who haven't set their gpg keys
, I recommend them to read through GnuPG - ArchWiki, which is comprehensive and practical.
Solve the First Condition
To deal with those few but annoying secrets, we can use sed
to replace the original secrets with encrypted code. For example, we have a file whose content look like this:
|
|
We want to change the string notapassword
in the first
line to an encrypted string which can only be decrypted by ourselves. What should we do?
Encrypt Secrets
First, Get the secret needed to be encrypted
To get the secret string, we can either manually copy the content if we can ensure that it will not change in the later update or using sed
command. Here I'd like to briefly introduce the second option.
If your match regex is robust(deal with all input and only output a single pattern), like this regex:
|
|
Then the following command will only print out the single password notapassword
|
|
or If you want to restrict the output to a specific range, you can execute the following command:
|
|
Second, encrypt the secret using GPG and base64
Continuing with the above example, here we already have the secret string notapassword
, we then need to encrypt it into a non-breaking string:
|
|
Note 389582ABC15D64CCFB74D82F934AE9B6B6E9FF34
is the keyid
of the key which you want to use to encrypt the string. Toe know your keyid
, please execute command gpg --list-keys
:
|
|
Since we use gpg
to encrypt the string(actually use the public key to encrypt string), we can confirm that unless our private key has leaked out, only ourselves can decrypt the string.
Now that we have gotten the encrypted string, which should be like:
|
|
To get one line string, we can either extract the content in the middle of the encrypted data, or use base64
algorithm to encrypt it. I will introduce the second method.
|
|
-w 0
means don't add line wrap in the base64
encrypted data. By default, base64
will add a line wrap after every 76 characters.
Third, replace the secret with the encrypted string.
We can use a variable to store the previous encrypted string, and the insert the content into the original place.
|
|
Note that under bash environment, string between single quote is raw(e.g. '$a'
means exactly '$a'
), and if you want to insert variables into the string, use nested single quote(e.g. ''$a''
means 'hello'
if $a
equals to 'hello'
).
Sidenote: Decryption
|
|
Automate Dot Files Management Workflow
Please refer to Handle secrets and Transformations entry sections of dotdrop
.
Side Note: For write transformations, {0} and {1} in the 'transformations entry' section means the source file path and the temporary file path. The temporary file will be firstly copied from source file, and then you can modify it in the script. Afterward dotdrop will import it into your repo.
Second Condition
I have no energy to write more (\=____\=). Please refer to the first condition.