/
policy.go
218 lines (181 loc) · 4.74 KB
/
policy.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package cmd
import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
"path"
"path/filepath"
"strings"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/cilium/cilium/pkg/fqdn/re"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/policy/api"
"github.com/cilium/cilium/pkg/safeio"
)
// policyCmd represents the policy command
var policyCmd = &cobra.Command{
Use: "policy",
Short: "Manage security policies",
}
var (
ignoredFileNames = []string{
".git",
}
)
func init() {
rootCmd.AddCommand(policyCmd)
// Initialize LRU here because the policy subcommands (validate, import)
// will call down to sanitizing the FQDN rules which contains regexes.
// Regexes need to be compiled as part of the FQDN rule validation.
//
// It's not necessary to pass a useful size here because it's not
// necessary to cache regexes in a short-lived binary (CLI).
re.InitRegexCompileLRU(1)
}
func getContext(content []byte, offset int64) (int, string, int) {
if offset >= int64(len(content)) || offset < 0 {
return 0, fmt.Sprintf("[error: Offset %d is out of bounds 0..%d]", offset, len(content)), 0
}
lineN := strings.Count(string(content[:offset]), "\n") + 1
start := strings.LastIndexByte(string(content[:offset]), '\n')
if start == -1 {
start = 0
} else {
start++
}
end := strings.IndexByte(string(content[start:]), '\n')
var l string
if end == -1 {
l = string(content[start:])
} else {
end = end + start
l = string(content[start:end])
}
return lineN, l, (int(offset) - start)
}
func handleUnmarshalError(f string, content []byte, err error) error {
switch e := err.(type) {
case *json.SyntaxError:
line, ctx, off := getContext(content, e.Offset)
if off <= 1 {
return fmt.Errorf("malformed policy, not JSON?")
}
preoff := off - 1
pre := make([]byte, preoff)
copy(pre, ctx[:preoff])
for i := 0; i < preoff && i < len(pre); i++ {
if pre[i] != '\t' {
pre[i] = ' '
}
}
return fmt.Errorf("%s:%d: syntax error at offset %d:\n%s\n%s^",
path.Base(f), line, off, ctx, pre)
case *json.UnmarshalTypeError:
line, ctx, off := getContext(content, e.Offset)
return fmt.Errorf("%s:%d: unable to assign value '%s' to type '%v':\n%s\n%*c",
path.Base(f), line, e.Value, e.Type, ctx, off, '^')
default:
return fmt.Errorf("%s: unknown error:%s", path.Base(f), err)
}
}
func ignoredFile(name string) bool {
for _, n := range ignoredFileNames {
if name == n {
logrus.WithField(logfields.Path, name).Debug("Ignoring file")
return true
}
}
return false
}
func loadPolicyFile(path string) (api.Rules, error) {
var content []byte
var err error
var r io.Reader
logrus.WithField(logfields.Path, path).Debug("Loading file")
if path == "-" {
r = bufio.NewReader(os.Stdin)
} else {
fr, err := os.Open(path)
if err != nil {
return nil, err
}
defer fr.Close()
r = fr
}
content, err = safeio.ReadAllLimit(r, safeio.MB)
if err != nil {
return nil, err
}
var ruleList api.Rules
err = json.Unmarshal(content, &ruleList)
if err != nil {
return nil, handleUnmarshalError(path, content, err)
}
return ruleList, nil
}
func loadPolicy(name string) (api.Rules, error) {
logrus.WithField(logfields.Path, name).Debug("Entering directory")
if name == "-" {
return loadPolicyFile(name)
}
if fi, err := os.Stat(name); err != nil {
return nil, err
} else if fi.Mode().IsRegular() {
return loadPolicyFile(name)
} else if !fi.Mode().IsDir() {
return nil, fmt.Errorf("Error: %s is not a file or a directory", name)
}
files, err := os.ReadDir(name)
if err != nil {
return nil, err
}
result := api.Rules{}
ruleList, err := processAllFilesFirst(name, files)
if err != nil {
return nil, err
}
result = append(result, ruleList...)
ruleList, err = recursiveSearch(name, files)
if err != nil {
return nil, err
}
result = append(result, ruleList...)
logrus.WithField(logfields.Path, name).Debug("Leaving directory")
return result, nil
}
func processAllFilesFirst(name string, files []os.DirEntry) (api.Rules, error) {
result := api.Rules{}
for _, f := range files {
if f.IsDir() || ignoredFile(path.Base(f.Name())) {
continue
}
ruleList, err := loadPolicyFile(filepath.Join(name, f.Name()))
if err != nil {
return nil, err
}
result = append(result, ruleList...)
}
return result, nil
}
func recursiveSearch(name string, files []os.DirEntry) (api.Rules, error) {
result := api.Rules{}
for _, f := range files {
if f.IsDir() {
if ignoredFile(path.Base(f.Name())) {
continue
}
subpath := filepath.Join(name, f.Name())
ruleList, err := loadPolicy(subpath)
if err != nil {
return nil, err
}
result = append(result, ruleList...)
}
}
return result, nil
}