Skip to content

Commit

Permalink
Merge pull request #232 from chef/tm/logging
Browse files Browse the repository at this point in the history
Move to using a logger for all user output
  • Loading branch information
thommay authored Oct 20, 2016
2 parents 02653cb + d39fc98 commit 8b9d419
Show file tree
Hide file tree
Showing 19 changed files with 39 additions and 48 deletions.
5 changes: 3 additions & 2 deletions bin/chef-vault
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,13 @@ require "rubygems"
$:.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
require "chef-vault"

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

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

options[:values].split(",").each do |value|
value.strip! # remove white space
$stdout.puts("\t#{value}: #{item[value]}")
ChefVault::Log.info ("\t#{value}: #{item[value]}")
end
8 changes: 8 additions & 0 deletions lib/chef-vault.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
require "chef-vault/chef_api"
require "chef-vault/chef_key"

require "mixlib/log"

class ChefVault
attr_accessor :vault

Expand All @@ -55,4 +57,10 @@ def certificate(name)
def self.load_config(chef_config_file)
Chef::Config.from_file(chef_config_file)
end

class Log
extend Mixlib::Log
end

Log.level = :error
end
2 changes: 1 addition & 1 deletion lib/chef-vault/certificate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def [](key)
end

def decrypt_contents
$stdout.puts "WARNING: This method is deprecated, please switch to item['value'] calls"
ChefVault::Log.warn "This method is deprecated, please switch to item['value'] calls"
@item["contents"]
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/chef-vault/chef_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def get_admin_key
raise http_error
when "404"
begin
$stdout.puts "WARNING: The default key for #{actor_name} not found in users, trying client keys."
ChefVault::Log.warn "The default key for #{actor_name} not found in users, trying client keys."
get_key("clients")
rescue Net::HTTPServerException => http_error
case http_error.response.code
Expand Down Expand Up @@ -125,8 +125,8 @@ def get_key(request_actor_type)
end

def print_forbidden_error
$stdout.puts <<EOF
ERROR: You received a 403 FORBIDDEN while requesting an #{actor_type} key for #{actor_name}.
ChefVault::Log.error <<EOF
You received a 403 FORBIDDEN while requesting an #{actor_type} key for #{actor_name}.
If you are on Chef Server < 12.5:
Clients do not have access to all public keys within their org.
Expand Down
6 changes: 3 additions & 3 deletions lib/chef-vault/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def clients(search_or_client, action = :add)
client_key = load_public_key(node.name, "clients")
add_client(client_key)
rescue ChefVault::Exceptions::ClientNotFound
$stdout.puts "node '#{node.name}' has no private key; skipping"
ChefVault::Log.warn "node '#{node.name}' has no private key; skipping"
end
when :delete
delete_client_or_node(node)
Expand All @@ -105,7 +105,7 @@ def clients(search_or_client, action = :add)
end

unless results_returned
$stdout.puts "WARNING: No clients were returned from search, you may not have "\
ChefVault::Log.warn "No clients were returned from search, you may not have "\
"got what you expected!!"
end
end
Expand Down Expand Up @@ -416,7 +416,7 @@ def remove_unknown_nodes
end
# now delete any flagged clients from the keys data bag
clients_to_remove.each do |client|
$stdout.puts "Removing unknown client '#{client}'"
ChefVault::Log.warn "Removing unknown client '#{client}'"
keys.delete(load_public_key(client, "clients"))
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/chef-vault/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def [](key)
end

def decrypt_password
$stdout.puts "WARNING: This method is deprecated, please switch to item['value'] calls"
ChefVault::Log.warn "This method is deprecated, please switch to item['value'] calls"
@item["password"]
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/chef/knife/decrypt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Decrypt < VaultDecrypt
banner "knife decrypt VAULT ITEM [VALUES] (options)"

def run
$stdout.puts "DEPRECATION WARNING: knife decrypt is deprecated. Please use knife vault decrypt instead."
Chef.log_deprecation "knife decrypt is deprecated. Please use knife vault decrypt instead."
super
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/chef/knife/encrypt_create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class EncryptCreate < VaultCreate
:description => "File to be added to vault item as file-content"

def run
$stdout.puts "DEPRECATION WARNING: knife encrypt is deprecated. Please use knife vault instead."
Chef.log_deprecation "knife encrypt is deprecated. Please use knife vault instead."
super
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/chef/knife/encrypt_delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class EncryptDelete < VaultDelete
banner "knife encrypt delete VAULT ITEM (options)"

def run
$stdout.puts "DEPRECATION WARNING: knife encrypt is deprecated. Please use knife vault instead."
Chef.log_deprecation "knife encrypt is deprecated. Please use knife vault instead."
super
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/chef/knife/encrypt_remove.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class EncryptRemove < VaultRemove
:description => "Chef users to be added as admins"

def run
$stdout.puts "DEPRECATION WARNING: knife encrypt is deprecated. Please use knife vault instead."
Chef.log_deprecation "knife encrypt is deprecated. Please use knife vault instead."
super
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/chef/knife/encrypt_rotate_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class EncryptRotateKeys < VaultRotateKeys
banner "knife encrypt rotate keys VAULT ITEM (options)"

def run
$stdout.puts "DEPRECATION WARNING: knife encrypt is deprecated. Please use knife vault instead."
Chef.log_deprecation "knife encrypt is deprecated. Please use knife vault instead."
super
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/chef/knife/encrypt_update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class EncryptUpdate < VaultUpdate
banner "knife encrypt update VAULT ITEM VALUES (options)"

def run
$stdout.puts "DEPRECATION WARNING: knife encrypt is deprecated. Please use knife vault instead."
Chef.log_deprecation "knife encrypt is deprecated. Please use knife vault instead."
super
end
end
Expand Down
5 changes: 5 additions & 0 deletions lib/chef/knife/vault_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ def show_usage
exit 1
end

def configure_chef
super
ChefVault::Log.logger = Chef::Log.logger
end

private

def bag_is_vault?(bagname)
Expand Down
2 changes: 1 addition & 1 deletion lib/chef/knife/vault_decrypt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class VaultDecrypt < Knife
banner "knife vault decrypt VAULT ITEM [VALUES] (options)"

def run
$stdout.puts "DEPRECATION WARNING: knife vault decrypt is deprecated. Please use knife vault show instead."
Chef.log_deprecation "knife vault decrypt is deprecated. Please use knife vault show instead."
vault = @name_args[0]
item = @name_args[1]
values = @name_args[2]
Expand Down
2 changes: 1 addition & 1 deletion lib/chef/knife/vault_rotate_all_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def vault_items(vault)
end

def rotate_vault_item_keys(vault, item, clean_unknown_clients)
$stdout.puts "Rotating keys for: #{vault} #{item}"
ui.info "Rotating keys for: #{vault} #{item}"
ChefVault::Item.load(vault, item).rotate_keys!(clean_unknown_clients)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/chef/knife/vault_update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def run
if clean
vault_clients = vault_item.get_clients.clone().sort()
vault_clients.each do |client|
$stdout.puts "Deleting #{client}"
ui.info "Deleting #{client}"
vault_item.delete_client(client)
end
end
Expand Down
11 changes: 2 additions & 9 deletions spec/chef-vault/certificate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@
allow(ChefVault::Item).to receive(:load).with("foo", "bar") { item }
allow(item).to receive(:[]).with("id") { "bar" }
allow(item).to receive(:[]).with("contents") { "baz" }
@orig_stdout = $stdout
$stdout = File.open(File::NULL, "w")
end

after do
$stdout = @orig_stdout
end

describe "#new" do
Expand All @@ -30,9 +24,8 @@

describe "decrypt_contents" do
it "echoes warning" do
expect { cert.decrypt_contents }
.to output("WARNING: This method is deprecated, please switch to item['value'] calls\n")
.to_stdout
expect(ChefVault::Log).to receive(:warn).with("This method is deprecated, please switch to item['value'] calls")
cert.decrypt_contents
end

it "returns items contents" do
Expand Down
13 changes: 2 additions & 11 deletions spec/chef-vault/item_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
require "openssl"

RSpec.describe ChefVault::Item do
before do
@orig_stdout = $stdout
$stdout = File.open(File::NULL, "w")
end

after do
$stdout = @orig_stdout
end

subject(:item) { ChefVault::Item.new("foo", "bar") }

before do
Expand Down Expand Up @@ -247,8 +238,8 @@

it "should emit a warning if search returns a node without a public key" do
# it should however emit a warning that you have a borked node
expect { item.clients("*:*") }
.to output(/node 'bar' has no private key; skipping/).to_stdout
expect(ChefVault::Log).to receive(:warn).with(/node 'bar' has no private key; skipping/)
item.clients("*:*")
end
end

Expand Down
11 changes: 2 additions & 9 deletions spec/chef-vault/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@
allow(ChefVault::Item).to receive(:load).with("foo", "bar") { item }
allow(item).to receive(:[]).with("id") { "bar" }
allow(item).to receive(:[]).with("password") { "baz" }
@orig_stdout = $stdout
$stdout = File.open(File::NULL, "w")
end

after do
$stdout = @orig_stdout
end

describe "#new" do
Expand All @@ -30,9 +24,8 @@

describe "decrypt_password" do
it "echoes warning" do
expect { user.decrypt_password }
.to output("WARNING: This method is deprecated, please switch to item['value'] calls\n")
.to_stdout
expect(ChefVault::Log).to receive(:warn).with("This method is deprecated, please switch to item['value'] calls")
user.decrypt_password
end

it "returns items password" do
Expand Down

0 comments on commit 8b9d419

Please sign in to comment.