从Chef中的encyrpted数据包中检索秘密值

时间:2016-06-29 20:08:24

标签: chef chef-recipe chef-solo

我正在使用ruby块来获取秘密值。但是,我想以某种方式更改它,以便不将秘密存储为节点属性。否则,在Chef中查看属性时可以看到它。

ruby_block 'load_acl_master_token' do
  block do
  secret = Chef::EncryptedDataBagItem.load_secret("/etc/chef/app_encrypted_data_bag_secret")
  acl_master_token = Chef::EncryptedDataBagItem.load("app_consul_secrets", "app_consul_acl_mastertoken", secret)
  node.set['cluster']['acl_master_token'] = acl_master_token['keyval']
  end
  only_if { node['cluster']['acl'] == true }
end

bootstrap_json = Chef::JSONCompat.to_json_pretty(node['consul']['config'].to_hash)
rb_bootstrap_hash = JSON.parse(bootstrap_json)

require "active_support/core_ext/hash"

rb_cluster_hash_acl_master_token = rb_bootstrap_hash.deep_merge({ "acl_master_token" => acl_master_token['uuidgen'] } )

如何以更有效的方式编写相同的代码?

代码如下所示

rb_cluster_hash_acl_master_token = rb_bootstrap_hash.deep_merge({ "acl_master_token" => acl_master_token['uuidgen'] } ) 

而且,我收到此错误

NameError
--------- 
No resource, method, or local variable named acl_master_token' for Chef::Recipe "server"' `

我应该acl_master_token['uuidgen']作为lazy attribute。如果是的话,它会是什么样子?

2 个答案:

答案 0 :(得分:0)

只需重写当前使用node属性的代码来代替使用数据包吗?效率并不是一个真正的问题,你只需要做你说过的事情。

答案 1 :(得分:0)

我可以使用lambda懒惰地使用Chef评估任意变量。

rb_cluster_hash_acl_master_token = lambda { rb_cluster_hash_acl_policy.deep_merge({ "acl_master_token" => acl_master_token['uuidgen'] } ) }