-
Notifications
You must be signed in to change notification settings - Fork 2
/
keyless.go
121 lines (95 loc) · 3.65 KB
/
keyless.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
// Copyright 2021 Herman Slatman
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package keyless
import (
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/modules/caddytls"
"go.uber.org/zap"
"github.com/hslatman/caddy-keyless/internal/client"
)
func init() {
caddy.RegisterModule(KeylessLoader{})
}
type KeylessLoader struct {
CertificateFile string `json:"cert"` // TODO: nest inside some kind of Auth block?
KeyFile string `json:"key"`
CAFile string `json:"ca"`
DisableVerification bool `json:"disable_verification"`
Server string `json:"server"`
CertificateFiles []string `json:"certificates"`
logger *zap.Logger
client *client.Client
}
// CaddyModule returns the Caddy module information.
func (KeylessLoader) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "tls.certificates.keyless",
New: func() caddy.Module { return new(KeylessLoader) },
}
}
// Provision sets up the handler.
func (m *KeylessLoader) Provision(ctx caddy.Context) error {
m.logger = ctx.Logger(m)
defer m.logger.Sync()
c, err := client.New(m.Server, m.CertificateFile, m.KeyFile, m.CAFile) // TODO: add options?
if err != nil {
return err
}
m.client = c
if m.DisableVerification {
m.client.DisableVerification()
}
// TODO: add active validation (i.e. try to contact the server, see if it knows the key, etc)?
// TODO: add support for Caddyfile (shouldn't be too hard at this time; not that much to configure right now)
// TODO: provide a means for automatically knowing which certificates to use with keyless?
return nil
}
func (m *KeylessLoader) LoadCertificates() ([]caddytls.Certificate, error) {
// TODO: provide a custom layer on top of keyless to get the certificate from
// the keyless server too? Although it can be done manually, for now ...
certs := []caddytls.Certificate{}
// TODO: rewrite into actual loader that can read certs multiple ways (dir, files, pems, remote?, etc)
for _, certFile := range m.CertificateFiles {
cert, err := m.loadCertificate(certFile) // TODO: call this with an identifier/file for the cert
if err != nil {
return []caddytls.Certificate{}, err
}
if cert != nil {
// fmt.Println("appending the cert") // TODO: do proper logging for this
// fmt.Println(fmt.Sprintf("%#+v", cert))
// fmt.Println(fmt.Sprintf("%#+v", cert.Certificate))
// fmt.Println(fmt.Sprintf("%T", cert.Certificate.PrivateKey))
// leaf := cert.Certificate.Leaf
// fmt.Println(fmt.Sprintf("%#+v", leaf))
// fmt.Println(leaf.Subject)
// fmt.Println(leaf.DNSNames)
certs = append(certs, *cert)
}
}
return certs, nil
}
func (m *KeylessLoader) loadCertificate(certFile string) (*caddytls.Certificate, error) {
tlsCert, err := m.client.LoadTLSCertificate(certFile) // TODO: ensure SNI is also sent to remote, if possible?
cert := &caddytls.Certificate{
Certificate: tlsCert,
Tags: []string{}, // TODO: add some useful info here (look into how Caddy uses these)
}
return cert, err
}
// Interface guards
var (
_ caddy.Module = (*KeylessLoader)(nil)
_ caddy.Provisioner = (*KeylessLoader)(nil)
_ caddytls.CertificateLoader = (*KeylessLoader)(nil)
)