plugin.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
  19. )
  20. type AuthProvider interface {
  21. // WrapTransport allows the plugin to create a modified RoundTripper that
  22. // attaches authorization headers (or other info) to requests.
  23. WrapTransport(http.RoundTripper) http.RoundTripper
  24. // Login allows the plugin to initialize its configuration. It must not
  25. // require direct user interaction.
  26. Login() error
  27. }
  28. // Factory generates an AuthProvider plugin.
  29. //
  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. type noopPersister struct{}
  40. func (n *noopPersister) Persist(_ map[string]string) error {
  41. // no operation persister
  42. return nil
  43. }
  44. // All registered auth provider plugins.
  45. var pluginsLock sync.Mutex
  46. var plugins = make(map[string]Factory)
  47. func RegisterAuthProviderPlugin(name string, plugin Factory) error {
  48. pluginsLock.Lock()
  49. defer pluginsLock.Unlock()
  50. if _, found := plugins[name]; found {
  51. return fmt.Errorf("auth Provider Plugin %q was registered twice", name)
  52. }
  53. // RegisterAuthProviderPlugin gets called during the init phase before
  54. // logging is initialized and therefore should not emit logs. If you
  55. // need this message for debugging something, then uncomment it.
  56. // klog.V(4).Infof("Registered Auth Provider Plugin %q", name)
  57. plugins[name] = plugin
  58. return nil
  59. }
  60. func GetAuthProvider(clusterAddress string, apc *clientcmdapi.AuthProviderConfig, persister AuthProviderConfigPersister) (AuthProvider, error) {
  61. pluginsLock.Lock()
  62. defer pluginsLock.Unlock()
  63. p, ok := plugins[apc.Name]
  64. if !ok {
  65. return nil, fmt.Errorf("no Auth Provider found for name %q", apc.Name)
  66. }
  67. if persister == nil {
  68. persister = &noopPersister{}
  69. }
  70. return p(clusterAddress, apc.Config, persister)
  71. }