fix: remove duplicate reads in the static discovery subcommand to avoid reading past EOF when using stdin#109
fix: remove duplicate reads in the static discovery subcommand to avoid reading past EOF when using stdin#109Wafffle77 wants to merge 2 commits into
Conversation
Signed-off-by: Ethan Clark <ethan.clark@trojans.dsu.edu>
|
Good catch! I've examined the code and can see the duplicate payload handling. I'm a bit hesitant to introduce a global variable here and I think the better solution might be to deduplicate the What I propose is putting the // Read data from file or stdin
nodes := discover.NodeListDeprecated{}
if cmd.Flag("data").Changed {
cli.HandlePayload(cmd, &nodes)
} else {
cli.HandlePayloadStdin(cmd, &nodes)
}block before the We would have to unmarshal the data into a On the other hand, it might be time to remove the deprecated discovery format code, since it was deprecated in v0.6.0 (#63). This would eliminate this issue entirely while slimming the codebase. |
|
I think refactoring/removing the deprecated discovery is the best option, but I also wanted to account for any other duplicate reads in the code, since AFAIK there isn't any spec on whether or not duplicate reads should be allowed. It might be a good idea as well to add an error to the ReadStdin function if it gets called more than once per invocation (though that also may require a global variable unless we close stdin when we're done) |
Signed-off-by: Ethan Clark <ethan.clark@trojans.dsu.edu>
|
I've removed the global stdin buffer variable in favor of deduplicating the stdin reads. Long term it might be a good idea to refactor the data/payload system to make it a bit harder for duplicate reads to happen. |
synackd
left a comment
There was a problem hiding this comment.
Looks better. Left a comment about some small changes I think would improve this patchset.
|
|
||
| // Read data from file or stdin | ||
| // Convert discovery data to struct | ||
| rawData, err := json.Marshal(discoveryData) |
There was a problem hiding this comment.
Can we use format.UnmarshalData() and format.MarshalData() here instead?
Also, can we be more generic with the discoveryData type and use an interface{}? See below for an example using an interface{} with both functions:
Lines 360 to 365 in e7536ed
| rawData, err := json.Marshal(discoveryData) | ||
| if err != nil { | ||
| log.Logger.Error().Err(err).Msg("unable to marshal discovery data to json") | ||
| // cli.LogHelpError(cmd) |
There was a problem hiding this comment.
Any reason these are commented out?
|
Updated title to follow Conventional Commit formatting for the future squash merge commit message. |
Pull Request Template
Thank you for your contribution! Please ensure the following before submitting:
Checklist
make test(or equivalent) locally and all tests passgit commit -s) with my real name and email<filename>.licensesidecarLICENSES/directoryDescription
Fixes an issue where some subcommands (tested with
ochami discover static) would attempt to read a data file multiple times. If the data file was stdin, this would result in the file being read successfully once and subsequent reads would be empty. This commit patches the internalio.ReadStdin()to buffer its first successful read from stdin (Full read until EOF, not just a singleRead()call) and return the buffered data on subsequent calls. This may break anything that expects this behavior fromio.ReadStdin(), but I didn't see anything that relied on it and all the tests came back fine.Type of Change
For more info, see Contributing Guidelines.