Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Policy name randomizer for network-policy #678

Merged
merged 2 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package common

import (
"fmt"
"hash/fnv"
"strings"
)

//Basic Constant
// Basic Constant
const (
STATUS = "Passed"
LIMIT = " limit "
Expand All @@ -22,7 +23,7 @@ const (
L3_L4 = "L3_L4"
)

//Query Constant
// Query Constant
const (
WHERE_NAMESPACE_NAME = ` where namespace_name = "`
WHERE = ` where `
Expand All @@ -33,14 +34,14 @@ const (
ORDER_BY_UPDATED_TIME = ` order by updated_time DESC`
)

//Error Constant
// Error Constant
const (
INCORRECT_DIRECTION = "incorrect direction"
INCORRECT_VERDICT = "incorrect verdict"
INCORRECT_TYPE = "incorrect type"
)

//ConvertArrayToString - Convert Array of string to String
// ConvertArrayToString - Convert Array of string to String
func ConvertArrayToString(arr []string) string {
var str string
for _, label := range arr {
Expand All @@ -58,7 +59,7 @@ func ConvertArrayToString(arr []string) string {

}

//ConvertStringToArray - Convert String to Array of string
// ConvertStringToArray - Convert String to Array of string
func ConvertStringToArray(str string) []string {
return strings.Split(str, ",")
}
Expand Down Expand Up @@ -86,3 +87,9 @@ func StringDeDuplication(strSlice []string) []string {
}
return list
}

func HashInt(s string) uint32 {
h := fnv.New32a()
_, _ = h.Write([]byte(s))
return h.Sum32()
}
16 changes: 5 additions & 11 deletions src/networkpolicy/deduplicator.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package networkpolicy

import (
"strconv"
"strings"

"github.com/accuknox/auto-policy-discovery/src/common"
"github.com/accuknox/auto-policy-discovery/src/libs"
types "github.com/accuknox/auto-policy-discovery/src/types"

Expand Down Expand Up @@ -761,19 +763,11 @@ func existPolicyName(policyNamesMap map[string]bool, name string) bool {
}

func GeneratePolicyName(policyNamesMap map[string]bool, policy types.KnoxNetworkPolicy, clusterName string) types.KnoxNetworkPolicy {
egressPrefix := "autopol-egress-"
ingressPrefix := "autopol-ingress-"

polType := policy.Metadata["type"]
name := "autopol-" + polType + "-" + libs.RandSeq(15)

for existPolicyName(policyNamesMap, name) {
if polType == "egress" {
name = egressPrefix + libs.RandSeq(15)
} else {
name = ingressPrefix + libs.RandSeq(15)
}
}
hashInt := common.HashInt(polType+policy.Metadata["labels"]+policy.Metadata["namespace"]+policy.Metadata["clustername"]+policy.Metadata["containername"])
hash := strconv.FormatUint(uint64(hashInt), 10)
name := "autopol-" + polType + "-" + hash

policyNamesMap[name] = true

Expand Down
11 changes: 3 additions & 8 deletions src/systempolicy/systemPolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package systempolicy

import (
"errors"
"hash/fnv"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -771,16 +770,12 @@ func mergeFromSource(pols []types.KnoxSystemPolicy) []types.KnoxSystemPolicy {
return results
}

func hashInt(s string) uint32 {
h := fnv.New32a()
_, _ = h.Write([]byte(s))
return h.Sum32()
}

func mergeSysPolicies(pols []types.KnoxSystemPolicy) []types.KnoxSystemPolicy {
var results []types.KnoxSystemPolicy
for _, pol := range pols {
pol.Metadata["name"] = "autopol-system-" + strconv.FormatUint(uint64(hashInt(pol.Metadata["labels"]+pol.Metadata["namespace"]+pol.Metadata["clustername"]+pol.Metadata["containername"])), 10)
pol.Metadata["name"] = "autopol-system-" +
strconv.FormatUint(uint64(common.HashInt(pol.Metadata["labels"]+
pol.Metadata["namespace"]+pol.Metadata["clustername"]+pol.Metadata["containername"])), 10)
i := checkIfMetadataMatches(pol, results)
if i < 0 {
results = append(results, pol)
Expand Down