Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 38 ยท Projects and Practice

Project: A Security Baseline

Design a Puppet security baseline: SSH hardening, password policy, firewall and sudo rules, encrypted secrets with eyaml, compliance data from reports and a drift check.

Security is a perfect job for configuration management. A baseline is a list of settings every server must have, which is exactly what Puppet enforces and re-enforces every 30 minutes. If someone weakens sshd_config by hand, the next run puts it back, and the report records the corrective change, which tells you someone is working around the process.

In this project you design a baseline profile, handle secrets properly and add compliance checks. It is a design exercise that you implement on your own test machines (never apply untested firewall or SSH changes to a machine you cannot reach another way). The runnable blocks are JavaScript stand-ins for checkers; they do not touch any system.

The baseline

A small, defensible baseline:

Area Setting
SSH No root login, no password authentication, protocol-safe ciphers, idle timeout
Users Named admin accounts only; sudo through a managed file; no shared accounts
Passwords Minimum length and aging via PAM or login.defs (when passwords are used at all)
Network Host firewall with default deny and explicit allows
Services Only needed services enabled; unneeded ones stopped and disabled
Files Correct permissions on sensitive files (/etc/shadow, /etc/ssh)
Updates Automatic security updates or a clear patch schedule
Logging Audit and syslog forwarding to a central host

Write each area as a section in a profile, with parameters that have secure defaults, so a new server is safe even before you tune it.

The sshd configuration

PUPPET
class profile::security::ssh (
  Boolean       $permit_root_login   = false,
  Boolean       $password_auth       = false,
  Integer       $client_alive_interval = 300,
  Array[String] $allow_users         = [],
) {
  file { '/etc/ssh/sshd_config':
    ensure       => file,
    owner        => 'root',
    group        => 'root',
    mode         => '0600',
    content      => epp('profile/sshd_config.epp', {
      'permit_root_login'     => $permit_root_login,
      'password_auth'         => $password_auth,
      'client_alive_interval' => $client_alive_interval,
      'allow_users'           => $allow_users,
    }),
    validate_cmd => '/usr/sbin/sshd -t -f %',
    notify       => Service['sshd'],
  }

  service { 'sshd':
    ensure => running,
    enable => true,
  }
}

The most important line is validate_cmd. Puppet writes the candidate file to a temporary path and runs the validation command on it; if it fails, the real file is not replaced. A typo cannot lock you out of every server.

Always keep a second way in (console access, a second admin account) when you change SSH or firewall rules, and test on one machine first. A wrong baseline pushed to a whole fleet is an outage.

Firewall: default deny

Using the puppetlabs-firewall module (or nftables), order matters: accept established traffic and SSH before the final drop rule, and purge unmanaged rules so manual additions disappear.

PUPPET
firewall { '000 accept established':
  proto => 'all',
  state => ['RELATED', 'ESTABLISHED'],
  jump  => 'accept',
}
firewall { '010 accept ssh':
  proto => 'tcp',
  dport => 22,
  jump  => 'accept',
}
firewall { '999 drop everything else':
  proto => 'all',
  jump  => 'drop',
}

The numbers in the titles define the rule order; the module sorts by title.

Secrets: never in plain YAML

Passwords, API tokens and private keys must not sit unencrypted in Git. Options:

  • hiera-eyaml: Hiera values encrypted with a public key, shown as ENC[PKCS7,...] in the file. The Puppet server holds the private key and decrypts at compile time. Encrypting needs only the public key, so developers can add secrets without being able to read the others.
  • A secrets manager (Vault, a cloud secret store) queried with a Hiera backend or a function such as Deferred values.
  • Sensitive type: wrap secrets in Sensitive so they are redacted in logs and reports.
YAML
# data/common.yaml
profile::app::db_password: >
  ENC[PKCS7,MIIBiQYJKoZIhvcNAQcDoIIBejCCAXYCAQAxggEhMIIBHQIBAD...]
PUPPET
class profile::app (Sensitive[String] $db_password) {
  file { '/etc/app/db.conf':
    content => Sensitive("password=${db_password.unwrap}\n"),
    mode    => '0600',
    owner   => 'app',
  }
}

With Sensitive, Puppet prints [redacted] in the logs and reports instead of the value. Also remember that file contents and facts show up in PuppetDB; keep secrets out of custom facts.

Compliance and drift

Because the baseline is code, two kinds of evidence are available:

  1. Policy as code: the profile itself documents the required state and can be unit-tested (RSpec-puppet) to guarantee, say, that PermitRootLogin can never render as yes with default parameters.
  2. Runtime evidence: run in noop mode on a schedule. Every "would change" line is drift. Reports in PuppetDB list nodes with corrective changes, with the exact resource.
Terminal
puppet agent --test --noop       # drift check on one node
puppet query 'events[certname, resource_title, property] { corrective_change = true and latest_report? = true }'
flowchart LR
  B["Baseline profile"] --> N["Nodes"]
  N -->|report| D[("PuppetDB")]
  D --> C["Corrective-change query"]
  C --> A["Alert / ticket"]
  M["Manual change"] -.-> N

A manual change is corrected on the next run, and the correction is visible in the reports.

Stand-ins

The first stand-in is a baseline checker for an sshd_config text, the second a drift report built from reports. They only analyze text and data in the browser.

JS Stand-in: check an sshd_config against the baseline

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Stand-in: drift report from run results

Press Run (or Ctrl + Enter) to see the console output here.

No output.

If sshd_config is corrected on many nodes, someone (or some tool) keeps editing it: a training problem, a broken update script, or a baseline that is too strict. The report tells you where to look.

Rollout checklist

  1. Write the profile with secure defaults and unit tests.
  2. Apply to one lab VM with console access; confirm SSH still works from a second session.
  3. Run a noop on staging; read every pending change.
  4. Roll to production in small batches (for example with a Bolt plan), checking SSH health after each batch.
  5. Schedule the drift report and review it weekly.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does validate_cmd on a file resource protect against?

    Puppet validates the candidate file first; if the command fails, the existing file stays.
  2. 2. Why encrypt Hiera secrets with eyaml (or use a secrets manager)?

    Secrets in plain text spread through repositories, backups and logs.
  3. 3. What does the Sensitive data type do?

    Sensitive values are shown as redacted in output; the real value is still used by the resource.
  4. 4. Many nodes show corrective changes on sshd_config. What does it suggest?

    Corrective changes mean the state drifted and Puppet had to repair it.

Summary

  • A baseline is a profile with secure defaults, covering SSH, users, firewall, services, files, updates and logging.
  • Use validate_cmd so a bad config cannot replace a good one, and keep a second way into every machine.
  • Keep secrets encrypted (eyaml or a secrets manager) and typed as Sensitive.
  • Drift shows up as corrective changes in reports; schedule noop checks and review them.
  • Roll out gradually, starting with a lab machine.

Next: Practice Exercises and Cheat Sheet