Hello, i have new RHEL9 server with Puppet 7 (open...
# puppet
o
Hello, i have new RHEL9 server with Puppet 7 (open source) and PuppetDB installed on it. I want to configure Hiera to work with AWS Secrets Manager (as backend so to speak). I started using module https://forge.puppet.com/modules/accenture/hiera_aws_sm/readme and during PoC it worked ok. Main aspect - during PoC i was setting AWS Credentials manually. But now to go to production i need to encrypt those credentials to not store them in git in plain text. Here is example of my hiera.yaml file:
Copy code
---
version: 5
defaults:
  datadir: hieradata
  data_hash: yaml_data

hierarchy:
  - name: "Per-node data"
    path: "node/%{trusted.certname}.yaml"
  - name: "OS major version-based data"
    path: "os/%{facts.os.family}/version/%{facts.os.release.major}.yaml"
  - name: "OS family-based data"
    path: "os/%{facts.os.family}.yaml"

  - name: "[ENCRYPTED] AWS Secrets Manager lookup - Dev"
    lookup_key: hiera_aws_sm
    options:
      continue_if_not_found: false
      aws_access_key: "%{lookup('hiera_aws_sm::dev::aws_access_key')}"
      aws_secret_key: "%{lookup('hiera_aws_sm::dev::aws_secret_key')}"
      region: us-east-1
      delimiter: /
      prefixes:
        - puppet/dev/common/
      confine_to_keys:
        - '^dev_.*'

  - name: "[ENCRYPTED] AWS Secrets Manager lookup - Prod"
    lookup_key: hiera_aws_sm
    options:
      continue_if_not_found: false
      aws_access_key: "%{lookup('hiera_aws_sm::prod::aws_access_key')}"
      aws_secret_key: "%{lookup('hiera_aws_sm::prod::aws_secret_key')}"
      region: us-east-1
      delimiter: /
      prefixes:
        - puppet/prod/common/
      confine_to_keys:
        - '^prod_.*'

  - name: "[ENCRYPTED] Global default data"
    path: "defaults.eyaml"
    lookup_key: eyaml_lookup_key
    options:
      pkcs7_private_key: /etc/puppetlabs/puppet/eyaml/private_key.pkcs7.pem
      pkcs7_public_key:  /etc/puppetlabs/puppet/eyaml/public_key.pkcs7.pem

  - name: "Global default data"
    path: "defaults.yaml"
What i did - i generated eyaml keys, encrypted aws secrets via eyaml and added encrypted values into "defaults.eyaml" and then in this file hiera.yaml "reference" those secrets. But when i try to run "puppet agent -t" i get an error:
Copy code
# puppet agent -tv --noop
Info: Using environment 'main'
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Notice: Requesting catalog from puppet.server.fqdn:8140 (X.X.X.X)
Notice: Catalog compiled by puppet.server.fqdn
Error: Could not retrieve catalog from remote server: Error 500 on SERVER: Server Error: Interpolation using method syntax is not allowed in this context (file: /etc/puppetlabs/code/environments/main/hiera.yaml) on node puppet.server.fqdn
Warning: Not using cache on failed catalog
Error: Could not retrieve catalog; skipping run
My goals: 1/ Store in git encrypted AWS credentials 2/ Populate/fill-out/put correct credentials into /etc/puppetlabs/code/environments/My_Environment/hiera.yaml Could you please help my to understand how to fix contents of hiera.yaml file to make Puppet happy? 🙂
c
The
lookup()
function cannot be used in
hiera.yaml
.
lookup()
returns values by traversing the entries defined in the
hierarchy
section of
hiera.yaml
. So, allowing the use of
lookup()
within the
hierarchy
section would create a circular dependency.
o
😭
Is there any way potentially how to achieve this goal in maybe another ways? I wonder how module original writer did it. Most likely they didn't commited aws credentials in git in plaintext
c
From a quick skim of the module
README
, it looks like plaintext, directly in the
hiera.yaml
is the only option the module authors provided. I don't see a method for reading from Environment variables, or other sources. Is the Puppet Server running in AWS?
o
Is the Puppet Server running in AWS?
No, in DC on-prem
c
Aha. So, the suggestion to use an IAM role assigned to the EC2 instance won't be available.
Is this going to be a single Puppet Server instance, or multiple that need to be deployed? If it is just a small number and the AWS credential doesn't rotate that often, then just manually entering the values into the file when deploying the Puppet Server should be fine. I don't recommend setting
hiera.yaml
content with
puppet
as a break in that management tends to break catalog compilation for every node and you have to go in and manually fix it. Circular dependencies may work fine, but when they go bad, they go really bad.
o
Right now it will be single Puppet server. I tried to look into it behind the scenes (source code). Module expect to pass in options/plaintext 3 inputs ( https://github.com/Accenture/hiera-aws-sm/blob/master/lib/puppet/functions/hiera_aws_sm.rb#L96-L102 ) In code for "aws-sdk-secretsmanager" i found out that if credentils not provided it will search in a few other places auto-magically https://github.com/aws/aws-sdk-ruby/blob/version-3/gems/aws-sdk-secretsmanager/lib/aws-sdk-secretsmanager/client.rb#L127-L139 So i will try to comment out credentials and somehow pass inside puppetserver ENV variables and hope that module will pick up them
c
Aha. If the module is using a Ruby gem that has other logic for searching around in the environment, then that is definitely something to try.
o
I have a side question: how to pass/export ENV variables so that they will be visible inside Puppet? What i mean: i did "googling" and found out that if i comment out
aws_access_key
and
aws_secret_key
in "hiera_aws_sm" so Ruby will try to use default workflow: https://github.com/aws/aws-sdk-ruby/blob/version-3/gems/aws-sdk-secretsmanager/lib/aws-sdk-secretsmanager/client.rb#L127-L144
Copy code
#     When `:credentials` are not configured directly, the following locations will be searched for credentials:
    #
    #     * `Aws.config[:credentials]`
    #
    #     * The `:access_key_id`, `:secret_access_key`, `:session_token`, and
    #       `:account_id` options.
    #
    #     * `ENV['AWS_ACCESS_KEY_ID']`, `ENV['AWS_SECRET_ACCESS_KEY']`,
    #       `ENV['AWS_SESSION_TOKEN']`, and `ENV['AWS_ACCOUNT_ID']`.
    #
    #     * `~/.aws/credentials`
    #
    #     * `~/.aws/config`
So i was wondering how can i pass/set/expose ENV variables "AWS_ACCESS_KEY_ID" and "AWS_SECRET_ACCESS_KEY" inside Puppet? I did some initial googling and did some testing to add env variable in /etc/sysconfig/puppetserver AWS_ACCESS_KEY_ID=111 AWS_SECRET_ACCESS_KEY=11111 (i'm not sure if it is supported to add ENV variable in this file in the forst place) And tried to see if it will work, but it didn't worked (i checked by doing daemon reload / reexec and restating puppetserver systemd service and tried yo tun puppet agent -t to see if hiera backend will do lookup of secrets in AW Secrets Manager but it returned error:
Copy code
Error: Could not retrieve catalog from remote server: Error 500 on SERVER: Server Error: Evaluation Error: Error while evaluating a Function Call, unable to sign request without credentials set (file: /etc/puppetlabs/code/environments/main/manifests/site.pp, line: 3, column: 18) on node XXX
(basically saying that https://forge.puppet.com/modules/accenture/hiera_aws_sm didn't get proper aws credentials to run) Also i did like this:
/opt/puppetlabs/bin/puppetserver ruby -e 'puts ENV["AWS_ACCESS_KEY_ID"]'
but it returned empty result. From what i understood it means that Puppetserver (or its JRuby?) does not see those ENV varialbes
y
Use systemd override to inject the env vars
👀 1
thisdead 1
c
Environment variables can also be set in the
puppetserver
files under
/etc/default
or
/etc/sysconfig
(Debian vs. RedHat). SystemD config has the advantage that it only needs to be readable by
init
.
o
> Environment variables can also be set in the
puppetserver
files under
/etc/default
or
/etc/sysconfig
(Debian vs. RedHat). Thanks. i found out about it too late 🙂 In the end i forked repo https://github.com/Accenture/hiera-aws-sm and with help of AI/ChatGPT was able to adjust Ruby code in a way what works with AWS profiles and now i no longer need to hardcode secrets in hiera.yaml
Copy code
- name: "[ENCRYPTED] AWS Secrets Manager lookup - Stage"
    lookup_key: hiera_aws_sm
    options:
      continue_if_not_found: false
      aws_profile: stage
      region: us-east-1
      delimiter: /
      prefixes:
        - puppet/stage/common/
      confine_to_keys:
        - '^stage_.*'
  - name: "[ENCRYPTED] AWS Secrets Manager lookup - Prod"
    lookup_key: hiera_aws_sm
    options:
      continue_if_not_found: false
      aws_profile: prod
      region: us-east-1
      delimiter: /
      prefixes:
        - puppet/prod/common/
      confine_to_keys:
        - '^prod_.*'
(and in the process i found out that i can pass env variables via /etc/sysconfig/puppetserver to pass properly puppet systemd user home folder ) PS. Why i initially started working on ENV vars? Because colleague with more senior title said "it should work" and insisted on it. Turned out it was not right approach from the start. Learning experience for me to say "*No*" sometimes to other peoples opinions Thank you everyone for assistance 👍