Consider a file with user-read-only permissions, for example ...

-r--------+ admin secrets.txt

How can such a file be put under revision control, so that its contents remain secret, even from the revision control administrator?


Use GPG to encrypt the file before commiting to your repo.

Yes, it's cumbersome (you won't be able to diff/merge/etc. without decrypting first) but I can't conceive of any other way to skin this cat.


Store the secrets in a separate file (not under version control), and insert the secret content into the other file with a script or Puppet-like tool.

Working from this other answer, a simple example could be:

netjoin.sh.erb (stored in version control):

# Usage: netjoin.sh /path/to/samba/binary/net pdc-hostname
HOSTNAME=`facter hostname`-`facter operatingsystem`
PASSWORD=<%= scope.function_generate("/etc/puppet/auth/getpwd", "ad", "netjoin") %>
${NET} rpc user delete ${HOSTNAME}\$ -U netjoin%${PASSWORD} -S ${SERVER}
${NET} rpc join -U netjoin%${PASSWORD} -S ${SERVER}
/bin/rm -f $0

/etc/puppet/auth/getpwd (can also be stored in version control):

# /etc/puppet/auth/getpwd
if [ "$#" -ne 2 ]; then
    echo "Usage: $0 <db> <user>"
    exit 1
if [ ! -x /usr/bin/pwgen ]; then
    echo "missing pwgen!" >&2
    exit 1
workdir=`dirname $0`
[ ! -r $workfile ] && exit 2
# get password from storage
pwd=`egrep "^${get_name}:" ${workfile} | cut -d: -f2-`
if [ "$pwd" = "" ]; then
    # generate new password & store it
    len=$((60 + $RANDOM % 9 ))
    pwd=$(/usr/bin/pwgen -s $len 1)
    echo "${get_name}:${pwd}" >> $workfile
# echo password (without new line)
echo -n "$pwd"

/etc/puppet/auth/passwd_ad (absolutely not in version control):


Be careful when you version sensitive files with Perforce - since Perforce doesn't deal with file permission other than the executable bit depending your umask you will be surprised to see file permissions will get messed up as you check them into Perforce:

$ p4 init
Matching server configuration from 'perforce:1666':
case-sensitive (-C0), unicode (-xi)
Server lester-dvcs-1449094406 saved.
$ date > x 
$ l x      
-rw-rw-r--. 1 lester lester 30 Dec  3 09:13 x
$ chmod 400 x
$ l x        
-r--------. 1 lester lester 30 Dec  3 09:13 x
$ umask  
$ p4 add x; p4 submit -d add 
//stream/main/x#1 - opened for add
Submitting change 1.
Locking 1 files ...
add //stream/main/x#1
Change 1 submitted.
$ l x                        
-rw-rw-r--. 1 lester lester 30 Dec  3 09:13 x   <-- oops!

If anyone have a good workaround I'm all ears! In the meantime I'm using mercurial/git to version my secret files (/etc actually) and push the the repo somewhere safe - at least permissions of my local files will remain unchanged.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.