From d6bd491a6be671521832e3c1559ecb1628ca8402 Mon Sep 17 00:00:00 2001 From: JU4N98 Date: Wed, 29 Nov 2023 11:42:50 -0300 Subject: [PATCH] Updates writeJSON and error message. Signed-off-by: JU4N98 --- pkg/sidecar/config.go | 2 +- pkg/sidecar/config_test.go | 2 +- pkg/sidecar/sidecar.go | 16 ++++++++++------ 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/pkg/sidecar/config.go b/pkg/sidecar/config.go index afb5f252..e83c30bd 100644 --- a/pkg/sidecar/config.go +++ b/pkg/sidecar/config.go @@ -124,7 +124,7 @@ func ValidateConfig(c *Config) error { jwtSVIDEmptyCount := countEmpty(c.JWTSvidFilename, c.JWTAudience) jwtBundleEmptyCount := countEmpty(c.SvidBundleFileName) if x509EmptyCount == 3 && jwtSVIDEmptyCount == 2 && jwtBundleEmptyCount == 1 { - return errors.New("at least one of the sets ('svid_file_name', 'svid_key_file_name', 'svid_bundle_file_name'), ('jwt_file_name', 'jwt_audience') or ('jwt_bundle_file_name') must be fully specified") + return errors.New("at least one of the sets ('svid_file_name', 'svid_key_file_name', 'svid_bundle_file_name'), ('jwt_file_name', 'jwt_audience'), or ('jwt_bundle_file_name') must be fully specified") } if x509EmptyCount != 0 && x509EmptyCount != 3 { diff --git a/pkg/sidecar/config_test.go b/pkg/sidecar/config_test.go index c0a43100..36fad5b4 100644 --- a/pkg/sidecar/config_test.go +++ b/pkg/sidecar/config_test.go @@ -70,7 +70,7 @@ func TestValidateConfig(t *testing.T) { config: &Config{ AgentAddress: "path", }, - expectError: "at least one of the sets ('svid_file_name', 'svid_key_file_name', 'svid_bundle_file_name'), ('jwt_file_name', 'jwt_audience') or ('jwt_bundle_file_name') must be fully specified", + expectError: "at least one of the sets ('svid_file_name', 'svid_key_file_name', 'svid_bundle_file_name'), ('jwt_file_name', 'jwt_audience'), or ('jwt_bundle_file_name') must be fully specified", }, { name: "missing svid config", diff --git a/pkg/sidecar/sidecar.go b/pkg/sidecar/sidecar.go index 70f67473..b11355d8 100644 --- a/pkg/sidecar/sidecar.go +++ b/pkg/sidecar/sidecar.go @@ -232,17 +232,18 @@ func (s *Sidecar) dumpBundles(svidResponse *workloadapi.X509Context) error { return nil } -func (s *Sidecar) writeJSON(fileName string, certs map[string]interface{}) { +func (s *Sidecar) writeJSON(fileName string, certs map[string]interface{}) error { file, err := json.Marshal(certs) if err != nil { - s.config.Log.Errorf("Unable to parse certs: %v", err) - return + return err } jsonPath := path.Join(s.config.CertDir, fileName) if err = os.WriteFile(jsonPath, file, os.ModePerm); err != nil { - s.config.Log.Errorf("Unable to write JSON file: %v", err) + return err } + + return nil } func (s *Sidecar) updateJWTBundle(jwkSet *jwtbundle.Set) { @@ -258,8 +259,11 @@ func (s *Sidecar) updateJWTBundle(jwkSet *jwtbundle.Set) { bundles[bundle.TrustDomain().Name()] = base64.StdEncoding.EncodeToString(bytes) } - s.writeJSON(s.config.JWTBundleFilename, bundles) - s.config.Log.Info("JWT bundle updated") + if err := s.writeJSON(s.config.JWTBundleFilename, bundles); err != nil { + s.config.Log.Errorf("Unable to write JSON file: %v", err) + } else { + s.config.Log.Info("JWT bundle updated") + } } func (s *Sidecar) fetchJWTSVID(ctx context.Context, options ...workloadapi.ClientOption) (*jwtsvid.SVID, error) {