Defined Type: cfssl::cert

Defined in:
modules/cfssl/manifests/cert.pp

Summary

a resource for creating csr json files

Overview

SPDX-License-Identifier: Apache-2.0

Parameters:

  • common_name (String) (defaults to: $title)

    th common name and SNI for the certificate

  • names (Array[Cfssl::Name]) (defaults to: [])

    an array of values used for the certificate subject

  • key (Cfssl::Key) (defaults to: { 'algo' => 'ecdsa', 'size' => 256 })

    the key algorithm and size

  • ensure (Wmflib::Ensure) (defaults to: 'present')

    the ensure parameter

  • owner (String) (defaults to: 'root')

    the user to use as the owner of files

  • group (String) (defaults to: 'root')

    the user to use as the owner of files

  • auto_renew (Boolean) (defaults to: true)

    if true we will auto_renew the certificate

  • renew_seconds (Integer[1800]) (defaults to: 952200)

    renew the certificate if its due to expire in this many seconds

  • provide_chain (Boolean) (defaults to: false)

    provide the certificate chain in the output dir

  • mode (Stdlib::Filemode) (defaults to: '0740')

    the file mode to use for the outdir

  • environment (Array[String]) (defaults to: ['GODEBUG=x509ignoreCN=0'])

    environment to use when running commands

  • label (Optional[Cfssl::Ca_name]) (defaults to: undef)

    the cfssl label to use, this is essentially the CA

  • profile (Optional[String]) (defaults to: undef)

    the cfssl profile to use

  • notify_services (Array[String]) (defaults to: [])

    array of service names to notify when the certificate changes

  • outdir (Optional[Stdlib::Unixpath]) (defaults to: undef)

    specify a specific directory to write all certificate files to

  • tls_cert (Optional[Stdlib::Unixpath]) (defaults to: undef)

    the tls client certificate use when requesting signing

  • tls_key (Optional[Stdlib::Unixpath]) (defaults to: undef)

    the tls client key use when requesting signing

  • tls_remote_ca (Optional[Stdlib::Unixpath]) (defaults to: undef)

    the CA bundle used for connecting to the pki service

  • signer_config (Optional[Cfssl::Signer_config]) (defaults to: undef)

    the configuration used for signing (only for advance usage)

  • hosts (Array[Cfssl::Common_name]) (defaults to: [])

    an array of hosts to be added to the SNI



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'modules/cfssl/manifests/cert.pp', line 23

define cfssl::cert (
    String                         $common_name     = $title,
    Array[Cfssl::Name]             $names           = [],
    Cfssl::Key                     $key             = { 'algo' => 'ecdsa', 'size' => 256 },
    Wmflib::Ensure                 $ensure          = 'present',
    String                         $owner           = 'root',
    String                         $group           = 'root',
    Boolean                        $auto_renew      = true,
    # the default https checks go warning after 10 full days i.e. anywhere
    # from 864000 to 950399 seconds before the certificate expires.  As such set this to
    # 11 days + 30 minutes to capture the puppet run schedule.
    Integer[1800]                  $renew_seconds   = 952200,
    Boolean                        $provide_chain   = false,
    Stdlib::Filemode               $mode            = '0740',
    # We need this because the puppet CA cert used for TLS mutual auth has no SAN
    Array[String]                  $environment     = ['GODEBUG=x509ignoreCN=0'],
    Optional[Cfssl::Ca_name]       $label           = undef,
    Optional[String]               $profile         = undef,
    Array[String]                  $notify_services = [],
    Optional[Stdlib::Unixpath]     $outdir          = undef,
    Optional[Stdlib::Unixpath]     $tls_cert        = undef,
    Optional[Stdlib::Unixpath]     $tls_key         = undef,
    Optional[Stdlib::Unixpath]     $tls_remote_ca   = undef,
    Optional[Cfssl::Signer_config] $signer_config   = undef,
    Array[Cfssl::Common_name]      $hosts           = [],

) {
    include cfssl
    include cfssl::client
    $_tls_cert = $tls_cert ? {
        undef   => $cfssl::client::mutual_tls_client_cert,
        default => $tls_cert,
    }
    $_tls_key = $tls_key ? {
        undef   => $cfssl::client::mutual_tls_client_key,
        default => $tls_key,
    }
    $_tls_remote_ca = $tls_remote_ca ? {
        undef   => $cfssl::client::tls_remote_ca,
        default => $tls_remote_ca,
    }
    # use the client config by default
    $_signer_config = pick($signer_config, { 'config_file' => $cfssl::client::conf_file })

    if $key['algo'] == 'rsa' and $key['size'] < 2048 {
        fail('RSA keys must be either 2048, 4096 or 8192 bits')
    }
    if $key['algo'] == 'ecdsa' and $key['size'] > 2048 {
        fail('ECDSA keys must be either 256, 384 or 521 bits')
    }

    $safe_title = $title.regsubst('[^\w\-]', '_', 'G')
    $csr_json_path = "${cfssl::csr_dir}/${safe_title}.csr"
    $_outdir   = $outdir ? {
        undef   => "${cfssl::ssl_dir}/${safe_title}",
        default => $outdir,
    }

    cfssl::csr { $csr_json_path:
        common_name => $common_name,
        key         => $key,
        names       => $names,
        hosts       => $hosts,
    }

    unless defined(File[$_outdir]) {
        file { $_outdir:
            ensure  => stdlib::ensure($ensure, 'directory'),
            owner   => $owner,
            group   => $group,
            recurse => true,
            mode    => $mode,
        }
    }
    $tls_config = ($_tls_cert and $_tls_key) ? {
        true    => "-mutual-tls-client-cert ${_tls_cert} -mutual-tls-client-key ${_tls_key}",
        default => '',
    }
    $tls_remote_ca_config = $_tls_remote_ca ? {
        undef   => '',
        default => "-tls-remote-ca ${_tls_remote_ca}",
    }
    $_label = $label ? {
        undef   => '',
        default => "-label ${label}",
    }
    $_profile = $profile ? {
        undef   => '',
        default => "-profile ${profile}",
    }
    $signer_args = $_signer_config ? {
        Stdlib::HTTPUrl              => "-remote ${_signer_config} ${tls_remote_ca_config} ${tls_config} ${_label}",
        Cfssl::Signer_config::Client => "-config ${_signer_config['config_file']} ${tls_remote_ca_config} ${tls_config} ${_label}",
        default                      => @("SIGNER_ARGS"/L)
            -ca=${_signer_config['config_dir']}/ca/ca.pem \
            -ca-key=${_signer_config['config_dir']}/ca/ca-key.pem \
            -config=${_signer_config['config_dir']}/cfssl.conf \
            -db-config=${_signer_config['config_dir']}/db.conf \
            | SIGNER_ARGS
    }
    $cert_path = "${_outdir}/${safe_title}.pem"
    $key_path = "${_outdir}/${safe_title}-key.pem"
    $csr_pem_path = "${_outdir}/${safe_title}.csr"
    $gen_command = @("GEN_COMMAND"/L)
        /usr/bin/cfssl gencert ${signer_args} ${_profile} ${csr_json_path} \
        | /usr/bin/cfssljson -bare ${_outdir}/${safe_title}
        | GEN_COMMAND
    $sign_command = @("SIGN_COMMAND"/L)
        /usr/bin/cfssl sign ${signer_args} ${_profile} ${csr_pem_path} \
        | /usr/bin/cfssljson -bare ${_outdir}/${safe_title}
        | SIGN_COMMAND

    # TODO: would be nice to check its signed with the correct CA
    $test_command = @("TEST_COMMAND"/L)
        /usr/bin/test \
        "$(/usr/bin/openssl x509 -in ${cert_path} -noout -pubkey 2>&1)" == \
        "$(/usr/bin/openssl pkey -pubout -in ${key_path} 2>&1)"
        | TEST_COMMAND
    if $ensure == 'present' {
        $_notify_services = $notify_services.empty() ? {
            true    => undef,
            default => Service[$notify_services],
        }
        exec { "Generate cert ${title}":
            command     => $gen_command,
            environment => $environment,
            unless      => $test_command,
            notify      => $_notify_services,
            require     => Cfssl::Csr[$csr_json_path],
        }
        exec { "Generate cert ${title} refresh":
            command     => $gen_command,
            environment => $environment,
            refreshonly => true,
            notify      => $_notify_services,
            subscribe   => File[$csr_json_path],
        }
        if $auto_renew {
            exec { "renew certificate - ${title}":
                command     => $sign_command,
                environment => $environment,
                unless      => "/usr/bin/openssl x509 -in ${cert_path} -checkend ${renew_seconds}",
                require     => Exec["Generate cert ${title}"],
                notify      => $_notify_services,
            }
        }
    }

    file { [$cert_path, $csr_pem_path]:
        ensure => stdlib::ensure($ensure, 'file'),
        owner  => $owner,
        group  => $group,
        mode   => '0440',
    }
    file { $key_path:
        ensure    => stdlib::ensure($ensure, 'file'),
        owner     => $owner,
        group     => $group,
        mode      => '0440',
        show_diff => false,
        backup    => false,
    }
    if $provide_chain {
        # TODO: we need to replace how we fetch bundles as fetching over a
        # http source seems like a really bad idea.
        # Ideally the gencert command would support the bundle options but
        # there has been little progress on this upstream
        # https://github.com/cloudflare/cfssl/issues/779
        # We may be better of implementing the certificate creations directly
        # via the API
        unless $label {
            fail('you must provide a $label if specifying $provide_chain')
        }
        # Just copy the CA file locally once
        $cert_chain_path = "${_outdir}/${safe_title}.chain.pem"
        $cert_chained_path = "${_outdir}/${safe_title}.chained.pem"
        file { $cert_chain_path:
            ensure => stdlib::ensure($ensure, 'file'),
            owner  => $owner,
            group  => $group,
            mode   => '0440',
            source => "${cfssl::client::bundles_source}/${label}-cert.pem",
        }

        if $ensure == 'present' {
            $test_chained = @("TEST_CHAINED"/L)
                /usr/bin/test \
                "$(/bin/cat ${cert_path} ${cert_chain_path} | sha512sum)" == \
                "$(/bin/cat ${cert_chained_path} | sha512sum)"
                | TEST_CHAINED
            # TODO: use sslcert::chained
            $subscribe = $auto_renew ? {
                true    => [Exec["renew certificate - ${title}"], File[$cert_chain_path, $cert_path]],
                default => File[$cert_chain_path, $cert_path],
            }
            exec { "create chained cert ${cert_chain_path}":
                command   => "/bin/cat ${cert_path} ${cert_chain_path} > ${cert_chained_path}",
                unless    => $test_chained,
                notify    => $_notify_services,
                subscribe => $subscribe,
            }
        }
        # create chained cert is not/no longer defined in case ensure==absent
        # so don't define it as requirement in that case.
        $_require = $ensure ? {
            'present' => Exec["create chained cert ${cert_chain_path}"],
            'absent'  => undef
        }
        file { $cert_chained_path:
            ensure  => stdlib::ensure($ensure, 'file'),
            owner   => $owner,
            group   => $group,
            require => $_require,
        }
    }
}