Write to temp file before saving data (#238)

* Create temp file before saving data

On the event of full disk, we may encounter the case where the
destination file get replaced by an empty file as the
ioutil.WriteFile truncates the destination before write.

* Close the tempFile instance before moving it

* Blacklist Wireguard interfaces for ICE checks
This commit is contained in:
Maycon Santos 2022-02-20 19:03:16 +01:00 committed by GitHub
parent 60a9da734f
commit 5546eba36a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 8 deletions

View File

@ -2,6 +2,7 @@ package peer
import (
"context"
"golang.zx2c4.com/wireguard/wgctrl"
"net"
"sync"
"time"
@ -87,11 +88,20 @@ func interfaceFilter(blackList []string) func(string) bool {
}
}
return func(iFace string) bool {
if len(blackListMap) == 0 {
return true
}
_, ok := blackListMap[iFace]
return !ok
if ok {
return false
}
// look for unlisted Wireguard interfaces
wg, err := wgctrl.New()
if err != nil {
log.Debugf("trying to create a wgctrl client failed with: %v", err)
}
defer wg.Close()
_, err = wg.Device(iFace)
return err != nil
}
}

2
go.sum
View File

@ -433,8 +433,6 @@ github.com/vishvananda/netlink v1.1.0 h1:1iyaYNBLmP6L0220aDnYQpo1QEV4t4hJ+xEEhhJ
github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE=
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df h1:OviZH7qLw/7ZovXvuNyL3XQl8UFofeikI1NW1Gypu7k=
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU=
github.com/wiretrustee/ice/v2 v2.1.21-0.20220216144753-138db20d36ad h1:S61dy7FWFITWs/WHk2JJvJd600rWyT8Qsm9ct9nUpOQ=
github.com/wiretrustee/ice/v2 v2.1.21-0.20220216144753-138db20d36ad/go.mod h1:XT1Nrb4OxbVFPffbQMbq4PaeEkpRLVzdphh3fjrw7DY=
github.com/wiretrustee/ice/v2 v2.1.21-0.20220218121004-dc81faead4bb h1:CU1/+CEeCPvYXgfAyqTJXSQSf6hW3wsWM6Dfz6HkHEQ=
github.com/wiretrustee/ice/v2 v2.1.21-0.20220218121004-dc81faead4bb/go.mod h1:XT1Nrb4OxbVFPffbQMbq4PaeEkpRLVzdphh3fjrw7DY=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=

View File

@ -12,7 +12,7 @@ import (
// The output JSON is pretty-formatted
func WriteJson(file string, obj interface{}) error {
configDir := filepath.Dir(file)
configDir, configFileName := filepath.Split(file)
err := os.MkdirAll(configDir, 0750)
if err != nil {
return err
@ -24,7 +24,31 @@ func WriteJson(file string, obj interface{}) error {
return err
}
err = ioutil.WriteFile(file, bs, 0600)
tempFile, err := ioutil.TempFile(configDir, ".*"+configFileName)
if err != nil {
return err
}
tempFileName := tempFile.Name()
// closing file ops as windows doesn't allow to move it
err = tempFile.Close()
if err != nil {
return err
}
defer func() {
_, err = os.Stat(tempFileName)
if err == nil {
os.Remove(tempFileName)
}
}()
err = ioutil.WriteFile(tempFileName, bs, 0600)
if err != nil {
return err
}
err = os.Rename(tempFileName, file)
if err != nil {
return err
}