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

Function pluginConfigFromJSON

credentials/tls/certprovider/pemfile/builder.go:62–103  ·  view source on GitHub ↗
(jd json.RawMessage)

Source from the content-addressed store, hash-verified

60}
61
62func pluginConfigFromJSON(jd json.RawMessage) (Options, error) {
63 // The only difference between this anonymous struct and the Options struct
64 // is that the refresh_interval is represented here as a duration proto,
65 // while in the latter a time.Duration is used.
66 cfg := &struct {
67 CertificateFile string `json:"certificate_file,omitempty"`
68 PrivateKeyFile string `json:"private_key_file,omitempty"`
69 CACertificateFile string `json:"ca_certificate_file,omitempty"`
70 SPIFFETrustBundleMapFile string `json:"spiffe_trust_bundle_map_file,omitempty"`
71 RefreshInterval json.RawMessage `json:"refresh_interval,omitempty"`
72 }{}
73 if err := json.Unmarshal(jd, cfg); err != nil {
74 return Options{}, fmt.Errorf("pemfile: json.Unmarshal(%s) failed: %v", string(jd), err)
75 }
76 if !envconfig.XDSSPIFFEEnabled {
77 cfg.SPIFFETrustBundleMapFile = ""
78 }
79
80 opts := Options{
81 CertFile: cfg.CertificateFile,
82 KeyFile: cfg.PrivateKeyFile,
83 RootFile: cfg.CACertificateFile,
84 SPIFFEBundleMapFile: cfg.SPIFFETrustBundleMapFile,
85 // Refresh interval is the only field in the configuration for which we
86 // support a default value. We cannot possibly have valid defaults for
87 // file paths to watch. Also, it is valid to specify an empty path for
88 // some of those fields if the user does not want to watch them.
89 RefreshDuration: defaultRefreshInterval,
90 }
91 if cfg.RefreshInterval != nil {
92 dur := &durationpb.Duration{}
93 if err := protojson.Unmarshal(cfg.RefreshInterval, dur); err != nil {
94 return Options{}, fmt.Errorf("pemfile: protojson.Unmarshal(%+v) failed: %v", cfg.RefreshInterval, err)
95 }
96 opts.RefreshDuration = dur.AsDuration()
97 }
98
99 if err := opts.validate(); err != nil {
100 return Options{}, err
101 }
102 return opts, nil
103}

Callers 1

ParseConfigMethod · 0.85

Calls 3

validateMethod · 0.95
UnmarshalMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected