我在我的一个资源中有这个:
service 'my_service' do
...
end
template config_file do
source new_resource.template_file
variables({
config: node[:a][:b][:c][:configs][new_resource.name]
})
notifies :restart, "service[my_service]"
end
当我的某个模板包含诸如bcrypt密码之类的内容时,会出现问题:
...
passhash = <%=
require 'bcrypt'
::BCrypt::Password::create(@config[:password])
%>
这将导致模板资源更新,从而在每次主厨运行时重新启动我的服务。
您是否知道如何防止在每次运行时生成不同的哈希?
在配方外部生成哈希并将其保存在普通属性中似乎很脏......
答案 0 :(得分:1)
在用code的BCrypt模块挖掘一下并使用来自node['hostname']
的盐来为每台机器提供不同的盐,同时仍然在每次运行时给出连贯的盐,这应该有效:
passhash = <%=
require 'bcrypt'
salt="$2a$05$Whatever_you_want#{node['hostname'].hash.to_s}"
::BCrypt::Engine.hash_secret(@config[:password],salt)
%>
irb中的虚拟数据示例:
require 'bcrypt'
irb(main):067:0> salt="$2a$05$abcdefghijklmnopqrstuvwxyz"
=> "$2a$05$abcdefghijklmnopqrstuvwxyz"
irb(main):068:0> BCrypt::Engine.hash_secret("toto",salt)
=> "$2a$05$abcdefghijklmnopqrstuuysiG0IbQUOykz7YKwKH2q3gpjfBWJZG"
irb(main):069:0> BCrypt::Engine.hash_secret("toto",salt)
=> "$2a$05$abcdefghijklmnopqrstuuysiG0IbQUOykz7YKwKH2q3gpjfBWJZG"
请注意,盐将被截断,最后一个$之后的部分必须超过22个字符。 Validation code is here
salt的开头是/etc/shadow
中的条目,crypt方法(对于bcrypt是2a
),第二部分是根据wikipedia进行键控的次数,下一步盐本身后面跟密码哈希。
答案 1 :(得分:0)
似乎bcrypt在主厨12上消失了。
opt/chef/embedded/bin/irb
irb(main):001:0> require 'bcrypt'
LoadError: cannot load such file -- bcrypt
from /opt/chef/embedded/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /opt/chef/embedded/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from (irb):1
from /opt/chef/embedded/bin/irb:11:in `<main>'
ls lib/ruby/gems/2.3.0/gems/
addressable-2.4.0 hashie-3.5.3 net-telnet-0.1.1 rspec-its-1.2.0
appbundler-0.10.0 highline-1.7.8 nokogiri-1.7.0.1 rspec_junit_formatter-0.2.3
ast-2.3.0 iniparse-1.4.2 ohai-8.23.0 rspec-mocks-3.5.0
binding_of_caller-0.7.2 ipaddress-0.8.3 parser-2.4.0.0 rspec-support-3.5.0
builder-3.2.3 json-2.0.3 plist-3.2.0 rubocop-0.47.1
bundler-1.12.5 libyajl2-1.2.0 power_assert-0.2.6 rubygems-update-2.6.10
byebug-9.0.6 method_source-0.8.2 powerpack-0.1.1 ruby-prof-0.16.2
chef-12.19.36 mini_portile2-2.1.0 proxifier-1.0.3 ruby-progressbar-1.8.1
chef-config-12.19.36 minitest-5.8.3 pry-0.10.4 ruby-shadow-2.5.0
cheffish-4.1.1 mixlib-archive-0.4.1 pry-byebug-3.4.2 serverspec-2.38.0
chefstyle-0.5.0 mixlib-authentication-1.4.1 pry-remote-0.1.8 sfl-2.3
chef-zero-5.3.0 mixlib-cli-1.7.0 pry-stack_explorer-0.4.9.2 simplecov-0.13.0
coderay-1.1.1 mixlib-config-2.2.4 rack-2.0.1 simplecov-html-0.10.0
debug_inspector-0.0.2 mixlib-log-1.7.1 rainbow-2.2.1 slop-3.6.0
did_you_mean-1.0.0 mixlib-shellout-2.2.7 rake-10.4.2 specinfra-2.66.9
diff-lcs-1.3 multi_json-1.12.1 rake-11.3.0 syslog-logger-1.6.8
docile-1.1.5 net-scp-1.2.1 rb-readline-0.5.4 systemu-2.6.5
erubis-2.7.0 net-sftp-2.1.2 rdoc-4.2.1 test-unit-3.1.5
ffi-1.9.17 net-ssh-4.0.1 rspec-3.5.0 unicode-display_width-1.1.3
ffi-yajl-2.3.0 net-ssh-gateway-1.3.0 rspec-core-3.5.4 uuidtools-2.1.5
fuzzyurl-0.9.0 net-ssh-multi-1.2.1 rspec-expectations-3.5.0 wmi-lite-1.0.0