Repository: grpc/grpc-go
File: credentials/tls/certprovider/pemfile/builder.go (Go)

Code before:
package pemfile

import (
	"encoding/json"
	"fmt"
	"time"

	"google.golang.org/grpc/credentials/tls/certprovider"
	"google.golang.org/protobuf/encoding/protojson"
	"google.golang.org/protobuf/types/known/durationpb"
)

const (
	// PluginName is the name of the PEM file watcher plugin.
	PluginName             = "file_watcher"
	defaultRefreshInterval = 10 * time.Minute
)

func init() {
	certprovider.Register(&pluginBuilder{})
}

type pluginBuilder struct{}

// ParseConfig parses the configuration data from JSON and returns a
// certprovider.BuildableConfig for the PEM file watcher.
func (p *pluginBuilder) ParseConfig(c any) (*certprovider.BuildableConfig, error) {
	data, ok := c.(json.RawMessage)
	if !ok {
		return nil, fmt.Errorf("meshca: unsupported config type: %T", c)
	}
	opts, err := pluginConfigFromJSON(data)
	if err != nil {
		return nil, err
	}
	return certprovider.NewBuildableConfig(PluginName, opts.canonical(), func(certprovider.BuildOptions) certprovider.Provider {
		return newProvider(opts)
	}), nil
}

// Name returns the name of the PEM file watcher plugin.
func (p *pluginBuilder) Name() string {
	return PluginName
}

func pluginConfigFromJSON(jd json.RawMessage) (Options, error) {
	// The only difference between this anonymous struct and the Options struct
	// is that the refresh_interval is represented here as a duration proto,
	// while in the latter a time.Duration is used.
	cfg := &struct {
		CertificateFile   string          `json:"certificate_file,omitempty"`

Review comment (bug):
Is vet failing for these?

It shouldn't.

This is an unexported type, so the docstrings won't appear anywhere.

These don't seem to provide any real value, so I'd just as soon delete them.  And I really hope we can configure the vet check better to not complain (if it is).

Code after:
package pemfile

import (
	"encoding/json"
	"fmt"
	"time"

	"google.golang.org/grpc/credentials/tls/certprovider"
	"google.golang.org/protobuf/encoding/protojson"
	"google.golang.org/protobuf/types/known/durationpb"
)

const (
	// PluginName is the name of the PEM file watcher plugin.
	PluginName             = "file_watcher"
	defaultRefreshInterval = 10 * time.Minute
)

func init() {
	certprovider.Register(&pluginBuilder{})
}

type pluginBuilder struct{}

func (p *pluginBuilder) ParseConfig(c any) (*certprovider.BuildableConfig, error) {
	data, ok := c.(json.RawMessage)
	if !ok {
		return nil, fmt.Errorf("meshca: unsupported config type: %T", c)
	}
	opts, err := pluginConfigFromJSON(data)
	if err != nil {
		return nil, err
	}
	return certprovider.NewBuildableConfig(PluginName, opts.canonical(), func(certprovider.BuildOptions) certprovider.Provider {
		return newProvider(opts)
	}), nil
}

func (p *pluginBuilder) Name() string {
	return PluginName
}

func pluginConfigFromJSON(jd json.RawMessage) (Options, error) {
	// The only difference between this anonymous struct and the Options struct
	// is that the refresh_interval is represented here as a duration proto,
	// while in the latter a time.Duration is used.
	cfg := &struct {
		CertificateFile   string          `json:"certificate_file,omitempty"`
		PrivateKeyFile    string          `json:"private_key_file,omitempty"`
		CACertificateFile string          `json:"ca_certificate_file,omitempty"`
		RefreshInterval   json.RawMessage `json:"refresh_interval,omitempty"`