【问题标题】:How to create .htpasswd file for nginx using chef如何使用 Chef 为 nginx 创建 .htpasswd 文件
【发布时间】:2019-04-16 02:36:43
【问题描述】:

我是新来的厨师,我正在尝试生成一个 .htpasswd 文件来存储用户哈希,我经历了一些 links 但这无济于事。 我需要 ngnix 来保护 Kibana,并且我想为使用 openssl 的用户生成密码。我创建了一个模板文件为 .htpasswd_temp.erb 它看起来像这样:

<% @kibana_user.each do |user| %>
<%= user %>: 

<% end %>

我的食谱是:

kibana_configs = node['kibana']['kibana_auth']
template 'path/to/.htpasswd' do 
source '.htpasswd_temp.erb '
variables(
  kibana_user: kibana_configs['kiba_user']
)
end

我创建了一个角色文件,其中定义了所有默认属性(包括 kiba_user)。 上面的代码将用户添加到 .htpasswd 文件,但我不知道如何使用 openssl 生成密码。 Openssl 命令使用执行资源可以正常工作,但执行资源在模板资源中不起作用,因此不会反映在 .htpasswd 文件中。我真的很困惑。非常感谢您的帮助^^

【问题讨论】:

    标签: nginx openssl chef-infra kibana .htpasswd


    【解决方案1】:

    以下食谱 sn-p 可能对您很方便...

    假设您熟悉encrypted data bag,并且您已将凭据存储在名为creds 的数据包中,其中包含名为nginx 的加密项目,其中包含usernamepassword 密钥。

    htpassed_file = '/root/.htpasswd'
    
    chef_gem 'htauth'
    
    ruby_block 'create .htpasswd' do
      block do
        require 'htauth'
        creds = data_bag_item('creds', 'nginx')
        HTAuth::PasswdFile.open(htpassed_file, HTAuth::File::CREATE) do |pf|
          pf.add(creds['username'], creds['password'])
        end
        FileUtils.chmod 0o600, htpassed_file
      end
    end
    

    【讨论】:

    • @sayali.k:我的荣幸 :)
    • @user2066657:这是为参考而给出的。继续进行修改以使其具有幂等性。
    • 那么,不是吗?好的,那么它需要保护语句才能完成。谢谢!
    【解决方案2】:

    您也可以使用 https://github.com/redguide/htpasswd 和数据包组合来加密您的密码(以及用户名)。

    htpasswd "/etc/nginx/htpassword" do
      user "foo"
      password "bar"
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-30
      相关资源
      最近更新 更多