psp-util

command module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 27, 2021 License: Apache-2.0 Imports: 1 Imported by: 0

README ΒΆ

Pod Security Policy Utility

GoReportCard Krew GithubDownloadTotals GithubActionsStatus

A Kubectl plugin to manage Pod Security Policy(PSP) and RBAC Resources.

Attach/Detach PSP to/from RBACs(Group, User) or ServiceAccounts and view the relations which PSP is effected to the Subjects in cluster.

See the details of PSP:

See the Best Practices of PSP:

Installation

You can install it by krew. After installing krew, run the following command:

kubectl krew install psp-util

Usage

$ kubectl psp-util

A Kubectl plugin to manage Pod Security Policy(PSP) and the related RBAC Resources.
Attach/Detach PSP to/from RBACs(Group, User) or ServiceAccounts and
view the relations which PSP is effected to the Subjects in cluster.

Complete documentation is available at http://github.com/jlandowner/psp-util

Usage:
  psp-util [command]

Available Commands:
  attach      Attach PSP to RBAC Subject (Auto generate managed ClusterRole and ClusterRoleBinding)
  clean       Clean managed ClusterRole and ClusterRoleBinding
  detach      Detach PSP from RBAC Subject
  help        Help about any command
  list        List PSP and RBAC associated with it.
  tree        View relational tree between PSP and Subjects
  version     Print the version number

Flags:
  -h, --help                help for psp-util
      --kubeconfig string   kube config file (default is $HOME/.kube/config)

Use "psp-util [command] --help" for more information about a command.

Command details

list

list shows all PSPs in cluster, and also ClusterRoles and ClusterRoleBindings associated with each of them.

A column Managed is whether these ClusterRoles and ClusterRoleBindings are auto-created and managed by psp-util.

$ kubectl psp-util list
PSP                                      ClusterRole                                       ClusterRoleBinding                                NS/Role         NS/RoleBinding   Managed
eks.privileged                           eks:podsecuritypolicy:privileged                  eks:podsecuritypolicy:authenticated                                                false
pod-security-policy-all-20200702180710   psp-util.pod-security-policy-all-20200702180710   psp-util.pod-security-policy-all-20200702180710                                    true
restricted                               psp-util.restricted                               psp-util.restricted                                                                true
myapp                                                                                                                                        default/myapp   default/myapp    false

tree

tree shows the relations between PSP and Subjects by tree expressions.

$ kubectl psp-util tree
πŸ“™ PSP eks.privileged
└── πŸ“• ClusterRole eks:podsecuritypolicy:privileged
    └── πŸ“˜ ClusterRoleBinding eks:podsecuritypolicy:authenticated
        └── πŸ“— Subject{Kind: Group, Name: system:master, Namespace: }
        └── πŸ“— Subject{Kind: ServiceAccount, Name: default, Namespace: kube-system}

πŸ“™ PSP pod-security-policy-all-20200702180710
└── πŸ“• ClusterRole psp-util.pod-security-policy-all-20200702180710
    └── πŸ“˜ ClusterRoleBinding psp-util.pod-security-policy-all-20200702180710
        └── πŸ“— Subject{Kind: Group, Name: system:authenticated, Namespace: }

πŸ“™ PSP restricted
└── πŸ“• ClusterRole psp-util.restricted
    └── πŸ“˜ ClusterRoleBinding psp-util.restricted
        └── πŸ“— Subject{Kind: Group, Name: my:group, Namespace: }
        └── πŸ“— Subject{Kind: ServiceAccount, Name: default, Namespace: default}

πŸ“™ PSP myapp
└── πŸ““ Role default/myapp
    └── πŸ““ RoleBinding default/myapp
        └── πŸ“— Subject{Kind: ServiceAccount, Name: myapp, Namespace: default}

attach

attach attaches PSP to Subjects(Group, User or ServiceAccount).

Usage:
  psp-util attach PSP      [ --group | --user | --sa ] SUBJECT-NAME [flags]

Flags:
  -g, --group string       set Subject's Name and use Kind Group
  -u, --user string        set Subject's Name and use Kind User
  -s, --sa string          set Subject's Name and use Kind ServiceAccount
  -n, --namespace string   set Subject's Namespace (only used when kind is ServiceAccount)
      --api-group string   set Subject's APIGroup
      --kind string        set Subject's Kind
      --name string        set Subject's Name

If there is no managed ClusterRole and ClusterRoleBinding associated with the given PSP, it will generate them automaticaly.

Examples

Attaching my-psp to Group system:authenticated.

$ kubectl psp-util attach my-psp --group system:authenticated

Attaching my-psp to default ServiceAccount in kube-system namespace.

$ kubectl psp-util attach my-psp --sa default -n kube-system

Or, you can set all Subject's info directly.

$ kubectl psp-util attach my-psp --api-group=rbac.authorization.k8s.io --kind=Group --name=system:authenticated

detach

detach detached a Subject from PSP.

It removes the Subject from the ClusterRoleBinding only if there is a managed ClusterRoleBinding in cluster.

All the options are the same as for the attach command.

Usage:
  psp-util detach PSP-NAME [ --group | --user | --sa ] SUBJECT-NAME [flags]

Flags:
  -g, --group string       set Subject's Name and use Kind Group
  -u, --user string        set Subject's Name and use Kind User
  -s, --sa string          set Subject's Name and use Kind ServiceAccount
  -n, --namespace string   set Subject's Namespace (only used when kind is ServiceAccount)
      --api-group string   set Subject's APIGroup
      --kind string        set Subject's Kind
      --name string        set Subject's Name

clean

clean delete a managed ClusterRole and ClusterRoleBinding.

NOTE: It does not delete the given PSP resource and non-managed ClusterRole and ClusterRoleBinding.

Usage:
  psp-util clean PSP-NAME

Demo

Create PSP by using kube-psp-advisor.

$ kubectl advise-psp inspect | kubectl apply -f -

See the PSP has been created.

$ kubectl psp-util list
PSP                                      ClusterRole                        ClusterRoleBinding                    Managed
eks.privileged                           eks:podsecuritypolicy:privileged   eks:podsecuritypolicy:authenticated   false
pod-security-policy-all-20200702180710  

Attach the PSP to Group named system:serviceaccounts:default

$ kubectl psp-util attach pod-security-policy-all-20200702180710 --group system:serviceaccounts:default
Managed ClusterRole is not found...Created
Managed ClusterRoleBinding is not found...Created

Then you can see a ClusterRole and ClusterRoleBinding are created and the PSP is effective to the Subject.

$ kubectl psp-util list
PSP                                      ClusterRole                                       ClusterRoleBinding                                Managed
eks.privileged                           eks:podsecuritypolicy:privileged                  eks:podsecuritypolicy:authenticated               false
pod-security-policy-all-20200702180710   psp-util.pod-security-policy-all-20200702180710   pdp-util.pod-security-policy-all-20200702180710   true

$ kubectl psp-util tree
πŸ“™ PSP eks.privileged
└── πŸ“• ClusterRole eks:podsecuritypolicy:privileged
    └── πŸ“˜ ClusterRoleBinding eks:podsecuritypolicy:authenticated
        └── πŸ“— Subject{Kind: Group, Name: system:authenticated, Namespace: }

πŸ“™ PSP pod-security-policy-all-20200702180710
└── πŸ“• ClusterRole psp-util.pod-security-policy-all-20200702180710
    └── πŸ“˜ ClusterRoleBinding psp-util.pod-security-policy-all-20200702180710
        └── πŸ“— Subject{Kind: Group, Name: system:serviceaccounts:default, Namespace: }

$ kubectl describe clusterrolebindings psp-util.pod-security-policy-all-20200702180710
Name:         psp-util.pod-security-policy-all-20200702180710
Labels:       <none>
Annotations:  psp-util.k8s.jlandowner.com/psp: pod-security-policy-all-20200702180710
Role:
  Kind:  ClusterRole
  Name:  psp-util.pod-security-policy-all-20200702180710
Subjects:
  Kind   Name                            Namespace
  ----   ----                            ---------
  Group  system:serviceaccounts:default  

LICENSE

Apache License Version 2.0 Copyright 2020 jlandowner

Documentation ΒΆ

The Go Gopher

There is no documentation for this package.

Directories ΒΆ

Path Synopsis
cmd
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL