mirror of
https://github.com/netbirdio/netbird.git
synced 2024-11-07 08:44:07 +01:00
Allow to create config file next to binary (#701)
Force to use the proper temp dir If we do not define the configDir then the Go create a random temp dir for copy routine. It is not optimal from security purpose.
This commit is contained in:
parent
5bb875a0fa
commit
462a86cfcc
13
util/file.go
13
util/file.go
@ -11,8 +11,7 @@ import (
|
||||
// The output JSON is pretty-formatted
|
||||
func WriteJson(file string, obj interface{}) error {
|
||||
|
||||
configDir, configFileName := filepath.Split(file)
|
||||
err := os.MkdirAll(configDir, 0750)
|
||||
configDir, configFileName, err := prepareConfigFileDir(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -100,3 +99,13 @@ func CopyFileContents(src, dst string) (err error) {
|
||||
err = out.Sync()
|
||||
return
|
||||
}
|
||||
|
||||
func prepareConfigFileDir(file string) (string, string, error) {
|
||||
configDir, configFileName := filepath.Split(file)
|
||||
if configDir == "" {
|
||||
return filepath.Dir(file), configFileName, nil
|
||||
}
|
||||
|
||||
err := os.MkdirAll(configDir, 0750)
|
||||
return configDir, configFileName, err
|
||||
}
|
||||
|
@ -3,11 +3,13 @@ package util_test
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"github.com/netbirdio/netbird/util"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"github.com/netbirdio/netbird/util"
|
||||
)
|
||||
|
||||
var _ = Describe("Client", func() {
|
||||
@ -102,4 +104,23 @@ var _ = Describe("Client", func() {
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Describe("Handle config file without full path", func() {
|
||||
Context("config file handling", func() {
|
||||
It("should be successful", func() {
|
||||
written := &TestConfig{
|
||||
SomeField: 123,
|
||||
}
|
||||
cfgFile := "test_cfg.json"
|
||||
defer os.Remove(cfgFile)
|
||||
|
||||
err := util.WriteJson(cfgFile, written)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
read, err := util.ReadJson(cfgFile, &TestConfig{})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(read).NotTo(BeNil())
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user