迭代 Chef Recipe 中的 EncryptedDataBagItem
Iterating over EncryptedDataBagItem in Chef Recipe
我想解密厨师数据包项目(名为 passwords
)并将其所有属性存储在临时 JSON 文件中,该文件由 node.js 应用程序。有没有办法迭代数据包 ITEM 的属性并获取它们的值?
plain_data = Chef::EncryptedDataBagItem.load("/home/me/data_bags/secrets/passwords.json", secret_key)
由于 EncryptedDataBagItem class 没有 each
方法,是否有任何解决方法?我不想将每个密码存储在单独的 json 文件(数据包项)中。
显然,仅使用 Chef API/DSL 没有简单的方法可以做到这一点。您仍然可以在 Ruby 中执行此操作。好消息是您可以 运行 在 Chef 食谱中任意 Ruby 代码。这是我的做法:
# Load my secret key from a path specified in a Chef attribute
secret_key = Chef::EncryptedDataBagItem.load_secret("#{node[:my_repo_name][:secret_key_file_path]}")
# Use the ruby_block statement to run arbitrary Ruby code in the Chef DSL
ruby_block "decrypt passwords" do
block do
encrypted_path = "/home/me/data_bags/secrets/passwords.json"
encrypted_data = JSON.parse(File.read(encrypted_path))
plain_data = Chef::EncryptedDataBagItem.new(encrypted_data, secret_key).to_hash
File.open('/opt/me/passwords.json', 'w') { |f|
f.write(JSON.pretty_generate(plain_data))
}
end
end
为什么不是这样的:
decrypted_item = data_bag_item('secrets',
'passwords',
node['my_repo_name']['secret_key_file_path'])
file '/opt/me/passwords.json' do
content decrypted_item.to_hash.to_json
mode 600
end
我想解密厨师数据包项目(名为 passwords
)并将其所有属性存储在临时 JSON 文件中,该文件由 node.js 应用程序。有没有办法迭代数据包 ITEM 的属性并获取它们的值?
plain_data = Chef::EncryptedDataBagItem.load("/home/me/data_bags/secrets/passwords.json", secret_key)
由于 EncryptedDataBagItem class 没有 each
方法,是否有任何解决方法?我不想将每个密码存储在单独的 json 文件(数据包项)中。
显然,仅使用 Chef API/DSL 没有简单的方法可以做到这一点。您仍然可以在 Ruby 中执行此操作。好消息是您可以 运行 在 Chef 食谱中任意 Ruby 代码。这是我的做法:
# Load my secret key from a path specified in a Chef attribute
secret_key = Chef::EncryptedDataBagItem.load_secret("#{node[:my_repo_name][:secret_key_file_path]}")
# Use the ruby_block statement to run arbitrary Ruby code in the Chef DSL
ruby_block "decrypt passwords" do
block do
encrypted_path = "/home/me/data_bags/secrets/passwords.json"
encrypted_data = JSON.parse(File.read(encrypted_path))
plain_data = Chef::EncryptedDataBagItem.new(encrypted_data, secret_key).to_hash
File.open('/opt/me/passwords.json', 'w') { |f|
f.write(JSON.pretty_generate(plain_data))
}
end
end
为什么不是这样的:
decrypted_item = data_bag_item('secrets',
'passwords',
node['my_repo_name']['secret_key_file_path'])
file '/opt/me/passwords.json' do
content decrypted_item.to_hash.to_json
mode 600
end