Skip to content

[confighttp] Use configoptional.Optional for optional fields #13109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .chloggen/mx-psi_use-configoptional-in-confighttp-tls.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: breaking

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: confighttp

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Rename `ClientConfig.TLSSetting` and `ServerConfig.TLSSetting` to `ClientConfig.TLS` and `ServerConfig.TLS`."

# One or more tracking issues or pull requests related to the change
issues: [9478]

# (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:

# 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: [api]
25 changes: 25 additions & 0 deletions .chloggen/mx-psi_use-configoptional-in-confighttp.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: breaking

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: confighttp

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Use configooptional.Optional in confighttp"

# One or more tracking issues or pull requests related to the change
issues: [9478]

# (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:

# 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: [api]
37 changes: 20 additions & 17 deletions config/confighttp/confighttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"go.opentelemetry.io/collector/config/confighttp/internal"
"go.opentelemetry.io/collector/config/configmiddleware"
"go.opentelemetry.io/collector/config/configopaque"
"go.opentelemetry.io/collector/config/configoptional"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/extension/extensionauth"
)
Expand Down Expand Up @@ -74,7 +75,7 @@ type ClientConfig struct {
Headers map[string]configopaque.String `mapstructure:"headers,omitempty"`

// Auth configuration for outgoing HTTP calls.
Auth *configauth.Config `mapstructure:"auth,omitempty"`
Auth configoptional.Optional[configauth.Config] `mapstructure:"auth,omitempty"`

// The compression key for supported compression types within collector.
Compression configcompression.Type `mapstructure:"compression,omitempty"`
Expand Down Expand Up @@ -225,13 +226,14 @@ func (hcs *ClientConfig) ToClient(ctx context.Context, host component.Host, sett
// The Auth RoundTripper should always be the innermost to ensure that
// request signing-based auth mechanisms operate after compression
// and header middleware modifies the request
if hcs.Auth != nil {
if hcs.Auth.HasValue() {
ext := host.GetExtensions()
if ext == nil {
return nil, errors.New("extensions configuration not found")
}

httpCustomAuthRoundTripper, aerr := hcs.Auth.GetHTTPClientAuthenticator(ctx, ext)
auth := hcs.Auth.Get()
httpCustomAuthRoundTripper, aerr := auth.GetHTTPClientAuthenticator(ctx, ext)
if aerr != nil {
return nil, aerr
}
Expand Down Expand Up @@ -315,13 +317,13 @@ type ServerConfig struct {
Endpoint string `mapstructure:"endpoint,omitempty"`

// TLS struct exposes TLS client configuration.
TLS *configtls.ServerConfig `mapstructure:"tls"`
TLS configoptional.Optional[configtls.ServerConfig] `mapstructure:"tls"`

// CORS configures the server for HTTP cross-origin resource sharing (CORS).
CORS *CORSConfig `mapstructure:"cors"`
CORS configoptional.Optional[CORSConfig] `mapstructure:"cors"`

// Auth for this receiver
Auth *AuthConfig `mapstructure:"auth,omitempty"`
Auth configoptional.Optional[AuthConfig] `mapstructure:"auth,omitempty"`

// MaxRequestBodySize sets the maximum request body size in bytes. Default: 20MiB.
MaxRequestBodySize int64 `mapstructure:"max_request_body_size,omitempty"`
Expand Down Expand Up @@ -378,7 +380,6 @@ type ServerConfig struct {
func NewDefaultServerConfig() ServerConfig {
return ServerConfig{
ResponseHeaders: map[string]configopaque.String{},
CORS: NewDefaultCORSConfig(),
WriteTimeout: 30 * time.Second,
ReadHeaderTimeout: 1 * time.Minute,
IdleTimeout: 1 * time.Minute,
Expand All @@ -403,9 +404,9 @@ func (hss *ServerConfig) ToListener(ctx context.Context) (net.Listener, error) {
return nil, err
}

if hss.TLS != nil {
if hss.TLS.HasValue() {
var tlsCfg *tls.Config
tlsCfg, err = hss.TLS.LoadTLSConfig(ctx)
tlsCfg, err = hss.TLS.Get().LoadTLSConfig(ctx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -484,25 +485,27 @@ func (hss *ServerConfig) ToServer(ctx context.Context, host component.Host, sett
handler = maxRequestBodySizeInterceptor(handler, hss.MaxRequestBodySize)
}

if hss.Auth != nil {
server, err := hss.Auth.GetServerAuthenticator(context.Background(), host.GetExtensions())
if hss.Auth.HasValue() {
auth := hss.Auth.Get()
server, err := auth.GetServerAuthenticator(context.Background(), host.GetExtensions())
if err != nil {
return nil, err
}

handler = authInterceptor(handler, server, hss.Auth.RequestParameters, serverOpts)
handler = authInterceptor(handler, server, auth.RequestParameters, serverOpts)
}

if hss.CORS != nil && len(hss.CORS.AllowedOrigins) > 0 {
if hss.CORS.HasValue() && len(hss.CORS.Get().AllowedOrigins) > 0 {
corsConfig := hss.CORS.Get()
co := cors.Options{
AllowedOrigins: hss.CORS.AllowedOrigins,
AllowedOrigins: corsConfig.AllowedOrigins,
AllowCredentials: true,
AllowedHeaders: hss.CORS.AllowedHeaders,
MaxAge: hss.CORS.MaxAge,
AllowedHeaders: corsConfig.AllowedHeaders,
MaxAge: corsConfig.MaxAge,
}
handler = cors.New(co).Handler(handler)
}
if hss.CORS != nil && len(hss.CORS.AllowedOrigins) == 0 && len(hss.CORS.AllowedHeaders) > 0 {
if hss.CORS.HasValue() && len(hss.CORS.Get().AllowedOrigins) == 0 && len(hss.CORS.Get().AllowedHeaders) > 0 {
settings.Logger.Warn("The CORS configuration specifies allowed headers but no allowed origins, and is therefore ignored.")
}

Expand Down
Loading
Loading