From 519448ec805ef7fa42b6e900c64e37f4ec9d1ae6 Mon Sep 17 00:00:00 2001 From: my-git9 Date: Thu, 18 Aug 2022 22:14:47 +0800 Subject: [PATCH] fix: Add logic to handle for fileHandle.Close() (#9963) (#10361) Signed-off-by: xin.li Signed-off-by: xin.li --- util/cert/cert.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/util/cert/cert.go b/util/cert/cert.go index e42f5bb126ebd..c7ac284c20314 100644 --- a/util/cert/cert.go +++ b/util/cert/cert.go @@ -16,6 +16,7 @@ import ( "regexp" "strings" + log "github.com/sirupsen/logrus" "golang.org/x/crypto/ssh" "github.com/argoproj/argo-cd/v2/common" @@ -130,7 +131,14 @@ func ParseTLSCertificatesFromPath(sourceFile string) ([]string, error) { if err != nil { return nil, err } - defer fileHandle.Close() + defer func() { + if err = fileHandle.Close(); err != nil { + log.WithFields(log.Fields{ + common.SecurityField: common.SecurityMedium, + common.SecurityCWEField: 775, + }).Errorf("error closing file %q: %v", fileHandle.Name(), err) + } + }() return ParseTLSCertificatesFromStream(fileHandle) } @@ -187,7 +195,14 @@ func ParseSSHKnownHostsFromPath(sourceFile string) ([]string, error) { if err != nil { return nil, err } - defer fileHandle.Close() + defer func() { + if err = fileHandle.Close(); err != nil { + log.WithFields(log.Fields{ + common.SecurityField: common.SecurityMedium, + common.SecurityCWEField: 775, + }).Errorf("error closing file %q: %v", fileHandle.Name(), err) + } + }() return ParseSSHKnownHostsFromStream(fileHandle) }