Skip to content

Commit

Permalink
introduced opa policy generation
Browse files Browse the repository at this point in the history
  • Loading branch information
darryk10 authored and Kaizhe committed Sep 21, 2021
1 parent feb1b53 commit 0186148
Show file tree
Hide file tree
Showing 10 changed files with 937 additions and 103 deletions.
19 changes: 17 additions & 2 deletions advisor/advisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package advisor
import (
"encoding/json"
"fmt"
"github.com/open-policy-agent/opa/ast"
"os"

"github.com/sysdiglabs/kube-psp-advisor/advisor/types"
Expand All @@ -18,6 +19,7 @@ import (

type Advisor struct {
podSecurityPolicy *v1beta1.PodSecurityPolicy
OPAModulePolicy *ast.Module
k8sClient *kubernetes.Clientset
processor *processor.Processor
report *report.Report
Expand All @@ -41,7 +43,7 @@ func NewAdvisor(kubeconfig string) (*Advisor, error) {
}, nil
}

func (advisor *Advisor) Process(namespace string, excludeNamespaces []string) error {
func (advisor *Advisor) Process(namespace string, excludeNamespaces []string, OPAformat bool, OPAdefaultRule bool) error {
advisor.processor.SetNamespace(namespace)
advisor.processor.SetExcludeNamespaces(excludeNamespaces)

Expand All @@ -51,7 +53,11 @@ func (advisor *Advisor) Process(namespace string, excludeNamespaces []string) er
return err
}

advisor.podSecurityPolicy = advisor.processor.GeneratePSP(cssList, pssList)
if OPAformat {
advisor.OPAModulePolicy = advisor.processor.GenerateOPA(cssList, pssList, OPAdefaultRule)
} else {
advisor.podSecurityPolicy = advisor.processor.GeneratePSP(cssList, pssList)
}

advisor.report = advisor.processor.GenerateReport(cssList, pssList)

Expand All @@ -77,6 +83,15 @@ func (advisor *Advisor) PrintPodSecurityPolicy() error {
return err
}

func (advisor *Advisor) PrintOPAPolicy() string {
if advisor.OPAModulePolicy != nil {
err := advisor.OPAModulePolicy.String()
fmt.Printf(err)
return err
} else {
return ""
}
}
func (advisor *Advisor) GetPodSecurityPolicy() *v1beta1.PodSecurityPolicy {
return advisor.podSecurityPolicy
}
Expand Down
5 changes: 5 additions & 0 deletions advisor/processor/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package processor

import (
"fmt"
"github.com/open-policy-agent/opa/ast"
"sort"
"strings"

Expand Down Expand Up @@ -79,6 +80,10 @@ func (p *Processor) GeneratePSP(cssList []types.ContainerSecuritySpec, pssList [
return p.gen.GeneratePSP(cssList, pssList, p.namespace, p.serverGitVersion)
}

func (p *Processor) GenerateOPA(cssList []types.ContainerSecuritySpec, pssList []types.PodSecuritySpec, OPAdefaultRule bool) *ast.Module {
return p.gen.GenerateOPA(cssList, pssList, p.namespace, p.serverGitVersion, OPAdefaultRule)
}

// GeneratePSPGrant generates Pod Security Policies, Roles, RoleBindings for service accounts to use PSP
func (p *Processor) GeneratePSPGrant(cssList []types.ContainerSecuritySpec, pssList []types.PodSecuritySpec) (types.PSPGrantList, string) {
saSecuritySpecMap := map[string]*types.SASecuritySpec{}
Expand Down
1 change: 1 addition & 0 deletions advisor/types/securityspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ type PodSecuritySpec struct {
HostNetwork bool `json:"hostNetwork,omitempty"`
HostIPC bool `json:"hostIPC,omitempty"`
VolumeTypes []string `json:"volumeTypes,omitempty"`
VolumeMounts map[string]bool `json:"volumeMounts,omitempty"` //--> NEW
MountHostPaths map[string]bool `json:"mountedHostPath,omitempty"`
ServiceAccount string `json:"serviceAccount,omitempty"`
Sysctls []string `json:"sysctls,omitempty"`
Expand Down
Loading

0 comments on commit 0186148

Please sign in to comment.