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 19 // Package bootstrap provides the functionality to register possible options 20 // for aspects of the xDS client through the bootstrap file. 21 // 22 // # Experimental 23 // 24 // Notice: This package is EXPERIMENTAL and may be changed or removed 25 // in a later release. 26 package bootstrap 27 28 import ( 29 "encoding/json" 30 31 "google.golang.org/grpc/credentials" 32 ) 33 34 // registry is a map from credential type name to Credential builder. 35 var registry = make(map[string]Credentials) 36 37 // Credentials interface encapsulates a credentials.Bundle builder 38 // that can be used for communicating with the xDS Management server. 39 type Credentials interface { 40 // Build returns a credential bundle associated with this credential, and 41 // a function to cleans up additional resources associated with this bundle 42 // when it is no longer needed. 43 Build(config json.RawMessage) (credentials.Bundle, func(), error) 44 // Name returns the credential name associated with this credential. 45 Name() string 46 } 47 48 // RegisterCredentials registers Credentials used for connecting to the xds 49 // management server. 50 // 51 // NOTE: this function must only be called during initialization time (i.e. in 52 // an init() function), and is not thread-safe. If multiple credentials are 53 // registered with the same name, the one registered last will take effect. 54 func RegisterCredentials(c Credentials) { 55 registry[c.Name()] = c 56 } 57 58 // GetCredentials returns the credentials associated with a given name. 59 // If no credentials are registered with the name, nil will be returned. 60 func GetCredentials(name string) Credentials { 61 if c, ok := registry[name]; ok { 62 return c 63 } 64 65 return nil 66 } 67