-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstore.go
More file actions
342 lines (289 loc) · 9.42 KB
/
store.go
File metadata and controls
342 lines (289 loc) · 9.42 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// SPDX-FileCopyrightText: Copyright 2025 Carabiner Systems, Inc
// SPDX-License-Identifier: Apache-2.0
package policy
import (
"crypto/sha256"
"errors"
"fmt"
"strings"
intoto "github.com/in-toto/attestation/go/v1"
"github.com/sirupsen/logrus"
api "github.com/carabiner-dev/policy/api/v1"
)
// Storage backend is an interface that fronts systems that store and index policies
type StorageBackend interface {
StoreReference(api.RemoteReference) error
StoreReferenceWithReturn(api.RemoteReference) (*api.PolicySet, *api.Policy, *api.PolicyGroup, error)
GetReferencedPolicy(api.RemoteReference) (*api.Policy, error)
GetReferencedGroup(api.RemoteReference) (*api.PolicyGroup, error)
}
func newRefStore() *refStore {
return &refStore{
references: map[string]api.RemoteReference{},
policySets: map[string]*api.PolicySet{},
policies: map[string]*api.Policy{},
policyGroups: map[string]*api.PolicyGroup{},
ids: map[string]string{},
urls: map[string]string{},
hashes: map[string]string{},
}
}
var _ StorageBackend = &refStore{}
type refStore struct {
references map[string]api.RemoteReference
policySets map[string]*api.PolicySet
policies map[string]*api.Policy
policyGroups map[string]*api.PolicyGroup
ids map[string]string
urls map[string]string
hashes map[string]string
}
// StoreReference stores a reference and adds it to the index
func (rs *refStore) StoreReference(ref api.RemoteReference) error {
_, _, _, err := rs.StoreReferenceWithReturn(ref)
return err
}
// StoreReferenceWithReturn stores a policy reference returning the parsed
// Policy or PolicySet from the ref content.
func (rs *refStore) StoreReferenceWithReturn(ref api.RemoteReference) (*api.PolicySet, *api.Policy, *api.PolicyGroup, error) {
if ref.GetLocation() == nil {
return nil, nil, nil, fmt.Errorf("unable to store policy no location data found")
}
// If the policy content is nil at some point we could try to fetch it
// but for now we use the fetcher as it it can fet in parallel.
if ref.GetLocation().GetContent() == nil {
return nil, nil, nil, fmt.Errorf("unable to store policy, content is empty")
}
if ref.GetLocation().GetDigest() == nil {
ref.GetLocation().Digest = map[string]string{}
}
// Hash the policy contents, this will be the main storage key
h := sha256.New()
h.Write(ref.GetLocation().GetContent())
contentHash := fmt.Sprintf("%x", h.Sum(nil))
// If the ref is missing its sha256 digest, generate it
if _, ok := ref.GetLocation().GetDigest()[string(intoto.AlgorithmSHA256)]; !ok {
ref.GetLocation().GetDigest()[string(intoto.AlgorithmSHA256)] = contentHash
} else if contentHash != ref.GetLocation().GetDigest()[string(intoto.AlgorithmSHA256)] {
return nil, nil, nil, fmt.Errorf("policy sha256 digest does not match content")
}
// TODO(puerco) Here the reference should be augmented if it already exists
rs.references[contentHash] = ref
uri := ref.GetLocation().GetDownloadLocation()
if uri == "" {
uri = ref.GetLocation().GetUri()
}
if uri != "" {
rs.urls[uri] = contentHash
}
for algo, val := range ref.GetLocation().GetDigest() {
rs.hashes[fmt.Sprintf("%s:%s", algo, val)] = contentHash
}
// Parse the data and assign whatever comes out of it
set, pcy, grp, _, err := NewParser().ParseVerifyPolicyOrSetOrGroup(ref.GetLocation().GetContent())
// Here, we record the policy source url in the origin. It's not great
// to be doing it here but otherwise the cached data will not have it.
sourceURI := ref.GetLocation().GetDownloadLocation()
if sourceURI == "" {
sourceURI = ref.GetLocation().GetUri()
}
switch {
case set != nil:
if err := rs.registerPolicySet(contentHash, set); err != nil {
return nil, nil, nil, fmt.Errorf("indexing policy set: %w", err)
}
set.GetMeta().GetOrigin().DownloadLocation = sourceURI
if set.GetMeta().GetOrigin().Uri == "" {
set.GetMeta().GetOrigin().Uri = sourceURI
}
case pcy != nil:
if err := rs.registerPolicy(contentHash, pcy); err != nil {
return nil, nil, nil, fmt.Errorf("indexing policy: %w", err)
}
pcy.GetMeta().GetOrigin().DownloadLocation = sourceURI
if pcy.GetMeta().GetOrigin().Uri == "" {
pcy.GetMeta().GetOrigin().Uri = sourceURI
}
case grp != nil:
if err := rs.registerGroup(contentHash, grp); err != nil {
return nil, nil, nil, fmt.Errorf("indexing group: %w", err)
}
grp.GetMeta().GetOrigin().DownloadLocation = sourceURI
if grp.GetMeta().GetOrigin().Uri == "" {
grp.GetMeta().GetOrigin().Uri = sourceURI
}
case err != nil:
return nil, nil, nil, err
}
return set, pcy, grp, err
}
// registerPolicy register a policy, not form a set.
func (rs *refStore) registerPolicy(contentHash string, pcy *api.Policy) error {
if pcy.GetId() != "" {
if currentHash, ok := rs.ids[pcy.GetId()]; ok {
if currentHash != contentHash {
return fmt.Errorf("duplicate policy ID %q with different hash", pcy.GetId())
}
}
}
rs.ids[pcy.GetId()] = contentHash
rs.policies[contentHash] = pcy
return nil
}
// registerPolicy register a policy, not form a set.
func (rs *refStore) registerGroup(contentHash string, grp *api.PolicyGroup) error {
if grp.GetId() != "" {
if currentHash, ok := rs.ids[grp.GetId()]; ok {
if currentHash != contentHash {
return fmt.Errorf("duplicate policy group ID %q with different hash", grp.GetId())
}
}
}
rs.ids[grp.GetId()] = contentHash
rs.policyGroups[contentHash] = grp
return nil
}
// registerPolicySet registers the policy in the storage index
func (rs *refStore) registerPolicySet(contentHash string, set *api.PolicySet) error {
if set == nil {
return errors.New("attempt to index null policy set")
}
rs.policySets[contentHash] = set
// Store all the policy IDs in the referenced set
for _, p := range set.GetPolicies() {
// If a policy does not have an id, it cannot be referenced in a
// set, so we skip indexing it.
if p.GetId() == "" {
continue
}
// Check we don't already have the policy ID in another file
if currentHash, ok := rs.ids[p.GetId()]; ok {
if currentHash != contentHash {
return fmt.Errorf("duplicate policy id %q with different hash", p.GetId())
}
}
rs.ids[p.GetId()] = contentHash
}
return nil
}
// This retrieves a policy from the sets by its source URL
func (rs *refStore) GetPolicyByURL(url string) *api.Policy {
sha, ok := rs.urls[url]
if !ok {
return nil
}
return rs.GetPolicyBySHA256(sha)
}
// This retrieves a policy from the sets by its source URL
func (rs *refStore) GetPolicyGroupByURL(url string) *api.PolicyGroup {
sha, ok := rs.urls[url]
if !ok {
return nil
}
return rs.GetPolicyGroupBySHA256(sha)
}
// This retrieves a policy from the sets by its ID
func (rs *refStore) GetPolicyByID(id string) *api.Policy {
sha, ok := rs.ids[id]
if !ok || id == "" {
return nil
}
if _, ok := rs.policies[sha]; ok {
return rs.policies[sha]
}
if _, ok := rs.policyGroups[sha]; ok {
return nil
}
if _, ok := rs.policySets[sha]; !ok {
return nil
}
for _, p := range rs.policySets[sha].GetPolicies() {
if p.GetId() == id {
return p
}
}
// This should never happen as it would point to a corrupt index
logrus.Warnf("Indexed policy-id %q points to PolicySet that does not have it", id)
return nil
}
// This retrieves a policy from the sets by its ID
func (rs *refStore) GetPolicyGroupByID(id string) *api.PolicyGroup {
sha, ok := rs.ids[id]
if !ok || id == "" {
return nil
}
if _, ok := rs.policyGroups[sha]; ok {
return rs.policyGroups[sha]
}
if _, ok := rs.policies[sha]; ok {
return nil
}
if _, ok := rs.policySets[sha]; !ok {
return nil
}
for _, p := range rs.policySets[sha].GetGroups() {
if p.GetId() == id {
return p
}
}
// This should never happen as it would point to a corrupt index
logrus.Warnf("Indexed PolicyGroup id %q points to PolicySet that does not have it", id)
return nil
}
func (rs *refStore) GetPolicyBySHA256(sha string) *api.Policy {
if _, ok := rs.policies[sha]; ok {
return rs.policies[sha]
}
if _, ok := rs.policySets[sha]; !ok {
return nil
}
return nil
}
func (rs *refStore) GetPolicySetBySHA256(sha string) *api.PolicySet {
sha = strings.TrimPrefix(sha, "sha256:")
if v, ok := rs.policySets[sha]; ok {
return v
}
return nil
}
func (rs *refStore) GetPolicyGroupBySHA256(sha string) *api.PolicyGroup {
sha = strings.TrimPrefix(sha, "sha256:")
if v, ok := rs.policyGroups[sha]; ok {
return v
}
return nil
}
func (rs *refStore) GetRemoteRefBySHA256(sha string) api.RemoteReference {
if v, ok := rs.references[sha]; ok {
return v
}
return nil
}
// GetReferencedPolicy
func (rs *refStore) GetReferencedPolicy(ref api.RemoteReference) (*api.Policy, error) {
// Try finding the policy by indexed ID
if p := rs.GetPolicyByID(ref.GetId()); p != nil {
return p, nil
}
if p := rs.GetPolicyByURL(ref.GetSourceURL()); p != nil {
return p, nil
}
// Can't locate it through any other means
return nil, nil
}
// GetReferencedPolicy
func (rs *refStore) GetReferencedGroup(ref api.RemoteReference) (*api.PolicyGroup, error) {
// Try finding the policy by indexed ID
if p := rs.GetPolicyGroupByID(ref.GetId()); p != nil {
return p, nil
}
if p := rs.GetPolicyGroupByURL(ref.GetSourceURL()); p != nil {
return p, nil
}
// Can't locate it through any other means
return nil, nil
}
// This error is thrown if a fetchedRef lists a policy ID not
// contained in its policy or policy set. If it's ever thrown
// it is definitely a bug:
var ErrParseInconsistency = errors.New("internal error: fetched reference ID and policy ID mismatch")