Skip to content

Commit

Permalink
split blockIPs function
Browse files Browse the repository at this point in the history
  • Loading branch information
santinoncs committed Jun 5, 2023
1 parent b9d39cc commit 34f9cce
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
56 changes: 48 additions & 8 deletions app/actor/armor_actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ func NewGCPArmorActor(config *ActorConfig) (*GCPArmorActor, error) {
}, nil
}

func (g *GCPArmorActor) BlockIPs(sourceIPs []app.IPCount) error {
// GetIPsToBlock: this function will get the IPs candidate to Block by BlockIPs function
func (g *GCPArmorActor) getBlockIPs(sourceIPs []app.IPCount) ([]string, int32, error) {

var sourceIPstring []string

Expand All @@ -51,40 +52,74 @@ func (g *GCPArmorActor) BlockIPs(sourceIPs []app.IPCount) error {
rules, err := RulesGetter.GetSecurityRules()
if err != nil {
klog.Error("\nError: ", err)
return err
return nil, 0, err
}

ipGetter := NewIPGetter(g)
alreadyBlockedIPs, err := ipGetter.GetBlockedIPs(rules)
if err != nil {
klog.Error("\nError: ", err)
return err
return nil, 0, err
}

lastprio := getLastPriority(rules)

excludedIPsinArray, err := utils.ConvertCSVToArray(g.ActorConfig.ExcludeIPs)
if err != nil {
klog.Error("\nError with exclude IPs function: ", err)
return err
return nil, 0, err
}

handler := utils.UtilsIPListHandler{}
candidateIPsToBlock := getCandidateIPsToBlock(handler, sourceIPstring, alreadyBlockedIPs, excludedIPsinArray)

if len(candidateIPsToBlock) == 0 {
return nil
return nil, 0, nil
}

now := time.Now()
secs := now.Unix()
return candidateIPsToBlock, lastprio, nil

}

// BlockIPs: this function will block the IPs
func (g *GCPArmorActor) BlockIPs(sourceIPs []app.IPCount) error {

candidateIPsToBlock, lastprio, err := g.getBlockIPs(sourceIPs)
if err != nil {
return err
}

description := setDescriptionForNewRules()

description := "ipblocker:" + strconv.FormatInt(secs, 10)
priority := lastprio + 1

action := fmt.Sprintf("%v", conf.Data["action"])
preview, _ := strconv.ParseBool(fmt.Sprintf("%v", conf.Data["preview"]))

err = addNewFirewallRules(g, candidateIPsToBlock, priority, action, description, preview)
if err != nil {
return err
}

return nil

}

// this function will compute the timestamp to be used in the description of the new rules
func setDescriptionForNewRules() string {

now := time.Now()
secs := now.Unix()

description := "ipblocker:" + strconv.FormatInt(secs, 10)

return description

}

// this function will add the new rules to GCPArmor
func addNewFirewallRules(g *GCPArmorActor, candidateIPsToBlock []string, priority int32, action string, description string, preview bool) error {

chunkSize := 10
for i := 0; i < len(candidateIPsToBlock); i += chunkSize {
end := i + chunkSize
Expand All @@ -104,6 +139,11 @@ func (g *GCPArmorActor) BlockIPs(sourceIPs []app.IPCount) error {
}

return nil
}

func getFieldFromData(confData string) interface{} {

return fmt.Sprintf("%v", conf.Data[confData])

}

Expand Down
1 change: 0 additions & 1 deletion app/source/elasticConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ type SourceConfig struct {
Address string
Username string
Password string
Namespace string
Threshold int
CACert string
}
Expand Down
Binary file modified ip-blocker
Binary file not shown.

0 comments on commit 34f9cce

Please sign in to comment.