项目上传github前清除敏感文件的历史防止隐私泄露

来源:互联网 发布:单词书推荐 知乎 编辑:程序博客网 时间:2024/05/14 18:05

关键代码引用自https://gist.github.com/gabrielemariotti/6856974

原标题 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.keystoreSTORE_PASSWORD=yourkeystorepassKEY_ALIAS=projectkeyaliasKEY_PASSWORD=keyaliaspassword

这里配置就是填入原来build.gradle 的signingConfigs{ }下的内容了,直接填入具体的名字,不用加什么引号之类

然后项目根目录.gitignore 加入signing.properties 这个文件名(非项目根目录下具体写出路径),让他被git忽略

然后要删除原来.git下所有build.gradle的历史文件,免得被人恢复历史版本去了

引用https://help.github.com/articles/remove-sensitive-data/ github官方给出的删除隐私文件命令如下:

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文件

以上

0 0