-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Using httputil for NVD #610
Conversation
ext/vulnmdsrc/nvd/nvd.go
Outdated
if err != nil { | ||
log.WithError(err).WithField(logDataFeedName, dataFeedName).Error("could not download NVD data feed") | ||
return dataFeedReaders, dataFeedHashes, commonerr.ErrCouldNotDownload | ||
} | ||
// r is closed in BuildCache() |
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.
not if an error occurs
ext/vulnmdsrc/nvd/nvd.go
Outdated
if err != nil { | ||
return "", err | ||
} | ||
defer r.Body.Close() | ||
|
||
if !httputil.Status2xx(r) { | ||
return "", errors.New("Unsuccesuful status code: " + string(r.StatusCode)) |
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.
Unsuccessful
Also having some more context in this message would be nice. I have no idea what request was being tried that returned a non-200 status code.
I was wrong at first with that comment, because that's where it crashed when I had defer there. |
ext/vulnmdsrc/nvd/nvd.go
Outdated
@@ -171,7 +171,7 @@ func getDataFeeds(dataFeedHashes map[string]string, localPath string) (map[strin | |||
log.WithError(err).WithField(logDataFeedName, dataFeedName).Error("could not download NVD data feed") | |||
return dataFeedReaders, dataFeedHashes, commonerr.ErrCouldNotDownload | |||
} | |||
// r is closed in BuildCache() | |||
defer r.Body.Close() |
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.
This won't close anything until the whole function getDataFeeds() exits. This is not what we want to do.
It could end up being more trouble than it's worth, but wrapping everything inside the loop iteration into a it's own function might be the best way to get defer to execute at the right time.
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.
Yeah, it was a little outside of what I set out with my initial PR, but I agree with you. I think I wrapped it up nicely now. Without spending too much effort.
ext/vulnmdsrc/nvd/nvd.go
Outdated
log.WithError(err).WithField(logDataFeedName, dataFeedName).Error("could not download NVD data feed") | ||
return dataFeedReaders, dataFeedHashes, commonerr.ErrCouldNotDownload | ||
} | ||
downloadAndSave := func() error { |
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.
I think you should pull this out into a top-level function called downloadFeed, rather than an inline function. After that LGTM.
cd0db72
to
fecdf48
Compare
ext/vulnmdsrc/nvd/nvd.go
Outdated
// Store it to a file at the same time if possible. | ||
f, err := os.Create(fileName) | ||
if err != nil { | ||
|
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.
Sorry for the OCD, but drop this newline and we're good to go.
nvd was missed when moving to httputil, this fixes it
…elp with resource management
fecdf48
to
30848d9
Compare
nvd was missed when moving to httputil, this fixes it