-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[config{grpc,http}] Add warning when using unspecified address (#6267)
* [config/config{grpc,http}] Add warning when using a 0.0.0.0 endpoint * Add warning when using unspecified address * Add changelog entry * Fix tests * Fix HTTP tests * Apply suggestions from code review Co-authored-by: Alex Boten <[email protected]> * Use IsUnspecified method * no else after return * Move shared code to internal Co-authored-by: Alex Boten <[email protected]>
- Loading branch information
Showing
7 changed files
with
246 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# 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. otlpreceiver) | ||
component: receiver/otlp | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Add warning when using unspecified (`0.0.0.0`) address on HTTP or gRPC servers | ||
|
||
# One or more tracking issues or pull requests related to the change | ||
issues: [6151] | ||
|
||
# (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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package internal // import "go.opentelemetry.io/collector/config/internal" | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
// WarnOnUnspecifiedHost emits a warning if an endpoint has an unspecified host. | ||
func WarnOnUnspecifiedHost(logger *zap.Logger, endpoint string) error { | ||
host, _, err := net.SplitHostPort(endpoint) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse endpoint: %w", err) | ||
} | ||
|
||
if ip := net.ParseIP(host); ip != nil && ip.IsUnspecified() { | ||
logger.Warn( | ||
"Using the 0.0.0.0 address exposes this server to every network interface, which may facilitate Denial of Service attacks", | ||
zap.String( | ||
"documentation", | ||
"https://github.com/open-telemetry/opentelemetry-collector/blob/main/docs/security-best-practices.md#safeguards-against-denial-of-service-attacks", | ||
), | ||
) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package internal // import "go.opentelemetry.io/collector/config/internal" | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zaptest/observer" | ||
) | ||
|
||
func TestWarnOnUnspecifiedHost(t *testing.T) { | ||
tests := []struct { | ||
endpoint string | ||
warn bool | ||
err string | ||
}{ | ||
{ | ||
endpoint: "0.0.0.0:0", | ||
warn: true, | ||
}, | ||
{ | ||
endpoint: "127.0.0.1:0", | ||
}, | ||
{ | ||
endpoint: "localhost:0", | ||
}, | ||
{ | ||
endpoint: "localhost::0", | ||
err: "too many colons in address", | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.endpoint, func(t *testing.T) { | ||
core, observed := observer.New(zap.DebugLevel) | ||
logger := zap.New(core) | ||
err := WarnOnUnspecifiedHost(logger, test.endpoint) | ||
if test.err != "" { | ||
assert.ErrorContains(t, err, test.err) | ||
return | ||
} | ||
|
||
require.NoError(t, err) | ||
|
||
var len int | ||
if test.warn { | ||
len = 1 | ||
} | ||
require.Len(t, observed.FilterLevelExact(zap.WarnLevel).All(), len) | ||
}) | ||
} | ||
|
||
} |