실수로 GitHub에 커밋된 암호 제거

7425 단어 bfgterminalgitcli
보너스 - 내 Twitch Stream에서 내가 이것에 대해 이야기하는 것을 볼 수도 있습니다.

실수로 GitHub에 커밋된 암호 제거



매일 코드를 작성한다는 것은 비밀, 연결 문자열 등이 실수로 코드에 추가된다는 것을 의미합니다. 그리고 당신이 나와 같다면 그들은 당신의 GitHub 리포지토리에 전념하고 당신은 부끄러운 삶을 살아야 합니다. :) 이 게시물에서는 이미 비밀을 커밋한 GitHub 저장소에서 비밀을 제거하는 방법을 안내합니다.

1부 - 초기 설정:

시나리오: 값이 qph@}uC,7cGLBdsX인 암호를 GitHub 리포지토리에 커밋했습니다. 이 암호는 기밀이어야 하며 코드에 저장되지 않아야 합니다.

어떻게 수정합니까?
  • 로컬 디스크에 리포지토리가 있는지 확인하거나 HTTPS 또는 SSH를 사용하여 새 복사본을 복제하십시오. SSHgit clone [email protected]:mbcrump/crumpbot.git를 샘플로 사용하겠습니다.
  • 다음git clone --mirror [email protected]:mbcrump/crumpbot.git과 같이 미러 옵션을 사용하여 비밀이 저장된 저장소의 복사본을 복제합니다.
  • 이제 BARE 저장소가 생깁니다. cd crumpbot.git를 사용하여 CD에 넣고 ls -l를 실행하여 macOS에서 콘텐츠를 나열하거나 Windows에서 dir 콘텐츠를 나열합니다.

  • 아래는 내 repo의 예입니다.

    [mbcrump@Michaels-MBP-3]:[~/Documents/code]$ cd crumpbot.git
    [mbcrump@Michaels-MBP-3]:[~/Documents/code/crumpbot.git] (BARE:master)$ ls -l
    total 32
    -rw-r--r--   1 mbcrump  staff   23 Dec  1 19:47 HEAD
    -rw-r--r--   1 mbcrump  staff  211 Dec  1 19:47 config
    -rw-r--r--   1 mbcrump  staff   73 Dec  1 19:47 description
    drwxr-xr-x  13 mbcrump  staff  416 Dec  1 19:47 hooks
    drwxr-xr-x   3 mbcrump  staff   96 Dec  1 19:47 info
    drwxr-xr-x  27 mbcrump  staff  864 Dec  1 19:48 objects
    -rw-r--r--   1 mbcrump  staff  105 Dec  1 19:47 packed-refs
    drwxr-xr-x   4 mbcrump  staff  128 Dec  1 19:47 refs
    


    2부 - 제거하려는 비밀번호 파일을 만듭니다.
  • passwords.txt 파일을 만들고 GitHub 리포지토리에서 제거하려는 암호를 배치하고 입력합니다.

  • Windows에서 touch passwords.txt 또는 echo some-text > passwords.txt를 사용하여 macOS에서 광산을 만들고 실수로 커밋한 암호를 추가했습니다.

    qph@}uC,7cGLBdsX
    


  • 파일을 저장합니다.

  • 3부 - BFG 설치:

    BFG 을 입력합니다. 저자에 따르면:

    BFG is a simpler, faster alternative to git-filter-branch for cleansing bad data out of your Git repository history:
    Removing Crazy Big Files
    Removing Passwords, Credentials & other Private data


  • Homebrew가 설치되어 있고 Mac을 사용하고 있다고 가정하고 brew install bfg 또는 Windows를 사용하는 경우 BFG JAR 파일을 사용하여 download을 설치합니다.

  • 파트 4 - 이전에 커밋된 암호 정리:
  • Mac에서 bfg --replace-text passwords.txt crumpbot.git를 실행하거나 JAR 파일을 사용하는 경우 java -jar bfg.jar --replace-text passwords.txt crumpbot.git를 실행하십시오.
  • 다음은 해당 명령의 출력입니다.

  • [mbcrump@Michaels-MBP-3]:[~/Documents/code]$ bfg --replace-text passwords.txt crumpbot.git
    
    Using repo : /Users/mbcrump/Documents/code/crumpbot.git
    
    Found 2489 objects to protect
    Found 2 commit-pointing refs : HEAD, refs/heads/master
    
    Protected commits
    -----------------
    
    These are your protected commits, and so their contents will NOT be altered:
    
     * commit 58969937 (protected by 'HEAD')
    
    Cleaning
    --------
    
    Found 11 commits
    Cleaning commits:       100% (11/11)
    Cleaning commits completed in 96 ms.
    
    Updating 1 Ref
    --------------
    
            Ref                 Before     After   
            ---------------------------------------
            refs/heads/master | 58969937 | 3f9041c9
    
    Updating references:    100% (1/1)
    ...Ref update completed in 24 ms.
    
    Commit Tree-Dirt History
    ------------------------
    
            Earliest      Latest
            |                  |
            D D D D DD D D m m m
    
            D = dirty commits (file tree fixed)
            m = modified commits (commit message or parents changed)
            . = clean commits (no changes to file tree)
    
                                    Before     After   
            -------------------------------------------
            First modified commit | 39e68d03 | 95e6f9f4
            Last dirty commit     | 2007b5c5 | 0f57a693
    
    Changed files
    -------------
    
            Filename   Before & After                               
            --------------------------------------------------------
            bot.js   | 1b55a8d0 ⇒ 02758dd8, cba19782 ⇒ db95f8c2, ...
    
    
    In total, 19 object ids were changed. Full details are logged here:
    
            /Users/mbcrump/Documents/code/crumpbot.git.bfg-report/2019-12-01/19-48-22
    
    BFG run is complete! When ready, run: git reflog expire --expire=now --all && git gc --prune=now --aggressive
    


    5부 - GitHub로 푸시:
  • 출력에 표시된 대로 실행git reflog expire --expire=now --all && git gc --prune=now --aggressive합니다.
  • git push를 실행하여 저장소로 푸시합니다.

  • 6부 - 정리 및 리포지토리가 성공적으로 업데이트되었는지 확인합니다.

    GitHub 리포지토리로 돌아가 이전 커밋을 보면 다음과 같이 *REMOVED*가 표시되어야 합니다.

    var tmi = require("tmi.js")
    var channel = "mbcrump"
    
    var config = {
        options: {
            debug: true
        }, 
        connection: {
            cluster: "aws", 
            reconnect: true
        },
        identity: {
            username: "mbcrump",
            password: "***REMOVED***"
        },
        channels: [channel]
    }
    


    에서 지적한 바와 같이

    Such commits should always be treated as the equivalent of a password leak.

    There is no way to guarantee that the exposed passwords have not been copied elsewhere so should always be considered compromised and changed.



    이것이 누군가에게 도움이 되기를 바라며 연락을 유지하고 싶다면 Twitch 또는 GitHub 에서 찾을 수 있습니다.

    좋은 웹페이지 즐겨찾기