从 puppet 中的文件访问密码以在 exec 中使用

Accessing password from a file in puppet for use in exec

我想 运行 开源 puppet 中的命令来激活 Unity3D 许可证,但我不想要我的 git 存储库中的序列号或密码:

exec { 'license-unity': 
  command => '/opt/Unity/Editor/Unity -batchmode -nographics -serial A1-ABCD-1234-ETC -username my.user@a.b.ca -password myPassword -quit'
  subscribe   => Package['UnityEditor'],
  refreshonly => true,
}

如何从文件(在 puppetserver 或节点上)读取序列号和密码并在命令中替换它?

例如,如果我在 puppet 服务器上有一个名为 .secret 的文件,由 root 和 perms 400 拥有。如何将内容读入变量以在 puppet 清单中使用?

根据您喜欢的路线,有两种标准方法可以实现此目的:

  1. 使用file函数。这适用于无主 Puppet 或文件托管在 Puppet Master 上。

    # using the module path instead of the absolute path would end up storing your secret in git, which is what you are trying to avoid
    $password = file('/absolute/path/to/.secret')
    
    exec { 'license-unity': 
      command     => "/opt/Unity/Editor/Unity -batchmode -nographics -serial A1-ABCD-1234-ETC -username my.user@a.b.ca -password $password -quit"
      subscribe   => Package['UnityEditor'],
      refreshonly => true,
    }
    

    文档:https://puppet.com/docs/puppet/5.3/function.html#file

    推论:如果您需要对文件进行某种解析,例如它不仅仅是一个包含密码的文本文件,那么您可以使用现代 Ruby 的自定义函数API。 https://puppet.com/docs/puppet/5.3/functions_ruby_overview.html。如果是这种情况,请告诉我。

  2. 使用自定义事实。这是为了在 Master/Client 设置中将文件存储在客户端上。使用外部事实也会最终将秘密存储在 git 中,这会出现您试图避免的问题。

    # module/lib/facter/password.rb
    Facter.add(:password) do
      setcode do
        File.read('/absolute/path/to/.secret')
      end
    end
    
    # manifest.pp
    exec { 'license-unity': 
      command     => "/opt/Unity/Editor/Unity -batchmode -nographics -serial A1-ABCD-1234-ETC -username my.user@a.b.ca -password $password -quit"
      subscribe   => Package['UnityEditor'],
      refreshonly => true,
    }
    

    文档:https://puppet.com/docs/facter/3.9/custom_facts.html#configuring-facts

    推论:如果你需要对文件进行某种解析,比如如果它不仅仅是一个包含密码的文本文件,那么你可以使用原生 Ruby 类和方法(即 JSON.parseYAML.load_file 如果文件是这些格式)。

您正在采用的方法的显着替代方法包括使用 Puppet 从机密管理软件(例如 Vault)中检索,或使用 encryption/decryption 算法(例如 AES-256)将加密文件存储在您的SCM然后目录编译时解密