fix(SecurityValidator): download should close plugin data file exactly once

* Previously, the download method would attempt to close twice, once in a deferred call and once explicitly at the end of the method
This commit is contained in:
xmbhasin 2025-03-22 17:26:25 -04:00
parent d2b654fb40
commit df698db644
1 changed files with 11 additions and 12 deletions

View File

@ -44,11 +44,11 @@ var (
) )
const ( const (
Hosturl = "https://ci.jenkins.io/job/Infra/job/plugin-site-api/job/generate-data/lastSuccessfulBuild/artifact/plugins.json.gzip" Hosturl = "https://ci.jenkins.io/job/Infra/job/plugin-site-api/job/generate-data/lastSuccessfulBuild/artifact/plugins.json.gzip"
CompressedFilePath = "/tmp/plugins.json.gzip" PluginDataFileCompressedPath = "/tmp/plugins.json.gzip"
PluginDataFile = "/tmp/plugins.json" PluginDataFile = "/tmp/plugins.json"
shortenedCheckingPeriod = 1 * time.Hour shortenedCheckingPeriod = 1 * time.Hour
defaultCheckingPeriod = 12 * time.Minute defaultCheckingPeriod = 12 * time.Minute
) )
func (in *Jenkins) SetupWebhookWithManager(mgr ctrl.Manager) error { func (in *Jenkins) SetupWebhookWithManager(mgr ctrl.Manager) error {
@ -264,12 +264,14 @@ func (in *SecurityValidator) fetchPluginData() error {
} }
func (in *SecurityValidator) download() error { func (in *SecurityValidator) download() error {
out, err := os.Create(CompressedFilePath) pluginDataFileCompressed, err := os.Create(PluginDataFileCompressedPath)
if err != nil { if err != nil {
return err return err
} }
// ensure pluginDataFileCompressed is closed
defer func() { defer func() {
if err := out.Close(); err != nil { if err := pluginDataFileCompressed.Close(); err != nil {
jenkinslog.V(log.VDebug).Info("Failed to close SecurityValidator.download io", "error", err) jenkinslog.V(log.VDebug).Info("Failed to close SecurityValidator.download io", "error", err)
} }
}() }()
@ -291,16 +293,13 @@ func (in *SecurityValidator) download() error {
defer httpResponseCloser(response) defer httpResponseCloser(response)
if err := out.Close(); err != nil { _, err = io.Copy(pluginDataFileCompressed, response.Body)
jenkinslog.V(log.VDebug).Info("Failed to send file", "error", err.Error())
}
_, err = io.Copy(out, response.Body)
return err return err
} }
func (in *SecurityValidator) extract() error { func (in *SecurityValidator) extract() error {
reader, err := os.Open(CompressedFilePath) reader, err := os.Open(PluginDataFileCompressedPath)
if err != nil { if err != nil {
return err return err