-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcerts.go
More file actions
122 lines (101 loc) · 3.94 KB
/
certs.go
File metadata and controls
122 lines (101 loc) · 3.94 KB
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
122
// Copyright (c) Roman Atachiants and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
package config
import (
"crypto/tls"
"errors"
"io/ioutil"
"net/http"
"os"
"strings"
"golang.org/x/crypto/acme/autocert"
)
// CertCacher represents a contract which allows for retrieval of certificate cache
type CertCacher interface {
Name() string
GetCache() (autocert.Cache, bool)
}
// DirCache implements Cache using a directory on the local filesystem.
// If the directory does not exist, it will be created with 0700 permissions.
type dirCache struct{}
func (c *dirCache) Name() string {
return "dircache"
}
func (c *dirCache) GetCache() (autocert.Cache, bool) {
return autocert.DirCache("certs"), true
}
// TLS returns a TLS configuration which can be applied or validated. This requires a set of valid
// stores in the first place, so for this to work it needs to be called once the stores are configured
// (aka Configure() method was called). This should work well if called at some point after calling
// config.ReadOrCreate().
func TLS(cfg *TLSConfig, stores ...CertCacher) (*tls.Config, http.Handler, CertCacher) {
stores = append(stores, new(dirCache)) // Fallback to DirCache
// Go through all of the certificate stores
for _, store := range stores {
if cache, valid := store.GetCache(); valid {
if tls, val, err := cfg.Load(cache); err == nil {
return tls, val, store
} else {
panic(err)
}
}
}
return nil, nil, nil
}
// TLSConfig represents TLS listener configuration.
type TLSConfig struct {
ListenAddr string `json:"listen"` // The address to listen on.
Host string `json:"host"` // The hostname to whitelist.
Email string `json:"email,omitempty"` // The email address for autocert.
Certificate string `json:"certificate,omitempty"` // The certificate request.
PrivateKey string `json:"private,omitempty"` // The private key for the certificate.
}
// Load loads the certificates from the cache or the configuration.
func (c *TLSConfig) Load(certCache autocert.Cache) (*tls.Config, http.Handler, error) {
if c.Certificate != "" {
return c.loadFromLocal(certCache)
}
return c.loadFromAutocert(certCache)
}
// loadFromLocal loads TLS configuration from pre-existing certificate
func (c *TLSConfig) loadFromLocal(certCache autocert.Cache) (*tls.Config, http.Handler, error) {
if c.PrivateKey == "" {
return &tls.Config{}, nil, errors.New("No certificate or private key configured")
}
// If the certificate provided is in plain text, write to file so we can read it.
if strings.HasPrefix(c.Certificate, "---") {
if err := ioutil.WriteFile("broker.crt", []byte(c.Certificate), os.ModePerm); err == nil {
c.Certificate = "broker.crt"
}
}
// If the private key provided is in plain text, write to file so we can read it.
if strings.HasPrefix(c.PrivateKey, "---") {
if err := ioutil.WriteFile("broker.key", []byte(c.PrivateKey), os.ModePerm); err == nil {
c.PrivateKey = "broker.key"
}
}
// Make sure the paths are absolute, otherwise we won't be able to read the files.
c.Certificate = resolvePath(c.Certificate)
c.PrivateKey = resolvePath(c.PrivateKey)
// Load the certificate from the cert/key files.
cer, err := tls.LoadX509KeyPair(c.Certificate, c.PrivateKey)
return &tls.Config{
Certificates: []tls.Certificate{cer},
}, nil, err
}
// loadFromAutocert loads TLS configuration from Letsencrypt
func (c *TLSConfig) loadFromAutocert(certCache autocert.Cache) (*tls.Config, http.Handler, error) {
if c.Host == "" {
return nil, nil, errors.New("unable to request a certificate, no host name configured")
}
// Create an auto-cert manager
certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(c.Host),
Email: c.Email,
Cache: certCache,
}
return &tls.Config{
GetCertificate: certManager.GetCertificate,
}, certManager.HTTPHandler(nil), nil
}