...

Source file src/k8s.io/client-go/rest/plugin.go

Documentation: k8s.io/client-go/rest

     1  /*
     2  Copyright 2016 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 rest
    18  
    19  import (
    20  	"fmt"
    21  	"net/http"
    22  	"sync"
    23  
    24  	"k8s.io/klog/v2"
    25  
    26  	clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
    27  )
    28  
    29  type AuthProvider interface {
    30  	// WrapTransport allows the plugin to create a modified RoundTripper that
    31  	// attaches authorization headers (or other info) to requests.
    32  	WrapTransport(http.RoundTripper) http.RoundTripper
    33  	// Login allows the plugin to initialize its configuration. It must not
    34  	// require direct user interaction.
    35  	Login() error
    36  }
    37  
    38  // Factory generates an AuthProvider plugin.
    39  //
    40  //	clusterAddress is the address of the current cluster.
    41  //	config is the initial configuration for this plugin.
    42  //	persister allows the plugin to save updated configuration.
    43  type Factory func(clusterAddress string, config map[string]string, persister AuthProviderConfigPersister) (AuthProvider, error)
    44  
    45  // AuthProviderConfigPersister allows a plugin to persist configuration info
    46  // for just itself.
    47  type AuthProviderConfigPersister interface {
    48  	Persist(map[string]string) error
    49  }
    50  
    51  type noopPersister struct{}
    52  
    53  func (n *noopPersister) Persist(_ map[string]string) error {
    54  	// no operation persister
    55  	return nil
    56  }
    57  
    58  // All registered auth provider plugins.
    59  var pluginsLock sync.Mutex
    60  var plugins = make(map[string]Factory)
    61  
    62  func RegisterAuthProviderPlugin(name string, plugin Factory) error {
    63  	pluginsLock.Lock()
    64  	defer pluginsLock.Unlock()
    65  	if _, found := plugins[name]; found {
    66  		return fmt.Errorf("auth Provider Plugin %q was registered twice", name)
    67  	}
    68  	klog.V(4).Infof("Registered Auth Provider Plugin %q", name)
    69  	plugins[name] = plugin
    70  	return nil
    71  }
    72  
    73  func GetAuthProvider(clusterAddress string, apc *clientcmdapi.AuthProviderConfig, persister AuthProviderConfigPersister) (AuthProvider, error) {
    74  	pluginsLock.Lock()
    75  	defer pluginsLock.Unlock()
    76  	p, ok := plugins[apc.Name]
    77  	if !ok {
    78  		return nil, fmt.Errorf("no Auth Provider found for name %q", apc.Name)
    79  	}
    80  	if persister == nil {
    81  		persister = &noopPersister{}
    82  	}
    83  	return p(clusterAddress, apc.Config, persister)
    84  }
    85  

View as plain text