Skip to content

Commit

Permalink
Updates writeJSON and error message.
Browse files Browse the repository at this point in the history
Signed-off-by: JU4N98 <[email protected]>
  • Loading branch information
JU4N98 committed Nov 29, 2023
1 parent 85b2382 commit d6bd491
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkg/sidecar/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/sidecar/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 10 additions & 6 deletions pkg/sidecar/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit d6bd491

Please sign in to comment.