-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbundle_file_resolver.go
125 lines (103 loc) · 3.21 KB
/
bundle_file_resolver.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
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1
package policy
import (
"context"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/cockroachdb/errors"
"github.com/rs/zerolog/log"
)
type fileBundleResolver struct{}
func defaultFileBundleResolver() *fileBundleResolver {
return NewFileBundleResolver()
}
func (l *fileBundleResolver) Load(ctx context.Context, path string) (*Bundle, error) {
return loadBundlesFromPaths(path)
}
func NewFileBundleResolver() *fileBundleResolver {
return &fileBundleResolver{}
}
func (r *fileBundleResolver) IsApplicable(path string) bool {
_, err := os.Stat(path)
return err == nil
}
// loadBundlesFromPaths loads a single policy bundle file or a bundle that
// was split into multiple files into a single PolicyBundle struct
func loadBundlesFromPaths(paths ...string) (*Bundle, error) {
// load all the source files
resolvedFilenames, err := WalkPolicyBundleFiles(paths...)
if err != nil {
log.Error().Err(err).Msg("could not resolve bundle files")
return nil, err
}
// aggregate all files into a single policy bundle
aggregatedBundle, err := aggregateFilesToBundle(resolvedFilenames)
if err != nil {
log.Debug().Err(err).Msg("could not merge bundle files")
return nil, err
}
return aggregatedBundle, nil
}
// WalkPolicyBundleFiles iterates over all provided filenames and
// checks if the name is a file or a directory. If the filename
// is a directory, it walks the directory recursively
func WalkPolicyBundleFiles(filenames ...string) ([]string, error) {
// resolve file names
resolvedFilenames := []string{}
for i := range filenames {
filename := filenames[i]
fi, err := os.Stat(filename)
if err != nil {
return nil, errors.Wrap(err, "could not load policy bundle file: "+filename)
}
if fi.IsDir() {
err := filepath.WalkDir(filename, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
// we ignore directories because WalkDir already walks them
if d.IsDir() {
return nil
}
// only consider .yaml|.yml files
if strings.HasSuffix(d.Name(), ".mql.yaml") || strings.HasSuffix(d.Name(), ".mql.yml") {
resolvedFilenames = append(resolvedFilenames, path)
}
return nil
})
if err != nil {
return nil, err
}
} else {
resolvedFilenames = append(resolvedFilenames, filename)
}
}
return resolvedFilenames, nil
}
// aggregateFilesToBundle iterates over all provided files and loads its content.
// It assumes that all provided files are checked upfront and are not a directory
func aggregateFilesToBundle(paths []string) (*Bundle, error) {
// iterate over all files, load them and merge them
mergedBundle := &Bundle{}
for i := range paths {
path := paths[i]
log.Debug().Str("path", path).Msg("local>loading policy bundle file")
bundle, err := bundleFromSingleFile(path)
if err != nil {
return nil, errors.Wrap(err, "could not load file: "+path)
}
mergedBundle = Merge(mergedBundle, bundle)
}
return mergedBundle, nil
}
// bundleFromSingleFile loads a policy bundle from a single file
func bundleFromSingleFile(path string) (*Bundle, error) {
bundleData, err := os.ReadFile(path)
if err != nil {
return nil, err
}
return BundleFromYAML(bundleData)
}