bootstrap.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. *
  3. * Copyright 2022 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. // Package bootstrap provides the functionality to register possible options
  19. // for aspects of the xDS client through the bootstrap file.
  20. //
  21. // # Experimental
  22. //
  23. // Notice: This package is EXPERIMENTAL and may be changed or removed
  24. // in a later release.
  25. package bootstrap
  26. import (
  27. "encoding/json"
  28. "google.golang.org/grpc/credentials"
  29. )
  30. // registry is a map from credential type name to Credential builder.
  31. var registry = make(map[string]Credentials)
  32. // Credentials interface encapsulates a credentials.Bundle builder
  33. // that can be used for communicating with the xDS Management server.
  34. type Credentials interface {
  35. // Build returns a credential bundle associated with this credential.
  36. Build(config json.RawMessage) (credentials.Bundle, error)
  37. // Name returns the credential name associated with this credential.
  38. Name() string
  39. }
  40. // RegisterCredentials registers Credentials used for connecting to the xds
  41. // management server.
  42. //
  43. // NOTE: this function must only be called during initialization time (i.e. in
  44. // an init() function), and is not thread-safe. If multiple credentials are
  45. // registered with the same name, the one registered last will take effect.
  46. func RegisterCredentials(c Credentials) {
  47. registry[c.Name()] = c
  48. }
  49. // GetCredentials returns the credentials associated with a given name.
  50. // If no credentials are registered with the name, nil will be returned.
  51. func GetCredentials(name string) Credentials {
  52. if c, ok := registry[name]; ok {
  53. return c
  54. }
  55. return nil
  56. }