1. 程式人生 > >專案上傳github前清除敏感檔案的歷史防止隱私洩露

專案上傳github前清除敏感檔案的歷史防止隱私洩露

原標題 Android專案上傳github前對 Build.gradle改寫 防止隱私洩露。不過我覺得其實這個方法很通用,就改標題了。真好前幾天也爆出github歷史發現了XX公司管理人員上傳了賬號密碼等敏感資訊. 如果不是Android專案,就直接跳到後半部分開始看起

正文
以前都是習慣直接把keystore硬編碼到gradle.build裡頭。這就會造成如果上傳到github之類的網站就會造成資訊洩露(比如keystore密碼之流)
首先改寫原來含有簽名配置的gradle檔案內容如下

android {

    signingConfigs {
        release
    }

    buildTypes {
            release {
                signingConfig signingConfigs.release
            }
    }
}

def Properties props = new
Properties() def propFile = new File('signing.properties') if (propFile.canRead()){ props.load(new FileInputStream(propFile)) if (props!=null && props.containsKey('STORE_FILE') && props.containsKey('STORE_PASSWORD') && props.containsKey('KEY_ALIAS') && props.containsKey('KEY_PASSWORD'
)) { android.signingConfigs.release.storeFile = file(props['STORE_FILE']) android.signingConfigs.release.storePassword = props['STORE_PASSWORD'] android.signingConfigs.release.keyAlias = props['KEY_ALIAS'] android.signingConfigs.release.keyPassword = props['KEY_PASSWORD'
] } else { println 'signing.properties found but some entries are missing' android.buildTypes.release.signingConfig = null } }else { println 'signing.properties not found' android.buildTypes.release.signingConfig = null }

如上圖所示,其實很多程式碼都是自解釋了:保留一個signingConfigs標籤和裡頭那個配置名字(這裡是release),刪除裡頭硬編碼的內容。到時候就是用android.signingConfigs.release.storeFile 來引用那個配置名字來賦值寫入資訊。其他的def propFile = new File('signing.properties') 就是在和build.gradle同一個目錄找配置檔案signing.properties,如果放在上層(專案根目錄下),就變成../signing.properties 就好了

然後在上面程式碼裡頭寫的目標目錄下建立 signing.properties ,內容如下

STORE_FILE=/path/to/your.keystore
STORE_PASSWORD=yourkeystorepass
KEY_ALIAS=projectkeyalias
KEY_PASSWORD=keyaliaspassword

這裡配置就是填入原來build.gradle 的signingConfigs{ }下的內容了,直接填入具體的名字,不用加什麼引號之類

然後專案根目錄.gitignore 加入signing.properties 這個檔名(非專案根目錄下具體寫出路徑),讓他被git忽略

然後要刪除原來.git下所有build.gradle的歷史檔案,免得被人恢復歷史版本去了

git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch NAME' \
--prune-empty --tag-name-filter cat -- --all

解釋如下:
Force Git to process, but not check out, the entire history of every branch and tag
Remove the specified file, as well as any empty commits generated as a result
Overwrite your existing tags

進入project根目錄下 把NAME替換成要刪除的路徑就可以了

但是以上方法針對的是Linux下的環境,針對Windows這樣執行就會出問題。
問題所在stackoverflow說的很清楚,記得用雙引號代替單引號
還有一點就是不用寫根路徑的/舉個例子,比如我要刪除專案下app資料夾的build.gradle的所有目錄,目錄應該寫成app/build.gradle 而不是/app/build.gradle或者./app/build.gradle
另外就是要把那個換行符號給去了,否則沒法複製貼上

舉個完整的例子如下:比如我們要刪除app/build.gradle的git的所有歷史提交記錄以防有人恢復出密碼等

git filter-branch --force --index-filter "git rm --cached --ignore-unmatch app/build.gradle" --prune-empty --tag-name-filter cat -- --all

github那個指南里頭還提到了用另一個現成的專用於repo清理的工具BFG. 。他會保留HEAD指向的最新的那次提交,刪除所有舊的提交記錄。正合吾意

Using the BFG

The BFG Repo-Cleaner is a faster, simpler alternative to git filter-branch for removing unwanted data. For example, to remove any file named ‘Rakefile’ (and leave your latest commit untouched), run:

bfg --delete-files Rakefile
To replace all text listed in passwords.txt wherever it can be found in your repository’s history, run:

bfg --replace-text passwords.txt
See the BFG Repo-Cleaner’s documentation for full usage and download instructions.

於是去下了bfg,
windows下使用:
進入專案根目錄,CMD

java -jar bfg所在完整路徑 --delete-files 待刪除檔案對於專案根目錄的路徑

清理完用push –force 就OK啦
以上都是補救方法,其實最最主要的還是在提交前多做好審查,寫好.gitignore檔案

以上