-
Notifications
You must be signed in to change notification settings - Fork 15
/
root_certificate_provider.go
244 lines (211 loc) · 6.62 KB
/
root_certificate_provider.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// SPDX-License-Identifier: Apache-2.0
package services
import (
"context"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"fmt"
"go.opentelemetry.io/otel/codes"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
"go.opentelemetry.io/otel/trace"
"io"
"k8s.io/utils/clock"
"net/http"
"os"
"sync"
"time"
)
type RootCertificateProviderService interface {
ProvideCertificates(ctx context.Context) ([]*x509.Certificate, error)
}
type CompositeRootCertificateProviderService struct {
Providers []RootCertificateProviderService
}
func (c CompositeRootCertificateProviderService) ProvideCertificates(ctx context.Context) ([]*x509.Certificate, error) {
var certs []*x509.Certificate
for _, provider := range c.Providers {
providerCerts, err := provider.ProvideCertificates(ctx)
if err != nil {
return nil, err
}
certs = append(certs, providerCerts...)
}
return certs, nil
}
type FileRootCertificateProviderService struct {
FilePaths []string
}
func (s FileRootCertificateProviderService) ProvideCertificates(context.Context) (certs []*x509.Certificate, err error) {
for _, pemFile := range s.FilePaths {
//#nosec G304 - only files specified by the person running the application will be loaded
bytes, e := os.ReadFile(pemFile)
if e != nil {
return nil, e
}
intermediateCerts, e := parseCertificates(bytes)
if e != nil {
return nil, e
}
certs = append(certs, intermediateCerts...)
}
return
}
func parseCertificates(pemData []byte) ([]*x509.Certificate, error) {
var certs []*x509.Certificate
for {
cert, rest, err := parseCertificate(pemData)
if err != nil {
return nil, err
}
if cert == nil {
break
}
certs = append(certs, cert)
pemData = rest
}
return certs, nil
}
func parseCertificate(pemData []byte) (cert *x509.Certificate, rest []byte, err error) {
block, rest := pem.Decode(pemData)
if block == nil {
return
}
if block.Type != "CERTIFICATE" {
return
}
cert, err = x509.ParseCertificate(block.Bytes)
if err != nil {
cert = nil
return
}
return
}
type CachingRootCertificateProviderService struct {
sync.Mutex
delegate RootCertificateProviderService
ttl time.Duration
clock clock.PassiveClock
expiry time.Time
certs []*x509.Certificate
}
func NewCachingRootCertificateProviderService(delegate RootCertificateProviderService, ttl time.Duration, clock clock.PassiveClock) *CachingRootCertificateProviderService {
return &CachingRootCertificateProviderService{
delegate: delegate,
ttl: ttl,
clock: clock,
expiry: clock.Now(),
}
}
func (c *CachingRootCertificateProviderService) ProvideCertificates(ctx context.Context) ([]*x509.Certificate, error) {
c.Lock()
defer c.Unlock()
if c.clock.Now().After(c.expiry) {
var err error
c.certs, err = c.delegate.ProvideCertificates(ctx)
if err != nil {
return nil, err
}
c.expiry = c.clock.Now().Add(c.ttl)
}
return c.certs, nil
}
type OpcpRootCertificateReturnType struct {
RootCertificateCollection OpcpRootCertificateCollectionType `json:"RootCertificateCollection"`
}
type OpcpRootCertificateCollectionType struct {
RootCertificates []OpcpRootCertificateType `json:"rootCertificates"`
}
type OpcpRootCertificateType struct {
RootCertificateId string `json:"rootCertificateId"`
DN string `json:"distinguishedName"`
CACertificate string `json:"caCertificate"`
CommonName string `json:"commonName"`
RootAuthorityKeyIdentifier string `json:"rootAuthorityKeyIdentifier"`
RootIssuerSerialNumber string `json:"rootIssuerSerialNumber"`
ValidFrom string `json:"validFrom"`
ValidTo string `json:"validTo"`
OrganizationName string `json:"organizationName"`
CertificateRevocationList string `json:"certificateRevocationList"`
CrossCertificatePair string `json:"crossCertificatePair"`
LabeledURI string `json:"labeledUri"`
RootType string `json:"rootType"`
Fingerprint string `json:"fingerprint"`
}
type OpcpRootCertificateProviderService struct {
BaseURL string
TokenService HttpTokenService
HttpClient *http.Client
}
func (s OpcpRootCertificateProviderService) ProvideCertificates(ctx context.Context) (certs []*x509.Certificate, err error) {
body, err := s.retrieveCertificatesFromUrlWithRetry(ctx)
if err != nil {
return nil, fmt.Errorf("failed to retrieve certificates from url: %w", err)
}
var rootCertificates OpcpRootCertificateReturnType
err = json.Unmarshal(body, &rootCertificates)
if err != nil {
return nil, err
}
for _, certDetails := range rootCertificates.RootCertificateCollection.RootCertificates {
certBytes, err := base64.StdEncoding.DecodeString(certDetails.CACertificate)
if err != nil {
return nil, fmt.Errorf("failed to decode certificate %s: %w", certDetails.RootCertificateId, err)
}
cert, err := x509.ParseCertificate(certBytes)
if err != nil {
return nil, fmt.Errorf("failed to parse certificate %s: %w", certDetails.RootCertificateId, err)
}
certs = append(certs, cert)
}
return
}
func (s OpcpRootCertificateProviderService) retrieveCertificatesFromUrlWithRetry(ctx context.Context) ([]byte, error) {
span := trace.SpanFromContext(ctx)
newCtx, span := span.TracerProvider().Tracer("manager").Start(ctx, "get_certificates_from_url")
defer span.End()
certs, err := s.retrieveCertificatesFromUrl(newCtx, false)
if err != nil {
span.SetAttributes(semconv.HTTPResendCount(1))
certs, err = s.retrieveCertificatesFromUrl(newCtx, true)
if err != nil {
span.SetStatus(codes.Error, "retries exhausted")
span.RecordError(err)
}
}
return certs, err
}
func (s OpcpRootCertificateProviderService) retrieveCertificatesFromUrl(ctx context.Context, retry bool) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/v1/root/rootCerts", s.BaseURL), nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/json")
token, err := s.TokenService.GetToken(ctx, retry)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
req.Header.Set("X-API-Key", token)
resp, err := s.HttpClient.Do(req)
if err != nil {
return nil, err
} else if resp.StatusCode != http.StatusOK {
return nil, HttpError(resp.StatusCode)
}
defer func() {
_ = resp.Body.Close()
}()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
type X509RootCertificateProviderService struct {
Certificates []*x509.Certificate
}
func (x X509RootCertificateProviderService) ProvideCertificates(ctx context.Context) ([]*x509.Certificate, error) {
return x.Certificates, nil
}