Skip to content

Commit

Permalink
[chore] Remove internal package localhostgate, move gate definition t…
Browse files Browse the repository at this point in the history
…o globalgate (open-telemetry#11150)

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored Sep 11, 2024
1 parent 0e22dc3 commit 2166b11
Show file tree
Hide file tree
Showing 24 changed files with 134 additions and 207 deletions.
8 changes: 4 additions & 4 deletions config/configgrpc/configgrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
"go.opentelemetry.io/collector/extension/auth"
"go.opentelemetry.io/collector/extension/auth/authtest"
"go.opentelemetry.io/collector/featuregate"
"go.opentelemetry.io/collector/internal/localhostgate"
"go.opentelemetry.io/collector/internal/globalgates"
"go.opentelemetry.io/collector/pdata/ptrace/ptraceotlp"
)

Expand Down Expand Up @@ -494,11 +494,11 @@ func TestUseSecure(t *testing.T) {
}

func TestGRPCServerWarning(t *testing.T) {
prev := localhostgate.UseLocalHostAsDefaultHostfeatureGate.IsEnabled()
require.NoError(t, featuregate.GlobalRegistry().Set(localhostgate.UseLocalHostAsDefaultHostID, false))
prev := globalgates.UseLocalHostAsDefaultHostfeatureGate.IsEnabled()
require.NoError(t, featuregate.GlobalRegistry().Set(globalgates.UseLocalHostAsDefaultHostID, false))
defer func() {
// Restore previous value.
require.NoError(t, featuregate.GlobalRegistry().Set(localhostgate.UseLocalHostAsDefaultHostID, prev))
require.NoError(t, featuregate.GlobalRegistry().Set(globalgates.UseLocalHostAsDefaultHostID, prev))
}()

tests := []struct {
Expand Down
12 changes: 2 additions & 10 deletions config/configgrpc/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.22.0
require (
github.com/mostynb/go-grpc-compression v1.2.3
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/collector v0.109.0
go.opentelemetry.io/collector/client v1.15.0
go.opentelemetry.io/collector/component v0.109.0
go.opentelemetry.io/collector/config/configauth v0.109.0
Expand All @@ -17,6 +16,7 @@ require (
go.opentelemetry.io/collector/config/internal v0.109.0
go.opentelemetry.io/collector/extension/auth v0.109.0
go.opentelemetry.io/collector/featuregate v1.15.0
go.opentelemetry.io/collector/internal/globalgates v0.109.0
go.opentelemetry.io/collector/pdata v1.15.0
go.opentelemetry.io/collector/pdata/testdata v0.109.0
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.54.0
Expand Down Expand Up @@ -63,8 +63,6 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace go.opentelemetry.io/collector => ../../

replace go.opentelemetry.io/collector/client => ../../client

replace go.opentelemetry.io/collector/config/configauth => ../configauth
Expand All @@ -85,8 +83,6 @@ replace go.opentelemetry.io/collector/extension => ../../extension

replace go.opentelemetry.io/collector/extension/auth => ../../extension/auth

replace go.opentelemetry.io/collector/confmap => ../../confmap

replace go.opentelemetry.io/collector/featuregate => ../../featuregate

replace go.opentelemetry.io/collector/pdata => ../../pdata
Expand All @@ -99,8 +95,4 @@ replace go.opentelemetry.io/collector/component => ../../component

replace go.opentelemetry.io/collector/consumer => ../../consumer

replace go.opentelemetry.io/collector/consumer/consumerprofiles => ../../consumer/consumerprofiles

replace go.opentelemetry.io/collector/consumer/consumertest => ../../consumer/consumertest

replace go.opentelemetry.io/collector/component/componentstatus => ../../component/componentstatus
replace go.opentelemetry.io/collector/internal/globalgates => ../../internal/globalgates
8 changes: 4 additions & 4 deletions config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
"go.opentelemetry.io/collector/extension/auth"
"go.opentelemetry.io/collector/extension/auth/authtest"
"go.opentelemetry.io/collector/featuregate"
"go.opentelemetry.io/collector/internal/localhostgate"
"go.opentelemetry.io/collector/internal/globalgates"
)

type customRoundTripper struct {
Expand Down Expand Up @@ -520,11 +520,11 @@ func TestHTTPServerSettingsError(t *testing.T) {
}

func TestHTTPServerWarning(t *testing.T) {
prev := localhostgate.UseLocalHostAsDefaultHostfeatureGate.IsEnabled()
require.NoError(t, featuregate.GlobalRegistry().Set(localhostgate.UseLocalHostAsDefaultHostID, false))
prev := globalgates.UseLocalHostAsDefaultHostfeatureGate.IsEnabled()
require.NoError(t, featuregate.GlobalRegistry().Set(globalgates.UseLocalHostAsDefaultHostID, false))
defer func() {
// Restore previous value.
require.NoError(t, featuregate.GlobalRegistry().Set(localhostgate.UseLocalHostAsDefaultHostID, prev))
require.NoError(t, featuregate.GlobalRegistry().Set(globalgates.UseLocalHostAsDefaultHostID, prev))
}()

tests := []struct {
Expand Down
12 changes: 2 additions & 10 deletions config/confighttp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/klauspost/compress v1.17.9
github.com/rs/cors v1.11.1
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/collector v0.109.0
go.opentelemetry.io/collector/client v1.15.0
go.opentelemetry.io/collector/component v0.109.0
go.opentelemetry.io/collector/config/configauth v0.109.0
Expand All @@ -18,6 +17,7 @@ require (
go.opentelemetry.io/collector/config/internal v0.109.0
go.opentelemetry.io/collector/extension/auth v0.109.0
go.opentelemetry.io/collector/featuregate v1.15.0
go.opentelemetry.io/collector/internal/globalgates v0.109.0
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0
go.opentelemetry.io/otel v1.30.0
go.opentelemetry.io/otel/metric v1.30.0
Expand Down Expand Up @@ -58,8 +58,6 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace go.opentelemetry.io/collector => ../../

replace go.opentelemetry.io/collector/config/configauth => ../configauth

replace go.opentelemetry.io/collector/config/configcompression => ../configcompression
Expand All @@ -76,8 +74,6 @@ replace go.opentelemetry.io/collector/extension => ../../extension

replace go.opentelemetry.io/collector/extension/auth => ../../extension/auth

replace go.opentelemetry.io/collector/confmap => ../../confmap

replace go.opentelemetry.io/collector/featuregate => ../../featuregate

replace go.opentelemetry.io/collector/pdata => ../../pdata
Expand All @@ -92,8 +88,4 @@ replace go.opentelemetry.io/collector/pdata/testdata => ../../pdata/testdata

replace go.opentelemetry.io/collector/pdata/pprofile => ../../pdata/pprofile

replace go.opentelemetry.io/collector/consumer/consumerprofiles => ../../consumer/consumerprofiles

replace go.opentelemetry.io/collector/consumer/consumertest => ../../consumer/consumertest

replace go.opentelemetry.io/collector/component/componentstatus => ../../component/componentstatus
replace go.opentelemetry.io/collector/internal/globalgates => ../../internal/globalgates
25 changes: 3 additions & 22 deletions config/internal/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.22.0

require (
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/collector v0.109.0
go.opentelemetry.io/collector/internal/globalgates v0.109.0
go.uber.org/goleak v1.3.0
go.uber.org/zap v1.27.0
)
Expand All @@ -13,31 +13,12 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
go.opentelemetry.io/collector/featuregate v1.15.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace go.opentelemetry.io/collector => ../../
replace go.opentelemetry.io/collector/internal/globalgates => ../../internal/globalgates

replace go.opentelemetry.io/collector/featuregate => ../../featuregate

replace go.opentelemetry.io/collector/confmap => ../../confmap

replace go.opentelemetry.io/collector/config/configtelemetry => ../configtelemetry

replace go.opentelemetry.io/collector/pdata => ../../pdata

replace go.opentelemetry.io/collector/consumer => ../../consumer

replace go.opentelemetry.io/collector/component => ../../component

replace go.opentelemetry.io/collector/pdata/testdata => ../../pdata/testdata

replace go.opentelemetry.io/collector/pdata/pprofile => ../../pdata/pprofile

replace go.opentelemetry.io/collector/consumer/consumerprofiles => ../../consumer/consumerprofiles

replace go.opentelemetry.io/collector/consumer/consumertest => ../../consumer/consumertest

replace go.opentelemetry.io/collector/component/componentstatus => ../../component/componentstatus
6 changes: 3 additions & 3 deletions config/internal/warning.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"go.uber.org/zap"

"go.opentelemetry.io/collector/internal/localhostgate"
"go.opentelemetry.io/collector/internal/globalgates"
)

func shouldWarn(endpoint string) bool {
Expand Down Expand Up @@ -38,14 +38,14 @@ func shouldWarn(endpoint string) bool {

// WarnOnUnspecifiedHost emits a warning if an endpoint has an unspecified host.
func WarnOnUnspecifiedHost(logger *zap.Logger, endpoint string) {
if !localhostgate.UseLocalHostAsDefaultHostfeatureGate.IsEnabled() && shouldWarn(endpoint) {
if !globalgates.UseLocalHostAsDefaultHostfeatureGate.IsEnabled() && shouldWarn(endpoint) {
logger.Warn(
"Using the 0.0.0.0 address exposes this server to every network interface, which may facilitate Denial of Service attacks. Enable the feature gate to change the default and remove this warning.",
zap.String(
"documentation",
"https://github.com/open-telemetry/opentelemetry-collector/blob/main/docs/security-best-practices.md#safeguards-against-denial-of-service-attacks",
),
zap.String("feature gate ID", localhostgate.UseLocalHostAsDefaultHostID),
zap.String("feature gate ID", globalgates.UseLocalHostAsDefaultHostID),
)
}
}
2 changes: 0 additions & 2 deletions connector/connectorprofiles/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ replace go.opentelemetry.io/collector/consumer/consumertest => ../../consumer/co

replace go.opentelemetry.io/collector/pdata/pprofile => ../../pdata/pprofile

replace go.opentelemetry.io/collector/featuregate => ../../featuregate

replace go.opentelemetry.io/collector/consumer/consumerprofiles => ../../consumer/consumerprofiles

replace go.opentelemetry.io/collector/component/componentprofiles => ../../component/componentprofiles
Expand Down
2 changes: 0 additions & 2 deletions connector/forwardconnector/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ replace go.opentelemetry.io/collector/pdata => ../../pdata

replace go.opentelemetry.io/collector/pdata/testdata => ../../pdata/testdata

replace go.opentelemetry.io/collector/featuregate => ../../featuregate

replace go.opentelemetry.io/collector/consumer => ../../consumer

replace go.opentelemetry.io/collector/confmap => ../../confmap
Expand Down
2 changes: 0 additions & 2 deletions connector/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ replace go.opentelemetry.io/collector/confmap => ../confmap

replace go.opentelemetry.io/collector/consumer => ../consumer

replace go.opentelemetry.io/collector/featuregate => ../featuregate

replace go.opentelemetry.io/collector/pdata => ../pdata

replace go.opentelemetry.io/collector/pdata/testdata => ../pdata/testdata
Expand Down
3 changes: 3 additions & 0 deletions exporter/otlpexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ require (
go.opentelemetry.io/collector/extension/auth v0.109.0 // indirect
go.opentelemetry.io/collector/extension/experimental/storage v0.109.0 // indirect
go.opentelemetry.io/collector/featuregate v1.15.0 // indirect
go.opentelemetry.io/collector/internal/globalgates v0.109.0 // indirect
go.opentelemetry.io/collector/pdata/pprofile v0.109.0 // indirect
go.opentelemetry.io/collector/receiver v0.109.0 // indirect
go.opentelemetry.io/collector/receiver/receiverprofiles v0.109.0 // indirect
Expand Down Expand Up @@ -141,3 +142,5 @@ replace go.opentelemetry.io/collector/component/componentstatus => ../../compone
replace go.opentelemetry.io/collector/receiver/receiverprofiles => ../../receiver/receiverprofiles

replace go.opentelemetry.io/collector/exporter/exporterprofiles => ../exporterprofiles

replace go.opentelemetry.io/collector/internal/globalgates => ../../internal/globalgates
3 changes: 3 additions & 0 deletions exporter/otlphttpexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ require (
go.opentelemetry.io/collector/extension/auth v0.109.0 // indirect
go.opentelemetry.io/collector/extension/experimental/storage v0.109.0 // indirect
go.opentelemetry.io/collector/featuregate v1.15.0 // indirect
go.opentelemetry.io/collector/internal/globalgates v0.109.0 // indirect
go.opentelemetry.io/collector/pdata/pprofile v0.109.0 // indirect
go.opentelemetry.io/collector/receiver v0.109.0 // indirect
go.opentelemetry.io/collector/receiver/receiverprofiles v0.109.0 // indirect
Expand Down Expand Up @@ -138,3 +139,5 @@ replace go.opentelemetry.io/collector/component/componentstatus => ../../compone
replace go.opentelemetry.io/collector/receiver/receiverprofiles => ../../receiver/receiverprofiles

replace go.opentelemetry.io/collector/exporter/exporterprofiles => ../exporterprofiles

replace go.opentelemetry.io/collector/internal/globalgates => ../../internal/globalgates
2 changes: 0 additions & 2 deletions extension/memorylimiterextension/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ replace go.opentelemetry.io/collector/confmap => ../../confmap

replace go.opentelemetry.io/collector/extension => ../../extension

replace go.opentelemetry.io/collector/featuregate => ../../featuregate

replace go.opentelemetry.io/collector/pdata => ../../pdata

replace go.opentelemetry.io/collector/consumer => ../../consumer
Expand Down
3 changes: 3 additions & 0 deletions extension/zpagesextension/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ require (
go.opentelemetry.io/collector/config/internal v0.109.0 // indirect
go.opentelemetry.io/collector/extension/auth v0.109.0 // indirect
go.opentelemetry.io/collector/featuregate v1.15.0 // indirect
go.opentelemetry.io/collector/internal/globalgates v0.109.0 // indirect
go.opentelemetry.io/collector/pdata v1.15.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0 // indirect
go.opentelemetry.io/otel v1.30.0 // indirect
Expand Down Expand Up @@ -114,3 +115,5 @@ replace go.opentelemetry.io/collector/consumer/consumertest => ../../consumer/co
replace go.opentelemetry.io/collector/client => ../../client

replace go.opentelemetry.io/collector/component/componentstatus => ../../component/componentstatus

replace go.opentelemetry.io/collector/internal/globalgates => ../../internal/globalgates
4 changes: 0 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ require (
go.opentelemetry.io/collector/consumer v0.109.0
go.opentelemetry.io/collector/consumer/consumerprofiles v0.109.0
go.opentelemetry.io/collector/consumer/consumertest v0.109.0
go.opentelemetry.io/collector/featuregate v1.15.0
go.opentelemetry.io/collector/pdata v1.15.0
go.opentelemetry.io/collector/pdata/pprofile v0.109.0
go.opentelemetry.io/collector/pdata/testdata v0.109.0
Expand All @@ -39,7 +38,6 @@ require (
github.com/go-viper/mapstructure/v2 v2.1.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
Expand Down Expand Up @@ -88,8 +86,6 @@ replace go.opentelemetry.io/collector/consumer => ./consumer

replace go.opentelemetry.io/collector/consumer/consumertest => ./consumer/consumertest

replace go.opentelemetry.io/collector/featuregate => ./featuregate

replace go.opentelemetry.io/collector/pdata => ./pdata

replace go.opentelemetry.io/collector/pdata/testdata => ./pdata/testdata
Expand Down
2 changes: 0 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 37 additions & 1 deletion internal/globalgates/globalgates.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,46 @@

package globalgates // import "go.opentelemetry.io/collector/internal/globalgates"

import "go.opentelemetry.io/collector/featuregate"
import (
"errors"

"go.opentelemetry.io/collector/featuregate"
)

var NoopTracerProvider = featuregate.GlobalRegistry().MustRegister("service.noopTracerProvider",
featuregate.StageAlpha,
featuregate.WithRegisterFromVersion("v0.107.0"),
featuregate.WithRegisterToVersion("v0.109.0"),
featuregate.WithRegisterDescription("Sets a Noop OpenTelemetry TracerProvider to reduce memory allocations. This featuregate is incompatible with the zPages extension."))

const UseLocalHostAsDefaultHostID = "component.UseLocalHostAsDefaultHost"

// UseLocalHostAsDefaultHostfeatureGate is the feature gate that controls whether
// server-like receivers and extensions such as the OTLP receiver use localhost as the default host for their endpoints.
var UseLocalHostAsDefaultHostfeatureGate = mustRegisterOrLoad(
featuregate.GlobalRegistry(),
UseLocalHostAsDefaultHostID,
featuregate.StageBeta,
featuregate.WithRegisterDescription("controls whether server-like receivers and extensions such as the OTLP receiver use localhost as the default host for their endpoints"),
)

// mustRegisterOrLoad tries to register the feature gate and loads it if it already exists.
// It panics on any other error.
func mustRegisterOrLoad(reg *featuregate.Registry, id string, stage featuregate.Stage, opts ...featuregate.RegisterOption) *featuregate.Gate {
gate, err := reg.Register(id, stage, opts...)

if errors.Is(err, featuregate.ErrAlreadyRegistered) {
// Gate is already registered; find it.
// Only a handful of feature gates are registered, so it's fine to iterate over all of them.
reg.VisitAll(func(g *featuregate.Gate) {
if g.ID() == id {
gate = g
return
}
})
} else if err != nil {
panic(err)
}

return gate
}
Loading

0 comments on commit 2166b11

Please sign in to comment.