@marcel91200 / caddy Mirror · read-only / commits / 20fe9cf0
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

tls: Add pem_loader module

This migrates a feature that was previously reserved for enterprise users, according to https://github.com/caddyserver/caddy/issues/2786. The PEM loader allows you to embed PEM files (certificates and keys) directly into your config, rather than requiring them to be stored on potentially insecure storage, which adds attack vectors. This is useful in automated settings where sensitive key material is stored only in memory. Note that if the config is persisted to disk, that added benefit may go away, but there will still be the benefit of having lesser dependence on external files.

Matthew Holt committed Oct 9, 2019 at 19:34 UTC 20fe9cf024898c73725eab0d4306dfd3b6ccd6d8
1 file changed +65
modules/caddytls/pemloader.go new
+65
@@ -0,0 +1,65 @@
1 +// Copyright 2015 Matthew Holt and The Caddy Authors
2 +//
3 +// Licensed under the Apache License, Version 2.0 (the "License");
4 +// you may not use this file except in compliance with the License.
5 +// You may obtain a copy of the License at
6 +//
7 +// http://www.apache.org/licenses/LICENSE-2.0
8 +//
9 +// Unless required by applicable law or agreed to in writing, software
10 +// distributed under the License is distributed on an "AS IS" BASIS,
11 +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 +// See the License for the specific language governing permissions and
13 +// limitations under the License.
14 +
15 +package caddytls
16 +
17 +import (
18 + "crypto/tls"
19 + "fmt"
20 +
21 + "github.com/caddyserver/caddy/v2"
22 +)
23 +
24 +func init() {
25 + caddy.RegisterModule(PEMLoader{})
26 +}
27 +
28 +// PEMLoader loads certificates and their associated keys by
29 +// decoding their PEM blocks directly. This has the advantage
30 +// of not needing to store them on disk at all.
31 +type PEMLoader []CertKeyPEMPair
32 +
33 +// CaddyModule returns the Caddy module information.
34 +func (PEMLoader) CaddyModule() caddy.ModuleInfo {
35 + return caddy.ModuleInfo{
36 + Name: "tls.certificates.load_pem",
37 + New: func() caddy.Module { return PEMLoader{} },
38 + }
39 +}
40 +
41 +// CertKeyPEMPair pairs certificate and key PEM blocks.
42 +type CertKeyPEMPair struct {
43 + CertificatePEM string `json:"certificate"`
44 + KeyPEM string `json:"key"`
45 + Tags []string `json:"tags,omitempty"`
46 +}
47 +
48 +// LoadCertificates returns the certificates contained in pl.
49 +func (pl PEMLoader) LoadCertificates() ([]Certificate, error) {
50 + var certs []Certificate
51 + for i, pair := range pl {
52 + cert, err := tls.X509KeyPair([]byte(pair.CertificatePEM), []byte(pair.KeyPEM))
53 + if err != nil {
54 + return nil, fmt.Errorf("PEM pair %d: %v", i, err)
55 + }
56 + certs = append(certs, Certificate{
57 + Certificate: cert,
58 + Tags: pair.Tags,
59 + })
60 + }
61 + return certs, nil
62 +}
63 +
64 +// Interface guard
65 +var _ CertificateLoader = (PEMLoader)(nil)