/
signingRecords.go
155 lines (134 loc) · 3.73 KB
/
signingRecords.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
package mongodb
import (
"context"
"errors"
"time"
"github.com/hashicorp/go-multierror"
"github.com/plgd-dev/hub/v2/certificate-authority/store"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
const signingRecordsCol = "signedCertificateRecords"
var ErrCannotRemoveSigningRecord = errors.New("cannot remove signing record")
func (s *Store) CreateSigningRecord(ctx context.Context, signingRecord *store.SigningRecord) error {
if err := signingRecord.Validate(); err != nil {
return err
}
_, err := s.Collection(signingRecordsCol).InsertOne(ctx, signingRecord)
if err != nil {
return err
}
return nil
}
func (s *Store) UpdateSigningRecord(_ context.Context, signingRecord *store.SigningRecord) error {
if err := signingRecord.Validate(); err != nil {
return err
}
s.bulkWriter.Push(signingRecord)
return nil
}
func toCommonNameQueryFilter(owner string, commonName string) bson.D {
f := bson.D{
{Key: store.CommonNameKey, Value: commonName},
}
if owner != "" {
f = append(f, bson.E{Key: store.OwnerKey, Value: owner})
}
return f
}
func toDeviceIDQueryFilter(owner string, deviceID string) bson.D {
f := bson.D{
{Key: store.DeviceIDKey, Value: deviceID},
}
if owner != "" {
f = append(f, bson.E{Key: store.OwnerKey, Value: owner})
}
return f
}
func toIDQueryFilter(owner string, id string) bson.D {
f := bson.D{
{Key: "_id", Value: id},
}
if owner != "" {
f = append(f, bson.E{Key: store.OwnerKey, Value: owner})
}
return f
}
func toSigningRecordsQueryFilter(owner string, queries *store.SigningRecordsQuery) interface{} {
or := []bson.D{}
for _, q := range queries.GetIdFilter() {
or = append(or, toIDQueryFilter(owner, q))
}
for _, q := range queries.GetCommonNameFilter() {
or = append(or, toCommonNameQueryFilter(owner, q))
}
for _, q := range queries.GetDeviceIdFilter() {
or = append(or, toDeviceIDQueryFilter(owner, q))
}
switch len(or) {
case 0:
if owner == "" {
return bson.D{}
}
return bson.D{
{Key: store.OwnerKey, Value: owner},
}
case 1:
return or[0]
}
return bson.M{"$or": or}
}
func (s *Store) DeleteSigningRecords(ctx context.Context, owner string, query *store.DeleteSigningRecordsQuery) (int64, error) {
q := store.SigningRecordsQuery{
IdFilter: query.GetIdFilter(),
DeviceIdFilter: query.GetDeviceIdFilter(),
}
res, err := s.Collection(signingRecordsCol).DeleteMany(ctx, toSigningRecordsQueryFilter(owner, &q))
if err != nil {
return -1, multierror.Append(ErrCannotRemoveSigningRecord, err)
}
return res.DeletedCount, nil
}
func (s *Store) DeleteNonDeviceExpiredRecords(ctx context.Context, now time.Time) (int64, error) {
t := now.UnixNano()
res, err := s.Collection(signingRecordsCol).DeleteMany(ctx, bson.M{
store.CredentialKey + "." + store.ValidUntilDateKey: bson.M{"$lt": t},
store.DeviceIDKey: bson.M{"$exists": false},
})
if err != nil {
return -1, multierror.Append(ErrCannotRemoveSigningRecord, err)
}
return res.DeletedCount, nil
}
func (s *Store) LoadSigningRecords(ctx context.Context, owner string, query *store.SigningRecordsQuery, h store.LoadSigningRecordsFunc) error {
col := s.Collection(signingRecordsCol)
iter, err := col.Find(ctx, toSigningRecordsQueryFilter(owner, query))
if errors.Is(err, mongo.ErrNilDocument) {
return nil
}
if err != nil {
return err
}
i := SigningRecordsIterator{
iter: iter,
}
err = h(ctx, &i)
errClose := iter.Close(ctx)
if err == nil {
return errClose
}
return err
}
type SigningRecordsIterator struct {
iter *mongo.Cursor
}
func (i *SigningRecordsIterator) Next(ctx context.Context, s *store.SigningRecord) bool {
if !i.iter.Next(ctx) {
return false
}
err := i.iter.Decode(s)
return err == nil
}
func (i *SigningRecordsIterator) Err() error {
return i.iter.Err()
}