@marcel91200 / caddy Mirror · read-only
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 11, 01:00 UTC

master
go 106 lines 2.85 KB
Raw
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/x509"
19 "fmt"
20 "os"
21 "path/filepath"
22 "strings"
23
24 "github.com/caddyserver/caddy/v2"
25 "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
26 )
27
28 func init() {
29 caddy.RegisterModule(LeafFolderLoader{})
30 }
31
32 // LeafFolderLoader loads certificates from disk
33 // by recursively walking the specified directories, looking for PEM
34 // files which contain a certificate.
35 type LeafFolderLoader struct {
36 Folders []string `json:"folders,omitempty"`
37 }
38
39 // CaddyModule returns the Caddy module information.
40 func (LeafFolderLoader) CaddyModule() caddy.ModuleInfo {
41 return caddy.ModuleInfo{
42 ID: "tls.leaf_cert_loader.folder",
43 New: func() caddy.Module { return new(LeafFolderLoader) },
44 }
45 }
46
47 // Provision implements caddy.Provisioner.
48 func (fl *LeafFolderLoader) Provision(ctx caddy.Context) error {
49 repl, ok := ctx.Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
50 if !ok {
51 repl = caddy.NewReplacer()
52 }
53 for k, path := range fl.Folders {
54 fl.Folders[k] = repl.ReplaceKnown(path, "")
55 }
56 return nil
57 }
58
59 // UnmarshalCaddyfile implements caddyfile.Unmarshaler.
60 func (fl *LeafFolderLoader) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
61 d.NextArg()
62 fl.Folders = append(fl.Folders, d.RemainingArgs()...)
63 return nil
64 }
65
66 // LoadLeafCertificates loads all the leaf certificates in the directories
67 // listed in fl from all files ending with .pem.
68 func (fl LeafFolderLoader) LoadLeafCertificates() ([]*x509.Certificate, error) {
69 var certs []*x509.Certificate
70 for _, dir := range fl.Folders {
71 err := filepath.Walk(dir, func(fpath string, info os.FileInfo, err error) error {
72 if err != nil {
73 return fmt.Errorf("unable to traverse into path: %s", fpath)
74 }
75 if info.IsDir() {
76 return nil
77 }
78 if !strings.HasSuffix(strings.ToLower(info.Name()), ".pem") {
79 return nil
80 }
81
82 certData, err := convertPEMFilesToDERBytes(fpath)
83 if err != nil {
84 return err
85 }
86 cert, err := x509.ParseCertificate(certData)
87 if err != nil {
88 return fmt.Errorf("%s: %w", fpath, err)
89 }
90
91 certs = append(certs, cert)
92
93 return nil
94 })
95 if err != nil {
96 return nil, err
97 }
98 }
99 return certs, nil
100 }
101
102 var (
103 _ LeafCertificateLoader = (*LeafFolderLoader)(nil)
104 _ caddy.Provisioner = (*LeafFolderLoader)(nil)
105 _ caddyfile.Unmarshaler = (*LeafFolderLoader)(nil)
106 )