-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
filestream: validate input id on startup #41731
Merged
AndersonQ
merged 11 commits into
elastic:main
from
AndersonQ:40540-filestream-require-unique-ids
Dec 3, 2024
Merged
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b6ecc4e
filestream: validate input ids on startup
AndersonQ 70c3569
fix typos
AndersonQ 0bc1392
pr changes
AndersonQ e05c888
pr changes
AndersonQ 885799e
allow empty IDs
AndersonQ 5b70c55
Merge branch 'main' into 40540-filestream-require-unique-ids
AndersonQ 100e0f5
adjust integration tests
AndersonQ ca65d39
Merge remote-tracking branch 'origin/40540-filestream-require-unique-…
AndersonQ c8505e0
Merge branch 'main' into 40540-filestream-require-unique-ids
AndersonQ cec09f3
wee refactor on docs and names
AndersonQ f599e74
Merge remote-tracking branch 'origin/40540-filestream-require-unique-…
AndersonQ File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -19,6 +19,7 @@ package filestream | |
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/dustin/go-humanize" | ||
|
@@ -27,6 +28,7 @@ import ( | |
"github.com/elastic/beats/v7/libbeat/reader/parser" | ||
"github.com/elastic/beats/v7/libbeat/reader/readfile" | ||
conf "github.com/elastic/elastic-agent-libs/config" | ||
"github.com/elastic/elastic-agent-libs/logp" | ||
) | ||
|
||
// Config stores the options of a file stream. | ||
|
@@ -142,3 +144,78 @@ func (c *config) Validate() error { | |
|
||
return nil | ||
} | ||
|
||
// ValidateInputIDs checks all filestream inputs to ensure all have an ID and | ||
// the ID is unique. If there is any empty or duplicated ID, it logs an error | ||
// containing the offending input configurations and returns an error containing | ||
// the duplicated IDs. | ||
rdner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func ValidateInputIDs(inputs []*conf.C, logger *logp.Logger) error { | ||
ids := make(map[string][]*conf.C) | ||
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. [Suggestion] |
||
var duplicates []string | ||
var empty []*conf.C | ||
for _, input := range inputs { | ||
fsInput := struct { | ||
ID string `config:"id"` | ||
Type string `config:"type"` | ||
}{} | ||
err := input.Unpack(&fsInput) | ||
if err != nil { | ||
return fmt.Errorf("failed to unpack filestream input configuration: %w", err) | ||
} | ||
if fsInput.Type == "filestream" { | ||
if fsInput.ID == "" { | ||
empty = append(empty, input) | ||
continue | ||
} | ||
ids[fsInput.ID] = append(ids[fsInput.ID], input) | ||
if len(ids[fsInput.ID]) > 1 { | ||
rdner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
duplicates = append(duplicates, fsInput.ID) | ||
} | ||
} | ||
} | ||
|
||
var errs []string | ||
if empty != nil { | ||
errs = append(errs, "input without ID") | ||
} | ||
if len(duplicates) != 0 { | ||
errs = append(errs, fmt.Sprintf("filestream inputs with duplicated IDs: %v", strings.Join(duplicates, ","))) | ||
} | ||
|
||
if len(errs) != 0 { | ||
jsonDupCfg := collectOffendingInputs(duplicates, empty, ids) | ||
logger.Errorw("filestream inputs with invalid IDs", "inputs", jsonDupCfg) | ||
|
||
return fmt.Errorf("filestream inputs validation error: %s", strings.Join(errs, ", ")) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func collectOffendingInputs(duplicates []string, empty []*conf.C, ids map[string][]*conf.C) []map[string]interface{} { | ||
var cfgs []map[string]interface{} | ||
|
||
if len(empty) > 0 { | ||
for _, cfg := range empty { | ||
toJson := map[string]interface{}{} | ||
err := cfg.Unpack(&toJson) | ||
if err != nil { | ||
toJson["emptyID"] = fmt.Sprintf("failed to unpack config: %v", err) | ||
} | ||
cfgs = append(cfgs, toJson) | ||
} | ||
} | ||
|
||
for _, id := range duplicates { | ||
for _, dupcfgs := range ids[id] { | ||
toJson := map[string]interface{}{} | ||
err := dupcfgs.Unpack(&toJson) | ||
if err != nil { | ||
toJson[id] = fmt.Sprintf("failed to unpack config: %v", err) | ||
} | ||
cfgs = append(cfgs, toJson) | ||
} | ||
} | ||
rdner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return cfgs | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
filestream
input actually usesinput.filestream
as the logger name, so I suggest keeping the pattern.