diff --git a/metadata/updater/updater.go b/metadata/updater/updater.go index 1b9d21b8..b3ac7232 100644 --- a/metadata/updater/updater.go +++ b/metadata/updater/updater.go @@ -596,11 +596,22 @@ func (update *Updater) persistMetadata(roleName string, data []byte) error { if err != nil { return err } - defer file.Close() + // change the file permissions to our desired permissions + err = file.Chmod(0644) + if err != nil { + // close and delete the temporary file if there was an error while writing + file.Close() + errRemove := os.Remove(file.Name()) + if errRemove != nil { + log.Info("Failed to delete temporary file", "name", file.Name()) + } + return err + } // write the data content to the temporary file - err = os.WriteFile(file.Name(), data, 0644) + _, err = file.Write(data) if err != nil { - // delete the temporary file if there was an error while writing + // close and delete the temporary file if there was an error while writing + file.Close() errRemove := os.Remove(file.Name()) if errRemove != nil { log.Info("Failed to delete temporary file", "name", file.Name())