{"id":8001,"date":"2021-06-23T12:37:39","date_gmt":"2021-06-23T17:37:39","guid":{"rendered":"https:\/\/www.rushworth.us\/lisa\/?p=8001"},"modified":"2021-06-23T12:51:31","modified_gmt":"2021-06-23T17:51:31","slug":"git-removing-confidential-info-from-history","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=8001","title":{"rendered":"Git &#8211; Removing Confidential Info From History"},"content":{"rendered":"<p>The first cut of code may contain &#8230; not best practice code. Sometimes this is just hard coding something you&#8217;ll want to compute \/ look up in the future. Hard coding user input isn&#8217;t a problem if my first cut is always searching for ABC123. Hard coding the system creds? Not good. You sort that before you actually <em>deploy<\/em> the code. But some old iteration of the file has MyP2s5w0rD sitting right there in plain text. That&#8217;s bad in a system that maintains file history! The quick\/easy way to clean up passwords stashed within the history is to download the <a href=\"https:\/\/rtyley.github.io\/bfg-repo-cleaner\/\" target=\"_blank\" rel=\"noopener\">BFG JAR<\/a> file.<\/p>\n<p>For this test, I created a new repository in .\\source then created three clones of the repo (.\\clone1, .\\clone2, and .\\clone3). In each cloneX copy, I created a tX folder that has a file named ldapAuthTest.py &#8212; a file that contains a statically assigned password as<\/p>\n<pre>strSystemAccountPass = \"MyP2s5w0rD\"<\/pre>\n<p>The first thing I did was to redact the password in the files &#8212; this means anyone looking at HEAD <em>won&#8217;t<\/em> see the password. Source, clone1, and clone2 are all current. The clone3 copy has pulled all changes but has a local change committed but not merged.<\/p>\n<p>To clean the password from the git history, first create a backup of your repo (just in case!). Then mirror the repo to work on it<\/p>\n<pre>mkdir mirror\r\ncd mirror\r\ngit clone --mirror d:\\git\\testFilterBranch\\source<\/pre>\n<p>&nbsp;<\/p>\n<p>Create file .\\replacements.txt with the string to be redacted &#8212; in this case:<\/p>\n<pre>strSystemAccountPass = \"MyP2s5w0rD\"<\/pre>\n<p>Formatting notes for replacements.txt<\/p>\n<pre>MyP2s5w0rD # Replaces string with default ***REMOVED***\r\nMyP2s4w0rD==&gt;REDACTED # Replaces string using custom string REDACTED\r\nMyP2s3w0rD==&gt; # Replaces string with null -- i.e. removes the string\r\nregex:strSystemAccountPass\\s?=\\s?\"MyP2s2w0rD\"\"==&gt;REDACTED # Uses a regex match -- in this case we may or may not have a space around the equal sign<\/pre>\n<p>So, in my mirror folder, I have the replacement.txt file which defines which strings are replaced. I have a folder that contains the mirror of my repo.<\/p>\n<pre>lisa@FLEX3 \/cygdrive\/d\/git\/testFilterBranch\/mirror\r\n$ ls\r\nreplacements.txt source.git<\/pre>\n<p>To replace my &#8220;stuff&#8221;, run bfg using the &#8211;replace-text option. Because I <em>only<\/em> want to replace the text in files named ldapAuthTest.py, I also added the -fi option<\/p>\n<pre>java -jar ..\/bfg-1.14.0.jar --replace-text ..\\replacements.txt -fi ldapAuthTest.py source.git<\/pre>\n<p>&nbsp;<\/p>\n<pre>lisa@FLEX3 \/cygdrive\/d\/git\/testFilterBranch\/mirror\r\n$ java -jar ..\/bfg-1.14.0.jar --replace-text replacements.txt -fi ldapAuthTest.py source.git\r\n\r\nUsing repo : D:\\git\\testFilterBranch\\mirror\\source.git\r\n\r\nFound 3 objects to protect\r\nFound 2 commit-pointing refs : HEAD, refs\/heads\/master\r\n\r\nProtected commits\r\n-----------------\r\nThese are your protected commits, and so their contents will NOT be altered:\r\n* commit 87f1b398 (protected by 'HEAD')\r\n\r\nCleaning\r\n--------\r\nFound 5 commits\r\nCleaning commits: 100% (5\/5)\r\nCleaning commits completed in 613 ms.\r\n\r\nUpdating 1 Ref\r\n--------------\r\n\r\nRef Before After\r\n---------------------------------------\r\nrefs\/heads\/master | 87f1b398 | 919c8f0f\r\n\r\nUpdating references: 100% (1\/1)\r\n...Ref update completed in 151 ms.\r\n\r\nCommit Tree-Dirt History\r\n------------------------\r\n\r\nEarliest Latest\r\n| |\r\n. D D D m\r\n\r\nD = dirty commits (file tree fixed)\r\nm = modified commits (commit message or parents changed)\r\n. = clean commits (no changes to file tree)\r\n\r\nBefore After\r\n-------------------------------------------\r\nFirst modified commit | dc2cd935 | 8764f6f1\r\nLast dirty commit | 9665c4e0 | ccdf0359\r\n\r\nChanged files\r\n-------------\r\n\r\nFilename Before &amp; After\r\n-------------------------------------\r\nldapAuthTest.py | 25e79fa6 ? 4d12fdad\r\n\r\nIn total, 8 object ids were changed. Full details are logged here:\r\nD:\\git\\testFilterBranch\\mirror\\source.git.bfg-report\\2021-06-23\\12-50-00\r\n\r\nBFG run is complete! When ready, run: git reflog expire --expire=now --all &amp;&amp; git gc --prune=now --aggressive<\/pre>\n<p>Check to make sure nothing looks abjectly wrong. Assuming the repo is sound, we&#8217;re ready to clean up and push these changes.<\/p>\n<pre>cd source.git\r\n\r\ngit reflog expire --expire=now --all &amp;&amp; git gc --prune=now --aggressive\r\ngit push<\/pre>\n<p>&nbsp;<\/p>\n<p>Pulling the update from my source repo, I have merge conflicts<\/p>\n<p><a href=\"https:\/\/www.rushworth.us\/lisa\/?attachment_id=8002\" rel=\"attachment wp-att-8002\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-8002\" src=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2021\/06\/clone3-mergeConflicts-1024x414.png\" alt=\"\" width=\"960\" height=\"388\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2021\/06\/clone3-mergeConflicts-1024x414.png 1024w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2021\/06\/clone3-mergeConflicts-300x121.png 300w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2021\/06\/clone3-mergeConflicts-768x310.png 768w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2021\/06\/clone3-mergeConflicts-750x303.png 750w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2021\/06\/clone3-mergeConflicts.png 1200w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p>These are readily resolved and the source repo can be merged into my local copy.<\/p>\n<p><a href=\"https:\/\/www.rushworth.us\/lisa\/?attachment_id=8003\" rel=\"attachment wp-att-8003\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8003\" src=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2021\/06\/clone3-ResolvedSavedAndStaged.png\" alt=\"\" width=\"606\" height=\"301\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2021\/06\/clone3-ResolvedSavedAndStaged.png 606w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2021\/06\/clone3-ResolvedSavedAndStaged-300x149.png 300w\" sizes=\"auto, (max-width: 606px) 100vw, 606px\" \/><\/a><\/p>\n<p>And the change I had committed but not pushed is still there.<\/p>\n<p><a href=\"https:\/\/www.rushworth.us\/lisa\/?attachment_id=8004\" rel=\"attachment wp-att-8004\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8004\" src=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2021\/06\/CommittedChangeStillThere.png\" alt=\"\" width=\"555\" height=\"135\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2021\/06\/CommittedChangeStillThere.png 555w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2021\/06\/CommittedChangeStillThere-300x73.png 300w\" sizes=\"auto, (max-width: 555px) 100vw, 555px\" \/><\/a><\/p>\n<p>Pushing that change produces no errors<\/p>\n<p><a href=\"https:\/\/www.rushworth.us\/lisa\/?attachment_id=8005\" rel=\"attachment wp-att-8005\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8005\" src=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2021\/06\/CommitedChangePushed.png\" alt=\"\" width=\"541\" height=\"179\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2021\/06\/CommitedChangePushed.png 541w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2021\/06\/CommitedChangePushed-300x99.png 300w\" sizes=\"auto, (max-width: 541px) 100vw, 541px\" \/><\/a><\/p>\n<p>Now &#8230; pushing the bfg changes may not work. In my case, the real repo has a bunch of branchs and I am getting &#8220;non fast-forward merges&#8221;. To get the history changed, I need to do a <em>force<\/em> push. Not so good for the other developers! In that case, everyone should get their changes committed and pushed. The servers should be checked to ensure they are up to date. Then the force push can be done and everyone can pull the new &#8220;good&#8221; data (which, really, shouldn&#8217;t differ from the old data &#8230; it&#8217;s just the history that is being tweaked).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The first cut of code may contain &#8230; not best practice code. Sometimes this is just hard coding something you&#8217;ll want to compute \/ look up in the future. Hard coding user input isn&#8217;t a problem if my first cut is always searching for ABC123. Hard coding the system creds? Not good. You sort that &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[233,69],"class_list":["post-8001","post","type-post","status-publish","format-standard","hentry","category-coding","tag-git","tag-security"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/8001","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8001"}],"version-history":[{"count":1,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/8001\/revisions"}],"predecessor-version":[{"id":8006,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/8001\/revisions\/8006"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8001"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8001"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}