github.com/google/fleetspeak@v0.1.15-0.20240426164851-4f31f62c1aea/fleetspeak/src/client/generic/config.go (about) 1 // Copyright 2019 Google LLC 2 // 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 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package generic provides support methods to build a generic client, not bound 16 // to a particular installation. This means that critical parameters must be 17 // read from a configuration file. 18 // 19 // Serious users might consider building and packaging their own clients with 20 // parameters hardcoded in the client binary. 21 package generic 22 23 import ( 24 "crypto/x509" 25 "errors" 26 "net/url" 27 "runtime" 28 "strings" 29 30 log "github.com/golang/glog" 31 32 "github.com/google/fleetspeak/fleetspeak/src/client/config" 33 34 gpb "github.com/google/fleetspeak/fleetspeak/src/client/generic/proto/fleetspeak_client_generic" 35 fspb "github.com/google/fleetspeak/fleetspeak/src/common/proto/fleetspeak" 36 ) 37 38 // MakeConfiguration returns a config.Configuration based on the provided gpb.Config. 39 func MakeConfiguration(cfg *gpb.Config) (config.Configuration, error) { 40 trustedCerts := x509.NewCertPool() 41 if !trustedCerts.AppendCertsFromPEM([]byte(cfg.TrustedCerts)) { 42 return config.Configuration{}, errors.New("unable to parse trusted_certs") 43 } 44 log.Infof("Read %d trusted certificates.", len(trustedCerts.Subjects())) 45 46 if len(cfg.Server) == 0 { 47 return config.Configuration{}, errors.New("no server provided") 48 } 49 50 labels := []*fspb.Label{ 51 {ServiceName: "client", Label: runtime.GOARCH}, 52 {ServiceName: "client", Label: runtime.GOOS}} 53 54 for _, l := range cfg.ClientLabel { 55 if strings.TrimSpace(l) == "" { 56 continue 57 } 58 labels = append(labels, &fspb.Label{ServiceName: "client", Label: l}) 59 } 60 61 ph, err := makePersistenceHandler(cfg) 62 if err != nil { 63 return config.Configuration{}, err 64 } 65 66 var proxy *url.URL 67 if cfg.Proxy != "" { 68 proxy, err = url.Parse(cfg.Proxy) 69 if err != nil { 70 return config.Configuration{}, err 71 } 72 } 73 74 return config.Configuration{ 75 TrustedCerts: trustedCerts, 76 Servers: cfg.Server, 77 PersistenceHandler: ph, 78 ClientLabels: labels, 79 Proxy: proxy, 80 ClientCertificateHeader: cfg.ClientCertificateHeader, 81 ServerName: cfg.ServerName, 82 }, nil 83 }