/
filereader.go
224 lines (183 loc) Β· 4.74 KB
/
filereader.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
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
package reader
import (
"bufio"
"fmt"
"os"
"path/filepath"
"github.com/open-policy-agent/gatekeeper/v3/pkg/oci"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
var allowedExtensions = []string{".yaml", ".yml", ".json"}
func ReadSources(filenames []string, images []string, tempDir string) ([]*unstructured.Unstructured, error) {
var sources []*source
// Read from --filename flag
s, err := readFiles(filenames)
if err != nil {
return nil, fmt.Errorf("reading from filenames: %w", err)
}
sources = append(sources, s...)
// Read from --image flag
s, err = readImages(images, tempDir)
if err != nil {
return nil, fmt.Errorf("pulling image: %w", err)
}
sources = append(sources, s...)
// Read stdin
stdinUnstructs, err := readStdin()
if err != nil {
return nil, fmt.Errorf("reading from stdin: %w", err)
}
sources = append(sources, &source{stdin: true, objs: stdinUnstructs})
conflicts := detectConflicts(sources)
for i := range conflicts {
logConflict(&conflicts[i])
}
return sourcesToUnstruct(sources), nil
}
func readImage(image string, tempDir string) ([]*source, error) {
dirPath, closeHandler, err := oci.PullImage(image, tempDir)
if closeHandler != nil {
defer closeHandler()
}
if err != nil {
return nil, err
}
sources, err := readFile(dirPath)
if err != nil {
return nil, err
}
for _, s := range sources {
s.image = image
}
return sources, nil
}
func readImages(images []string, tempDir string) ([]*source, error) {
var sources []*source
for _, image := range images {
s, err := readImage(image, tempDir)
if err != nil {
return nil, err
}
sources = append(sources, s...)
}
return sources, nil
}
func readFile(filename string) ([]*source, error) {
if err := verifyFile(filename); err != nil {
return nil, err
}
var sources []*source
expanded, err := expandDirectories([]string{filename})
if err != nil {
return nil, fmt.Errorf("normalizing filenames: %w", err)
}
for _, f := range expanded {
file, err := os.Open(f)
if err != nil {
return nil, fmt.Errorf("opening file %q: %w", f, err)
}
defer file.Close()
us, err := ReadK8sResources(bufio.NewReader(file))
if err != nil {
return nil, fmt.Errorf("reading file %q: %w", f, err)
}
sources = append(sources, &source{
filename: f,
objs: us,
})
}
return sources, nil
}
func readFiles(filenames []string) ([]*source, error) {
var sources []*source
for _, f := range filenames {
s, err := readFile(f)
if err != nil {
return nil, err
}
sources = append(sources, s...)
}
return sources, nil
}
// verifyFile checks that the filenames aren't themselves disallowed extensions.
// This yields a much better user experience when the user mis-uses the
// --filename flag.
func verifyFile(filename string) error {
// make sure it's a file, not a directory
fileInfo, err := os.Stat(filename)
if err != nil {
return fmt.Errorf("stat on path %q: %w", filename, err)
}
if fileInfo.IsDir() {
return nil
}
if !allowedExtension(filename) {
return fmt.Errorf("path %q must be of extensions: %v", filename, allowedExtensions)
}
return nil
}
func readStdin() ([]*unstructured.Unstructured, error) {
stdinfo, err := os.Stdin.Stat()
if err != nil {
return nil, fmt.Errorf("getting stdin info: %w", err)
}
// check if data is being piped or redirected to stdin
if (stdinfo.Mode() & os.ModeCharDevice) != 0 {
return nil, nil
}
us, err := ReadK8sResources(os.Stdin)
if err != nil {
return nil, fmt.Errorf("reading: %w", err)
}
return us, nil
}
func expandDirectories(filenames []string) ([]string, error) {
var output []string
for _, filename := range filenames {
paths, err := filesBelow(filename)
if err != nil {
return nil, fmt.Errorf("filename %q: %w", filename, err)
}
output = append(output, paths...)
}
return output, nil
}
// filesBelow walks the filetree from startPath and below, collecting a list of
// all the filepaths. Directories are excluded.
func filesBelow(startPath string) ([]string, error) {
var files []string
err := filepath.Walk(startPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// only add files to the normalized output
if info.IsDir() {
return nil
}
// make sure the file extension is valid
if !allowedExtension(path) {
return nil
}
files = append(files, path)
return nil
})
if err != nil {
return nil, fmt.Errorf("walking: %w", err)
}
return files, nil
}
func allowedExtension(path string) bool {
for _, ext := range allowedExtensions {
if ext == filepath.Ext(path) {
return true
}
}
return false
}
func sourcesToUnstruct(sources []*source) []*unstructured.Unstructured {
var us []*unstructured.Unstructured
for _, s := range sources {
us = append(us, s.objs...)
}
return us
}