Skip to content

Commit

Permalink
Roll back knife[:secret] and knife[:secret_file] stuff - this does no…
Browse files Browse the repository at this point in the history
…t match behavior in mainline, and it'd be nice to create files (in the future) without encryption/decryption being the default behavior.

Also, add in "knife file create."  Initial whack at getting an interactive file creation thing going.
  • Loading branch information
cparedes committed Sep 12, 2011
1 parent 22f70e3 commit 2704f14
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 20 deletions.
2 changes: 1 addition & 1 deletion knife-file.gemspec
Expand Up @@ -17,6 +17,6 @@ Gem::Specification.new do |s|
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.add_dependency "chef", ">= 0.9.14"
s.add_dependency "chef", ">= 0.10.0"
s.require_paths = ['lib']
end
74 changes: 74 additions & 0 deletions lib/chef/knife/file_create.rb
@@ -0,0 +1,74 @@
#
# Author:: Christian Paredes <cp@redbluemagenta.com>
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

class Chef
class Knife
class FileCreate < Knife

deps do
require 'chef/encrypted_data_bag_item'
require 'chef/data_bag'
end

banner "knife file create ITEM [options]"

option :secret,
:short => "-s SECRET",
:long => "--secret ",
:description => "The secret key to use to encrypt data bag item values"

option :secret_file,
:long => "--secret-file SECRET_FILE",
:description => "A file containing the secret key to use to encrypt data bag item values"

def read_secret
if config[:secret]
config[:secret]
else
Chef::EncryptedDataBagItem.load_secret(config[:secret_file])
end
end

def use_encryption
if config[:secret] && config[:secret_file]
stdout.puts "please specify only one of --secret, --secret-file"
exit(1)
end
config[:secret] || config[:secret_file]
end

def run
@item_name = @name_args
if @item_name.nil?
stdout.puts opt_parser
exit(1)
end

create_object({ "id" => @item_name }, "encrypted_file[#{@item_name}.json]") do |output_user|
item = Chef::DataBagItem.from_hash(
if use_encryption
Chef::EncryptedDataBagItem.encrypt_data_bag_item(output_user, secret)
else
output_user
end)
item.data_bag(@item_name)
output(format_for_display(item.to_hash))
end
end
end
end
end
16 changes: 7 additions & 9 deletions lib/chef/knife/file_decrypt.rb
Expand Up @@ -30,19 +30,17 @@ class FileDecrypt < Knife
option :secret,
:short => "-s SECRET",
:long => "--secret ",
:description => "The secret key to use to decrypt data bag item values",
:proc => Proc.new { |key| Chef::Config[:knife][:secret] = key }
:description => "The secret key to use to decrypt data bag item values"

option :secret_file,
:long => "--secret-file SECRET_FILE",
:description => "A file containing the secret key to use to decrypt data bag item values",
:proc => Proc.new { |key| Chef::Config[:knife][:secret_file] = key }
:description => "A file containing the secret key to use to decrypt data bag item values"

def read_secret
if Chef::Config[:knife][:secret]
Chef::Config[:knife][:secret]
if config[:secret]
config[:secret]
else
Chef::EncryptedDataBagItem.load_secret(Chef::Config[:knife][:secret_file])
Chef::EncryptedDataBagItem.load_secret(config[:secret_file])
end
end

Expand All @@ -58,11 +56,11 @@ def decrypt(plain_hash, secret)
end

def use_encryption
if Chef::Config[:knife][:secret] && Chef::Config[:knife][:secret_file]
if config[:secret] && config[:secret_file]
stdout.puts "please specify only one of --secret, --secret-file"
exit(1)
end
Chef::Config[:knife][:secret] || Chef::Config[:knife][:secret_file]
config[:secret] || config[:secret_file]
end

def loader
Expand Down
16 changes: 7 additions & 9 deletions lib/chef/knife/file_encrypt.rb
Expand Up @@ -30,28 +30,26 @@ class FileEncrypt < Knife
option :secret,
:short => "-s SECRET",
:long => "--secret ",
:description => "The secret key to use to encrypt data bag item values",
:proc => Proc.new { |key| Chef::Config[:knife][:secret] = key }
:description => "The secret key to use to encrypt data bag item values"

option :secret_file,
:long => "--secret-file SECRET_FILE",
:description => "A file containing the secret key to use to encrypt data bag item values",
:proc => Proc.new { |key| Chef::Config[:knife][:secret_file] = key }
:description => "A file containing the secret key to use to encrypt data bag item values"

def read_secret
if Chef::Config[:knife][:secret]
Chef::Config[:knife][:secret]
if config[:secret]
config[:secret]
else
Chef::EncryptedDataBagItem.load_secret(Chef::Config[:knife][:secret_file])
Chef::EncryptedDataBagItem.load_secret(config[:secret_file])
end
end

def use_encryption
if Chef::Config[:knife][:secret] && Chef::Config[:knife][:secret_file]
if config[:secret] && config[:secret_file]
stdout.puts "please specify only one of --secret, --secret-file"
exit(1)
end
Chef::Config[:knife][:secret] || Chef::Config[:knife][:secret_file]
config[:secret] || config[:secret_file]
end

def loader
Expand Down
2 changes: 1 addition & 1 deletion lib/knife-file/version.rb
@@ -1,6 +1,6 @@
module Knife
module File
VERSION = "0.1.5"
VERSION = "0.1.6"
MAJOR, MINOR, TINY = VERSION.split('.')
end
end

0 comments on commit 2704f14

Please sign in to comment.