-
Notifications
You must be signed in to change notification settings - Fork 23
/
multicert_disk_cache.go
208 lines (171 loc) · 6.57 KB
/
multicert_disk_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
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
// Copyright 2020 Google LLC
//
// 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
//
// https://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 certmanager
import (
"bytes"
"errors"
"log"
"path"
"github.com/gofrs/flock"
"github.com/google/renameio"
"github.com/google/webpackager/certchain"
"github.com/google/webpackager/certchain/certchainutil"
"github.com/hashicorp/go-multierror"
)
var (
errEmptyCertDir = errors.New("certmanager: empty CertDir")
errEmptyCertFile = errors.New("certmanager: empty CertFile")
errEmptyDigest = errors.New("certmanager: empty Digest")
errEmptyLockFile = errors.New("certmanager: empty LockFile")
errEmptyOCSPFile = errors.New("certmanager: empty OCSPFile")
)
// MultiCertDiskCache is a Cache on a local filesystem. It writes the
// certificate chain in the PEM format and the OCSP response in the DER format
// to separate files as specified by MultiCertDiskCacheConfig. It uses the
// digest of the certificate chain as the basename for the certificate and OCSP
// files.
type MultiCertDiskCache struct {
MultiCertDiskCacheConfig
}
var _ Cache = (*MultiCertDiskCache)(nil)
// MultiCertDiskCacheConfig configures DiskCache.
type MultiCertDiskCacheConfig struct {
// CertDir locates the directory to write the certificate chain to.
// If CertDir is empty, NewMultiCertDiskCache returns an error.
CertDir string
// LatestCertFile specifies the filename to be used for the latest
// version of the certificate. The file will be located in CertDir.
// If LatestCertFile is empty, NewMultiCertDiskCache returns an error.
LatestCertFile string
// LatestOCSPFile specifies the filename to be used for the latest
// version of the OCSP. The file will be located in CertDir.
// If LatestOCSPFile is empty, NewMultiCertDiskCache returns an error.
LatestOCSPFile string
// LockFile locates the lock file. Must be non-empty. The file will be
// located in CertDir. If LockFile is empty, NewMultiCertDiskCache returns
// an error.
LockFile string
}
// NewMultiCertDiskCache creates and initializes a new MultiCertDiskCache.
func NewMultiCertDiskCache(config MultiCertDiskCacheConfig) (*MultiCertDiskCache, error) {
var errs *multierror.Error
if config.CertDir == "" {
errs = multierror.Append(errs, errEmptyCertDir)
}
if config.LockFile == "" {
errs = multierror.Append(errs, errEmptyLockFile)
}
if config.LatestCertFile == "" {
errs = multierror.Append(errs, errEmptyCertFile)
}
if config.LatestOCSPFile == "" {
errs = multierror.Append(errs, errEmptyOCSPFile)
}
if err := errs.ErrorOrNil(); err != nil {
return nil, err
}
return &MultiCertDiskCache{config}, nil
}
// Read reads the certificate chain and the OCSP response from local files
// and reproduces an AugmentedChain. Read uses digest as the base filename
// used to retrieve the elements needed to construct the AugmentedChain.
// Read returns a multierror.Error (hashicorp/go-multierror) to report as many
// problems as possible.
func (d *MultiCertDiskCache) Read(digest string) (*certchain.AugmentedChain, error) {
var errs *multierror.Error
if digest == "" {
errs = multierror.Append(errs, errEmptyDigest)
}
lock := flock.New(path.Join(d.CertDir, d.LockFile))
errs = multierror.Append(errs, lock.RLock())
if err := errs.ErrorOrNil(); err != nil {
return nil, err
}
cert := path.Join(d.CertDir, digest+".pem")
ocsp := path.Join(d.CertDir, digest+".ocsp")
rawChain, err := certchainutil.ReadRawChainFile(cert)
if err != nil {
errs = multierror.Append(errs, ErrNotFound)
errs = multierror.Append(errs, err)
}
ocspResp, err := certchainutil.ReadOCSPRespFile(ocsp)
if err != nil {
errs = multierror.Append(errs, ErrNotFound)
errs = multierror.Append(errs, err)
}
var augChain *certchain.AugmentedChain
if errs.ErrorOrNil() == nil {
augChain = certchain.NewAugmentedChain(rawChain, ocspResp, nil)
}
errs = multierror.Append(errs, lock.Unlock())
return augChain, errs.ErrorOrNil()
}
// ReadLatest returns the latest version of the AugmentedChain in the cache,
// ErrNotFound otherwise.
func (d *MultiCertDiskCache) ReadLatest() (*certchain.AugmentedChain, error) {
var errs *multierror.Error
lock := flock.New(path.Join(d.CertDir, d.LockFile))
errs = multierror.Append(errs, lock.RLock())
if err := errs.ErrorOrNil(); err != nil {
return nil, err
}
cert := path.Join(d.CertDir, d.LatestCertFile)
ocsp := path.Join(d.CertDir, d.LatestOCSPFile)
rawChain, err := certchainutil.ReadRawChainFile(cert)
errs = multierror.Append(errs, err)
ocspResp, err := certchainutil.ReadOCSPRespFile(ocsp)
errs = multierror.Append(errs, err)
var augChain *certchain.AugmentedChain
if errs.ErrorOrNil() == nil {
augChain = certchain.NewAugmentedChain(rawChain, ocspResp, nil)
}
errs = multierror.Append(errs, lock.Unlock())
return augChain, errs.ErrorOrNil()
}
// Write writes the certificate chain and the OCSP response from ac into local
// files. It returns a multierror.Error (hashicorp/go-multierror) to report as
// many problems as possible.
func (d *MultiCertDiskCache) Write(ac *certchain.AugmentedChain) error {
var errs *multierror.Error
log.Printf("Writing to cache in %s\n", d.CertDir)
lock := flock.New(path.Join(d.CertDir, d.LockFile))
errs = multierror.Append(errs, lock.Lock())
if err := errs.ErrorOrNil(); err != nil {
return err
}
errs = multierror.Append(errs, d.writeCert(ac))
errs = multierror.Append(errs, d.writeOCSP(ac))
errs = multierror.Append(errs, lock.Unlock())
return errs.ErrorOrNil()
}
func (d *MultiCertDiskCache) writeCert(ac *certchain.AugmentedChain) error {
buf := new(bytes.Buffer)
if err := ac.WritePEM(buf); err != nil {
return err
}
certPath := path.Join(d.CertDir, ac.Digest+".pem")
if err := renameio.WriteFile(certPath, buf.Bytes(), 0600); err != nil {
return err
}
latest := path.Join(d.CertDir, d.LatestCertFile)
return renameio.WriteFile(latest, buf.Bytes(), 0600)
}
func (d *MultiCertDiskCache) writeOCSP(ac *certchain.AugmentedChain) error {
ocspPath := path.Join(d.CertDir, ac.Digest+".ocsp")
if err := renameio.WriteFile(ocspPath, ac.OCSPResp.Raw, 0600); err != nil {
return err
}
latest := path.Join(d.CertDir, d.LatestOCSPFile)
return renameio.WriteFile(latest, ac.OCSPResp.Raw, 0600)
}