k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/plugin/pkg/admission/certificates/signing/admission.go (about)

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package signing
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"io"
    23  	"reflect"
    24  
    25  	"k8s.io/klog/v2"
    26  
    27  	apiequality "k8s.io/apimachinery/pkg/api/equality"
    28  	"k8s.io/apiserver/pkg/admission"
    29  	genericadmissioninit "k8s.io/apiserver/pkg/admission/initializer"
    30  	"k8s.io/apiserver/pkg/authorization/authorizer"
    31  	api "k8s.io/kubernetes/pkg/apis/certificates"
    32  	"k8s.io/kubernetes/plugin/pkg/admission/certificates"
    33  )
    34  
    35  // PluginName is a string with the name of the plugin
    36  const PluginName = "CertificateSigning"
    37  
    38  // Register registers a plugin
    39  func Register(plugins *admission.Plugins) {
    40  	plugins.Register(PluginName, func(config io.Reader) (admission.Interface, error) {
    41  		return NewPlugin(), nil
    42  	})
    43  }
    44  
    45  // Plugin holds state for and implements the admission plugin.
    46  type Plugin struct {
    47  	*admission.Handler
    48  	authz authorizer.Authorizer
    49  }
    50  
    51  // SetAuthorizer sets the authorizer.
    52  func (p *Plugin) SetAuthorizer(authz authorizer.Authorizer) {
    53  	p.authz = authz
    54  }
    55  
    56  // ValidateInitialization ensures an authorizer is set.
    57  func (p *Plugin) ValidateInitialization() error {
    58  	if p.authz == nil {
    59  		return fmt.Errorf("%s requires an authorizer", PluginName)
    60  	}
    61  	return nil
    62  }
    63  
    64  var _ admission.ValidationInterface = &Plugin{}
    65  var _ genericadmissioninit.WantsAuthorizer = &Plugin{}
    66  
    67  // NewPlugin creates a new CSR approval admission plugin
    68  func NewPlugin() *Plugin {
    69  	return &Plugin{
    70  		Handler: admission.NewHandler(admission.Update),
    71  	}
    72  }
    73  
    74  var csrGroupResource = api.Resource("certificatesigningrequests")
    75  
    76  // Validate verifies that the requesting user has permission to sign
    77  // CertificateSigningRequests for the specified signerName.
    78  func (p *Plugin) Validate(ctx context.Context, a admission.Attributes, o admission.ObjectInterfaces) error {
    79  	// Ignore all calls to anything other than 'certificatesigningrequests/status'.
    80  	// Ignore all operations other than UPDATE.
    81  	if a.GetSubresource() != "status" ||
    82  		a.GetResource().GroupResource() != csrGroupResource {
    83  		return nil
    84  	}
    85  
    86  	oldCSR, ok := a.GetOldObject().(*api.CertificateSigningRequest)
    87  	if !ok {
    88  		return admission.NewForbidden(a, fmt.Errorf("expected type CertificateSigningRequest, got: %T", a.GetOldObject()))
    89  	}
    90  	csr, ok := a.GetObject().(*api.CertificateSigningRequest)
    91  	if !ok {
    92  		return admission.NewForbidden(a, fmt.Errorf("expected type CertificateSigningRequest, got: %T", a.GetObject()))
    93  	}
    94  
    95  	// only run if the status.certificate or status.conditions field has been changed
    96  	if reflect.DeepEqual(oldCSR.Status.Certificate, csr.Status.Certificate) && apiequality.Semantic.DeepEqual(oldCSR.Status.Conditions, csr.Status.Conditions) {
    97  		return nil
    98  	}
    99  
   100  	if !certificates.IsAuthorizedForSignerName(ctx, p.authz, a.GetUserInfo(), "sign", oldCSR.Spec.SignerName) {
   101  		klog.V(4).Infof("user not permitted to sign CertificateSigningRequest %q with signerName %q", oldCSR.Name, oldCSR.Spec.SignerName)
   102  		return admission.NewForbidden(a, fmt.Errorf("user not permitted to sign requests with signerName %q", oldCSR.Spec.SignerName))
   103  	}
   104  
   105  	return nil
   106  }