-
Notifications
You must be signed in to change notification settings - Fork 25
Integrate SecurityException CRDs into vulnerability scanning #342
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c44bd45
Add SecurityException CRD integration for vulnerability scanning
slashben 2277976
refactor: move SecurityException types from storage to local package
slashben 999792f
refactor: address review - repository layer, v1beta1, constructor inj…
slashben 72bc6a5
Merge branch 'main' into feature/security-exception-integration
slashben e4cc551
fix: add fake DynamicClient, move NoOp to repositories
slashben File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| golang 1.25.6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| package v1 | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "github.com/armosec/armoapi-go/armotypes" | ||
| "github.com/armosec/armoapi-go/identifiers" | ||
| sev1beta1 "github.com/kubescape/kubevuln/pkg/securityexception/v1beta1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| ) | ||
|
|
||
| // convertToVulnerabilityExceptionPolicies converts SecurityException and | ||
| // ClusterSecurityException CRDs into armotypes.VulnerabilityExceptionPolicy | ||
| // slices compatible with the existing exception pipeline. | ||
| func convertToVulnerabilityExceptionPolicies(exceptions []sev1beta1.SecurityException, clusterExceptions []sev1beta1.ClusterSecurityException) []armotypes.VulnerabilityExceptionPolicy { | ||
| var policies []armotypes.VulnerabilityExceptionPolicy | ||
|
|
||
| now := time.Now() | ||
|
|
||
| for i := range exceptions { | ||
| se := &exceptions[i] | ||
| if isExpired(se.Spec.ExpiresAt, now) { | ||
| continue | ||
| } | ||
| namespace := se.Namespace | ||
| for _, vuln := range se.Spec.Vulnerabilities { | ||
| p := buildPolicy(se.Spec, vuln, namespace) | ||
| policies = append(policies, p) | ||
| } | ||
| } | ||
|
|
||
| for i := range clusterExceptions { | ||
| cse := &clusterExceptions[i] | ||
| if isExpired(cse.Spec.ExpiresAt, now) { | ||
| continue | ||
| } | ||
| for _, vuln := range cse.Spec.Vulnerabilities { | ||
| p := buildPolicy(cse.Spec, vuln, "") | ||
| policies = append(policies, p) | ||
| } | ||
| } | ||
|
|
||
| return policies | ||
| } | ||
|
|
||
| func isExpired(expiresAt *metav1.Time, now time.Time) bool { | ||
| return expiresAt != nil && expiresAt.Time.Before(now) | ||
| } | ||
|
|
||
| func buildPolicy(spec sev1beta1.SecurityExceptionSpec, vuln sev1beta1.VulnerabilityException, namespace string) armotypes.VulnerabilityExceptionPolicy { | ||
| p := armotypes.VulnerabilityExceptionPolicy{ | ||
| PolicyType: "vulnerabilityExceptionPolicy", | ||
| Actions: []armotypes.VulnerabilityExceptionPolicyActions{armotypes.Ignore}, | ||
| VulnerabilityPolicies: []armotypes.VulnerabilityPolicy{ | ||
| {Name: vuln.Vulnerability.ID}, | ||
| }, | ||
| Reason: spec.Reason, | ||
| } | ||
|
|
||
| if spec.ExpiresAt != nil { | ||
| t := spec.ExpiresAt.Time | ||
| p.ExpirationDate = &t | ||
| } | ||
|
|
||
| if vuln.ExpiredOnFix { | ||
| b := true | ||
| p.ExpiredOnFix = &b | ||
| } | ||
|
|
||
| p.Designatores = buildDesignators(spec.Match.Resources, namespace) | ||
|
|
||
| return p | ||
| } | ||
|
|
||
| func buildDesignators(resources []sev1beta1.ResourceMatch, namespace string) []identifiers.PortalDesignator { | ||
| if len(resources) == 0 { | ||
| // No specific resource match — create a namespace-only designator | ||
| if namespace != "" { | ||
| return []identifiers.PortalDesignator{ | ||
| { | ||
| DesignatorType: identifiers.DesignatorAttributes, | ||
| Attributes: map[string]string{ | ||
| "namespace": namespace, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| designators := make([]identifiers.PortalDesignator, 0, len(resources)) | ||
| for _, r := range resources { | ||
| attrs := map[string]string{} | ||
| if namespace != "" { | ||
| attrs["namespace"] = namespace | ||
| } | ||
| if r.Kind != "" { | ||
| attrs["kind"] = r.Kind | ||
| } | ||
| if r.Name != "" { | ||
| attrs["name"] = r.Name | ||
| } | ||
| if len(attrs) == 0 { | ||
| continue | ||
| } | ||
| designators = append(designators, identifiers.PortalDesignator{ | ||
| DesignatorType: identifiers.DesignatorAttributes, | ||
| Attributes: attrs, | ||
| }) | ||
| } | ||
| return designators | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.