Skip to content

Commit

Permalink
Fix for chef-vault command was not returning any results (#383)
Browse files Browse the repository at this point in the history
* Fix for chef-vault command was not returning any results
* fixed chefstyle failure

Signed-off-by: snehaldwivedi <[email protected]>
  • Loading branch information
snehaldwivedi authored Nov 26, 2021
1 parent 65a4715 commit 6b8c706
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion bin/chef-vault
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ require "chef-vault"

ChefVault::Log.init(STDOUT)
ChefVault.load_config(options[:chef])
item = ChefVault::Item.load(options[:vault], options[:item])
item = ChefVault::Item.load(options[:vault], options[:item], options)

ChefVault::Log.info "#{options[:vault]}/#{options[:item]}"

Expand Down
9 changes: 8 additions & 1 deletion lib/chef-vault/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,17 @@ def self.load(vault, name, opts = {})
raise ChefVault::Exceptions::ItemNotFound,
"#{vault}/#{name} could not be found"
end

format_output(opts[:values], item) if opts[:values]
item
end

def self.format_output(values, item)
values.split(",").each do |value|
value.strip!
$stdout.puts("#{value}: #{item[value]}")
end
end

def delete_client(client_name)
client_key = load_actor(client_name, "clients")
keys.delete(client_key)
Expand Down
12 changes: 12 additions & 0 deletions spec/chef-vault/item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

RSpec.describe ChefVault::Item do
subject(:item) { ChefVault::Item.new("foo", "bar") }
let(:options) { { chef: "bar", values: "foo" } }

before do
item["foo"] = "bar"
Expand Down Expand Up @@ -47,9 +48,16 @@
@vaultdbki = Chef::DataBagItem.new
@vaultdbki.data_bag("vault")
@vaultdbki.raw_data = { "id" => "foo_keys" }

allow(@vaultdb).to receive(:load).with("foo_keys").and_return(@vaultdbki)
allow(Chef::DataBag).to receive(:load).with("vault").and_return(@vaultdb)
allow(Chef::DataBagItem).to receive(:load).with("vault", "foo").and_return(@vaultdbi)
@orig_stdout = $stdout
$stdout = File.open(File::NULL, "w")
end

after do
$stdout = @orig_stdout
end

describe "::vault?" do
Expand All @@ -75,6 +83,10 @@
it "should detect a normal data bag item" do
expect(ChefVault::Item.data_bag_item_type("normal", "foo")).to eq(:normal)
end

it "outputs the loaded item credentials on the stdout" do
expect { ChefVault::Item.format_output(options[:values], @dbi) }.to output("foo: bar\n").to_stdout
end
end
end

Expand Down

0 comments on commit 6b8c706

Please sign in to comment.