Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easier unencrypted keys access. #41

Merged
merged 7 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
eyaml (0.4.0)
eyaml (0.4.3)
rbnacl (~> 7.1)
thor (~> 1.1)

Expand Down
1 change: 0 additions & 1 deletion lib/eyaml/encryption_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ def traverse(tree, &block)
if value.is_a?(Hash)
next [key, traverse(value, &block)]
end
# TODO(es): Add tests for keys with an underscore prefix not doing a nested skip
if key.start_with?("_")
next [key, value]
end
Expand Down
3 changes: 2 additions & 1 deletion lib/eyaml/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ class ConflictError < StandardError
# for a public/private key in the key directory (either $EJSON_KEYDIR, if set, or /opt/ejson/keys)
cipherdata = YAML.load_file(file)
secrets = EYAML.decrypt(cipherdata, private_key: ENV[PRIVATE_KEY_ENV_VAR])
.except("_public_key")
secrets = EYAML::Util.with_deep_deundescored_keys(secrets)
.deep_symbolize_keys
.except(:_public_key)

break Rails.application.send(secrets_or_credentials).deep_merge!(secrets)
end
Expand Down
20 changes: 20 additions & 0 deletions lib/eyaml/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@ class << self
def pretty_yaml(some_hash)
some_hash.to_yaml.delete_prefix("---\n")
end

# This will look for any keys that starts with an underscore and duplicates that key-value pair
# but without the starting underscore.
# So {_a: "abab"} will become {_a: "abab", a: "abab"}
# This so we can easilly access our unencrypted secrets without having to add an underscore
def with_deep_deundescored_keys(hash)
hash.each_with_object({}) do |pair, total|
skatkov marked this conversation as resolved.
Show resolved Hide resolved
key, value = pair

value = with_deep_deundescored_keys(value) if value.is_a?(Hash)

if key.start_with?("_")
deunderscored_key = key[1..]

total[deunderscored_key] = value unless total.key?(deunderscored_key)
end

total[key] = value
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/eyaml/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module EYAML
VERSION = "0.4.2"
VERSION = "0.4.3"
end
18 changes: 16 additions & 2 deletions spec/eyaml/encrypted_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
"_public_key" => public_key,
"secret" => "EJ[1:egJgZHLIZfR836f9cOM7g49aPELl7ZgKRz7oDNGLa3s=:1NucdUwyqVGtv7Vj7fH7hfWzg70wUbKn:N5adZhS8xuySyQ2MvY7f027p0VqO3Qeb]",
"s3cr3t" => "p4ssw0rd",
"_secret" => "EJ[1:egJgZHLIZfR836f9cOM7g49aPELl7ZgKRz7oDNGLa3s=:1NucdUwyqVGtv7Vj7fH7hfWzg70wUbKn:N5adZhS8xuySyQ2MvY7f027p0VqO3Qeb]"
"_secret" => "EJ[1:egJgZHLIZfR836f9cOM7g49aPELl7ZgKRz7oDNGLa3s=:1NucdUwyqVGtv7Vj7fH7hfWzg70wUbKn:N5adZhS8xuySyQ2MvY7f027p0VqO3Qeb]",
"deep_nested" => {
"_underscored_secret" => "highly secret"
}
}
}

Expand All @@ -48,6 +51,10 @@
expect(subject.decrypt).to include("_secret" => data["_secret"])
end

it "doesn't skip nested secrets" do
expect(subject.decrypt.dig("deep_nested", "_underscored_secret")).to eq("highly secret")
end

it "accepts a private key with trailing newline" do
manager = EYAML::EncryptionManager.new(data, public_key, "#{private_key}\n")
expect { manager.decrypt }.not_to raise_error
Expand Down Expand Up @@ -75,7 +82,10 @@
"s3cr3t" => "p4ssw0rd",
"_skip_me" => "not_secret",
"_dont_skip_me" => {
"another_secret" => "ssshhh"
"another_secret" => "ssshhh",
"dont_skip_me_too" => {
"_deep_secret" => "ssh[FILTERED]hh"
}
}
}
}
Expand All @@ -96,6 +106,10 @@
expect(subject.encrypt.dig("_dont_skip_me", "another_secret")).to match(/\AEJ\[[\w:\/+=]+\]\z/)
end

it "doesn't skip deep nested underscored secrets" do
expect(subject.encrypt.dig("_dont_skip_me", "dont_skip_me_too", "_deep_secret")).to eq("ssh[FILTERED]hh")
end

it "encrypts values with the EJSON v1 format" do
expect(subject.encrypt["s3cr3t"]).to match(/\AEJ\[1:/)
end
Expand Down
12 changes: 12 additions & 0 deletions spec/eyaml/railtie_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,21 @@
remove_auth_files_that_dont_end_with(".eyaml")
run_load_hooks
expect(credentials).to(include(_extension: "eyaml"))
expect(credentials).to(include(extension: "eyaml"))
end

it "eyml" do
remove_auth_files_that_dont_end_with(".eyml")
run_load_hooks
expect(credentials).to(include(_extension: "eyml"))
expect(credentials).to(include(extension: "eyml"))
end

it "ejson" do
remove_auth_files_that_dont_end_with(".ejson")
run_load_hooks
expect(credentials).to(include(_extension: "ejson"))
expect(credentials).to(include(extension: "ejson"))
end
end

Expand All @@ -99,19 +102,22 @@
remove_auth_files_that_dont_end_with(".eyaml")
run_load_hooks
expect(credentials).to(include(_extension: "eyaml"))
expect(credentials).to(include(extension: "eyaml"))
end

it "eyml" do
remove_auth_files_that_dont_end_with(".eyml")

run_load_hooks
expect(credentials).to(include(_extension: "eyml"))
expect(credentials).to(include(extension: "eyml"))
end

it "ejson" do
remove_auth_files_that_dont_end_with(".ejson")
run_load_hooks
expect(credentials).to(include(_extension: "ejson"))
expect(credentials).to(include(extension: "ejson"))
end
end

Expand Down Expand Up @@ -185,18 +191,21 @@
remove_auth_files_that_dont_end_with(".eyaml")
run_load_hooks
expect(secrets).to(include(_extension: "eyaml"))
expect(secrets).to(include(extension: "eyaml"))
end

it "eyml" do
remove_auth_files_that_dont_end_with(".eyml")
run_load_hooks
expect(secrets).to(include(_extension: "eyml"))
expect(secrets).to(include(extension: "eyml"))
end

it "ejson" do
remove_auth_files_that_dont_end_with(".ejson")
run_load_hooks
expect(secrets).to(include(_extension: "ejson"))
expect(secrets).to(include(extension: "ejson"))
end
end

Expand All @@ -210,19 +219,22 @@
remove_auth_files_that_dont_end_with(".eyaml")
run_load_hooks
expect(secrets).to(include(_extension: "eyaml"))
expect(secrets).to(include(extension: "eyaml"))
end

it "eyml" do
remove_auth_files_that_dont_end_with(".eyml")

run_load_hooks
expect(secrets).to(include(_extension: "eyml"))
expect(secrets).to(include(extension: "eyml"))
end

it "ejson" do
remove_auth_files_that_dont_end_with(".ejson")
run_load_hooks
expect(secrets).to(include(_extension: "ejson"))
expect(secrets).to(include(extension: "ejson"))
end
end

Expand Down
16 changes: 15 additions & 1 deletion spec/eyaml/util_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,21 @@
describe ".pretty_yaml" do
it "will return a hash as YAML without the three dash prefix" do
yaml_without_prefix = File.read(fixtures_root.join("pretty.yml"))
expect(EYAML::Util.pretty_yaml({"a" => "1", "b" => "2"})).to eq(yaml_without_prefix)
expect(EYAML::Util.pretty_yaml({"a"=>"1", "b"=>"2", "_c"=>{"_d"=>"3"}})).to eq(yaml_without_prefix)
end
end

describe ".with_deep_deundescored_keys" do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: I'm wondering what would happen if we define _underscored_secret and underscored_secret in YAML?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously it would raise

Copy link
Contributor

@skatkov skatkov Jul 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems reasonable to raise? Why are we changing it? We are likely to replace one value with another - and if we forgot to remove encrypted or non-encrypted value - would be great to know about it.

Maybe at least log this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed it because else you can never have an encrypted and unencrypted key with the same name, which is something you could want. It's up to the user to decide what value they need.

it "will return a hash with all undescored entries duplicated" do
yaml_without_prefix = YAML.load_file(fixtures_root.join("pretty.yml"))

expect(EYAML::Util.with_deep_deundescored_keys(yaml_without_prefix)).to eq({"a"=>"1", "b"=>"2", "c"=>{"d"=>"3", "_d"=>"3"}, "_c"=>{"d"=>"3", "_d"=>"3"}})
end

it "does not overwrite the not underscored key when we have an underscored key" do
yaml_without_prefix = YAML.load_file(fixtures_root.join("pretty.yml")).merge("_b" => "X")

expect(EYAML::Util.with_deep_deundescored_keys(yaml_without_prefix)).to eq({"a"=>"1", "b"=>"2", "_b"=>"X", "c"=>{"d"=>"3", "_d"=>"3"}, "_c"=>{"d"=>"3", "_d"=>"3"}})
end
end
end
3 changes: 2 additions & 1 deletion spec/fixtures/data.ejson
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"_skip_me": "not_secret",
"_extension": "ejson",
"_dont_skip_me": {
"another_secret": "EJ[1:vr6e0PrO6xzH5N9c6Rs8ERt+DJXZeS0rZPDIZxMJWDg=:B1Iyfp3NBN/Kox9kQXWLV7F8BkNCckTA:MJh0KNGPimGadsUbQUZY21/nZFlFtw==]"
"another_secret": "EJ[1:vr6e0PrO6xzH5N9c6Rs8ERt+DJXZeS0rZPDIZxMJWDg=:B1Iyfp3NBN/Kox9kQXWLV7F8BkNCckTA:MJh0KNGPimGadsUbQUZY21/nZFlFtw==]",
"_underscored_secret": "not encrypted"
}
}
1 change: 1 addition & 0 deletions spec/fixtures/data.eyaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ _skip_me: "not_secret"
_extension: "eyaml"
_dont_skip_me:
another_secret: "ssshhh"
_underscored_secret: "not encrypted"
1 change: 1 addition & 0 deletions spec/fixtures/data.eyml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ _skip_me: "not_secret"
_extension: "eyml"
_dont_skip_me:
another_secret: "ssshhh"
_underscored_secret: "not encrypted"
2 changes: 2 additions & 0 deletions spec/fixtures/pretty.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
a: '1'
b: '2'
_c:
_d: '3'
3 changes: 2 additions & 1 deletion spec/support/encryption_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ module EncryptionHelper
"_skip_me" => "not_secret",
"_extension" => "ejson", # This is only the correct value for data.ejson
"_dont_skip_me" => {
"another_secret" => "ssshhh"
"another_secret" => "ssshhh",
"_underscored_secret" => "not encrypted"
}
}
}
Expand Down