简体   繁体   English

无法使用厨师属性检索加密数据包的数据包项目

[英]Unable to retrieve databag items for a encrypted data bag using chef attributes

I am working on this recipe which assigns api key from an encrypted data bag item. 我正在研究此食谱,该食谱从加密的数据包项目分配api密钥。 To retrieve the data bag item I am using chef attributes in the query. 为了检索数据包项目,我在查询中使用了厨师属性。 This is what I am doing to assign the attributes 这就是我要分配的属性

ruby_block "get_my_region" do
  block do
    node.set['aws']['account_number'] = `curl -s http://169.254.169.254/latest/dynamic/instance-identity/document|grep region|awk -F\\" '{print $4}'`.chop
    node.save
  end
  action :create
end

ruby_block "get_account" do
    block do
      node.set['aws']['region'] = `curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | grep -oP '(?<="accountId" : ")[^"]*(?=")'`.chop
      node.save
    end
    action :create
end

Once the attributes get assigned I am calling a template to using the assigned attribute to the config file 一旦分配了属性,我就调用模板以使用分配给配置文件的属性

template '/etc/default/polymur-proxy' do
  source 'polymur-proxy.erb'
  notifies :restart, 'service[polymur-proxy]', :delayed
  variables(
  api_key: (api_keys["#{node['aws']['region']}"]["#{node['aws']['account_number']}"]["key"]).to_s,
  )
end

The problem is the while debugging I can see the attribute got assigned the correct values however while using them in the query they return empty. 问题是调试时,我可以看到为属性分配了正确的值,但是在查询中使用它们时,它们返回空值。 If any one has any suggestion for this will be helpful 如果有人对此有任何建议,将有所帮助

You are experiencing Chef's two pass execution model, template variables are evaluated before ruby_block resources calls. 您正在体验Chef的两次通过执行模型,在调用ruby_block资源之前先评估模板变量。 You can wrap api_keys with lazy block. 您可以使用惰性块包装api_keys

However, there is no need to manually call curl on AWS metadata endpoint. 但是,无需在AWS元数据终端节点上手动调用curl。 Chef's ohai ec2 plugin is extracting them anyway - node["ec2"]["account_id"] and node["ec2"]["availability_zone"] (just drop az id here wit tr or gsub ). Chef的ohai ec2插件无论如何都提取它们-node node["ec2"]["account_id"]node["ec2"]["availability_zone"] (只需在此处添加tr id或trgsub )。 You can pass this right to the template or even use node directly in your template. 您可以将此权限传递给模板,甚至可以直接在模板中使用node

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM