mirror of
https://github.com/netbirdio/netbird.git
synced 2024-11-25 09:33:24 +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
|
// The output JSON is pretty-formatted
|
||||||
func WriteJson(file string, obj interface{}) error {
|
func WriteJson(file string, obj interface{}) error {
|
||||||
|
|
||||||
configDir, configFileName := filepath.Split(file)
|
configDir, configFileName, err := prepareConfigFileDir(file)
|
||||||
err := os.MkdirAll(configDir, 0750)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -100,3 +99,13 @@ func CopyFileContents(src, dst string) (err error) {
|
|||||||
err = out.Sync()
|
err = out.Sync()
|
||||||
return
|
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 (
|
import (
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"github.com/netbirdio/netbird/util"
|
|
||||||
. "github.com/onsi/ginkgo"
|
|
||||||
. "github.com/onsi/gomega"
|
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
. "github.com/onsi/ginkgo"
|
||||||
|
. "github.com/onsi/gomega"
|
||||||
|
|
||||||
|
"github.com/netbirdio/netbird/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ = Describe("Client", func() {
|
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