MCPcopy
hub / github.com/evilsocket/opensnitch / loadRule

Method loadRule

daemon/rule/loader.go:223–275  ·  view source on GitHub ↗
(fileName string)

Source from the content-addressed store, hash-verified

221}
222
223func (l *Loader) loadRule(fileName string) error {
224 raw, err := ioutil.ReadFile(fileName)
225 if err != nil {
226 return fmt.Errorf("Error while reading %s: %s", fileName, err)
227 }
228 l.Lock()
229 defer l.Unlock()
230
231 var r Rule
232 err = json.Unmarshal(raw, &r)
233 if err != nil {
234 return fmt.Errorf("Error parsing rule from %s: %s", fileName, err)
235 }
236 raw = nil
237
238 if oldRule, found := l.rules[r.Name]; found {
239 l.cleanListsRule(oldRule)
240 }
241
242 if !r.Enabled {
243 // XXX: we only parse and load the Data field if the rule is disabled and the Data field is not empty
244 // the rule will remain disabled.
245 if err = l.unmarshalOperatorList(&r.Operator); err != nil {
246 return err
247 }
248 } else {
249 if err := r.Operator.Compile(); err != nil {
250 log.Warning("Operator.Compile() error: %s, %s (%s)", err, r.Operator.Data, r.Name)
251 return fmt.Errorf("(1) Error compiling rule: %s", err)
252 }
253 if r.Operator.Type == List {
254 for i := 0; i < len(r.Operator.List); i++ {
255 if err := r.Operator.List[i].Compile(); err != nil {
256 log.Warning("Operator.Compile() error: %s (%s)", err, r.Name)
257 return fmt.Errorf("(1) Error compiling list rule: %s", err)
258 }
259 }
260 }
261 }
262 if oldRule, found := l.rules[r.Name]; found {
263 l.deleteOldRuleFromDisk(oldRule, &r)
264 }
265
266 log.Debug("Loaded rule from %s: %s", fileName, r.String())
267 l.rules[r.Name] = &r
268 l.sortRules()
269
270 if r.Enabled && l.isTemporary(&r) {
271 err = l.scheduleTemporaryRule(r)
272 }
273
274 return nil
275}
276
277// deleteRule deletes a rule from memory if it has been deleted from disk.
278// This is only called if fsnotify's Remove event is fired, thus it doesn't

Callers 4

TestRuleLoaderListFunction · 0.95
LoadMethod · 0.95
liveReloadWorkerMethod · 0.95

Calls 10

cleanListsRuleMethod · 0.95
unmarshalOperatorListMethod · 0.95
deleteOldRuleFromDiskMethod · 0.95
StringMethod · 0.95
sortRulesMethod · 0.95
isTemporaryMethod · 0.95
scheduleTemporaryRuleMethod · 0.95
LockMethod · 0.80
UnlockMethod · 0.80
CompileMethod · 0.80

Tested by 2

TestRuleLoaderListFunction · 0.76