Commit 2bc4f332 authored by Sybren A. Stüvel's avatar Sybren A. Stüvel

Be more careful saving credentials

parent 9c76ab2b
......@@ -26,6 +26,7 @@ import (
"crypto/rsa"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"github.com/sirupsen/logrus"
......@@ -107,7 +108,18 @@ func (creds Credentials) Save() {
logger.WithError(err).Fatal("unable to encode YAML")
}
if err := ioutil.WriteFile(creds.filename, bytes, 0600); err != nil {
tmpname := creds.filename + "~"
if err := ioutil.WriteFile(tmpname, bytes, 0600); err != nil {
logger.WithError(err).Fatal("unable to write YAML file")
}
if err := os.Remove(creds.filename); err != nil {
logger.WithError(err).Fatal("unable to remove old YAML file")
}
if err := os.Rename(tmpname, creds.filename); err != nil {
logger.WithFields(logrus.Fields{
"tmpname": tmpname,
logrus.ErrorKey: err,
}).Fatal("unable to rename just-written config file")
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment