• Stars
    star
    202
  • Rank 186,777 (Top 4 %)
  • Language
    Go
  • License
    BSD 2-Clause "Sim...
  • Created over 7 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Automated certificate management using a CFSSL CA.

certmgr

Build Status godoc

certmgr is a tool for managing certificates using CFSSL. It does the following:

  • Ensures certificates are present.
  • Renews certificates before they expire.
  • Triggering a service reload or restart on certificate updates.

It operates on certificate specs, which are JSON files containing the information needed to generate a certificate.

At regular intervals, certmgr will check that the parameters set in a certificate spec match the PKI material on disk. certmgr will take actions as needed in ensuring and regenerating PKI material as needed. If there's an error, a material refresh will happen at a later time.

When run without any subcommands, certmgr will start monitoring certificates. The configuration and specifications can be validated using the check subcommand.

If you want to further understand the package logic, take a look at the godocs.

Note: certmgr requires Go 1.11 or later due to cfssl dependency.

Web server

When appropriately configured, certmgr will start a web server that has the following endpoints:

  • / just contains certmgr start time and current address.
  • /metrics is the Prometheus endpoint (see the Metrics section).

Metrics

Prometheus is used to collect some useful certmgr metrics. You can find them in the godoc.

certmgr.yaml

The configuration file must be a YAML file; it is expected to be in /etc/certmgr/certmgr.yaml. The location can be changed using the -f flag.

An example certmgr.yaml file is:

dir: /etc/certmgr.d
default_remote: ca.example.net:8888
svcmgr: systemd
before: 72h
interval: 30m

metrics_port: 8080
metrics_address: localhost

This contains all of the currently available parameters:

  • dir: this specifies the directory containing the certificate specs
  • svcmgr: this specifies the service manager to use for restarting or reloading services. This can be systemd (using systemctl), sysv (using service), circus (using circusctl), openrc (using rc-service), dummy (no restart/reload behavior), or command (see the command svcmgr section for details of how to use this).
  • before: optional: this is the default duration before a certificate expiry that certmgr starts attempting to renew PKI. This defaults to 72 hours.
  • interval: optional: this is the default for how often certmgr will check certificate expirations and update PKI material on disk upon any changes (if necessary). This defaults to one hour.
  • interval_splay: optional: this is used to vary the interval period. A random time between 0 and this value is added to interval if specified. This defaults to 0.
  • initial_splay: if specified, a random sleep period between 0 and this value is used for the initial sleep after startup of a spec. This provides a way to ensure that if a fleet of certmgr are restarted at the same time, their period of wakeup is randomized to avoid said fleet waking up and doing interval checks at the same time for a given spec. This defaults to 0.
  • metrics_address: specifies the address for the Prometheus HTTP endpoint.
  • metrics_port: specifies the port for the Prometheus HTTP endpoint.
  • take_actions_only_if_running: boolean, if true, only fire a spec's action if the service is actually running. If this is set to false (the default for historical reasons), this can lead to certmgr starting a downed service when PKI expiry occurs.

PKI Specs

A spec is used to manage PKI material for a consuming app. A spec does not have to request a certificate/key, and does not have to request a CA; it must request at least one of those two modes, however.

Said another way; you can use this to maintain a CA on disk. You can use this to maintain certificate/key pair signed by the given authority; you can do both modes if you wish, but one must be specified by the spec.

An example spec that writes both a CA and certificate key pair defined in JSON:

{
    "service": "nginx",
    "action": "restart",
    "request": {
        "CN": "www.example.net",
        "hosts": [
            "example.net",
            "www.example.net"
        ],
        "key": {
            "algo": "ecdsa",
            "size": 521
        },
        "names": [
            {
                "C": "US",
                "ST": "CA",
                "L": "San Francisco",
                "O": "Example, LLC"
            }
        ]
    },
    "private_key": {
        "path": "/etc/ssl/private/www.key",
        "owner": "www-data",
        "group": "www-data",
        "mode": "0600"
    },
    "certificate": {
        "path": "/home/kyle/tmp/certmgr/certs/test1.pem",
        "owner": "www-data",
        "group": "www-data"
    },
    "ca": {
        "path": "/etc/myservice/ca.pem",
        "owner": "www-data",
        "group": "www-data"
    },
    "authority": {
        "remote": "ca.example.net:8888",
        "auth_key": "012345678012345678",
        "label": "www_ca",
        "profile": "three-month",
        "root_ca": "/etc/cfssl/api_server_ca.pem"
    }
}

And this is an example that writes just the CA to disk:

{
    "service": "nginx",
    "action": "restart",
    "authority": {
        "remote": "ca.example.net:8888",
        "auth_key": "012345678012345678",
        "label": "www_ca",
        "profile": "three-month",
        "file": {
            "path": "/etc/myservice/ca.pem",
            "owner": "www-data",
            "group": "www-data"
        },
        "root_ca": "/etc/cfssl/api_server_ca.pem"
    }
}

A certificate spec has the following fields:

  • service: this is optional, and names the service that the action should be applied to.
  • action: this is optional, and may be one of "restart", "reload", or "nop".
  • svcmgr: this is optional, and defaults to whatever the global config defines. This allows fine-grained control for specifying the svcmgr per cert. If you're using this in a raw certificate definition, you likely want the 'command' svcmgr- see that section for details of how to use it.
  • request: a CFSSL certificate request (see below). If this is specified, a certificate and private_key field is required.
  • private_key and certificate: file specifications (see below) for the private key and certificate. Both must be specified- as must request- if you wish to manage a certificate/key pair.
  • authority: contains the CFSSL CA configuration (see below).
  • before: optional: this is the default duration before a certificate expiry that certmgr starts attempting to renew PKI. This defaults to the managers default, which defaults to 72 hours if unspecified.
  • interval: optional: this is the default for how often certmgr will check certificate expirations and update PKI material on disk upon any changes (if necessary). This defaults to the managers default, which defaults to one hour if unspecified.
  • interval_splay: optional: this is used to vary the interval period. A random time between 0 and this value is added to interval if specified. This defaults to the managers default, which defaults to 0 if unspecified.
  • initial_splay: if specified, a random sleep period between 0 and this value is used for the initial sleep after startup of a spec. This provides a way to ensure that if a fleet of certmgr are restarted at the same time, their period of wakeup is randomized to avoid said fleet waking up and doing interval checks at the same time for a given spec. This defaults to the managers default, which defaults to 0 if unspecified.
  • take_actions_only_if_running: boolean, if true, only fire a spec's action if the service is actually running. If this is set to false (the default for historical reasons), this can lead to certmgr starting a downed service when PKI expiry occurs.
  • key_usages: optional: An array of strings defining what this key should be used for. Certmgr will consider a cert invalid if it does not contain these key usages. Possible values are from cfssl's ExtKeyUsage map

Note: certmgr will throw a warning if svcmgr is dummy AND action is "nop" or undefined. This is because such a setup will not properly restart or reload a service upon certificate renewal, which will likely cause your service to crash. Running certmgr with the --strict flag will not even load a certificate spec with a dummy svcmgr and undefined/nop action configuration.

File specifications contain the following fields:

  • path: this is required, and is the path to store the file.
  • owner: this is optional; if it's not provided, the current user is used.
  • group: this is optional; if it's not provided, the primary group of the current user is used.
  • mode: this is optional; if it's not provided, "0644" will be used. It should be a numeric file mode.

CFSSL certificate requests have the following fields:

  • CN: this contains the common name for the certificate.
  • hosts: this is a list of SANs and/or IP addresses for the certificate.
  • key: this is optional; it should contain an "algo" of either "rsa" or "ecdsa" and a "size" appropriate for the chosen algorithm. Recommendations are "rsa" and 2048 or "ecdsa" and 256. The default is "ecdsa" and 256.
  • names: contains PKIX name information, including the "C" (country), "ST" (state), "L" (locality/city), "O" (organisation), and "OU" (organisational unit) fields.

The CA specification contains the following fields:

  • remote: the CA to use. If not provided, the default remote from the config file is used.
  • auth_key: the authentication key used to request a certificate.
  • auth_key_file: optional, if defined read the auth_key from this. If auth_key and auth_key_file is defined, auth_key is used.
  • label: the CA to use for the certificate.
  • profile: the CA profile that should be used.
  • file: if this is included, the CA certificate will be saved here. It follows the same file specification format above. Use this if you want to save your CA cert to disk.
  • root_ca: optionally, a path to a certificate to trust as CA for the cfssl API server certificate. Usable if the "remote" is tls enabled and configured with a self-signed certificate. By default, the system root CA chain is trusted.

command svcmgr and how to use it

If the svcmgr is set to command, then action is interpreted as a shell snippet to invoke via bash -c. Bash is preferred since it allows parse checks to run. If Bash isn't available, parse checks are skipped and sh -c is used. If sh can't be found, then this svcmgr is disabled. The command svcmgr is useful in Marathon environments.

Environment variables are set as follows:

  • CERTMGR_CA_PATH: if CA was configured for the spec, this is the path to the CA ondisk that was changed.
  • CERTMGR_CERT_PATH: This is the path to the cert that was written.
  • CERTMGR_KEY_PATH: This is the path to the key that was written.

Subcommands

In addition to the certificate manager, there are a few utilities functions specified:

  • check: validates the configuration file and all the certificate specs available in the certificate spec directory. Note that if you wish to operate on just one spec, you can use -d /path/to/that/spec to accomplish it.
  • clean: removes all of the certificates and private keys specified by the certificate specs. Note that if you wish to operate on just one spec, you can use -d /path/to/that/spec to accomplish it.
  • ensure: attempts to load all certificate specs, and ensure that the TLS key pairs they identify exist, are valid, and that they are up-to-date. Note that if you wish to operate on just one spec, you can use -d /path/to/that/spec to accomplish this.
  • genconfig: generates a default configuration file and ensures the default service directory exists.
  • version: prints certificate manager's version, the version of Go it was built with, and shows the current configuration.

See also

The certmgr spec is included as SPEC.rst.

Contributing

To contribute, fork this repo and make your changes. Then, make a PR to this repo. A PR requires at least one approval from a repo admin and successful CI build.

Unit Testing

Unit tests can be written locally. This should be straightforward in a Linux environment. To run them in a non-Linux environment, have Docker up and run make test. This will spin up a container with your local build. From here you can go test -v ./... your files. This unconventional setup is because cfssl, the underlying logic of certmgr, uses cgo.

More Repositories

1

quiche

๐Ÿฅง Savoury implementation of the QUIC transport protocol and HTTP/3
Rust
8,654
star
2

cfssl

CFSSL: Cloudflare's PKI and TLS toolkit
Go
8,049
star
3

cloudflared

Cloudflare Tunnel client (formerly Argo Tunnel)
Go
5,870
star
4

boringtun

Userspace WireGuardยฎ Implementation in Rust
Rust
5,768
star
5

workerd

The JavaScript / Wasm runtime that powers Cloudflare Workers
C++
5,699
star
6

flan

A pretty sweet vulnerability scanner
Python
3,910
star
7

miniflare

๐Ÿ”ฅ Fully-local simulator for Cloudflare Workers. For the latest version, see https://github.com/cloudflare/workers-sdk/tree/main/packages/miniflare.
TypeScript
3,656
star
8

wrangler-legacy

๐Ÿค  Home to Wrangler v1 (deprecated)
Rust
3,233
star
9

cloudflare-docs

Cloudflareโ€™s documentation
CSS
2,578
star
10

tableflip

Graceful process restarts in Go
Go
2,549
star
11

workers-rs

Write Cloudflare Workers in 100% Rust via WebAssembly
Rust
2,182
star
12

workers-sdk

โ›…๏ธ Home to Wrangler, the CLI for Cloudflare Workersยฎ
TypeScript
2,047
star
13

wildebeest

Wildebeest is an ActivityPub and Mastodon-compatible server
TypeScript
2,026
star
14

gokey

A simple vaultless password manager in Go
Go
1,836
star
15

ebpf_exporter

Prometheus exporter for custom eBPF metrics
C
1,639
star
16

lol-html

Low output latency streaming HTML parser/rewriter with CSS selector-based API
Rust
1,388
star
17

redoctober

Go server for two-man rule style file encryption and decryption.
Go
1,373
star
18

cloudflare-go

The official Go library for the Cloudflare API
Go
1,313
star
19

cf-ui

๐Ÿ’Ž Cloudflare UI Framework
JavaScript
1,297
star
20

sslconfig

Cloudflare's Internet facing SSL configuration
1,287
star
21

foundations

Cloudflare's Rust service foundations library.
Rust
1,163
star
22

hellogopher

Hellogopher: "just clone and make" your conventional Go project
Makefile
1,153
star
23

production-saas

(WIP) Example SaaS application built in public on the Cloudflare stack!
TypeScript
1,099
star
24

bpftools

BPF Tools - packet analyst toolkit
Python
1,087
star
25

cloudflare-blog

Cloudflare Blog code samples
C
1,065
star
26

wrangler-action

๐Ÿง™โ€โ™€๏ธ easily deploy cloudflare workers applications using wrangler and github actions
TypeScript
993
star
27

templates

A collection of starter templates and examples for Cloudflare Workers and Pages
JavaScript
979
star
28

circl

CIRCL: Cloudflare Interoperable Reusable Cryptographic Library
Go
970
star
29

wirefilter

An execution engine for Wireshark-like filters
Rust
913
star
30

pingora

A library for building fast, reliable and evolvable network services.
Rust
896
star
31

cf-terraforming

A command line utility to facilitate terraforming your existing Cloudflare resources.
Go
859
star
32

next-on-pages

CLI to build and develop Next.js apps for Cloudflare Pages
TypeScript
845
star
33

utahfs

UtahFS is an encrypted storage system that provides a user-friendly FUSE drive backed by cloud storage.
Go
805
star
34

workers-chat-demo

JavaScript
779
star
35

pint

Prometheus rule linter/validator
Go
772
star
36

Stout

A reliable static website deploy tool
Go
749
star
37

goflow

The high-scalability sFlow/NetFlow/IPFIX collector used internally at Cloudflare.
Go
729
star
38

unsee

Alert dashboard for Prometheus Alertmanager
Go
710
star
39

terraform-provider-cloudflare

Cloudflare Terraform Provider
Go
704
star
40

mitmengine

A MITM (monster-in-the-middle) detection tool. Used to build MALCOLM:
Go
690
star
41

workers-graphql-server

๐Ÿ”ฅLightning-fast, globally distributed Apollo GraphQL server, deployed at the edge using Cloudflare Workers
JavaScript
635
star
42

react-gateway

Render React DOM into a new context (aka "Portal")
JavaScript
569
star
43

xdpcap

tcpdump like XDP packet capture
Go
567
star
44

cloudflare-php

PHP library for the Cloudflare v4 API
PHP
566
star
45

ahocorasick

A Golang implementation of the Aho-Corasick string matching algorithm
Go
541
star
46

lua-resty-logger-socket

Raw-socket-based Logger Library for Nginx (based on ngx_lua)
Perl
477
star
47

nginx-google-oauth

Lua module to add Google OAuth to nginx
Lua
425
star
48

gokeyless

Go implementation of the keyless protocol
Go
420
star
49

worker-typescript-template

ส• โ€ขฬุˆโ€ขฬ€) TypeScript template for Cloudflare Workers
TypeScript
416
star
50

golibs

Various small golang libraries
Go
402
star
51

stpyv8

Python 3 and JavaScript interoperability. Successor To PyV8 (https://github.com/flier/pyv8)
C++
388
star
52

sandbox

Simple Linux seccomp rules without writing any code
C
385
star
53

mmap-sync

Rust library for concurrent data access, using memory-mapped files, zero-copy deserialization, and wait-free synchronization.
Rust
380
star
54

speedtest

Component to perform network speed tests against Cloudflare's edge network
JavaScript
371
star
55

mmproxy

mmproxy, the magical PROXY protocol gateway
C
370
star
56

pages-action

JavaScript
355
star
57

rustwasm-worker-template

A template for kick starting a Cloudflare Worker project using workers-rs. Write your Cloudflare Worker entirely in Rust!
Rust
350
star
58

workers-types

TypeScript type definitions for authoring Cloudflare Workers.
TypeScript
350
star
59

cobweb

COBOL to WebAssembly compiler
COBOL
345
star
60

cloudflare-ingress-controller

A Kubernetes ingress controller for Cloudflare's Argo Tunnels
Go
344
star
61

lua-resty-cookie

Lua library for HTTP cookie manipulations for OpenResty/ngx_lua
Perl
340
star
62

svg-hush

Make it safe to serve untrusted SVG files
Rust
333
star
63

boring

BoringSSL bindings for the Rust programming language.
Rust
330
star
64

node-cloudflare

Node.js API for Client API
JavaScript
319
star
65

cfweb3

JavaScript
309
star
66

workerskv.gui

(WIP) A cross-platform Desktop application for exploring Workers KV Namespace data
Svelte
307
star
67

JSON.is

Open-source documentation for common JSON formats.
JavaScript
302
star
68

sqlalchemy-clickhouse

Python
299
star
69

cloudflare.github.io

Cloudflare โค๏ธ Open Source
CSS
298
star
70

json-schema-tools

Packages for working with JSON Schema and JSON Hyper-Schema
JavaScript
296
star
71

chatgpt-plugin

Build ChatGPT plugins with Cloudflare's Developer Platform ๐Ÿค–
JavaScript
290
star
72

tls-tris

crypto/tls, now with 100% more 1.3. THE API IS NOT STABLE AND DOCUMENTATION IS NOT GUARANTEED.
Go
283
star
73

gortr

The RPKI-to-Router server used at Cloudflare
Go
283
star
74

react-modal2

๐Ÿ’ญ Simple modal component for React.
JavaScript
279
star
75

doom-wasm

Chocolate Doom WebAssembly port with WebSockets support
C
273
star
76

keyless

Cloudflare's Keyless SSL Server Reference Implementation
C
272
star
77

isbgpsafeyet.com

Is BGP safe yet?
HTML
262
star
78

go

Go with Cloudflare experimental patches
Go
260
star
79

dog

Durable Object Groups
TypeScript
255
star
80

kv-asset-handler

Routes requests to KV assets
TypeScript
244
star
81

mod_cloudflare

C
243
star
82

tubular

BSD socket API on steroids
C
242
star
83

semver_bash

Semantic Versioning in Bash
Shell
238
star
84

cloudflare-rs

Rust library for the Cloudflare v4 API
Rust
236
star
85

cfssl_trust

CFSSL's CA trust store repository
Go
226
star
86

doca

A CLI tool that scaffolds API documentation based on JSON HyperSchemas.
JavaScript
224
star
87

pmtud

Path MTU daemon - broadcast lost ICMP packets on ECMP networks
C
218
star
88

alertmanager2es

Receives HTTP webhook notifications from AlertManager and inserts them into an Elasticsearch index for searching and analysis
Go
218
star
89

itty-router-openapi

OpenAPI 3 and 3.1 schema generator and validator for Cloudflare Workers
TypeScript
218
star
90

origin-ca-issuer

Go
216
star
91

worker-template-router

JavaScript
216
star
92

cloudflare-docs-engine

A documentation engine built on Gatsby, powering Cloudflareโ€™s docs https://github.com/cloudflare/cloudflare-docs
JavaScript
215
star
93

python-worker-hello-world

Python hello world for Cloudflare Workers
JavaScript
209
star
94

Cloudflare-WordPress

A Cloudflare plugin for WordPress
PHP
208
star
95

saffron

The cron parser powering Cron Triggers on Cloudflare Workers
Rust
207
star
96

collapsify

Collapsify inlines all the resources of a page into a single document
JavaScript
200
star
97

worker-speedtest-template

JavaScript
195
star
98

har-sanitizer

TypeScript
192
star
99

zkp-ecdsa

Proves knowledge of an ECDSA-P256 signature under one of many public keys that are stored in a list.
TypeScript
187
star
100

shellflip

Graceful process restarts in Rust
Rust
183
star