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

Add DenyPSALabel admission plugin #10950

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
93 changes: 93 additions & 0 deletions pkg/admission/denypsalabel/admission.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
Copyright 2020 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package denypsalabel

import (
"context"
"fmt"
"io"
"strings"

"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apiserver/pkg/admission"
"k8s.io/klog/v2"
"k8s.io/kubernetes/pkg/apis/core"
)

const (
// PluginName is the name of this admission controller plugin
PluginName = "DenyPSALabel"
PSALabelPrefix = "pod-security.kubernetes.io"
galal-hussein marked this conversation as resolved.
Show resolved Hide resolved
)

// Register registers a plugin
func Register(plugins *admission.Plugins) {
plugins.Register(PluginName, func(config io.Reader) (admission.Interface, error) {
plugin := newPlugin()
return plugin, nil
})
}

// externalIPsDenierPlugin holds state for and implements the admission plugin.
type psaLabelDenialPlugin struct {
*admission.Handler
}

var _ admission.Interface = &psaLabelDenialPlugin{}
var _ admission.ValidationInterface = &psaLabelDenialPlugin{}

// newPlugin creates a new admission plugin.
func newPlugin() *psaLabelDenialPlugin {
return &psaLabelDenialPlugin{
Handler: admission.NewHandler(admission.Create, admission.Update),
}
}

// Admit ensures that modifications of the Service.Spec.ExternalIPs field are
brandond marked this conversation as resolved.
Show resolved Hide resolved
// denied
func (plug *psaLabelDenialPlugin) Validate(ctx context.Context, attr admission.Attributes, o admission.ObjectInterfaces) error {
if attr.GetResource().GroupResource() != core.Resource("namespaces") {
return nil
}

if len(attr.GetSubresource()) != 0 {
return nil
}

// if we can't convert then we don't handle this object so just return
newNS, ok := attr.GetObject().(*core.Namespace)
if !ok {
klog.V(3).Infof("Expected Namespace resource, got: %v", attr.GetKind())
return errors.NewInternalError(fmt.Errorf("Expected Namespace resource, got: %v", attr.GetKind()))
}

if !isPSALabel(newNS) {
return nil
}

klog.V(4).Infof("Denying use of PSA label on namespace %s", newNS.Name)
return admission.NewForbidden(attr, fmt.Errorf("Denying use of PSA label on Namespace"))
}

func isPSALabel(newNS *core.Namespace) bool {
for labelName := range newNS.Labels {
if strings.HasPrefix(labelName, PSALabelPrefix) {
galal-hussein marked this conversation as resolved.
Show resolved Hide resolved
return true
}
}
return false
}
7 changes: 7 additions & 0 deletions pkg/cli/cmds/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ type Server struct {
EtcdS3Timeout time.Duration
EtcdS3Insecure bool
ServiceLBNamespace string
DenyPSALabel bool
}

var (
Expand Down Expand Up @@ -586,6 +587,12 @@ var ServerFlags = []cli.Flag{
Usage: "(flags) Customized flag for kube-cloud-controller-manager process",
Value: &ServerConfig.ExtraCloudControllerArgs,
},
&cli.BoolFlag{
Name: "deny-psa-label",
Usage: "(experimental) Deny modifying namespaces with PSA security label",
galal-hussein marked this conversation as resolved.
Show resolved Hide resolved
Hidden: true,
Destination: &ServerConfig.DenyPSALabel,
},
}

func NewServerCommand(action func(*cli.Context) error) cli.Command {
Expand Down
1 change: 1 addition & 0 deletions pkg/cli/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ func run(app *cli.Context, cfg *cmds.Server, leaderControllers server.CustomCont
serverConfig.ControlConfig.SupervisorMetrics = cfg.SupervisorMetrics
serverConfig.ControlConfig.VLevel = cmds.LogConfig.VLevel
serverConfig.ControlConfig.VModule = cmds.LogConfig.VModule
serverConfig.ControlConfig.DenyPSALabel = cfg.DenyPSALabel

if !cfg.EtcdDisableSnapshots || cfg.ClusterReset {
serverConfig.ControlConfig.EtcdSnapshotCompress = cfg.EtcdSnapshotCompress
Expand Down
1 change: 1 addition & 0 deletions pkg/daemons/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ type Control struct {
ServerNodeName string
VLevel int
VModule string
DenyPSALabel bool

BindAddress string
SANs []string
Expand Down
10 changes: 10 additions & 0 deletions pkg/daemons/control/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"time"

"github.com/k3s-io/k3s/pkg/admission/denypsalabel"
"github.com/k3s-io/k3s/pkg/authenticator"
"github.com/k3s-io/k3s/pkg/cluster"
"github.com/k3s-io/k3s/pkg/daemons/config"
Expand All @@ -20,8 +21,10 @@ import (
"github.com/sirupsen/logrus"
authorizationv1 "k8s.io/api/authorization/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apiserver/pkg/admission"
logsapi "k8s.io/component-base/logs/api/v1"
"k8s.io/kubernetes/pkg/kubeapiserver/authorizer/modes"
"k8s.io/kubernetes/pkg/kubeapiserver/options"
"k8s.io/kubernetes/pkg/registry/core/node"

// for client metric registration
Expand Down Expand Up @@ -228,6 +231,13 @@ func apiServer(ctx context.Context, cfg *config.Control) error {

args := config.GetArgs(argsMap, cfg.ExtraAPIArgs)

// Add extra admission plugins
if cfg.DenyPSALabel {
extraPlugins := make(map[string]func(*admission.Plugins))
extraPlugins[denypsalabel.PluginName] = denypsalabel.Register
options.AdmissionPlugins = extraPlugins
}

logrus.Infof("Running kube-apiserver %s", config.ArgString(args))

return executor.APIServer(ctx, runtime.ETCDReady, args)
Expand Down
Loading