-
Notifications
You must be signed in to change notification settings - Fork 18
/
evaluator_factory.go
84 lines (68 loc) · 2.24 KB
/
evaluator_factory.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
//Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
//Licensed under the Universal Permissive License (UPL) Version 1.0 as shown at http://oss.oracle.com/licenses/upl.
package eval
import (
"github.com/teramoby/speedle-plus/pkg/cfg"
"github.com/teramoby/speedle-plus/pkg/store"
adsapi "github.com/teramoby/speedle-plus/api/ads"
"github.com/teramoby/speedle-plus/api/pms"
log "github.com/sirupsen/logrus"
)
//New creates a policy evaluator based on the given configuration file
func New(configFile string) (InternalEvaluator, error) {
conf, err := cfg.ReadConfig(configFile)
if err != nil {
return nil, err
}
return NewFromConfig(conf)
}
// NewFromFile loads policies from a policy file, and returns an evaluator instance
func NewFromFile(fileLoc string, isWatch bool) (adsapi.PolicyEvaluator, error) {
storeConfig := cfg.StoreConfig{
StoreType: "file",
StoreProps: map[string]interface{}{
"FileLocation": fileLoc,
},
}
// For file store, watchuing a policy store is disabled
return NewFromConfig(&cfg.Config{
StoreConfig: &storeConfig,
EnableWatch: isWatch,
})
}
//NewFromConfig creates a policy evaluator based on the given configuration file
func NewFromConfig(conf *cfg.Config) (InternalEvaluator, error) {
s, err := store.NewStore(conf.StoreConfig.StoreType, conf.StoreConfig.StoreProps)
if err != nil {
return nil, err
}
return NewWithStore(conf, s)
}
// NewWithStore creates a policy evaluator with policy store
func NewWithStore(conf *cfg.Config, s pms.PolicyStoreManagerADS) (InternalEvaluator, error) {
ps, err := s.ReadPolicyStore()
if err != nil {
return nil, err
}
var updateChan pms.StorageChangeChannel
if conf.EnableWatch {
log.Info("Watch policy store.")
updateChan, err = s.Watch()
if err != nil {
return nil, err
}
}
runtimePolicyStore := NewRuntimePolicyStore()
runtimePolicyStore.init(ps, conf.FuncsvcEndpoint)
p := &PolicyEvalImpl{
RuntimePolicyStore: runtimePolicyStore,
Store: s,
}
// start a goroutine watching to the channel for update events and
// refresh runtime cache accordingly once receiving any events
if updateChan != nil {
go p.updateRuntimeCacheWithStoreChange(updateChan)
}
p.cleanExpiredFunctionResultPeriodically()
return p, nil
}