diff --git a/.chloggen/mx-psi_configgrpc-validation.yaml b/.chloggen/mx-psi_configgrpc-validation.yaml new file mode 100644 index 00000000000..fbc9a393eb6 --- /dev/null +++ b/.chloggen/mx-psi_configgrpc-validation.yaml @@ -0,0 +1,26 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. receiver/otlp) +component: pkg/config/configgrpc + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Statically validate gRPC endpoint + +# One or more tracking issues or pull requests related to the change +issues: [10451] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: | + This validation was already done in the OTLP exporter. It will now be applied to any gRPC client. + +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] diff --git a/config/configgrpc/configgrpc.go b/config/configgrpc/configgrpc.go index 1d728483188..e62f1db8d2a 100644 --- a/config/configgrpc/configgrpc.go +++ b/config/configgrpc/configgrpc.go @@ -9,6 +9,9 @@ import ( "errors" "fmt" "math" + "net" + "regexp" + "strconv" "strings" "time" @@ -224,6 +227,24 @@ func NewDefaultServerConfig() ServerConfig { } func (cc *ClientConfig) Validate() error { + if after, ok := strings.CutPrefix(cc.Endpoint, "unix://"); ok { + if after == "" { + return errors.New("unix socket path cannot be empty") + } + return nil + } + + if endpoint := cc.sanitizedEndpoint(); endpoint != "" { + // Validate that the port is in the address + _, port, err := net.SplitHostPort(endpoint) + if err != nil { + return err + } + if _, err := strconv.Atoi(port); err != nil { + return fmt.Errorf(`invalid port "%v"`, port) + } + } + if cc.BalancerName != "" { if balancer.Get(cc.BalancerName) == nil { return fmt.Errorf("invalid balancer_name: %s", cc.BalancerName) @@ -240,6 +261,9 @@ func (cc *ClientConfig) sanitizedEndpoint() string { return strings.TrimPrefix(cc.Endpoint, "http://") case cc.isSchemeHTTPS(): return strings.TrimPrefix(cc.Endpoint, "https://") + case strings.HasPrefix(cc.Endpoint, "dns://"): + r := regexp.MustCompile(`^dns:///?`) + return r.ReplaceAllString(cc.Endpoint, "") default: return cc.Endpoint } diff --git a/config/configgrpc/configgrpc_test.go b/config/configgrpc/configgrpc_test.go index bbcc3581ca1..355b106dd54 100644 --- a/config/configgrpc/configgrpc_test.go +++ b/config/configgrpc/configgrpc_test.go @@ -263,6 +263,24 @@ func TestAllGrpcClientSettings(t *testing.T) { } } +func TestSanitizeEndpoint(t *testing.T) { + cfg := NewDefaultClientConfig() + cfg.Endpoint = "dns://authority/backend.example.com:4317" + assert.Equal(t, "authority/backend.example.com:4317", cfg.sanitizedEndpoint()) + cfg.Endpoint = "dns:///backend.example.com:4317" + assert.Equal(t, "backend.example.com:4317", cfg.sanitizedEndpoint()) + cfg.Endpoint = "dns:////backend.example.com:4317" + assert.Equal(t, "/backend.example.com:4317", cfg.sanitizedEndpoint()) +} + +func TestValidateEndpoint(t *testing.T) { + cfg := NewDefaultClientConfig() + cfg.Endpoint = "dns://authority/backend.example.com:4317" + assert.NoError(t, cfg.Validate()) + cfg.Endpoint = "unix:///my/unix/socket.sock" + assert.NoError(t, cfg.Validate()) +} + func TestHeaders(t *testing.T) { traceServer := &grpcTraceServer{} server, addr := traceServer.startTestServer(t, configoptional.Some(ServerConfig{ @@ -457,7 +475,7 @@ func TestGRPCClientSettingsError(t *testing.T) { err: "failed to load TLS config: failed to load CA CertPool File: failed to load cert /doesnt/exist:", settings: ClientConfig{ Headers: nil, - Endpoint: "", + Endpoint: "localhost:1234", Compression: "", TLS: configtls.ClientConfig{ Config: configtls.Config{ @@ -472,7 +490,7 @@ func TestGRPCClientSettingsError(t *testing.T) { err: "failed to load TLS config: failed to load TLS cert and key: for auth via TLS, provide both certificate and key, or neither", settings: ClientConfig{ Headers: nil, - Endpoint: "", + Endpoint: "localhost:1234", Compression: "", TLS: configtls.ClientConfig{ Config: configtls.Config{ diff --git a/exporter/otlpexporter/config.go b/exporter/otlpexporter/config.go index 7bcd17c0235..1d1a9000712 100644 --- a/exporter/otlpexporter/config.go +++ b/exporter/otlpexporter/config.go @@ -5,15 +5,13 @@ package otlpexporter // import "go.opentelemetry.io/collector/exporter/otlpexpor import ( "errors" - "fmt" - "net" "regexp" - "strconv" "strings" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/config/configgrpc" "go.opentelemetry.io/collector/config/configretry" + "go.opentelemetry.io/collector/confmap/xconfmap" "go.opentelemetry.io/collector/exporter/exporterhelper" ) @@ -28,28 +26,15 @@ type Config struct { _ struct{} } -func (c *Config) Validate() error { - if after, ok := strings.CutPrefix(c.ClientConfig.Endpoint, "unix://"); ok { - if after == "" { - return errors.New("unix socket path cannot be empty") - } - return nil - } +var ( + _ component.Config = (*Config)(nil) + _ xconfmap.Validator = (*Config)(nil) +) - endpoint := c.sanitizedEndpoint() - if endpoint == "" { +func (c *Config) Validate() error { + if endpoint := c.sanitizedEndpoint(); endpoint == "" { return errors.New(`requires a non-empty "endpoint"`) } - - // Validate that the port is in the address - _, port, err := net.SplitHostPort(endpoint) - if err != nil { - return err - } - if _, err := strconv.Atoi(port); err != nil { - return fmt.Errorf(`invalid port "%s"`, port) - } - return nil } @@ -66,5 +51,3 @@ func (c *Config) sanitizedEndpoint() string { return c.ClientConfig.Endpoint } } - -var _ component.Config = (*Config)(nil) diff --git a/exporter/otlpexporter/config_test.go b/exporter/otlpexporter/config_test.go index c87e0d9de37..d641fff8b67 100644 --- a/exporter/otlpexporter/config_test.go +++ b/exporter/otlpexporter/config_test.go @@ -180,23 +180,12 @@ func TestValidDNSEndpoint(t *testing.T) { factory := NewFactory() cfg := factory.CreateDefaultConfig().(*Config) cfg.ClientConfig.Endpoint = "dns://authority/backend.example.com:4317" - assert.NoError(t, cfg.Validate()) + assert.NoError(t, xconfmap.Validate(cfg)) } func TestValidUnixSocketEndpoint(t *testing.T) { factory := NewFactory() cfg := factory.CreateDefaultConfig().(*Config) cfg.ClientConfig.Endpoint = "unix:///my/unix/socket.sock" - assert.NoError(t, cfg.Validate()) -} - -func TestSanitizeEndpoint(t *testing.T) { - factory := NewFactory() - cfg := factory.CreateDefaultConfig().(*Config) - cfg.ClientConfig.Endpoint = "dns://authority/backend.example.com:4317" - assert.Equal(t, "authority/backend.example.com:4317", cfg.sanitizedEndpoint()) - cfg.ClientConfig.Endpoint = "dns:///backend.example.com:4317" - assert.Equal(t, "backend.example.com:4317", cfg.sanitizedEndpoint()) - cfg.ClientConfig.Endpoint = "dns:////backend.example.com:4317" - assert.Equal(t, "/backend.example.com:4317", cfg.sanitizedEndpoint()) + assert.NoError(t, xconfmap.Validate(cfg)) }