-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathcertificates.go
217 lines (177 loc) · 4.93 KB
/
certificates.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package config
import (
"crypto/tls"
"crypto/x509"
"fmt"
"net/http"
"os"
"time"
ce "github.com/content-services/content-sources-backend/pkg/errors"
)
type CertUser interface {
ClientCert() string
ClientKey() string
CACert() string
ClientCertPath() string
ClientKeyPath() string
CACertPath() string
Label() string
}
func GetHTTPClient(certUser CertUser) (http.Client, error) {
timeout := 90 * time.Second
cert, err := ReadCert(certUser)
if err != nil {
return http.Client{}, fmt.Errorf("failed to read client certificate: %w", err)
}
key, err := ReadKey(certUser)
if err != nil {
return http.Client{}, fmt.Errorf("failed to read client key: %w", err)
}
caCert, err := ReadCACert(certUser)
if err != nil {
return http.Client{}, fmt.Errorf("failed to read client ca certificate: %w", err)
}
transport, err := GetTransport(cert, key, caCert, timeout)
if err != nil {
return http.Client{}, fmt.Errorf("error creating http transport: %w", err)
}
return http.Client{Transport: transport, Timeout: timeout}, nil
}
func ReadCert(certUser CertUser) ([]byte, error) {
var cert []byte
if certUser.ClientCert() != "" {
cert = []byte(certUser.ClientCert())
} else if certUser.ClientCertPath() != "" {
file, err := os.ReadFile(certUser.ClientCertPath())
if err != nil {
return nil, err
}
cert = file
}
return cert, nil
}
func ReadKey(certUser CertUser) ([]byte, error) {
var key []byte
if certUser.ClientKey() != "" {
key = []byte(certUser.ClientKey())
} else if certUser.ClientKeyPath() != "" {
file, err := os.ReadFile(certUser.ClientKeyPath())
if err != nil {
return nil, err
}
key = file
}
return key, nil
}
func ReadCACert(certUser CertUser) ([]byte, error) {
var caCert []byte
if certUser.CACert() != "" {
caCert = []byte(certUser.CACert())
} else if certUser.CACertPath() != "" {
file, err := os.ReadFile(certUser.CACertPath())
if err != nil {
return nil, err
}
caCert = file
}
return caCert, nil
}
func GetCertificate(certUser CertUser) (tls.Certificate, error) {
var tlsCert tls.Certificate
cert, err := ReadCert(certUser)
if err != nil {
return tls.Certificate{}, fmt.Errorf("failed to read client certificate: %w", err)
}
key, err := ReadKey(certUser)
if err != nil {
return tls.Certificate{}, fmt.Errorf("failed to read client key: %w", err)
}
tlsCert, err = getCertificate(cert, key)
if err != nil {
return tls.Certificate{}, fmt.Errorf("could not load keypair: %w", err)
}
return tlsCert, nil
}
func getCertificate(certBytes, keyBytes []byte) (tls.Certificate, error) {
var cert tls.Certificate
if certBytes == nil && keyBytes == nil {
return tls.Certificate{}, ce.ErrCertKeyNotFound
}
cert, err := tls.X509KeyPair(certBytes, keyBytes)
if err != nil {
return cert, fmt.Errorf("could not load keypair: %w", err)
}
return cert, nil
}
func GetTransport(certBytes, keyBytes, caCertBytes []byte, timeout time.Duration) (*http.Transport, error) {
transport := &http.Transport{ResponseHeaderTimeout: timeout}
if certBytes != nil && keyBytes != nil {
cert, err := getCertificate(certBytes, keyBytes)
if err != nil {
return transport, err
}
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
MinVersion: tls.VersionTLS12,
}
if caCertBytes != nil {
pool, err := certPool(caCertBytes)
if err != nil {
return transport, err
}
tlsConfig.RootCAs = pool
}
transport.TLSClientConfig = tlsConfig
}
return transport, nil
}
func certPool(caCert []byte) (*x509.CertPool, error) {
pool := x509.NewCertPool()
ok := pool.AppendCertsFromPEM(caCert)
if !ok {
return nil, fmt.Errorf("could not parse ca cert")
}
return pool, nil
}
type FeatureServiceCertUser struct {
}
func (c *FeatureServiceCertUser) ClientCert() string {
return Get().Clients.FeatureService.ClientCert
}
func (c *FeatureServiceCertUser) ClientKey() string {
return Get().Clients.FeatureService.ClientKey
}
func (c *FeatureServiceCertUser) CACert() string {
return Get().Clients.FeatureService.CACert
}
func (c *FeatureServiceCertUser) CACertPath() string {
return Get().Clients.FeatureService.CACertPath
}
func (c *FeatureServiceCertUser) ClientCertPath() string {
return Get().Clients.FeatureService.ClientCertPath
}
func (c *FeatureServiceCertUser) ClientKeyPath() string {
return Get().Clients.FeatureService.ClientKeyPath
}
func (c *FeatureServiceCertUser) Label() string { return "feature_service" }
type CandlepinCertUser struct {
}
func (c *CandlepinCertUser) ClientCert() string {
return Get().Clients.Candlepin.ClientCert
}
func (c *CandlepinCertUser) ClientKey() string {
return Get().Clients.Candlepin.ClientKey
}
func (c *CandlepinCertUser) CACert() string {
return Get().Clients.Candlepin.CACert
}
func (c *CandlepinCertUser) CACertPath() string {
return ""
}
func (c *CandlepinCertUser) ClientCertPath() string {
return ""
}
func (c *CandlepinCertUser) ClientKeyPath() string {
return ""
}
func (c *CandlepinCertUser) Label() string { return "candlepin" }