Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Jess Frazelle <[email protected]>
  • Loading branch information
jessfraz committed Sep 11, 2018
1 parent b2c6bd2 commit e27cae8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 34 deletions.
48 changes: 23 additions & 25 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ import (
"github.com/sirupsen/logrus"
)

var (
type apiServer struct {
// TODO: don't store these in memory
rules map[string]map[string]grpc.Rule
// rulesMutex locks the buffer for every transaction.
rulesMutex = sync.Mutex{}
)
rulesMutex sync.Mutex

type apiServer struct {
programs map[string]program.Program
actions map[string]action.Action

Expand All @@ -41,7 +39,13 @@ type Opts struct {

// NewServer returns grpc server instance.
func NewServer(opt Opts) (grpc.APIServer, error) {
rules = opt.Rules
server := &apiServer{
rules: opt.Rules,
programs: opt.Programs,
actions: opt.Actions,
programList: opt.ProgramList,
actionList: opt.ActionList,
}

// Load all the compiled in programs.
for p, prog := range opt.Programs {
Expand All @@ -63,7 +67,7 @@ func NewServer(opt Opts) (grpc.APIServer, error) {

event.ContainerRuntime = string(proc.GetContainerRuntime(int(event.TGID), int(event.PID)))

progRules, _ := rules[p]
progRules, _ := server.rules[p]

if len(progRules) < 1 {
// Just send to stdout and be done with it.
Expand All @@ -73,8 +77,7 @@ func NewServer(opt Opts) (grpc.APIServer, error) {

for _, rule := range progRules {
// Verify the event matches for the rules.
match, _ := rulespkg.Match(rule, event.Data, event.ContainerRuntime)
if !match {
if match := rulespkg.Match(rule, event.Data, event.ContainerRuntime); !match {
// We didn't find what we were searching for so continue.
continue
}
Expand Down Expand Up @@ -107,17 +110,12 @@ func NewServer(opt Opts) (grpc.APIServer, error) {
logrus.Infof("Watching events for plugin %s", p)
}

return &apiServer{
programs: opt.Programs,
actions: opt.Actions,
programList: opt.ProgramList,
actionList: opt.ActionList,
}, nil
return server, nil
}

func (s *apiServer) CreateRule(ctx context.Context, c *grpc.CreateRuleRequest) (*grpc.CreateRuleResponse, error) {
rulesMutex.Lock()
defer rulesMutex.Unlock()
s.rulesMutex.Lock()
defer s.rulesMutex.Unlock()

if c == nil || c.Rule == nil {
return nil, errors.New("rule cannot be nil")
Expand All @@ -137,30 +135,30 @@ func (s *apiServer) CreateRule(ctx context.Context, c *grpc.CreateRuleRequest) (
}).Infof("Created rule")

// Check if we already have rules for the program to avoid a panic.
_, ok := rules[c.Rule.Program]
_, ok := s.rules[c.Rule.Program]
if !ok {
rules[c.Rule.Program] = map[string]grpc.Rule{c.Rule.Name: *c.Rule}
s.rules[c.Rule.Program] = map[string]grpc.Rule{c.Rule.Name: *c.Rule}
return &grpc.CreateRuleResponse{}, nil
}

// Add the rule to our existing rules for the program.
// TODO: decide to error or not on overwrite
rules[c.Rule.Program][c.Rule.Name] = *c.Rule
s.rules[c.Rule.Program][c.Rule.Name] = *c.Rule
return &grpc.CreateRuleResponse{}, nil
}

// TODO: find a better way to remove without program
func (s *apiServer) RemoveRule(ctx context.Context, r *grpc.RemoveRuleRequest) (*grpc.RemoveRuleResponse, error) {
rulesMutex.Lock()
defer rulesMutex.Unlock()
s.rulesMutex.Lock()
defer s.rulesMutex.Unlock()

if r == nil || len(r.Name) < 1 {
return nil, errors.New("rule name cannot be empty")
}

// If they passed the program then only remove the rule for that program.
if len(r.Program) > 0 {
delete(rules[r.Program], r.Name)
delete(s.rules[r.Program], r.Name)

logrus.WithFields(logrus.Fields{
"program": r.Program,
Expand All @@ -171,10 +169,10 @@ func (s *apiServer) RemoveRule(ctx context.Context, r *grpc.RemoveRuleRequest) (
}

// Iterate over the programs and find the rule.
for p, prs := range rules {
for p, prs := range s.rules {
for name := range prs {
if name == r.Name {
delete(rules[p], r.Name)
delete(s.rules[p], r.Name)

logrus.WithFields(logrus.Fields{
"program": p,
Expand All @@ -191,7 +189,7 @@ func (s *apiServer) RemoveRule(ctx context.Context, r *grpc.RemoveRuleRequest) (
func (s *apiServer) ListRules(ctx context.Context, r *grpc.ListRulesRequest) (*grpc.ListRulesResponse, error) {
var rs []*grpc.Rule

for _, prs := range rules {
for _, prs := range s.rules {
for _, rule := range prs {
rs = append(rs, &rule)
}
Expand Down
16 changes: 8 additions & 8 deletions rules/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,19 @@ func ValidateProgramsAndActions(rule grpc.Rule, programs, actions []string) erro
}

// Match checks the filter properties for a rule against the data from
// the event. It returns a boolean and the actions for the rule.
func Match(rule grpc.Rule, data map[string]string, pidRuntime string) (bool, []string) {
// the event.
func Match(rule grpc.Rule, data map[string]string, pidRuntime string) bool {
// Return early if we have nothing to filter on.
if len(rule.ContainerRuntimes) < 1 && len(rule.FilterEvents) < 1 {
return true, rule.Actions
return true
}

matchedRuntime := false
for _, runtime := range rule.ContainerRuntimes {
if pidRuntime == runtime {
// Return early if we know we have nothing else to filter on.
if len(rule.FilterEvents) < 1 {
return true, rule.Actions
return true
}

// Continue to the next check.
Expand All @@ -127,12 +127,12 @@ func Match(rule grpc.Rule, data map[string]string, pidRuntime string) (bool, []s

// Return early here if we never matched a runtime.
if len(rule.ContainerRuntimes) > 0 && !matchedRuntime {
return false, rule.Actions
return false
}

// Return early here if we have nothing else to filter on.
if len(rule.FilterEvents) < 1 {
return true, rule.Actions
return true
}

for key, ogValue := range data {
Expand All @@ -143,13 +143,13 @@ func Match(rule grpc.Rule, data map[string]string, pidRuntime string) (bool, []s
for _, find := range s.Values {
if strings.Contains(ogValue, find) {
// Return early since we have nothing else to filter on.
return true, rule.Actions
return true
}
}
}

// We did not match any filters.
return false, rule.Actions
return false
}

func in(a []string, s string) bool {
Expand Down
2 changes: 1 addition & 1 deletion rules/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestMatch(t *testing.T) {
}

for name, tc := range testcases {
match, _ := Match(tc.rule, tc.data, string(tc.runtime))
match := Match(tc.rule, tc.data, string(tc.runtime))
if match != tc.expected {
t.Errorf("[%s]: expected match to be %t, got %t", name, tc.expected, match)
}
Expand Down

0 comments on commit e27cae8

Please sign in to comment.