-
Notifications
You must be signed in to change notification settings - Fork 3k
[receiver/tlscheck] Move Target Validation to scrape & Implement Scrape Errors #40341
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
Changes from 8 commits
310dabc
77416d0
89259d7
15c7ff6
cd22a4e
349c311
f446f5a
fdeb228
7400b26
4a98797
0895937
981ba62
bcbba1b
6809403
6e87da3
a868491
8122029
d5c93bf
cce662f
75be6f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# 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. filelogreceiver) | ||
component: tlscheckreceiver | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Do not crash on target validation & implement better scrape errors | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [40341] | ||
|
||
# (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: | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# 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: [user] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,12 +5,6 @@ package tlscheckreceiver // import "github.com/open-telemetry/opentelemetry-coll | |
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
|
||
"go.opentelemetry.io/collector/config/confignet" | ||
"go.opentelemetry.io/collector/scraper/scraperhelper" | ||
|
@@ -42,77 +36,13 @@ type Config struct { | |
_ struct{} | ||
} | ||
|
||
func validatePort(port string) error { | ||
portNum, err := strconv.Atoi(port) | ||
if err != nil { | ||
return fmt.Errorf("provided port is not a number: %s", port) | ||
} | ||
if portNum < 1 || portNum > 65535 { | ||
return fmt.Errorf("provided port is out of valid range (1-65535): %d", portNum) | ||
} | ||
return nil | ||
} | ||
|
||
func validateTarget(ct *CertificateTarget) error { | ||
// Check that exactly one of endpoint or file_path is specified | ||
if ct.Endpoint != "" && ct.FilePath != "" { | ||
return errors.New("cannot specify both endpoint and file_path") | ||
} | ||
if ct.Endpoint == "" && ct.FilePath == "" { | ||
return errors.New("must specify either endpoint or file_path") | ||
} | ||
|
||
// Validate endpoint if specified | ||
if ct.Endpoint != "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do these changes mean the collector can now start in an invalid state? We normally like to catch that at startup. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We still do this validation, but we do it in the scrape function. The user can now include an invalid target, but it will be logged, the |
||
if strings.Contains(ct.Endpoint, "://") { | ||
return fmt.Errorf("endpoint contains a scheme, which is not allowed: %s", ct.Endpoint) | ||
} | ||
|
||
_, port, err := net.SplitHostPort(ct.Endpoint) | ||
if err != nil { | ||
return fmt.Errorf("%s: %w", errInvalidEndpoint.Error(), err) | ||
} | ||
|
||
if err := validatePort(port); err != nil { | ||
return fmt.Errorf("%s: %w", errInvalidEndpoint.Error(), err) | ||
} | ||
} | ||
|
||
// Validate file path if specified | ||
if ct.FilePath != "" { | ||
// Clean the path to handle different path separators | ||
cleanPath := filepath.Clean(ct.FilePath) | ||
|
||
// Check if the path is absolute | ||
if !filepath.IsAbs(cleanPath) { | ||
return fmt.Errorf("file path must be absolute: %s", ct.FilePath) | ||
} | ||
|
||
// Check if path exists and is a regular file | ||
fileInfo, err := os.Stat(cleanPath) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
return fmt.Errorf("certificate file does not exist: %s", ct.FilePath) | ||
} | ||
return fmt.Errorf("error accessing certificate file %s: %w", ct.FilePath, err) | ||
} | ||
|
||
// check if it is a directory | ||
if fileInfo.IsDir() { | ||
return fmt.Errorf("path is a directory, not a file: %s", cleanPath) | ||
} | ||
|
||
// Check if it's a regular file (not a directory or special file) | ||
if !fileInfo.Mode().IsRegular() { | ||
return fmt.Errorf("certificate path is not a regular file: %s", ct.FilePath) | ||
} | ||
|
||
// Check if file is readable | ||
if _, err := os.ReadFile(cleanPath); err != nil { | ||
return fmt.Errorf("certificate file is not readable: %s", ct.FilePath) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.