Skip to content

Commit

Permalink
Update policy_yaml cluster_name from config (accuknox#636)
Browse files Browse the repository at this point in the history
Signed-off-by: Eswar Rajan Subramanian <eswar@accuknox.com>
  • Loading branch information
seswarrajan authored and Prateeknandle committed Jan 12, 2023
1 parent 8237f48 commit 05fe3a9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 15 deletions.
13 changes: 4 additions & 9 deletions src/networkpolicy/networkPolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2223,17 +2223,12 @@ func PopulateNetworkPoliciesFromNetworkLogs(networkLogs []types.KnoxNetworkLog)
}

func writeNetworkPoliciesYamlToDB(policies []types.KnoxNetworkPolicy) {
clusters := []string{}
res := []types.PolicyYaml{}

for _, pol := range policies {
clusters = append(clusters, pol.Metadata["cluster_name"])
}

if cfg.CurrentCfg.ConfigNetPolicy.NetworkLogFrom == "kubearmor" {
k8sNetPolicies := plugin.ConvertKnoxNetPolicyToK8sNetworkPolicy("", "", policies)

for i, np := range k8sNetPolicies {
for _, np := range k8sNetPolicies {
np.ClusterName = ""
jsonBytes, err := json.Marshal(np)
if err != nil {
Expand All @@ -2253,7 +2248,7 @@ func writeNetworkPoliciesYamlToDB(policies []types.KnoxNetworkPolicy) {
Namespace: np.Namespace,
WorkspaceId: cfg.GetCfgWorkspaceId(),
ClusterId: cfg.GetCfgClusterId(),
Cluster: clusters[i],
Cluster: cfg.GetCfgClusterName(),
Labels: np.Labels,
Yaml: yamlBytes,
}
Expand All @@ -2267,7 +2262,7 @@ func writeNetworkPoliciesYamlToDB(policies []types.KnoxNetworkPolicy) {
// convert knoxPolicy to CiliumPolicy
ciliumPolicies := plugin.ConvertKnoxPoliciesToCiliumPolicies(policies)

for i, ciliumPolicy := range ciliumPolicies {
for _, ciliumPolicy := range ciliumPolicies {
jsonBytes, err := json.Marshal(ciliumPolicy)
if err != nil {
log.Error().Msg(err.Error())
Expand All @@ -2291,7 +2286,7 @@ func writeNetworkPoliciesYamlToDB(policies []types.KnoxNetworkPolicy) {
Kind: ciliumPolicy.Kind,
Name: ciliumPolicy.Metadata["name"],
Namespace: ciliumPolicy.Metadata["namespace"],
Cluster: clusters[i],
Cluster: cfg.GetCfgClusterName(),
WorkspaceId: cfg.GetCfgWorkspaceId(),
ClusterId: cfg.GetCfgClusterId(),
Labels: labels,
Expand Down
8 changes: 2 additions & 6 deletions src/systempolicy/systemPolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -1375,15 +1375,11 @@ func GenFileSetForAllPodsInCluster(clusterName string, pods []types.Pod, settype
}

func insertSysPoliciesYamlToDB(policies []types.KnoxSystemPolicy) {
clusters := []string{}
for _, policy := range policies {
clusters = append(clusters, policy.Metadata["cluster_name"])
}

kubeArmorPolicies := plugin.ConvertKnoxSystemPolicyToKubeArmorPolicy(policies)

res := []types.PolicyYaml{}
for i, kubearmorPolicy := range kubeArmorPolicies {
for _, kubearmorPolicy := range kubeArmorPolicies {
// dont save network policies to db
kubearmorPolicy.Spec.Network = types.NetworkRule{}
jsonBytes, err := json.Marshal(kubearmorPolicy)
Expand All @@ -1402,7 +1398,7 @@ func insertSysPoliciesYamlToDB(policies []types.KnoxSystemPolicy) {
Kind: kubearmorPolicy.Kind,
Name: kubearmorPolicy.Metadata["name"],
Namespace: kubearmorPolicy.Metadata["namespace"],
Cluster: clusters[i],
Cluster: cfg.GetCfgClusterName(),
WorkspaceId: cfg.GetCfgWorkspaceId(),
ClusterId: cfg.GetCfgClusterId(),
Labels: kubearmorPolicy.Spec.Selector.MatchLabels,
Expand Down

0 comments on commit 05fe3a9

Please sign in to comment.