plugin.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package rest
  14. import (
  15. "fmt"
  16. "net/http"
  17. "sync"
  18. "github.com/golang/glog"
  19. clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
  20. )
  21. type AuthProvider interface {
  22. // WrapTransport allows the plugin to create a modified RoundTripper that
  23. // attaches authorization headers (or other info) to requests.
  24. WrapTransport(http.RoundTripper) http.RoundTripper
  25. // Login allows the plugin to initialize its configuration. It must not
  26. // require direct user interaction.
  27. Login() error
  28. }
  29. // Factory generates an AuthProvider plugin.
  30. // clusterAddress is the address of the current cluster.
  31. // config is the initial configuration for this plugin.
  32. // persister allows the plugin to save updated configuration.
  33. type Factory func(clusterAddress string, config map[string]string, persister AuthProviderConfigPersister) (AuthProvider, error)
  34. // AuthProviderConfigPersister allows a plugin to persist configuration info
  35. // for just itself.
  36. type AuthProviderConfigPersister interface {
  37. Persist(map[string]string) error
  38. }
  39. // All registered auth provider plugins.
  40. var pluginsLock sync.Mutex
  41. var plugins = make(map[string]Factory)
  42. func RegisterAuthProviderPlugin(name string, plugin Factory) error {
  43. pluginsLock.Lock()
  44. defer pluginsLock.Unlock()
  45. if _, found := plugins[name]; found {
  46. return fmt.Errorf("Auth Provider Plugin %q was registered twice", name)
  47. }
  48. glog.V(4).Infof("Registered Auth Provider Plugin %q", name)
  49. plugins[name] = plugin
  50. return nil
  51. }
  52. func GetAuthProvider(clusterAddress string, apc *clientcmdapi.AuthProviderConfig, persister AuthProviderConfigPersister) (AuthProvider, error) {
  53. pluginsLock.Lock()
  54. defer pluginsLock.Unlock()
  55. p, ok := plugins[apc.Name]
  56. if !ok {
  57. return nil, fmt.Errorf("No Auth Provider found for name %q", apc.Name)
  58. }
  59. return p(clusterAddress, apc.Config, persister)
  60. }