-
Notifications
You must be signed in to change notification settings - Fork 126
/
disk.go
144 lines (113 loc) · 3.67 KB
/
disk.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
// Copyright 2021-2024 Zenauth Ltd.
// SPDX-License-Identifier: Apache-2.0
package disk
import (
"context"
"fmt"
"io"
"path/filepath"
"go.uber.org/zap"
responsev1 "github.com/cerbos/cerbos/api/genpb/cerbos/response/v1"
"github.com/cerbos/cerbos/internal/config"
"github.com/cerbos/cerbos/internal/namer"
"github.com/cerbos/cerbos/internal/policy"
"github.com/cerbos/cerbos/internal/storage"
"github.com/cerbos/cerbos/internal/storage/index"
"github.com/cerbos/cerbos/internal/util"
)
const DriverName = "disk"
var (
_ storage.SourceStore = (*Store)(nil)
_ storage.Reloadable = (*Store)(nil)
)
func init() {
storage.RegisterDriver(DriverName, func(ctx context.Context, confW *config.Wrapper) (storage.Store, error) {
conf := new(Conf)
if err := confW.GetSection(conf); err != nil {
return nil, fmt.Errorf("failed to read disk configuration: %w", err)
}
return NewStore(ctx, conf)
})
}
type Store struct {
conf *Conf
idx index.Index
*storage.SubscriptionManager
}
func NewStore(ctx context.Context, conf *Conf) (*Store, error) {
dir, err := filepath.Abs(conf.Directory)
if err != nil {
return nil, fmt.Errorf("failed to determine absolute path of directory [%s]: %w", conf.Directory, err)
}
zap.S().Named("disk.store").Infof("Initializing disk store from %s", dir)
fsys, err := util.OpenDirectoryFS(dir)
if err != nil {
return nil, err
}
idx, err := index.Build(ctx, fsys, index.WithSourceAttributes(policy.SourceDriver(DriverName)))
if err != nil {
return nil, err
}
s := &Store{
conf: conf,
idx: idx,
SubscriptionManager: storage.NewSubscriptionManager(ctx),
}
if conf.WatchForChanges && !util.IsArchiveFile(dir) {
if err := watchDir(ctx, dir, s.idx, s.SubscriptionManager, defaultCooldownPeriod); err != nil {
return nil, err
}
}
return s, nil
}
func NewFromIndex(idx index.Index) (*Store, error) {
conf := &Conf{}
if err := config.GetSection(conf); err != nil {
return nil, err
}
return NewFromIndexWithConf(idx, conf), nil
}
func NewFromIndexWithConf(idx index.Index, conf *Conf) *Store {
return &Store{idx: idx, conf: conf}
}
func (s *Store) Driver() string {
return DriverName
}
func (s *Store) GetFirstMatch(_ context.Context, candidates []namer.ModuleID) (*policy.CompilationUnit, error) {
return s.idx.GetFirstMatch(candidates)
}
func (s *Store) GetCompilationUnits(_ context.Context, ids ...namer.ModuleID) (map[namer.ModuleID]*policy.CompilationUnit, error) {
return s.idx.GetCompilationUnits(ids...)
}
func (s *Store) GetDependents(_ context.Context, ids ...namer.ModuleID) (map[namer.ModuleID][]namer.ModuleID, error) {
return s.idx.GetDependents(ids...)
}
func (s *Store) InspectPolicies(ctx context.Context, _ storage.ListPolicyIDsParams) (map[string]*responsev1.InspectPoliciesResponse_Result, error) {
return s.idx.InspectPolicies(ctx)
}
func (s *Store) ListPolicyIDs(ctx context.Context, _ storage.ListPolicyIDsParams) ([]string, error) {
return s.idx.ListPolicyIDs(ctx)
}
func (s *Store) ListSchemaIDs(ctx context.Context) ([]string, error) {
return s.idx.ListSchemaIDs(ctx)
}
func (s *Store) LoadSchema(ctx context.Context, url string) (io.ReadCloser, error) {
return s.idx.LoadSchema(ctx, url)
}
func (s *Store) LoadPolicy(ctx context.Context, file ...string) ([]*policy.Wrapper, error) {
return s.idx.LoadPolicy(ctx, file...)
}
func (s *Store) RepoStats(ctx context.Context) storage.RepoStats {
return s.idx.RepoStats(ctx)
}
func (s *Store) Reload(ctx context.Context) error {
evts, err := s.idx.Reload(ctx)
if err != nil {
return fmt.Errorf("failed to reload the index: %w", err)
}
s.NotifySubscribers(evts...)
return nil
}
func (s *Store) Close() error {
return s.idx.Close()
}