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

set ownerreference for policy reports #346

Merged
merged 5 commits into from
Jul 22, 2022
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
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,6 @@ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a h1:DcqTD9SDLc+1P/r1EmRBwnVsrOwW+kk2vWf9n+1sGhs=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down
57 changes: 47 additions & 10 deletions outputs/policyreport.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import (
"context"
"fmt"
"log"
"os"

"github.com/DataDog/datadog-go/statsd"
"github.com/falcosecurity/falcosidekick/types"
"github.com/google/uuid"
"github.com/kubernetes-sigs/wg-policy-prototypes/policy-report/kube-bench-adapter/pkg/apis/wgpolicyk8s.io/v1alpha2"
wgpolicy "github.com/kubernetes-sigs/wg-policy-prototypes/policy-report/kube-bench-adapter/pkg/apis/wgpolicyk8s.io/v1alpha2"
crdClient "github.com/kubernetes-sigs/wg-policy-prototypes/policy-report/kube-bench-adapter/pkg/generated/v1alpha2/clientset/versioned"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8stypes "k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/retry"
Expand All @@ -39,12 +41,17 @@ var (
clusterPolicyReport *wgpolicy.ClusterPolicyReport = &wgpolicy.ClusterPolicyReport{
ObjectMeta: metav1.ObjectMeta{
Name: clusterPolicyReportBaseName,
Labels: map[string]string{
"app.kubernetes.io/created-by": "falcosidekick",
},
},
Summary: v1alpha2.PolicyReportSummary{
Summary: wgpolicy.PolicyReportSummary{
Fail: 0,
Warn: 0,
},
}
falcosidekickNamespace string
falcosidekickNamespaceUID k8stypes.UID
)

func NewPolicyReportClient(config *types.Configuration, stats *types.Statistics, promStats *types.PromStatistics, statsdClient, dogstatsdClient *statsd.Client) (*Client, error) {
Expand All @@ -62,6 +69,22 @@ func NewPolicyReportClient(config *types.Configuration, stats *types.Statistics,
if err != nil {
return nil, err
}
clientset, err := kubernetes.NewForConfig(clientConfig)
if err != nil {
return nil, err
}

falcosidekickNamespace = os.Getenv("NAMESPACE")
if falcosidekickNamespace == "" {
log.Println("[INFO] : PolicyReport - No env var NAMESPACE detected")
} else {
n, err := clientset.CoreV1().Namespaces().Get(context.TODO(), falcosidekickNamespace, metav1.GetOptions{})
if err != nil {
log.Printf("[ERROR] : PolicyReport - Can't get UID of namespace %v: %v\n", falcosidekickNamespace, err)
} else {
falcosidekickNamespaceUID = n.ObjectMeta.UID
}
}

return &Client{
OutputType: "PolicyReport",
Expand Down Expand Up @@ -124,8 +147,8 @@ func newResult(FalcoPayload types.FalcoPayload) (_ *wgpolicy.PolicyReportResult,
Source: policyReportSource,
Scored: false,
Timestamp: metav1.Timestamp{Seconds: int64(FalcoPayload.Time.Second()), Nanos: int32(FalcoPayload.Time.Nanosecond())},
Severity: v1alpha2.PolicyResultSeverity(severity),
Result: v1alpha2.PolicyResult(result),
Severity: wgpolicy.PolicyResultSeverity(severity),
Result: wgpolicy.PolicyResult(result),
Description: FalcoPayload.Output,
Properties: properties,
}, namespace
Expand Down Expand Up @@ -165,18 +188,32 @@ func updatePolicyReports(c *Client, namespace string, event *wgpolicy.PolicyRepo
policyReports[namespace] = &wgpolicy.PolicyReport{
ObjectMeta: metav1.ObjectMeta{
Name: policyReportBaseName + uuid.NewString()[:8],
Issif marked this conversation as resolved.
Show resolved Hide resolved
Labels: map[string]string{
"app.kubernetes.io/created-by": "falcosidekick",
},
},
Summary: v1alpha2.PolicyReportSummary{
Summary: wgpolicy.PolicyReportSummary{
Fail: 0,
Warn: 0,
},
}
if falcosidekickNamespace != "" && falcosidekickNamespaceUID != "" {
policyReports[namespace].ObjectMeta.OwnerReferences = []metav1.OwnerReference{
{
APIVersion: "v1",
Kind: "Namespace",
Name: falcosidekickNamespace,
UID: falcosidekickNamespaceUID,
Controller: new(bool),
},
}
}
}

policyr := c.Crdclient.Wgpolicyk8sV1alpha2().PolicyReports(namespace)
updatePolicyReportSummary(policyReports[namespace], event)
if len(policyReports[namespace].Results) == c.Config.PolicyReport.MaxEvents {
if c.Config.PolicyReport.PruneByPriority == true {
if c.Config.PolicyReport.PruneByPriority {
pruningLogicForPolicyReports(namespace)
} else {
if policyReports[namespace].Results[0].Severity == highpriority {
Expand All @@ -192,7 +229,7 @@ func updatePolicyReports(c *Client, namespace string, event *wgpolicy.PolicyRepo
if errors.IsNotFound(getErr) {
result, err := policyr.Create(context.TODO(), policyReports[namespace], metav1.CreateOptions{})
if err != nil {
log.Printf("[ERROR] : Can't create Policy Report %v in namespace %v\n", err, namespace)
log.Printf("[ERROR] : PolicyReport - Can't create Policy Report %v in namespace %v\n", err, namespace)
return err
}
log.Printf("[INFO] : PolicyReport - Create policy report %v in namespace %v\n", result.GetObjectMeta().GetName(), namespace)
Expand Down Expand Up @@ -229,7 +266,7 @@ func updateClusterPolicyReport(c *Client, event *wgpolicy.PolicyReportResult) er
clusterpr := c.Crdclient.Wgpolicyk8sV1alpha2().ClusterPolicyReports()

if len(clusterPolicyReport.Results) == c.Config.PolicyReport.MaxEvents {
if c.Config.PolicyReport.PruneByPriority == true {
if c.Config.PolicyReport.PruneByPriority {
pruningLogicForClusterPolicyReport()
} else {
if clusterPolicyReport.Results[0].Severity == highpriority {
Expand Down Expand Up @@ -309,15 +346,15 @@ func pruningLogicForClusterPolicyReport() {
}

func summaryDeletionCluster(rep *wgpolicy.ClusterPolicyReport, deleteFailevent bool) {
if deleteFailevent == true {
if deleteFailevent {
rep.Summary.Fail--
} else {
rep.Summary.Warn--
}
}

func summaryDeletion(rep *wgpolicy.PolicyReport, deleteFailevent bool) {
if deleteFailevent == true {
if deleteFailevent {
rep.Summary.Fail--
} else {
rep.Summary.Warn--
Expand Down