git 사용법 설치 명령어 사용 예시

비밀번호, Secret Key 값과 같이 보호되야 하는 데이터가 포함된 파일을 그냥 Git Commit & Push 하면 개인적인 데이터가 노출되는 문제점이 발생합니다.

git-secret 을 사용하면 개인적인 데이터가 포함된 파일을 암호화 한 후에 Git 저장소에 저장함으로써 문제점을 해결할 수 있습니다.

git-secret 은 Git 저장소에 개인적인 데이터를 gpg 를 기반으로 암호화하여 저장하는 Bash Tool 입니다.
즉, 암호화 대상 파일은 Public Key로 암호화하고, 복호화는 Private Key를 소유한 사용자만 할 수 있습니다.

이를 통해 실수로 비밀번호, Secret Key와 같이 노출되면 안되는 데이터를 그대로 Git 저장소에 업로드하는 실수를 막을 수 있습니다.

사용법

git-secret 의 기본적인 사용법은 다음과 같습니다. :

  1. gpg RSA key-pair 를 먼저 생성해야 합니다.
    RSA key-pair 는 E-Mail 주소로 증명되는 Public / Private Key 입니다.
  2. git-secret을 사용할 저장소에서 git-secret init 명령어로 초기화 합니다.
    이때 .gitsecret/ 디렉토리가 생성됩니다. 이 디렉토리가 .gitignore 에 의해 제외되지 않도록 해야 합니다.
  3. 첫 번째 사용자를 추가합니다. : git secret tell [email protected]
  4. git-secret으로 보호할 파일을 추가합니다. : git-secret add <filenames...>
    이때 추가할 파일은 .gitignore 를 사용하여 무시하도록 설정해야 합니다.
  5. 보호할 파일을 암호화 합니다. : git-secret hide
    이제 암호화된 파일을 Commit & Push 하더라도 안전하게 보관할 수 있습니다.
    단, 반드시 매 Commit 전에 반드시 git-secret hide 명령어를 실행하여 변경된 사항이 반영되도록 해야 합니다.
  6. 보호된 파일을 복호화 합니다. 이때 비밀번호 입력을 요구합니다. : git-secret reveal

다른 사용자를 추가하는 방법은 다음과 같습니다. :

  1. gpg Public Key를 받습니다. (Private Key는 필요 없습니다.)
  2. 당신의 gpg에 전달 받은 Public Key를 Import 합니다. : gpg --import KEY_NAME
  3. git-secret에 사용자를 추가합니다. : git-secret tell [email protected]
  4. 파일들을 다시 암호화 합니다.
    이제 새로 추가된 사용자들은 그들의 Private Key로 복호화 할 수 있습니다.

설치

설치 전 요구사항

  • git 2.7.0 버전 이상
  • gpg (GnuPG) 1.4.20 버전 이상
  • 운영체제 : Mac OS X >= 10.9, Ubuntu >- 14.04, Debian >= 8.3, Fedora

설치 방법

여기선 Mac OS X 환경에서 설치하는 과정만 포함합니다.
각 OS 별 설치 방법은
git-secret Installation 문서를 참고 바랍니다.

1
2
3
4
5
6
7
8
9
10
11
$ brew install git
$ git --version
git version 2.20.0

$ brew install gnupg
$ gpg --help
gpg (GnuPG) 2.2.11
...하단 생략...

$ brew install git-secret
0.2.4

명령어

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ git-secret usage

usage: git secret [--version] [add|cat|changes|clean|hide|init|killperson|list|remove|reveal|tell|usage|whoknows]
'git secret --version' will show version and exit

See 'git secret [command] -h' for more information on each command below
add [filename.txt] - adds file to be hidden. Also add this file to .gitignore
cat [filename.txt] - cats the decrypted contents of the named file to stdout
changes [filename.secret] - indicates if the file has changed since checkin
clean - deletes encrypted files
hide - encrypts (or re-encrypts) the files to be hidden
init - creates the .gitsecret directory and contents needed for git-secret
killperson [emails] - the reverse of 'tell', removes access for the named user
list - shows files to be hidden/encrypted, as in .gitsecret/paths/mapping.cfg
remove [files] - removes files from list of hidden files
reveal - decrypts all hidden files, as mentioned in 'git secret list'
tell [email] - add access for the user with imported public key with email
whoknows - shows list of email addresses associated with public keys that can reveal files

사용 예시

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

# gpg 키 생성
# - Real Name, E-Mail 입력
#. - --full-generate-key 옵션은 Mac OS X에서 사용합니다.

$ gpg --full-generate-key
또는
$ gpg --gen-key


# git-secret 초기화
#. - 명령어 : git-secret init

$ git-secret init
'/Users/jhyunto/Documents/workspace/oauth2.0-cookbook/.gitsecret/' created.
cleaning up...


# 사용자 추가
#. - git-secret

$ git secret tell [email protected]
gpg: keybox '/Users/hyunsoo0813/Documents/workspace/oauth2.0-cookbook/.gitsecret/keys/pubring.kbx' created
gpg: /Users/hyunsoo0813/Documents/workspace/oauth2.0-cookbook/.gitsecret/keys/trustdb.gpg: trustdb created
done. [email protected] added as someone who know(s) the secret.
cleaning up...


# 파일 추가
# - 추가할 파일이 git tracked 중 이라면 먼저 제거해야 합니다. : git rm --cached <filename>
#. - 명령어 : git-secret add <filename>

$ git rm --cached client-implicit/src/main/resources/application.properties
rm 'client-implicit/src/main/resources/application.properties'
$ echo 'client-implicit/src/main/resources/application.properties' >> .gitignore
$ git-secret add client-implicit/src/main/resources/application.properties
1 item(s) added.

$ git rm --cached social-authcode/src/main/resources/application.properties
rm 'social-authcode/src/main/resources/application.properties'
$ echo 'social-authcode/src/main/resources/application.properties' >> .gitignore
$ git-secret add social-authcode/src/main/resources/application.properties
1 item(s) added.


# 암호화
# - 명령어 : git-secret hide

$ git-secret hide
done. all 1 files are hidden.

###
# 복호화
# - 명령어 : git-secret reveal
###
$ git-secret reveal
File '/Users/hyunsoo0813/Documents/workspace/oauth2.0-cookbook/client-implicit/src/main/resources/application.properties' exists. Overwrite? (y/N) y
File '/Users/hyunsoo0813/Documents/workspace/oauth2.0-cookbook/social-authcode/src/main/resources/application.properties' exists. Overwrite? (y/N) y
File '/Users/hyunsoo0813/Documents/workspace/oauth2.0-cookbook/social-linkd-in/src/main/resources/application.properties' exists. Overwrite? (y/N) y
done. all 3 files are revealed.

Reference