MCPcopy
hub / github.com/grpc/grpc-go / UnmarshalJSON

Method UnmarshalJSON

internal/xds/bootstrap/bootstrap.go:349–406  ·  view source on GitHub ↗

UnmarshalJSON takes the json data (a server) and unmarshals it to the struct.

(data []byte)

Source from the content-addressed store, hash-verified

347
348// UnmarshalJSON takes the json data (a server) and unmarshals it to the struct.
349func (sc *ServerConfig) UnmarshalJSON(data []byte) error {
350 server := serverConfigJSON{}
351 if err := json.Unmarshal(data, &server); err != nil {
352 return fmt.Errorf("xds: failed to JSON unmarshal server configuration during bootstrap: %v, config:\n%s", err, string(data))
353 }
354
355 sc.serverURI = server.ServerURI
356 sc.channelCreds = server.ChannelCreds
357 sc.callCredsConfigs = server.CallCredsConfigs
358 sc.serverFeatures = server.ServerFeatures
359
360 for _, cc := range server.ChannelCreds {
361 // We stop at the first credential type that we support.
362 c := bootstrap.GetChannelCredentials(cc.Type)
363 if c == nil {
364 continue
365 }
366 bundle, cancel, err := c.Build(cc.Config)
367 if err != nil {
368 return fmt.Errorf("failed to build credentials bundle from bootstrap for %q: %v", cc.Type, err)
369 }
370 sc.selectedChannelCreds = cc
371 sc.credsDialOption = grpc.WithCredentialsBundle(bundle)
372 if d, ok := bundle.(extraDialOptions); ok {
373 sc.extraDialOptions = d.DialOptions()
374 }
375 sc.cleanups = append(sc.cleanups, cancel)
376 break
377 }
378
379 if envconfig.XDSBootstrapCallCredsEnabled {
380 // Process call credentials - unlike channel creds, we use ALL supported
381 // types. Also, call credentials are optional as per gRFC A97.
382 for _, cfg := range server.CallCredsConfigs {
383 c := bootstrap.GetCallCredentials(cfg.Type)
384 if c == nil {
385 // Skip unsupported call credential types (don't fail bootstrap).
386 continue
387 }
388 callCreds, cancel, err := c.Build(cfg.Config)
389 if err != nil {
390 // Call credential validation failed - this should fail bootstrap.
391 return fmt.Errorf("failed to build call credentials from bootstrap for %q: %v", cfg.Type, err)
392 }
393 sc.selectedCallCreds = append(sc.selectedCallCreds, callCreds)
394 sc.extraDialOptions = append(sc.extraDialOptions, grpc.WithPerRPCCredentials(callCreds))
395 sc.cleanups = append(sc.cleanups, cancel)
396 }
397 }
398
399 if sc.serverURI == "" {
400 return fmt.Errorf("xds: `server_uri` field in server config cannot be empty: %s", string(data))
401 }
402 if sc.credsDialOption == nil {
403 return fmt.Errorf("xds: `channel_creds` field in server config cannot be empty: %s", string(data))
404 }
405 return nil
406}

Calls 8

GetChannelCredentialsFunction · 0.92
WithCredentialsBundleFunction · 0.92
GetCallCredentialsFunction · 0.92
WithPerRPCCredentialsFunction · 0.92
UnmarshalMethod · 0.65
ErrorfMethod · 0.65
BuildMethod · 0.65
DialOptionsMethod · 0.65