-
Notifications
You must be signed in to change notification settings - Fork 110
/
reader.go
846 lines (734 loc) · 25.5 KB
/
reader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
package config
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"reflect"
"runtime"
"github.com/a8m/envsubst"
"github.com/edaniels/golog"
"github.com/mitchellh/copystructure"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
apppb "go.viam.com/api/app/v1"
"go.viam.com/utils"
"go.viam.com/utils/artifact"
"go.viam.com/utils/rpc"
"go.viam.com/rdk/resource"
rutils "go.viam.com/rdk/utils"
)
// RDK versioning variables which are replaced by LD flags.
var (
Version = ""
GitRevision = ""
)
// An AttributeConverter converts a single attribute into a possibly
// different representation.
type AttributeConverter func(val interface{}) (interface{}, error)
// An AttributeMapConverter converts an attribute map into a possibly
// different representation.
type AttributeMapConverter func(attributes AttributeMap) (interface{}, error)
// A ComponentAttributeConverterRegistration describes how to convert a specific attribute
// for a model of a type of component.
type ComponentAttributeConverterRegistration struct {
Subtype resource.Subtype
Model resource.Model
Attr string
Conv AttributeConverter
}
// A ComponentAttributeMapConverterRegistration describes how to convert all attributes
// for a model of a type of component.
type ComponentAttributeMapConverterRegistration struct {
Subtype resource.Subtype
Model resource.Model
Conv AttributeMapConverter
RetType interface{} // the shape of what is converted to
}
// A ServiceAttributeMapConverterRegistration describes how to convert all attributes
// for a model of a type of service.
type ServiceAttributeMapConverterRegistration struct {
SvcType resource.Subtype
Model resource.Model
Conv AttributeMapConverter
RetType interface{} // the shape of what is converted to
}
var (
componentAttributeConverters = []ComponentAttributeConverterRegistration{}
componentAttributeMapConverters = []ComponentAttributeMapConverterRegistration{}
serviceAttributeMapConverters = []ServiceAttributeMapConverterRegistration{}
)
// RegisterComponentAttributeConverter associates a component type and model with a way to convert a
// particular attribute name.
func RegisterComponentAttributeConverter(subtype resource.Subtype, model resource.Model, attr string, conv AttributeConverter) {
componentAttributeConverters = append(componentAttributeConverters, ComponentAttributeConverterRegistration{subtype, model, attr, conv})
}
// RegisterComponentAttributeMapConverter associates a component type and model with a way to convert all attributes.
func RegisterComponentAttributeMapConverter(
subtype resource.Subtype,
model resource.Model,
conv AttributeMapConverter,
retType interface{},
) {
if retType == nil {
panic("retType should not be nil")
}
componentAttributeMapConverters = append(
componentAttributeMapConverters,
ComponentAttributeMapConverterRegistration{subtype, model, conv, retType},
)
}
// TransformAttributeMapToStruct uses an attribute map to transform attributes to the perscribed format.
func TransformAttributeMapToStruct(to interface{}, attributes AttributeMap) (interface{}, error) {
var md mapstructure.Metadata
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
TagName: "json",
Result: to,
Metadata: &md,
})
if err != nil {
return nil, err
}
if err := decoder.Decode(attributes); err != nil {
return nil, err
}
if attributes.Has("attributes") || len(md.Unused) == 0 {
return to, nil
}
// set as many unused attributes as possible
toV := reflect.ValueOf(to)
if toV.Kind() == reflect.Ptr {
toV = toV.Elem()
}
if attrsV := toV.FieldByName("Attributes"); attrsV.IsValid() &&
attrsV.Kind() == reflect.Map &&
attrsV.Type().Key().Kind() == reflect.String {
if attrsV.IsNil() {
attrsV.Set(reflect.MakeMap(attrsV.Type()))
}
mapValueType := attrsV.Type().Elem()
for _, key := range md.Unused {
val := attributes[key]
valV := reflect.ValueOf(val)
if valV.Type().AssignableTo(mapValueType) {
attrsV.SetMapIndex(reflect.ValueOf(key), valV)
}
}
}
return to, nil
}
// RegisterServiceAttributeMapConverter associates a service type with a way to convert all attributes.
func RegisterServiceAttributeMapConverter(
svcType resource.Subtype,
model resource.Model,
conv AttributeMapConverter,
retType interface{},
) {
if retType == nil {
panic("retType should not be nil")
}
serviceAttributeMapConverters = append(
serviceAttributeMapConverters,
ServiceAttributeMapConverterRegistration{svcType, model, conv, retType},
)
}
// RegisteredComponentAttributeConverters returns a copy of the registered component attribute converters.
func RegisteredComponentAttributeConverters() []ComponentAttributeConverterRegistration {
copied, err := copystructure.Copy(componentAttributeConverters)
if err != nil {
panic(err)
}
return copied.([]ComponentAttributeConverterRegistration)
}
// RegisteredComponentAttributeMapConverters returns a copy of the registered component attribute converters.
func RegisteredComponentAttributeMapConverters() []ComponentAttributeMapConverterRegistration {
copied, err := copystructure.Copy(componentAttributeMapConverters)
if err != nil {
panic(err)
}
return copied.([]ComponentAttributeMapConverterRegistration)
}
// RegisteredServiceAttributeMapConverters returns a copy of the registered component attribute converters.
func RegisteredServiceAttributeMapConverters() []ServiceAttributeMapConverterRegistration {
copied, err := copystructure.Copy(serviceAttributeMapConverters)
if err != nil {
panic(err)
}
return copied.([]ServiceAttributeMapConverterRegistration)
}
func findConverter(subtype resource.Subtype, model resource.Model, attr string) AttributeConverter {
for _, r := range componentAttributeConverters {
if r.Subtype == subtype && r.Model == model && r.Attr == attr {
return r.Conv
}
}
return nil
}
func findMapConverter(subtype resource.Subtype, model resource.Model) AttributeMapConverter {
for _, r := range componentAttributeMapConverters {
if r.Subtype == subtype && r.Model == model {
return r.Conv
}
}
return nil
}
func findServiceMapConverter(svcType resource.Subtype, model resource.Model) AttributeMapConverter {
for _, r := range serviceAttributeMapConverters {
if r.SvcType == svcType && r.Model == model {
return r.Conv
}
}
return nil
}
const (
cloudConfigSecretField = "Secret"
cloudConfigUserInfoField = "User-Info"
cloudConfigUserInfoHostField = "host"
cloudConfigUserInfoOSField = "os"
cloudConfigUserInfoLocalIPsField = "ips"
cloudConfigVersionField = "version"
cloudConfigGitRevisionField = "gitRevision"
)
// CreateCloudRequest makes a request to fetch the robot config
// from a cloud endpoint.
func CreateCloudRequest(ctx context.Context, cloudCfg *Cloud) (*http.Request, error) {
url := fmt.Sprintf("%s?id=%s", cloudCfg.Path, cloudCfg.ID)
r, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, errors.Wrapf(err, "error creating request for %s", url)
}
r.Header.Set(cloudConfigSecretField, cloudCfg.Secret)
agentInfo, err := getAgentInfo()
if err != nil {
return nil, err
}
userInfo := map[string]interface{}{}
userInfo[cloudConfigUserInfoHostField] = agentInfo.Host
userInfo[cloudConfigUserInfoOSField] = agentInfo.Os
userInfo[cloudConfigUserInfoLocalIPsField] = agentInfo.Ips
userInfo[cloudConfigVersionField] = agentInfo.Version
userInfo[cloudConfigGitRevisionField] = agentInfo.GitRevision
userInfoBytes, err := json.Marshal(userInfo)
if err != nil {
return nil, err
}
r.Header.Set(cloudConfigUserInfoField, string(userInfoBytes))
return r, nil
}
func getAgentInfo() (*apppb.AgentInfo, error) {
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
ips, err := utils.GetAllLocalIPv4s()
if err != nil {
return nil, err
}
return &apppb.AgentInfo{
Host: hostname,
Ips: ips,
Os: runtime.GOOS,
Version: Version,
GitRevision: GitRevision,
}, nil
}
// createCloudCertificateRequest makes a request to fetch the robot's TLS
// certificate from a cloud endpoint.
func createCloudCertificateRequest(ctx context.Context, cloudCfg *Cloud) (*http.Request, error) {
url := fmt.Sprintf("%s?id=%s&cert=true", cloudCfg.Path, cloudCfg.ID)
r, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, errors.Wrapf(err, "error creating request for %s", url)
}
r.Header.Set(cloudConfigSecretField, cloudCfg.Secret)
return r, nil
}
var viamDotDir = filepath.Join(os.Getenv("HOME"), ".viam")
func getCloudCacheFilePath(id string) string {
return filepath.Join(viamDotDir, fmt.Sprintf("cached_cloud_config_%s.json", id))
}
func readFromCache(id string) (*Config, error) {
r, err := os.Open(getCloudCacheFilePath(id))
if err != nil {
return nil, err
}
defer utils.UncheckedErrorFunc(r.Close)
unprocessedConfig := &Config{
ConfigFilePath: "",
}
if err := json.NewDecoder(r).Decode(unprocessedConfig); err != nil {
// clear the cache if we cannot parse the file.
clearCache(id)
return nil, errors.Wrap(err, "cannot parse the cached config as json")
}
return unprocessedConfig, nil
}
func storeToCache(id string, cfg *Config) error {
if err := os.MkdirAll(viamDotDir, 0o700); err != nil {
return err
}
md, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return err
}
reader := bytes.NewReader(md)
path := getCloudCacheFilePath(id)
return artifact.AtomicStore(path, reader, id)
}
func clearCache(id string) {
utils.UncheckedErrorFunc(func() error {
return os.Remove(getCloudCacheFilePath(id))
})
}
// readCertificateDataFromCloud returns the certificate from the app. It returns it as properties of a new Cloud config.
// The argument `cloudConfigFromDisk` represents the Cloud config from disk and only the Path parameters are used to
// generate the url. This is different from the Cloud config returned from the HTTP or gRPC API which do not have it.
//
// TODO(RSDK-539): The TLS certificate data should not be part of the Cloud portion of the config.
func readCertificateDataFromCloud(ctx context.Context, signalingInsecure bool, cloudConfigFromDisk *Cloud) (*Cloud, error) {
certReq, err := createCloudCertificateRequest(ctx, cloudConfigFromDisk)
if err != nil {
return nil, err
}
var client http.Client
defer client.CloseIdleConnections()
resp, err := client.Do(certReq)
if err != nil {
return nil, err
}
defer func() {
utils.UncheckedError(resp.Body.Close())
}()
dec := json.NewDecoder(resp.Body)
var certData Cloud
if err := dec.Decode(&certData); err != nil {
return nil, errors.Wrap(err, "error decoding certificate data from cloud; try again later")
}
if !signalingInsecure {
if certData.TLSCertificate == "" {
return nil, errors.New("no TLS certificate yet from cloud; try again later")
}
if certData.TLSPrivateKey == "" {
return nil, errors.New("no TLS private key yet from cloud; try again later")
}
}
// TODO(RSDK-539): we might want to use an internal type here. The gRPC api will not return a Cloud json struct.
return &Cloud{
TLSCertificate: certData.TLSCertificate,
TLSPrivateKey: certData.TLSPrivateKey,
}, nil
}
func readCertificateDataFromCloudGRPC(ctx context.Context,
signalingInsecure bool,
cloudConfigFromDisk *Cloud,
logger golog.Logger,
) (*Cloud, error) {
conn, err := CreateNewGRPCClient(ctx, cloudConfigFromDisk, logger)
if err != nil {
return nil, err
}
defer utils.UncheckedErrorFunc(conn.Close)
service := apppb.NewRobotServiceClient(conn)
res, err := service.Certificate(ctx, &apppb.CertificateRequest{Id: cloudConfigFromDisk.ID})
if err != nil {
// Check cache?
return nil, err
}
if !signalingInsecure {
if res.TlsCertificate == "" {
return nil, errors.New("no TLS certificate yet from cloud; try again later")
}
if res.TlsPrivateKey == "" {
return nil, errors.New("no TLS private key yet from cloud; try again later")
}
}
// TODO(RSDK-539): we might want to use an internal type here. The gRPC api will not return a Cloud json struct.
return &Cloud{
TLSCertificate: res.TlsCertificate,
TLSPrivateKey: res.TlsPrivateKey,
}, nil
}
// shouldCheckForCert checks the Cloud config to see if the TLS cert should be refetched.
func shouldCheckForCert(prevCloud, cloud *Cloud) bool {
// only checking the same fields as the ones that are explicitly overwritten in mergeCloudConfig
diffFQDN := prevCloud.FQDN != cloud.FQDN
diffLocalFQDN := prevCloud.LocalFQDN != cloud.LocalFQDN
diffSignalingAddr := prevCloud.SignalingAddress != cloud.SignalingAddress
diffSignalInsecure := prevCloud.SignalingInsecure != cloud.SignalingInsecure
diffManagedBy := prevCloud.ManagedBy != cloud.ManagedBy
diffLocSecret := prevCloud.LocationSecret != cloud.LocationSecret || !isLocationSecretsEqual(prevCloud, cloud)
return diffFQDN || diffLocalFQDN || diffSignalingAddr || diffSignalInsecure || diffManagedBy || diffLocSecret
}
func isLocationSecretsEqual(prevCloud, cloud *Cloud) bool {
if len(prevCloud.LocationSecrets) != len(cloud.LocationSecrets) {
return false
}
for i := range cloud.LocationSecrets {
if cloud.LocationSecrets[i].Secret != prevCloud.LocationSecrets[i].Secret {
return false
}
if cloud.LocationSecrets[i].ID != prevCloud.LocationSecrets[i].ID {
return false
}
}
return true
}
// readFromCloud fetches a robot config from the cloud based
// on the given config.
func readFromCloud(
ctx context.Context,
originalCfg,
prevCfg *Config,
shouldReadFromCache bool,
checkForNewCert bool,
logger golog.Logger,
) (*Config, error) {
logger.Debug("reading configuration from the cloud")
cloudCfg := originalCfg.Cloud
unprocessedConfig, cached, err := getFromCloudOrCache(ctx, cloudCfg, shouldReadFromCache, logger)
if err != nil {
if !cached {
err = errors.Wrap(err, "error getting cloud config")
}
return nil, err
}
// process the config
cfg, err := processConfigFromCloud(unprocessedConfig)
if err != nil {
// If we cannot process the config from the cache we should clear it.
if cached {
// clear cache
logger.Warn("Detected failure to process the cached config, clearing cache.")
clearCache(cloudCfg.ID)
}
return nil, err
}
if cfg.Cloud == nil {
return nil, errors.New("expected config to have cloud section")
}
// empty if not cached, since its a separate request, which we check next
tlsCertificate := cfg.Cloud.TLSCertificate
tlsPrivateKey := cfg.Cloud.TLSPrivateKey
if !cached {
// get cached certificate data
// read cached config from fs.
// process the config with fromReader() use processed config as cachedConfig to update the cert data.
unproccessedCachedConfig, err := readFromCache(cloudCfg.ID)
if err == nil {
cachedConfig, err := processConfigFromCloud(unproccessedCachedConfig)
if err != nil {
// clear cache
logger.Warn("Detected failure to process the cached config when retrieving TLS config, clearing cache.")
clearCache(cloudCfg.ID)
return nil, err
}
if cachedConfig.Cloud != nil {
tlsCertificate = cachedConfig.Cloud.TLSCertificate
tlsPrivateKey = cachedConfig.Cloud.TLSPrivateKey
}
} else if !os.IsNotExist(err) {
return nil, err
}
}
if prevCfg != nil && shouldCheckForCert(prevCfg.Cloud, cfg.Cloud) {
checkForNewCert = true
}
if checkForNewCert || tlsCertificate == "" || tlsPrivateKey == "" {
logger.Debug("reading tlsCertificate from the cloud")
// Use the SignalingInsecure from the Cloud config returned from the app not the initial config.
var certData *Cloud
if originalCfg.Cloud.AppAddress == "" {
certData, err = readCertificateDataFromCloud(ctx, cfg.Cloud.SignalingInsecure, cloudCfg)
} else {
certData, err = readCertificateDataFromCloudGRPC(ctx, cfg.Cloud.SignalingInsecure, cloudCfg, logger)
}
if err != nil {
var urlErr *url.Error
if !errors.Is(err, context.DeadlineExceeded) && (!errors.As(err, &urlErr) || urlErr.Temporary()) {
return nil, err
}
if tlsCertificate == "" || tlsPrivateKey == "" {
return nil, errors.Wrap(err, "error getting certificate data from cloud; try again later")
}
logger.Warnw("failed to refresh certificate data; using cached for now", "error", err)
} else {
tlsCertificate = certData.TLSCertificate
tlsPrivateKey = certData.TLSPrivateKey
}
}
fqdn := cfg.Cloud.FQDN
localFQDN := cfg.Cloud.LocalFQDN
signalingAddress := cfg.Cloud.SignalingAddress
signalingInsecure := cfg.Cloud.SignalingInsecure
managedBy := cfg.Cloud.ManagedBy
locationSecret := cfg.Cloud.LocationSecret
locationSecrets := cfg.Cloud.LocationSecrets
mergeCloudConfig := func(to *Config) {
*to.Cloud = *cloudCfg
to.Cloud.FQDN = fqdn
to.Cloud.LocalFQDN = localFQDN
to.Cloud.SignalingAddress = signalingAddress
to.Cloud.SignalingInsecure = signalingInsecure
to.Cloud.ManagedBy = managedBy
to.Cloud.LocationSecret = locationSecret
to.Cloud.LocationSecrets = locationSecrets
to.Cloud.TLSCertificate = tlsCertificate
to.Cloud.TLSPrivateKey = tlsPrivateKey
}
mergeCloudConfig(cfg)
// TODO(RSDK-1960): add more tests around config caching
unprocessedConfig.Cloud.TLSCertificate = tlsCertificate
unprocessedConfig.Cloud.TLSPrivateKey = tlsPrivateKey
if err := storeToCache(cloudCfg.ID, unprocessedConfig); err != nil {
logger.Errorw("failed to cache config", "error", err)
}
return cfg, nil
}
// Read reads a config from the given file.
func Read(
ctx context.Context,
filePath string,
logger golog.Logger,
) (*Config, error) {
buf, err := envsubst.ReadFile(filePath)
if err != nil {
return nil, err
}
return FromReader(ctx, filePath, bytes.NewReader(buf), logger)
}
// ReadLocalConfig reads a config from the given file but does not fetch any config from the remote servers.
func ReadLocalConfig(
ctx context.Context,
filePath string,
logger golog.Logger,
) (*Config, error) {
buf, err := envsubst.ReadFile(filePath)
if err != nil {
return nil, err
}
return fromReader(ctx, filePath, bytes.NewReader(buf), logger, false)
}
// FromReader reads a config from the given reader and specifies
// where, if applicable, the file the reader originated from.
func FromReader(
ctx context.Context,
originalPath string,
r io.Reader,
logger golog.Logger,
) (*Config, error) {
return fromReader(ctx, originalPath, r, logger, true)
}
// FromReader reads a config from the given reader and specifies
// where, if applicable, the file the reader originated from.
func fromReader(
ctx context.Context,
originalPath string,
r io.Reader,
logger golog.Logger,
shouldReadFromCloud bool,
) (*Config, error) {
// First read and processes config from disk
unprocessedConfig := Config{
ConfigFilePath: originalPath,
}
err := json.NewDecoder(r).Decode(&unprocessedConfig)
if err != nil {
return nil, errors.Wrapf(err, "failed to decode Config from json")
}
cfgFromDisk, err := processConfigLocalConfig(&unprocessedConfig)
if err != nil {
return nil, errors.Wrapf(err, "failed to process Config")
}
if shouldReadFromCloud && cfgFromDisk.Cloud != nil {
cfg, err := readFromCloud(ctx, cfgFromDisk, nil, true, true, logger)
return cfg, err
}
return cfgFromDisk, err
}
// processConfigFromCloud returns a copy of the current config with all attributes parsed
// and config validated with the assumption the config came from the cloud.
// Returns an error if the unprocessedConfig is non-valid.
func processConfigFromCloud(unprocessedConfig *Config) (*Config, error) {
return processConfig(unprocessedConfig, true)
}
// processConfigLocalConfig returns a copy of the current config with all attributes parsed
// and config validated with the assumption the config came from a local file.
// Returns an error if the unprocessedConfig is non-valid.
func processConfigLocalConfig(unprocessedConfig *Config) (*Config, error) {
return processConfig(unprocessedConfig, false)
}
func processConfig(unprocessedConfig *Config, fromCloud bool) (*Config, error) {
if err := unprocessedConfig.Ensure(fromCloud); err != nil {
return nil, err
}
cfg, err := unprocessedConfig.CopyOnlyPublicFields()
if err != nil {
return nil, errors.Wrap(err, "error copying config")
}
// Copy does not presve ConfigFilePath and we need to pass it along manually
cfg.ConfigFilePath = unprocessedConfig.ConfigFilePath
for idx, c := range cfg.Components {
cType := resource.NewSubtype(c.Namespace, "component", c.Type)
conv := findMapConverter(cType, c.Model)
// inner attributes may have their own converters
for k, v := range c.Attributes {
attrConv := findConverter(cType, c.Model, k)
if attrConv == nil {
continue
}
n, err := attrConv(v)
if err != nil {
return nil, errors.Wrapf(err, "error converting attribute for (%s, %s, %s)", c.Type, c.Model, k)
}
cfg.Components[idx].Attributes[k] = n
}
if conv == nil {
continue
}
converted, err := conv(c.Attributes)
if err != nil {
return nil, errors.Wrapf(err, "error converting attributes for (%s, %s)", c.Type, c.Model)
}
cfg.Components[idx].Attributes = nil
cfg.Components[idx].ConvertedAttributes = converted
}
for idx, c := range cfg.Services {
conv := findServiceMapConverter(resource.NewSubtype(c.Namespace, resource.ResourceTypeService, c.Type), c.Model)
if conv == nil {
continue
}
converted, err := conv(c.Attributes)
if err != nil {
return nil, errors.Wrapf(err, "error converting attributes for %s", c.Type)
}
cfg.Services[idx].Attributes = nil
cfg.Services[idx].ConvertedAttributes = converted
}
if err := cfg.Ensure(fromCloud); err != nil {
return nil, err
}
return cfg, nil
}
// getFromCloudOrCache returns the config from either the legacy HTTP endpoint or gRPC endpoint depending if the original config
// has the AppAddress set. If failures during cloud lookup fallback to the local cache if the error indicates it should.
func getFromCloudOrCache(ctx context.Context, cloudCfg *Cloud, shouldReadFromCache bool, logger golog.Logger) (*Config, bool, error) {
var cached bool
var cfg *Config
var errorShouldCheckCache bool
var err error
if cloudCfg.AppAddress == "" {
cfg, errorShouldCheckCache, err = getFromCloudHTTP(ctx, cloudCfg)
} else {
cfg, errorShouldCheckCache, err = getFromCloudGRPC(ctx, cloudCfg, logger)
}
if err != nil {
if shouldReadFromCache && errorShouldCheckCache {
logger.Warnw("failed to read config from cloud, checking cache", "error", err)
cachedConfig, cacheErr := readFromCache(cloudCfg.ID)
if cacheErr != nil {
if os.IsNotExist(cacheErr) {
// Return original http error if failed to load from cache.
return nil, cached, err
}
// return cache err
return nil, cached, cacheErr
}
logger.Warnw("unable to get cloud config; using cached version", "error", err)
cached = true
return cachedConfig, cached, nil
}
return nil, cached, err
}
return cfg, cached, nil
}
// getFromCloud actually does the fetching of the robot config and parses to an unprocessed Config struct.
func getFromCloudHTTP(ctx context.Context, cloudCfg *Cloud) (*Config, bool, error) {
shouldCheckCacheOnFailure := false
cloudReq, err := CreateCloudRequest(ctx, cloudCfg)
if err != nil {
return nil, false, err
}
unprocessedConfig := &Config{
ConfigFilePath: "",
}
var client http.Client
defer client.CloseIdleConnections()
resp, err := client.Do(cloudReq)
// Try to load from the cache
if err != nil {
var urlErr *url.Error
if !errors.Is(err, context.DeadlineExceeded) && (!errors.As(err, &urlErr) || urlErr.Temporary()) {
return nil, shouldCheckCacheOnFailure, err
}
shouldCheckCacheOnFailure = true
return nil, shouldCheckCacheOnFailure, err
}
defer func() {
utils.UncheckedError(resp.Body.Close())
}()
rd, err := io.ReadAll(resp.Body)
if err != nil {
return nil, shouldCheckCacheOnFailure, err
}
if resp.StatusCode != http.StatusOK {
if len(rd) != 0 {
return nil, shouldCheckCacheOnFailure, errors.Errorf("unexpected status %d: %s", resp.StatusCode, string(rd))
}
return nil, shouldCheckCacheOnFailure, errors.Errorf("unexpected status %d", resp.StatusCode)
}
if err := json.Unmarshal(rd, unprocessedConfig); err != nil {
return nil, shouldCheckCacheOnFailure, errors.Wrap(err, "cannot parse cloud config")
}
return unprocessedConfig, shouldCheckCacheOnFailure, nil
}
// getFromCloudGRPC actually does the fetching of the robot config from the gRPC endpoint.
func getFromCloudGRPC(ctx context.Context, cloudCfg *Cloud, logger golog.Logger) (*Config, bool, error) {
shouldCheckCacheOnFailure := true
conn, err := CreateNewGRPCClient(ctx, cloudCfg, logger)
if err != nil {
return nil, shouldCheckCacheOnFailure, err
}
defer utils.UncheckedErrorFunc(conn.Close)
agentInfo, err := getAgentInfo()
if err != nil {
return nil, shouldCheckCacheOnFailure, err
}
service := apppb.NewRobotServiceClient(conn)
res, err := service.Config(ctx, &apppb.ConfigRequest{Id: cloudCfg.ID, AgentInfo: agentInfo})
if err != nil {
// Check cache?
return nil, shouldCheckCacheOnFailure, err
}
cfg, err := FromProto(res.Config)
if err != nil {
// Check cache?
return nil, shouldCheckCacheOnFailure, err
}
return cfg, false, nil
}
// CreateNewGRPCClient creates a new grpc cloud configured to communicate with the robot service based on the cloud config given.
func CreateNewGRPCClient(ctx context.Context, cloudCfg *Cloud, logger golog.Logger) (rpc.ClientConn, error) {
u, err := url.Parse(cloudCfg.AppAddress)
if err != nil {
return nil, err
}
dialOpts := make([]rpc.DialOption, 0, 2)
// Only add credentials when secret is set.
if cloudCfg.Secret != "" {
dialOpts = append(dialOpts, rpc.WithEntityCredentials(cloudCfg.ID,
rpc.Credentials{
Type: rutils.CredentialsTypeRobotSecret,
Payload: cloudCfg.Secret,
},
))
}
if u.Scheme == "http" {
dialOpts = append(dialOpts, rpc.WithInsecure())
}
return rpc.DialDirectGRPC(ctx, u.Host, logger, dialOpts...)
}