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
caddytls: Add `load_storage` module (#4055)
An idea that came up in https://caddy.community/t/save-internally-issued-wildcard-certificate-in-consul/11740, this a simple module that might be useful for anyone who uses storage modules that aren't filesystem, to let them load certs/keys externally issued for use by Caddy. Bit goofy, since we need to fetch the certmagic.Storage during provisioning, it needs a wrapping struct instead of just being an array like `load_files`. Future work might involve adding Caddyfile support via a subdirective of the `tls` directive maybe?
Francis Lavoie committed
Apr 21, 2021 at 19:05 UTC
1e218e1d2e3986b7cc1cd7335b43f11a4f194b6e
1 file changed
+89
modules/caddytls/storageloader.go
new
+89
@@ -0,0 +1,89 @@
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
+ "github.com/caddyserver/certmagic"
23
+)
24
+
25
+func init() {
26
+ caddy.RegisterModule(StorageLoader{})
27
+}
28
+
29
+// StorageLoader loads certificates and their associated keys
30
+// from the globally configured storage module.
31
+type StorageLoader struct {
32
+ // A list of pairs of certificate and key file names along with their
33
+ // encoding format so that they can be loaded from storage.
34
+ Pairs []CertKeyFilePair `json:"pairs,omitempty"`
35
+
36
+ // Reference to the globally configured storage module.
37
+ storage certmagic.Storage
38
+}
39
+
40
+// CaddyModule returns the Caddy module information.
41
+func (StorageLoader) CaddyModule() caddy.ModuleInfo {
42
+ return caddy.ModuleInfo{
43
+ ID: "tls.certificates.load_storage",
44
+ New: func() caddy.Module { return new(StorageLoader) },
45
+ }
46
+}
47
+
48
+// Provision loads the storage module for sl.
49
+func (sl *StorageLoader) Provision(ctx caddy.Context) error {
50
+ sl.storage = ctx.Storage()
51
+ return nil
52
+}
53
+
54
+// LoadCertificates returns the certificates to be loaded by sl.
55
+func (sl StorageLoader) LoadCertificates() ([]Certificate, error) {
56
+ certs := make([]Certificate, 0, len(sl.Pairs))
57
+ for _, pair := range sl.Pairs {
58
+ certData, err := sl.storage.Load(pair.Certificate)
59
+ if err != nil {
60
+ return nil, err
61
+ }
62
+ keyData, err := sl.storage.Load(pair.Key)
63
+ if err != nil {
64
+ return nil, err
65
+ }
66
+
67
+ var cert tls.Certificate
68
+ switch pair.Format {
69
+ case "":
70
+ fallthrough
71
+ case "pem":
72
+ cert, err = tls.X509KeyPair(certData, keyData)
73
+ default:
74
+ return nil, fmt.Errorf("unrecognized certificate/key encoding format: %s", pair.Format)
75
+ }
76
+ if err != nil {
77
+ return nil, err
78
+ }
79
+
80
+ certs = append(certs, Certificate{Certificate: cert, Tags: pair.Tags})
81
+ }
82
+ return certs, nil
83
+}
84
+
85
+// Interface guard
86
+var (
87
+ _ CertificateLoader = (*StorageLoader)(nil)
88
+ _ caddy.Provisioner = (*StorageLoader)(nil)
89
+)