sslobodr | d046be8 | 2019-01-16 10:02:22 -0500 | [diff] [blame] | 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" |
| 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 | // clusterAddress is the address of the current cluster. |
| 40 | // config is the initial configuration for this plugin. |
| 41 | // persister allows the plugin to save updated configuration. |
| 42 | type Factory func(clusterAddress string, config map[string]string, persister AuthProviderConfigPersister) (AuthProvider, error) |
| 43 | |
| 44 | // AuthProviderConfigPersister allows a plugin to persist configuration info |
| 45 | // for just itself. |
| 46 | type AuthProviderConfigPersister interface { |
| 47 | Persist(map[string]string) error |
| 48 | } |
| 49 | |
| 50 | // All registered auth provider plugins. |
| 51 | var pluginsLock sync.Mutex |
| 52 | var plugins = make(map[string]Factory) |
| 53 | |
| 54 | func RegisterAuthProviderPlugin(name string, plugin Factory) error { |
| 55 | pluginsLock.Lock() |
| 56 | defer pluginsLock.Unlock() |
| 57 | if _, found := plugins[name]; found { |
| 58 | return fmt.Errorf("Auth Provider Plugin %q was registered twice", name) |
| 59 | } |
| 60 | klog.V(4).Infof("Registered Auth Provider Plugin %q", name) |
| 61 | plugins[name] = plugin |
| 62 | return nil |
| 63 | } |
| 64 | |
| 65 | func GetAuthProvider(clusterAddress string, apc *clientcmdapi.AuthProviderConfig, persister AuthProviderConfigPersister) (AuthProvider, error) { |
| 66 | pluginsLock.Lock() |
| 67 | defer pluginsLock.Unlock() |
| 68 | p, ok := plugins[apc.Name] |
| 69 | if !ok { |
| 70 | return nil, fmt.Errorf("No Auth Provider found for name %q", apc.Name) |
| 71 | } |
| 72 | return p(clusterAddress, apc.Config, persister) |
| 73 | } |