From 287874763d7328d638ea4af24056263518b7de84 Mon Sep 17 00:00:00 2001 From: Rob Crittenden Date: Wed, 21 Aug 2013 23:00:04 -0400 Subject: [PATCH] Initial Release --- CHANGELOG | 2 + LICENSE | 13 +++++ README.md | 4 ++ manifests/add_cert_and_key.pp | 57 ++++++++++++++++++++++ manifests/create.pp | 92 +++++++++++++++++++++++++++++++++++ tests/create.pp | 27 ++++++++++ 6 files changed, 195 insertions(+) create mode 100644 CHANGELOG create mode 100644 LICENSE create mode 100644 README.md create mode 100644 manifests/add_cert_and_key.pp create mode 100644 manifests/create.pp create mode 100644 tests/create.pp diff --git a/CHANGELOG b/CHANGELOG new file mode 100644 index 0000000..9baf73b --- /dev/null +++ b/CHANGELOG @@ -0,0 +1,2 @@ +2013-08-22 1.0.0 +- Initial Release. Create the database, add certificates. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..3fa0da3 --- /dev/null +++ b/LICENSE @@ -0,0 +1,13 @@ +Copyright 2013 Red Hat, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/README.md b/README.md new file mode 100644 index 0000000..fcdb513 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# nssdb puppet module + +very simple puppet module to create an NSS database and add a certificate +and key via PEM files. diff --git a/manifests/add_cert_and_key.pp b/manifests/add_cert_and_key.pp new file mode 100644 index 0000000..9c83a0d --- /dev/null +++ b/manifests/add_cert_and_key.pp @@ -0,0 +1,57 @@ +# Loads a certificate and key into an NSS database. +# +# Parameters: +# $dbname - required - the directory to store the db +# $nickname - required - the nickname for the NSS certificate +# $cert - required - path to certificate in PEM format +# $key - required - path to unencrypted key in PEM format +# $basedir - optional - defaults to /etc/pki +# +# Actions: +# loads certificate and key into the NSS database. +# +# Requires: +# $dbname +# $nickname +# $cert +# $key +# +# Sample Usage: +# +# nssdb::add_cert_and_key{"qpidd": +# nickname=> 'Server-Cert', +# cert => '/tmp/server.crt', +# key => '/tmp/server.key', +# } +# +define nssdb::add_cert_and_key ( + $dbname = $title, + $nickname, + $cert, + $key, + $basedir = '/etc/pki' +) { + package { 'openssl': ensure => present } + + exec {'generate_pkcs12': + command => "/usr/bin/openssl pkcs12 -export -in $cert -inkey $key -password 'file:${basedir}/${dbname}/password.conf' -out '${basedir}/${dbname}/$dbname.p12' -name $nickname", + require => [ + File["${basedir}/${dbname}/password.conf"], + File["${basedir}/${dbname}/cert8.db"], + Package['openssl'], + ], + before => Exec['load_pkcs12'], + notify => Exec['load_pkcs12'], + subscribe => File["${basedir}/${dbname}/password.conf"], + refreshonly => true, + } + + exec {'load_pkcs12': + command => "/usr/bin/pk12util -i '${basedir}/${dbname}/$dbname.p12' -d '${basedir}/${dbname}' -w '${basedir}/${dbname}/password.conf' -k '${basedir}/${dbname}/password.conf'", + require => [ + Exec["generate_pkcs12"], + Package['nss-tools'], + ], + refreshonly => true, + } +} diff --git a/manifests/create.pp b/manifests/create.pp new file mode 100644 index 0000000..05d3c13 --- /dev/null +++ b/manifests/create.pp @@ -0,0 +1,92 @@ +# Create an empty NSS database with a password file. +# +# Parameters: +# $dbname - required - the directory to store the db +# $owner_id - required - the file/directory user +# $group_id - required - the file/directory group +# $password - required - password to set on the database +# $basedir - optional - defaults to /etc/pki +# $cacert - optional - path to CA certificate in PEM format +# $canickname - default CA nickname +# $catrust - default CT,CT, +# +# Actions: +# creates a new NSS database, consisting of 4 files: +# cert8.db, key3.db, secmod.db and a password file, password.conf +# +# Requires: +# $dbname must be set +# $owner_id must be set +# $group_id must be set +# $password must be set +# +# Sample Usage: +# +# secure::nssdb {'test': +# owner_id => 'qpidd', +# group_id => 'qpidd', +# password => 'test'} +# +# This will create an NSS database in /etc/pki/test +# +define nssdb::create ( + $dbname = $title, + $owner_id, + $group_id, + $password, + $basedir = '/etc/pki', + $cacert = '/etc/pki/certs/CA/ca.crt', + $canickname = 'CA', + $catrust = 'CT,CT,' +) { + package { 'nss-tools': ensure => present } + + file {"${basedir}/${dbname}": + ensure => directory, + mode => 0600, + owner => $owner_id, + group => $group_id, + } + file {"${basedir}/${dbname}/password.conf": + ensure => file, + mode => 0600, + owner => $owner_id, + group => $group_id, + content => $password, + require => [ + File["${basedir}/${dbname}"], + ], + } + file { ["${basedir}/${dbname}/cert8.db", "${basedir}/${dbname}/key3.db", "${basedir}/${dbname}/secmod.db"] : + ensure => file, + mode => 0600, + owner => $owner_id, + group => $group_id, + require => [ + File["${basedir}/${dbname}/password.conf"], + Exec['create_nss_db'], + ], + } + + exec {'create_nss_db': + command => "/usr/bin/certutil -N -d ${basedir}/${dbname} -f ${basedir}/${dbname}/password.conf", + creates => ["${basedir}/${dbname}/cert8.db", "${basedir}/${dbname}/key3.db", "${basedir}/${dbname}/secmod.db"], + require => [ + File["${basedir}/${dbname}"], + File["${basedir}/${dbname}/password.conf"], + Package['nss-tools'], + ], + notify => [ + Exec["add_ca_cert"], + ], + } + + exec {'add_ca_cert': + command => "/usr/bin/certutil -A -n ${canickname} -d ${basedir}/${dbname} -t ${catrust} -a -i ${cacert}", + require => [ + Package['nss-tools'], + ], + refreshonly => true, + onlyif => "/usr/bin/test -e $cacert", + } +} diff --git a/tests/create.pp b/tests/create.pp new file mode 100644 index 0000000..f313cba --- /dev/null +++ b/tests/create.pp @@ -0,0 +1,27 @@ +# NOTE: This requires that the directory /tmp/nssdb already exists + +# Create a test database owned by the user rcrit +nssdb::create {'test': + owner_id => 'rcrit', + group_id => 'rcrit', + password => 'test', + cacert => '/etc/ipa/ca.crt', + catrust => 'CT,,', + basedir => '/tmp/nssdb', +} + +# Add a certificate and private key from PEM fiels +nssdb::add_cert_and_key {'test': + cert => '/tmp/cert.pem', + key => '/tmp/key.pem', + nickname => 'test', + basedir => '/tmp/nssdb', +} + +# You can confirm that things are loaded properly with: +# +# List the certs: +# certutil -L -d /tmp/nssdb/test +# +# Verify the cert: +# certutil -V -u V -d /tmp/nssdb/test -n test