0

在一些厨师零运行(将节点状态保存为本地 .json 文件)之后,我很沮丧地在节点文件中找到了这一点: ... "ssmtp": { "auth_username": "secret user name", "auth_password": "even-more-secret password" } 在 Chef Server 上运行相同的操作会将节点数据保存在服务器上。这当然是一个问题,我将不得不更换凭据,修改食谱等。我仍在调查最初是什么原因造成的,但我的问题是:

如何为配方创建 rspec/chefspec 测试以验证特定节点属性是否未永久保存在节点的 .json 文件或 Chef 服务器上?

我想将此添加到我的规格中,以确保它不再发生。

结语

这里的重要教训是,任何进入任何节点属性的方式最终都会保存在节点对象表示中。

4

2 回答 2

2

Chef Server 将保存所有属性,即使它们是正常或默认或其他优先级。您可以通过使用node.run_state来解决这个问题,它可用于“在 chef-client 运行期间存储瞬态数据”。

您可以在elkstack社区食谱中看到这一点,尤其是在_lumberjack_secrets.rb食谱中。

node.run_state正在设置:

if <CONDITION ABBREVIATED>
  node.run_state['lumberjack_decoded_key'] = Base64.decode64(lumberjack_secrets['key'])
  ....
end

node.run_state正在使用:

lumberjack_keypair = node.run_state['lumberjack_decoded_key'] && node.run_state['lumberjack_decoded_certificate']

至于测试,我自己还没有“测试”过,但是您将进行测试以确保未设置属性,如下所示:

it 'should not have secret attributes set' do
  node = chef_run.node
  expect (node['my_secret']).to be_nil
end
于 2015-05-06T20:34:06.880 回答
0

关键在 Chef 文档的注释中,“普通属性是保留在节点对象中的设置。普通属性的属性优先级高于默认属性。” 这将问题分解为测试属性是否为“正常”属性。

深入研究 Chef::Node 和 Chef::Node::Attribute 类代码,我发现您可以调用node.attributes.normal来获取普通属性。所以这个简洁的测试正确地失败了,表明我的秘密信息将持久地存储在节点对象中!

it 'does not have a normal attribute node[ssmtp][auth_password]' do expect(subject.node.attributes.normal['ssmtp'].keys).to_not include 'auth_password' end

导致这个体面的错误消息:

Failure/Error: expect(subject.node.attributes.normal['ssmtp'].keys).to_not include 'auth_password' expected ["auth_username", "auth_password"] not to include "auth_password"

如果未设置特定值,您也可以进行测试,但我认为测试密钥不存在更重要。

不知何故,我总是通过简单地提出一个问题来在这里发布新的见解。就这样。

于 2015-05-06T17:33:22.687 回答