본문 바로가기
개발 공부

Github 레포에 올린 민감한 정보 예방하기 + 제거하기(.gitignore + BFG Repo-Cleaner 사용법)

by 김 Duke 2023. 6. 30.

목차

    API호출 시 필요한 개인정보를 레포에 올려버림 -> info.plist에 내용 옮기고 -> gitignore로 info.plist 등록 -> 캐시도 제거 -> 하지만 HomeViewModel.swift 파일에 있던 개인정보는 그대로 커밋에 남아있음 -> BFG 이용하여 커밋에서 HomeViewModel 파일 제거

     

    1. .gitignore를 통해 민감한 정보 제외하여 커밋하기

    예시로 적어둔 민감한 정보

    위의 사진처럼 민감한 정보가 퍼블릭 레포에 그대로 올라가있었다. 해당 ID와 Secret키는 Spotify를 구독해야만 얻을 수 있기 때문에, 악용될 가능성이 있어서 Info.plist에 정보를 넣어준 후 Info.plist파일을 .gitignore에 등록해줄 것이다.

     

    Bundle name의 +버튼을 눌러 Key값을 추가하고, Value에는 Key값에 해당하는 정보를 넣어준다.

     

        private var clientID: String {
          get {
            // 1
            guard let filePath = Bundle.main.path(forResource: "Info", ofType: "plist") else {
              fatalError("Couldn't find file 'Info.plist'.")
            }
            // 2
            let plist = NSDictionary(contentsOfFile: filePath)
            guard let value = plist?.object(forKey: "ClientID") as? String else {
              fatalError("Couldn't find key 'ClientID' in 'Info.plist'.")
            }
            return value
          }
        }
    
        private var clientSecret: String {
          get {
            // 1
            guard let filePath = Bundle.main.path(forResource: "Info", ofType: "plist") else {
              fatalError("Couldn't find file 'Info.plist'.")
            }
            // 2
            let plist = NSDictionary(contentsOfFile: filePath)
            guard let value = plist?.object(forKey: "ClientSecret") as? String else {
              fatalError("Couldn't find key 'ClientSecret' in 'Info.plist'.")
            }
            return value
          }
        }

    그리고 해당하는 정보에 접근하기 위해 정보가 쓰이는 파일 내에 작성해주면 plist에서 값을 불러와 사용할 수있다.

    그리고 .gitignore에 Info.plist를 넣어주면 타인은 개인정보에 접근할 수 없게된다.

     

    https://parkjye.tistory.com/28

     

    [Git] .gitignore 파일 생성 방법

    #.gitignore 프로젝트를 개발할 때 필요한 파일 이외의 파일들이 생성된다. .gitignore은 이러한 파일들을 git 관리 대상에서 제외하기 위해(commit에 포함하지 않도록) 규칙들을 저장한 파일이다. 예를

    parkjye.tistory.com

    위의 링크를 참고하여 만들 수 있다. 

     

    위에서 만든 .gitignore파일에 

    나같은 경우 YoutubeMusicAppClone폴더의 Info.plist를 등록해준다.

     

    등록했음에도 종종 작동하지 않는 경우 git의 캐시가 원인이라고 한다. -> git에 있는 캐시파일을 지우고 다시 add해주면 된다.

    git rm -r --cached .
    git add .
    git commit -m "removed cached"

     

     

    여기까지하면 민감한 정보가 앞으로 커밋할 때 올라가지 않는다. 하지만 Info.plist는 ignore해주었지만, 기존 파일에 있던 정보는 커밋에 그대로 남아있다.

     

    이럴 경우 두 가지 방법이 있다고 한다. Git 공식 문서에 보면 git filter-repo tool 또는 BFG Repo-Cleaner가 있다. 

    장단점이 있지만 전자에 비해 후자가 빠르고 간단하다고 한다. 

     

    2. BFG Repo-Cleaner를 사용하여 커밋에 남은 민감한 정보 지우기

     

    https://rtyley.github.io/bfg-repo-cleaner/

     

    BFG Repo-Cleaner by rtyley

    $ bfg --strip-blobs-bigger-than 100M --replace-text banned.txt repo.git an alternative to git-filter-branch The BFG is a simpler, faster alternative to git-filter-branch for cleansing bad data out of your Git repository history: Removing Crazy Big Files Re

    rtyley.github.io

    여기에서 BFG Repo-Cleaner를 다운받아준다.

     

    git clone --mirror git://example.com/some-big-repo.git
    java -jar bfg.jar --delete-files id_{dsa,rsa}  my-repo.git
    cd some-big-repo.git
    git reflog expire --expire=now --all && git gc --prune=now --aggressive
    git push

    터미널을 켜서 위의 순서대로 진행한다. 

     

    git clone --mirror https://github.com/yahoth/YoutubeMusicClone.git
    java -jar "/Users/taehyoung/Downloads/bfg-1.14.0.jar" --delete-files HomeViewModel.swift  YoutubeMusicClone.git
    cd YoutubeMusicClone.git
    git reflog expire --expire=now --all && git gc --prune=now --aggressive
    git push

    나의 경우는 이렇다. 두번째 코드에서 jar 뒤의 경로는 다운로드 받은 파일의 위치이다.

     

    만약 두번째 명령어를 썻을 때 아래처럼 나온다면,

    The operation couldn’t be completed. Unable to locate a Java Runtime. Please visit http://www.java.com for information on installing Java.

     

    brew install openjdk@11
    sudo ln -sfn $(brew --prefix)/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk

    Homebrew가 있다는 가정하에, 두 명령을 넣어주고 진행한다.

     

    다시 돌아가서, java로 시작하는 두번째 명령어가 잘 수행되면 이런 식의 응답이 아래쪽에 나온다. 

     

    그리고 레포를 확인해보면 기존 커밋들에서 해당 파일이 지워진 것을 확인할 수 있다.

     

     

    출처:

    https://growingarchive.tistory.com/244

     

    [Git] .gitignore 사용법 및 작동하지 않는 경우 (+ .gitignore 템플릿 사이트)

    1. .gitignore란? - 민감한 정보가 들어있는 파일이나 형상관리할 필요가 없는 파일들은 git에서 더이상 추적(track)하지 않도록 .gitignore 파일에 추가해 따로 빼준다. - .gitignore 파일은 프로젝트 디렉토

    growingarchive.tistory.com

    https://rtyley.github.io/bfg-repo-cleaner/

     

    BFG Repo-Cleaner by rtyley

    $ bfg --strip-blobs-bigger-than 100M --replace-text banned.txt repo.git an alternative to git-filter-branch The BFG is a simpler, faster alternative to git-filter-branch for cleansing bad data out of your Git repository history: Removing Crazy Big Files Re

    rtyley.github.io

    https://stackoverflow.com/questions/71059252/mac-the-operation-couldn-t-be-completed-unable-to-locate-a-java-runtime-that-su

     

    Mac The operation couldn’t be completed. Unable to locate a Java Runtime that supports jarsigner

    My purpose is to use jarsigner to sign apk. I get the following prompt: % jarsigner The operation couldn’t be completed. Unable to locate a Java Runtime that supports jarsigner. Please visit h...

    stackoverflow.com

    https://yiunsr.tistory.com/773

     

    git 히스토리 삭제 방법

    git 를 이용하게되면 민감한 데이터(패스워드나 SSH Key)를 commit 하기도 한다. 이 git 가 공개된 git 일 경우 보안상에 문제를 일으키기도 한다. 패스워드는 아니지만 API Key 같은 경우도 문제가 될 수

    yiunsr.tistory.com

    https://velog.io/@loopbackseal/Swift-Plist%EB%A5%BC-%ED%99%9C%EC%9A%A9%ED%95%B4%EC%84%9C-API-key%EB%AF%BC%EA%B0%90%EC%A0%95%EB%B3%B4-%EA%B0%80%EB%A6%AC%EA%B8%B0

     

    [Swift] Plist를 활용해서 API key(민감정보) 가리기 (plist, 연산 프로퍼티, .gitignore)

    GitGuardian에게 혼났을 때

    velog.io

     


    TOP

    Designed by 티스토리