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

Package policy recommendation #732

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

vishnusomank
Copy link
Contributor

No description provided.

@Vyom-Yadav
Copy link
Contributor

@vishnusomank Please rebase onto latest dev.

Signed-off-by: Vishnu Soman <[email protected]>
@vishnusomank
Copy link
Contributor Author

@vishnusomank Please rebase onto latest dev.

updated @Vyom-Yadav

Comment on lines +225 to +251
for _, d := range replicaSets {

labelMap := labelArrayToLabelMap(strings.Split(d.Labels, ","))

if recommendAdmissionControllerPolicy &&
isNamespaceAllowed(d.Namespace, nsNotFilterAdmissionControllerPolicy, nsFilterAdmissionControllerPolicy) {
policies = append(policies, generateAdmissionControllerPolicy(d.Name, d.Namespace, labelMap)...)
}
}
for _, d := range statefulSets {

labelMap := labelArrayToLabelMap(strings.Split(d.Labels, ","))

if recommendAdmissionControllerPolicy &&
isNamespaceAllowed(d.Namespace, nsNotFilterAdmissionControllerPolicy, nsFilterAdmissionControllerPolicy) {
policies = append(policies, generateAdmissionControllerPolicy(d.Name, d.Namespace, labelMap)...)
}
}
for _, d := range daemonSets {

labelMap := labelArrayToLabelMap(strings.Split(d.Labels, ","))

if recommendAdmissionControllerPolicy &&
isNamespaceAllowed(d.Namespace, nsNotFilterAdmissionControllerPolicy, nsFilterAdmissionControllerPolicy) {
policies = append(policies, generateAdmissionControllerPolicy(d.Name, d.Namespace, labelMap)...)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work for all types of pod controllers as we generate some fields in Kyverno policy according to the type of controller.

policySpec := ms.KyvernoPolicySpec.DeepCopy()
// Update kind from pod -> deployment
policySpec.Rules[0].MatchResources.Any[0].ResourceDescription.Kinds = []string{"Deployment"}

Other generated fields would work similarly for all pod controllers except CronJob.


So you would need to have the name of the kind passed while generating Kyverno policy so it gets updated accordingly. The merger of #734 would lead to some conflicts as we are shifting to kyverno policy interface.

Copy link
Contributor

@Vyom-Yadav Vyom-Yadav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add some PR description. Major item: #732 (comment)

Other items:

Comment on lines +585 to +587
if rs.Namespace == "kube-system" {
continue
}
Copy link
Contributor

@Vyom-Yadav Vyom-Yadav May 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this pattern for other controllers too, IMO resource filtration shouldn't be hardcoded and should be based on some configuration.

Edit-
There are ns filters in configuration for DE

namespace-filter:
- "!kube-system"

So, it shouldn't be hardcoded.

Comment on lines 78 to +81
deployments := cluster.GetDeploymentsFromK8sClient()
deployments = append(deployments, cluster.GetReplicaSetsFromK8sClient()...)
deployments = append(deployments, cluster.GetStatefulSetsFromK8sClient()...)
deployments = append(deployments, cluster.GetDaemonSetsFromK8sClient()...)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deployments -> podControllers, let's use better variable names.


"github.com/accuknox/auto-policy-discovery/src/admissioncontrollerpolicy"
"github.com/accuknox/auto-policy-discovery/src/cluster"
cfg "github.com/accuknox/auto-policy-discovery/src/config"
logger "github.com/accuknox/auto-policy-discovery/src/logging"
"github.com/accuknox/auto-policy-discovery/src/systempolicy"
"github.com/accuknox/auto-policy-discovery/src/types"
v1 "github.com/kyverno/kyverno/api/kyverno/v1"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v1 -> kyvernov1, we usually use v1 when we are referring to core v1.

Comment on lines +130 to +132
deployments := cluster.GetDeploymentsFromK8sClient()
if deployments == nil {
log.Error().Msg("Error getting Deployments from k8s client.")
Copy link
Contributor

@Vyom-Yadav Vyom-Yadav May 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the logic of cluster.GetDeploymentsFromK8sClient() function, it would automatically filter kube-system ns, this would alter the current behavior. Same for other pod controllers.

Comment on lines 153 to 157
if policies == nil {
log.Error().Msg("Error generating hardened policies")
return
}
systempolicy.UpdateSysPolicies(policies)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to the current changes, but this leads to the non-generation of Kyverno policies if no KubeArmor policies are found. This shouldn't be the expected behavior.


labelMap := labelArrayToLabelMap(strings.Split(d.Labels, ","))

if recommendAdmissionControllerPolicy &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

recommendAdmissionControllerPolicy should be the overall condition.

Comment on lines +256 to 259
func GetHardenPolicy(deployments, replicaSets, statefulSets, daemonSets []types.Deployment, nsNotFilter []string) []types.KnoxSystemPolicy {

var policies []types.KnoxSystemPolicy
if !isLatest() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to the current changes but can you please move the logic of DownloadAndUnzipRelease out of this function? It is not just related to hardening policy and results in this function doing more than what its name suggests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants