-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkcert-cache.go
148 lines (133 loc) · 3.89 KB
/
mkcert-cache.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Copyright (c) 2019-2021. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package providers
import (
"crypto/md5"
"encoding/hex"
"fmt"
"net/url"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"github.com/pydio/cells/common/config"
"github.com/pydio/cells/common/proto/install"
"github.com/pydio/cells/common/utils/net"
)
var cache *MkCertCache
func init() {
cache = &MkCertCache{
data: make(map[string]*pair),
lock: &sync.Mutex{},
}
}
type pair struct {
cert string
key string
}
type MkCertCache struct {
lock *sync.Mutex
data map[string]*pair
}
func GetMkCertCache() *MkCertCache {
return cache
}
func (m *MkCertCache) Uuid(hosts []string) string {
sort.Strings(hosts)
hh := strings.Join(hosts, "##")
hash := md5.New()
hash.Write([]byte(hh))
return hex.EncodeToString(hash.Sum(nil))
}
func (m *MkCertCache) LoadCertificates(config *install.ProxyConfig) (certFile string, keyFile string, err error) {
hosts := m.loadHosts(config)
uuid := cache.Uuid(hosts)
cache.lock.Lock()
defer cache.lock.Unlock()
if p, o := cache.data[uuid]; o {
return p.cert, p.key, nil
} else {
certFile, keyFile, err = cache.findOrGenerate(uuid, hosts)
if err == nil {
cache.data[uuid] = &pair{cert: certFile, key: keyFile}
}
return
}
}
func (m *MkCertCache) loadHosts(site *install.ProxyConfig) []string {
hns := site.GetSelfSigned().GetHostnames()
if len(hns) == 0 {
bindAll := false
// get hosts from default
for _, bind := range site.Binds {
u, err := url.Parse("https://" + bind)
if err != nil {
continue
}
bindHost := u.Hostname()
if bindHost == "0.0.0.0" || bindHost == "" {
bindAll = true
} else {
hns = append(hns, bindHost)
}
}
if bindAll {
// Special case for 0.0.0.0 => use all interfaces and external
ii, _ := net.GetAvailableIPs()
for _, i := range ii {
hns = append(hns, i.String())
}
if other, e := net.HostsFileLookup(); e == nil && len(other) > 0 {
hns = append(hns, other...)
}
}
}
return hns
}
func (m *MkCertCache) findOrGenerate(uuid string, hns []string) (certFile, keyFile string, err error) {
storageLocation := filepath.Join(config.ApplicationWorkingDir(), "certs")
os.MkdirAll(storageLocation, 0700)
mk := NewMkCert(storageLocation)
cn := filepath.Join(storageLocation, uuid+".pem")
kn := filepath.Join(storageLocation, uuid+"-key.pem")
_, e1 := os.Stat(cn)
_, e2 := os.Stat(kn)
if e1 == nil && e2 == nil {
certFile = cn
keyFile = kn
return
}
err = mk.MakeCert(hns, uuid)
if err != nil {
return
}
certFile, keyFile, _, _ = mk.GeneratedResources()
fmt.Println("")
fmt.Println("")
fmt.Println("👉 If you are behind a reverse proxy, you can either install the RootCA on the proxy machine " +
"trust store, or configure your proxy to `insecure_skip_verify` for pointing to Cells.")
fmt.Println("👉 If you are developing locally, you may install the RootCA in your system trust store to " +
"see a green light in your browser!")
fmt.Println("🗒 To easily install the RootCA in your trust store, use https://github.com/FiloSottile/mkcert. " +
"Set the $CAROOT environment variable to the rootCA folder then use 'mkcert -install'")
fmt.Println("")
return
}