redacted_credential_diff 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 95809195c46f686b6bcb4538b1521a919156d31a322597f1db2fe71ddaa843b3
4
- data.tar.gz: fede840e13b5a97a07d4a2f3acb8bec86c0911ad033026eb709d3c1d49b85afd
3
+ metadata.gz: 5c7857d95467a4330678c708f85dbcb31c88bd00735b9b9342c558de0eb9167e
4
+ data.tar.gz: 6def15904b3af0f793097ffe7c7ff79f0d21d0740d262d584cb418b817c93d0c
5
5
  SHA512:
6
- metadata.gz: 237dc762805b23f35c25e0bbd04801566926706d2222a6e6b8932fd2da68a9cfcf904b5760d246264517cc03c275334a3eefe20b295df6308975d04923433855
7
- data.tar.gz: 85714e50a0448902f47a9e80d305a3609ef244065296b52f82c65e1de57eb871d59a7486c0e826f1361cb48b7832ec95b7405441f3a31b62421d97a9131449b0
6
+ metadata.gz: 876b68b510a9b803ae2b3036d9fa93e7aaa90350c2bfdc917aabeba4b8d36a58b3d851b61658e4f15d0cacfc394d4eb2c8d336ba8931040d1bdfacfa7503e5b0
7
+ data.tar.gz: 1edca661b68cba5e6f1978637ec2abccc5df65b81992391425146e7da58d5077f4e2bc4a2222c6b00c74633fd6070686d436e6cb840860a9e9ad565ddf95ed88
data/README.md CHANGED
@@ -18,6 +18,10 @@ Rails' built-in driver (`bin/rails credentials:diff`) decrypts both sides of a d
18
18
  gem "redacted_credential_diff"
19
19
  ```
20
20
 
21
+ In a project already enrolled in Rails' credentials diffing, where `.gitattributes` names `rails_credentials`, that is the whole setup. The driver Rails installed is pointed at the redacting command the next time the app loads.
22
+
23
+ In a project that is not, or to name the driver explicitly:
24
+
21
25
  ```
22
26
  bin/rails redacted_credential_diff:enroll
23
27
  git commit .gitattributes
@@ -34,7 +38,7 @@ Git splits a diff driver into two parts.
34
38
  - `.gitattributes` is committed and names the driver for each credentials file. `enroll` assigns `diff=redacted_credentials`, replacing Rails' `diff=rails_credentials` entries.
35
39
  - The driver's command lives in each clone's `.git/config`, which cannot be committed. A Railtie writes it whenever the app loads in a development or test checkout.
36
40
 
37
- Git then calls `bundle exec redacted_credential_diff <path>` once for each side of a diff and compares the two outputs itself. The command never sees both sides; it only renders one file.
41
+ Git then calls `bundle exec redacted_credential_diff <path>` once for each side of a diff and compares the two outputs itself. The command never sees both sides; it only renders one file. Where it cannot run, the installed driver prints the file as stored, because git aborts the whole diff when a driver fails.
38
42
 
39
43
  The command does not load the Rails app. It decrypts with `ActiveSupport::EncryptedConfiguration` and finds the key by Rails' naming convention: `config/credentials/<name>.key` for `config/credentials/<name>.yml.enc` when that key exists, otherwise `config/master.key`. `RAILS_MASTER_KEY` takes precedence, as in Rails.
40
44
 
@@ -57,17 +61,34 @@ The result is checked before it is printed: it must parse to the same keys as th
57
61
  | Gem installed, app loaded at least once | Redacted |
58
62
  | Fresh clone, app never loaded | Encrypted file as stored |
59
63
  | No encryption key available | Encrypted file as stored |
60
- | Branch that predates the gem | `fatal: unable to read files to diff` |
61
- | Clone already holding Rails' driver, app not loaded since the gem was added, on a branch that predates the gem | Decrypted |
64
+ | Branch that predates the gem | Encrypted file as stored |
65
+ | Gem removed from the project | Encrypted file as stored |
66
+
67
+ Once the app has loaded in a clone, none of its branches print decrypted credentials. The Railtie also points Rails' `rails_credentials` driver at the redacting command, which covers branches whose `.gitattributes` still names it. Having that driver defined also stops `bin/rails credentials:edit` from installing the decrypting one.
68
+
69
+ ## Removing
70
+
71
+ ```
72
+ bin/rails redacted_credential_diff:unenroll
73
+ git commit .gitattributes
74
+ ```
75
+
76
+ Then remove the gem from the Gemfile. `unenroll` takes the driver out of `.gitattributes` and out of the current clone's git config, leaving credentials files with no diff driver, as Rails generates a project.
62
77
 
63
- Once the app has loaded in a checkout, none of its branches print decrypted credentials. The Railtie also points Rails' `rails_credentials` driver at the redacting command; on a branch where that command does not exist, git aborts the diff. Having the driver defined also stops `bin/rails credentials:edit` from installing the decrypting one.
78
+ The driver's command stays in every other clone's git config, where it prints the encrypted file once the gem is gone. Rails does not replace it, so each clone removes it by hand:
79
+
80
+ ```
81
+ git config --unset diff.redacted_credentials.textconv
82
+ git config --unset diff.rails_credentials.textconv
83
+ ```
64
84
 
65
- The last row is the gap: a clone that ran `credentials:edit` before the gem arrived keeps Rails' decrypting driver until the app next loads there, and that driver still applies on branches whose `.gitattributes` names it.
85
+ `bin/rails credentials:diff --enroll` then restores Rails' decrypting diffs.
66
86
 
67
87
  ## Limits
68
88
 
69
89
  - This covers git's diff output only. `bin/rails credentials:show` and `credentials:edit` are unchanged.
70
90
  - `git diff --no-textconv` and hosted diff views bypass the driver and show the encrypted file.
91
+ - Until the app first loads in a clone with the gem installed, that clone keeps whatever driver it already had.
71
92
  - Key names are printed. A file whose keys are themselves sensitive, such as a map keyed by customer, still shows them.
72
93
  - Identical values under different keys share a fingerprint.
73
94
  - Each side of a diff starts `bundle exec`, so `git log -p` across a long credentials history is slow.
@@ -1,8 +1,8 @@
1
1
  require "pathname"
2
2
 
3
3
  module RedactedCredentialDiff
4
- # The git diff driver that renders credentials files through
5
- # `bundle exec redacted_credential_diff`.
4
+ # The git diff driver that renders credentials files through the
5
+ # `redacted_credential_diff` command.
6
6
  #
7
7
  # Git splits a diff driver in two. The .gitattributes file is committed and
8
8
  # names the driver for each credentials file; the driver's command lives in
@@ -15,7 +15,11 @@ module RedactedCredentialDiff
15
15
  # command so that branches still naming it in .gitattributes are covered.
16
16
  NAMES = [ NAME, "rails_credentials" ]
17
17
 
18
- COMMAND = "bundle exec redacted_credential_diff"
18
+ # Git aborts the whole diff when a driver's command fails. The command
19
+ # outlives the gem in each clone's git config, so where it cannot run, on a
20
+ # branch that predates the gem or after the gem is removed, it prints the
21
+ # file as stored instead.
22
+ COMMAND = %(sh -c 'bundle exec redacted_credential_diff "$0" 2>/dev/null || cat "$0"')
19
23
 
20
24
  ATTRIBUTES = <<~END
21
25
  config/credentials/*.yml.enc diff=#{NAME}
@@ -37,7 +41,13 @@ module RedactedCredentialDiff
37
41
  end
38
42
 
39
43
  def installed?
40
- NAMES.all? { |name| git("config", "--get", "diff.#{name}.textconv").strip == COMMAND }
44
+ NAMES.all? { |name| command_for(name) == COMMAND }
45
+ end
46
+
47
+ # Removes the driver from this clone's git config. A name that has since
48
+ # been given another command is left alone.
49
+ def uninstall
50
+ NAMES.each { |name| git "config", "--unset", "diff.#{name}.textconv" if ours?(name) }
41
51
  end
42
52
 
43
53
  # Names the driver for the credentials files in .gitattributes, replacing
@@ -46,11 +56,26 @@ module RedactedCredentialDiff
46
56
  gitattributes.write(unassigned_attributes + ATTRIBUTES)
47
57
  end
48
58
 
59
+ # Leaves the credentials files with no diff driver in .gitattributes, which
60
+ # is how Rails generates a project.
61
+ def unenroll
62
+ gitattributes.write(unassigned_attributes) if gitattributes.file?
63
+ end
64
+
49
65
  private
50
66
  def checkout?
51
67
  @root.join(".git").exist?
52
68
  end
53
69
 
70
+ def command_for(name)
71
+ git("config", "--get", "diff.#{name}.textconv").strip
72
+ end
73
+
74
+ # True for any command this gem has installed, including earlier forms of it.
75
+ def ours?(name)
76
+ command_for(name).include?("redacted_credential_diff")
77
+ end
78
+
54
79
  # The existing .gitattributes without any diff driver for credentials files.
55
80
  def unassigned_attributes
56
81
  return "" unless gitattributes.file?
@@ -5,6 +5,24 @@ namespace :redacted_credential_diff do
5
5
  driver.enroll
6
6
  driver.install
7
7
 
8
- puts "Credentials files now diff through `#{RedactedCredentialDiff::DiffDriver::COMMAND}`. Commit .gitattributes to enroll the rest of the team."
8
+ puts "Credentials files now diff with their values redacted. Commit .gitattributes to enroll the rest of the team."
9
+ end
10
+
11
+ desc "Remove the redacting diff driver from .gitattributes and from this clone's git config"
12
+ task :unenroll do
13
+ driver = RedactedCredentialDiff::DiffDriver.new(Rails.root)
14
+ driver.unenroll
15
+ driver.uninstall
16
+
17
+ puts <<~END
18
+ Credentials files no longer have a diff driver. Commit .gitattributes and remove the gem from the Gemfile.
19
+
20
+ Other clones keep the driver in their git config and diff the encrypted file until it is removed:
21
+
22
+ git config --unset diff.redacted_credentials.textconv
23
+ git config --unset diff.rails_credentials.textconv
24
+
25
+ To restore Rails' decrypting diffs afterwards: bin/rails credentials:diff --enroll
26
+ END
9
27
  end
10
28
  end
@@ -1,3 +1,3 @@
1
1
  module RedactedCredentialDiff
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redacted_credential_diff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Tilberg