Skip to content

Commit 571bf1c

Browse files
authored
Merge pull request #46 from darryk10/opa-generate
Introduced OPA Policy Support
2 parents feb1b53 + 99fd2b3 commit 571bf1c

File tree

11 files changed

+950
-120
lines changed

11 files changed

+950
-120
lines changed

README.MD

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Kube PodSecurityPolicy Advisor
22

3-
kube-psp-advisor is a tool that makes it easier to create K8s Pod Security Policies (PSPs) from either a live K8s environment or from a single .yaml file containing a pod specification (Deployment, DaemonSet, Pod, etc).
3+
kube-psp-advisor is a tool that makes it easier to create K8s Pod Security Policies (PSPs) or OPA Policy from either a live K8s environment or from a single .yaml file containing a pod specification (Deployment, DaemonSet, Pod, etc).
44

5-
It has 2 subcommands, `kube-psp-advisor inspect` and `kube-psp-advisor convert`. `inspect` connects to a K8s API server, scans the security context of workloads in a given namespace or the entire cluster, and generates a PSP based on the security context. `convert` works without connecting to an API Server, reading a single .yaml file containing a object with a pod spec and generating a PSP based on the file.
5+
It has 2 subcommands, `kube-psp-advisor inspect` and `kube-psp-advisor convert`. `inspect` connects to a K8s API server, scans the security context of workloads in a given namespace or the entire cluster, and generates a PSP or an OPA Policy based on the security context. `convert` works without connecting to an API Server, reading a single .yaml file containing a object with a pod spec and generating a PSP or OPA Policy based on the file.
66

77
## Installation as a Krew Plugin
88

@@ -20,15 +20,20 @@ The plugin will be available as `kubectl advise-psp`.
2020
- 2.1 ```./kube-psp-advisor inspect --report``` to print the details reports (why this PSP is recommended for the cluster)
2121
- 2.2 ```./kube-psp-advisor inspect --grant``` to print PSPs, roles and rolebindings for service accounts (refer to [psp-grant.yaml](./test-yaml/psp-grant.yaml))
2222
- 2.3 ```./kube-psp-advisor inspect --namespace=<ns>``` to print report or PSP(s) within a given namespace (default to all)
23+
- 2.4 ```./kube-psp-advisor inspect --opa``` to generate OPA Policy based on running cluster configuration
24+
- 2.5 ```./kube-psp-advisor inspect --opa --deny-by-default``` to generate an OPA Policy, where OPA Default Rule is Deny ALL
2325
4. ```./kube-psp-advisor convert --podFile <path> --pspFile <path>``` to generate a PSP from a single .yaml file.
24-
26+
- 4.1 ```./kube-psp-advisor convert --podFile <path> --pspFile <path> --opa``` to generate an OPA Policy from a single .yaml file.
27+
- 4.2 ```./kube-psp-advisor convert --podFile <path> --pspFile <path> --opa --deny-by-default``` to generate an OPA Policy from a single .yaml file, where OPA Default Rule is Deny ALL.
28+
2529
## Build and Run as Container
2630
1. ```docker build -t <Image Name> -f container/Dockerfile .```
2731
2. ```docker run -v ~/.kube:/root/.kube -v ~/.aws:/root/.aws <Image Name>``` (the `.aws` folder mount is optional and totally depends on your clould provider)
2832

2933
## Use Cases
3034
1. Help verify the deployment, daemonset settings in cluster and plan to reduce unnecessary privileges/resources
3135
2. Apply Pod Security Policy to the target cluster
36+
3. Apply OPA Policy to the target cluster
3237
3. flag `--namespace=<namespace>` is introduced to debug and narrow down the security context per namespace
3338

3439
## Attributes Aggregated for Pod Security Policy

advisor/advisor.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"os"
77

8+
"github.com/open-policy-agent/opa/ast"
9+
810
"github.com/sysdiglabs/kube-psp-advisor/advisor/types"
911

1012
"github.com/sysdiglabs/kube-psp-advisor/advisor/processor"
@@ -18,6 +20,7 @@ import (
1820

1921
type Advisor struct {
2022
podSecurityPolicy *v1beta1.PodSecurityPolicy
23+
OPAModulePolicy *ast.Module
2124
k8sClient *kubernetes.Clientset
2225
processor *processor.Processor
2326
report *report.Report
@@ -41,7 +44,7 @@ func NewAdvisor(kubeconfig string) (*Advisor, error) {
4144
}, nil
4245
}
4346

44-
func (advisor *Advisor) Process(namespace string, excludeNamespaces []string) error {
47+
func (advisor *Advisor) Process(namespace string, excludeNamespaces []string, OPAformat string, OPAdefaultRule bool) error {
4548
advisor.processor.SetNamespace(namespace)
4649
advisor.processor.SetExcludeNamespaces(excludeNamespaces)
4750

@@ -51,7 +54,11 @@ func (advisor *Advisor) Process(namespace string, excludeNamespaces []string) er
5154
return err
5255
}
5356

54-
advisor.podSecurityPolicy = advisor.processor.GeneratePSP(cssList, pssList)
57+
if OPAformat == "opa" {
58+
advisor.OPAModulePolicy = advisor.processor.GenerateOPA(cssList, pssList, OPAdefaultRule)
59+
} else if OPAformat == "psp" {
60+
advisor.podSecurityPolicy = advisor.processor.GeneratePSP(cssList, pssList)
61+
}
5562

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

@@ -77,6 +84,15 @@ func (advisor *Advisor) PrintPodSecurityPolicy() error {
7784
return err
7885
}
7986

87+
func (advisor *Advisor) PrintOPAPolicy() string {
88+
if advisor.OPAModulePolicy != nil {
89+
err := advisor.OPAModulePolicy.String()
90+
fmt.Printf(err)
91+
return err
92+
} else {
93+
return ""
94+
}
95+
}
8096
func (advisor *Advisor) GetPodSecurityPolicy() *v1beta1.PodSecurityPolicy {
8197
return advisor.podSecurityPolicy
8298
}

advisor/processor/generate.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"sort"
66
"strings"
77

8+
"github.com/open-policy-agent/opa/ast"
9+
810
"github.com/sysdiglabs/kube-psp-advisor/advisor/report"
911
"github.com/sysdiglabs/kube-psp-advisor/advisor/types"
1012
"github.com/sysdiglabs/kube-psp-advisor/generator"
@@ -79,6 +81,10 @@ func (p *Processor) GeneratePSP(cssList []types.ContainerSecuritySpec, pssList [
7981
return p.gen.GeneratePSP(cssList, pssList, p.namespace, p.serverGitVersion)
8082
}
8183

84+
func (p *Processor) GenerateOPA(cssList []types.ContainerSecuritySpec, pssList []types.PodSecuritySpec, OPAdefaultRule bool) *ast.Module {
85+
return p.gen.GenerateOPA(cssList, pssList, p.namespace, p.serverGitVersion, OPAdefaultRule)
86+
}
87+
8288
// GeneratePSPGrant generates Pod Security Policies, Roles, RoleBindings for service accounts to use PSP
8389
func (p *Processor) GeneratePSPGrant(cssList []types.ContainerSecuritySpec, pssList []types.PodSecuritySpec) (types.PSPGrantList, string) {
8490
saSecuritySpecMap := map[string]*types.SASecuritySpec{}

advisor/types/securityspec.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ type PodSecuritySpec struct {
8989
HostNetwork bool `json:"hostNetwork,omitempty"`
9090
HostIPC bool `json:"hostIPC,omitempty"`
9191
VolumeTypes []string `json:"volumeTypes,omitempty"`
92+
VolumeMounts map[string]bool `json:"volumeMounts,omitempty"` //--> NEW
9293
MountHostPaths map[string]bool `json:"mountedHostPath,omitempty"`
9394
ServiceAccount string `json:"serviceAccount,omitempty"`
9495
Sysctls []string `json:"sysctls,omitempty"`

0 commit comments

Comments
 (0)