-
Notifications
You must be signed in to change notification settings - Fork 194
/
loader.go
140 lines (128 loc) · 3.89 KB
/
loader.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
package policy
import (
"context"
"errors"
"fmt"
"github.com/aquasecurity/trivy/pkg/fanal/types"
mp "github.com/aquasecurity/trivy/pkg/policy"
"github.com/bluele/gcache"
"github.com/go-logr/logr"
"golang.org/x/xerrors"
"os"
"path/filepath"
ctrl "sigs.k8s.io/controller-runtime"
"strings"
"sync"
"time"
)
const (
bundlePolicies = "bundlePolicies"
bundlePath = "bundlePath"
)
type Loader interface {
GetPoliciesAndBundlePath() ([]string, []string, error)
}
type policyLoader struct {
PolicyRepo string
mutex sync.RWMutex
cache gcache.Cache
expiration *time.Duration
options []mp.Option
logger logr.Logger
RegistryOptions types.RegistryOptions
}
func NewPolicyLoader(pr string, cache gcache.Cache, registryOptions types.RegistryOptions, opts ...mp.Option) Loader {
expiration := 24 * time.Hour
return &policyLoader{
PolicyRepo: pr,
cache: cache,
options: opts,
expiration: &expiration,
logger: ctrl.Log.WithName("policyLoader"),
RegistryOptions: registryOptions,
}
}
func (pl *policyLoader) GetPoliciesAndBundlePath() ([]string, []string, error) {
log := pl.logger.WithName("Get misconfig bundle policies")
var policies []string
var bundlePaths []string
var ok bool
plc, bndl, err := pl.getPoliciesFromCache()
if err != nil {
if !errors.Is(err, gcache.KeyNotFoundError) {
return []string{}, []string{}, err
}
policies, bundlePaths, err = pl.LoadPoliciesAndCommands()
if err != nil {
log.V(1).Error(err, "failed to load policies")
return []string{}, []string{}, nil
}
return policies, bundlePaths, nil
}
if policies, ok = plc.([]string); !ok {
return []string{}, []string{}, fmt.Errorf("failed to get policies from cache")
}
if bundlePaths, ok = bndl.([]string); !ok {
return []string{}, []string{}, fmt.Errorf("failed to get bundlePath from cache")
}
return policies, bundlePaths, nil
}
func (pl *policyLoader) getPoliciesFromCache() (interface{}, interface{}, error) {
pl.mutex.RLock()
defer pl.mutex.RUnlock()
policies, err := pl.cache.Get(bundlePolicies)
if err != nil {
return nil, nil, err
}
bundlePath, err := pl.cache.Get(bundlePath)
if err != nil {
return nil, nil, err
}
return policies, bundlePath, nil
}
func (pl *policyLoader) LoadPoliciesAndCommands() ([]string, []string, error) {
pl.mutex.Lock()
defer pl.mutex.Unlock()
bundlePaths, err := pl.getBuiltInPolicies(context.Background())
if err != nil {
return []string{}, []string{}, fmt.Errorf("failed to download policies: %w", err)
}
contentData, err := LoadPoliciesData(bundlePaths)
if err != nil {
return []string{}, []string{}, fmt.Errorf("failed to download policies: %w", err)
}
_ = pl.cache.SetWithExpire(bundlePolicies, contentData, *pl.expiration)
_ = pl.cache.SetWithExpire(bundlePath, bundlePaths, *pl.expiration)
return contentData, bundlePaths, nil
}
func (pl *policyLoader) getBuiltInPolicies(ctx context.Context) ([]string, error) {
client, err := mp.NewClient("tmp", true, pl.PolicyRepo, pl.options...)
if err != nil {
return nil, xerrors.Errorf("policy client error: %w", err)
}
if err = client.DownloadBuiltinPolicies(ctx, pl.RegistryOptions); err != nil {
return nil, xerrors.Errorf("failed to download built-in policies: %w", err)
}
return client.LoadBuiltinPolicies()
}
func LoadPoliciesData(policyPath []string) ([]string, error) {
policiesList := []string{}
fileList := []string{}
err := filepath.Walk(policyPath[0], func(path string, f os.FileInfo, err error) error {
if strings.Contains(path, "policies/kubernetes/") { // load only k8s policies
fileList = append(fileList, path)
}
return nil
})
if err != nil {
return []string{}, fmt.Errorf("failed to walk policy path: %w", err)
}
for _, file := range fileList {
data, err := os.ReadFile(file)
if err != nil {
continue
}
policiesList = append(policiesList, string(data))
}
return policiesList, nil
}