k8s.io/apiserver@v0.31.1/pkg/authentication/request/x509/verify_options.go (about)

     1  /*
     2  Copyright 2019 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 x509
    18  
    19  import (
    20  	"crypto/x509"
    21  	"fmt"
    22  
    23  	"k8s.io/client-go/util/cert"
    24  )
    25  
    26  // StaticVerifierFn is a VerifyOptionFunc that always returns the same value.  This allows verify options that cannot change.
    27  func StaticVerifierFn(opts x509.VerifyOptions) VerifyOptionFunc {
    28  	return func() (x509.VerifyOptions, bool) {
    29  		return opts, true
    30  	}
    31  }
    32  
    33  // NewStaticVerifierFromFile creates a new verification func from a file.  It reads the content and then fails.
    34  // It will return a nil function if you pass an empty CA file.
    35  func NewStaticVerifierFromFile(clientCA string) (VerifyOptionFunc, error) {
    36  	if len(clientCA) == 0 {
    37  		return nil, nil
    38  	}
    39  
    40  	// Wrap with an x509 verifier
    41  	var err error
    42  	opts := DefaultVerifyOptions()
    43  	opts.Roots, err = cert.NewPool(clientCA)
    44  	if err != nil {
    45  		return nil, fmt.Errorf("error loading certs from  %s: %v", clientCA, err)
    46  	}
    47  
    48  	return StaticVerifierFn(opts), nil
    49  }
    50  
    51  // StringSliceProvider is a way to get a string slice value.  It is heavily used for authentication headers among other places.
    52  type StringSliceProvider interface {
    53  	// Value returns the current string slice.  Callers should never mutate the returned value.
    54  	Value() []string
    55  }
    56  
    57  // StringSliceProviderFunc is a function that matches the StringSliceProvider interface
    58  type StringSliceProviderFunc func() []string
    59  
    60  // Value returns the current string slice.  Callers should never mutate the returned value.
    61  func (d StringSliceProviderFunc) Value() []string {
    62  	return d()
    63  }
    64  
    65  // StaticStringSlice a StringSliceProvider that returns a fixed value
    66  type StaticStringSlice []string
    67  
    68  // Value returns the current string slice.  Callers should never mutate the returned value.
    69  func (s StaticStringSlice) Value() []string {
    70  	return s
    71  }