From df698db644cfc49664a35a248965b71d99c9ea49 Mon Sep 17 00:00:00 2001 From: xmbhasin Date: Sat, 22 Mar 2025 17:26:25 -0400 Subject: [PATCH] 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 --- api/v1alpha2/jenkins_webhook.go | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/api/v1alpha2/jenkins_webhook.go b/api/v1alpha2/jenkins_webhook.go index d811d4d7..69d07b07 100644 --- a/api/v1alpha2/jenkins_webhook.go +++ b/api/v1alpha2/jenkins_webhook.go @@ -44,11 +44,11 @@ var ( ) const ( - Hosturl = "https://ci.jenkins.io/job/Infra/job/plugin-site-api/job/generate-data/lastSuccessfulBuild/artifact/plugins.json.gzip" - CompressedFilePath = "/tmp/plugins.json.gzip" - PluginDataFile = "/tmp/plugins.json" - shortenedCheckingPeriod = 1 * time.Hour - defaultCheckingPeriod = 12 * time.Minute + Hosturl = "https://ci.jenkins.io/job/Infra/job/plugin-site-api/job/generate-data/lastSuccessfulBuild/artifact/plugins.json.gzip" + PluginDataFileCompressedPath = "/tmp/plugins.json.gzip" + PluginDataFile = "/tmp/plugins.json" + shortenedCheckingPeriod = 1 * time.Hour + defaultCheckingPeriod = 12 * time.Minute ) func (in *Jenkins) SetupWebhookWithManager(mgr ctrl.Manager) error { @@ -264,12 +264,14 @@ func (in *SecurityValidator) fetchPluginData() error { } func (in *SecurityValidator) download() error { - out, err := os.Create(CompressedFilePath) + pluginDataFileCompressed, err := os.Create(PluginDataFileCompressedPath) if err != nil { return err } + + // ensure pluginDataFileCompressed is closed 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) } }() @@ -291,16 +293,13 @@ func (in *SecurityValidator) download() error { defer httpResponseCloser(response) - if err := out.Close(); err != nil { - jenkinslog.V(log.VDebug).Info("Failed to send file", "error", err.Error()) - } + _, err = io.Copy(pluginDataFileCompressed, response.Body) - _, err = io.Copy(out, response.Body) return err } func (in *SecurityValidator) extract() error { - reader, err := os.Open(CompressedFilePath) + reader, err := os.Open(PluginDataFileCompressedPath) if err != nil { return err