/
boltcertcache.go
80 lines (73 loc) · 2.5 KB
/
boltcertcache.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
package db
// Copyright 2021 Matthew R. Wilson <mwilson@mattwilson.org>
//
// This file is part of virtual1403
// <https://github.com/racingmars/virtual1403>.
//
// virtual1403 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// virtual1403 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with virtual1403. If not, see <https://www.gnu.org/licenses/>.
import (
"context"
"github.com/boltdb/bolt"
"golang.org/x/crypto/acme/autocert"
)
// In addition to our application's DB interface, our BoltDB implementation
// will implement the interface from golang.org/x/crypto/acme/autocert#Cache
// so that we can store SSL certificate info for autocert.
// Get returns a certificate data for the specified key. If there's no such
// key, Get returns ErrCacheMiss.
func (db *boltimpl) Get(_ context.Context, key string) ([]byte, error) {
var result []byte
if err := db.bdb.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(autocertBucketName))
v := bucket.Get([]byte(key))
if v == nil {
return autocert.ErrCacheMiss
}
result = make([]byte, len(v))
copy(result, v)
return nil
}); err != nil {
return nil, err
}
return result, nil
}
// Put stores the data in the cache under the specified key. Underlying
// implementations may use any data storage format, as long as the reverse
// operation, Get, results in the original data.
func (db *boltimpl) Put(_ context.Context, key string, data []byte) error {
if err := db.bdb.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(autocertBucketName))
if err := bucket.Put([]byte(key), data); err != nil {
return err
}
return nil
}); err != nil {
return err
}
return nil
}
// Delete removes a certificate data from the cache under the specified key.
// If there's no such key in the cache, Delete returns nil.
func (db *boltimpl) Delete(_ context.Context, key string) error {
if err := db.bdb.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(autocertBucketName))
if err := bucket.Delete([]byte(key)); err != nil {
return err
}
return nil
}); err != nil {
return err
}
return nil
}