Skip to content

Commit

Permalink
Merge branch 'relay/fix/wg-roaming' into eliminate-udp-proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
pappz committed Oct 9, 2024
2 parents 021eef5 + b8026ad commit 8739b2a
Show file tree
Hide file tree
Showing 7 changed files with 373 additions and 115 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/golang-test-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
matrix:
arch: [ '386','amd64' ]
store: [ 'sqlite', 'postgres']
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
steps:
- name: Install Go
uses: actions/setup-go@v5
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ concurrency:

jobs:
release:
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
env:
flags: ""
steps:
Expand Down
2 changes: 1 addition & 1 deletion management/cmd/management.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ func handlerFunc(gRPCHandler *grpc.Server, httpHandler http.Handler) http.Handle

func loadMgmtConfig(ctx context.Context, mgmtConfigPath string) (*server.Config, error) {
loadedConfig := &server.Config{}
_, err := util.ReadJson(mgmtConfigPath, loadedConfig)
_, err := util.ReadJsonWithEnvSub(mgmtConfigPath, loadedConfig)
if err != nil {
return nil, err
}
Expand Down
11 changes: 9 additions & 2 deletions management/server/sql_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path/filepath"
"runtime"
"runtime/debug"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -63,8 +64,14 @@ func NewSqlStore(ctx context.Context, db *gorm.DB, storeEngine StoreEngine, metr
if err != nil {
return nil, err
}
conns := runtime.NumCPU()
sql.SetMaxOpenConns(conns) // TODO: make it configurable

conns, err := strconv.Atoi(os.Getenv("NB_SQL_MAX_OPEN_CONNS"))
if err != nil {
conns = runtime.NumCPU()
}
sql.SetMaxOpenConns(conns)

log.Infof("Set max open db connections to %d", conns)

if err := migrate(ctx, db); err != nil {
return nil, fmt.Errorf("migrate: %w", err)
Expand Down
53 changes: 53 additions & 0 deletions util/file.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package util

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"text/template"

log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -160,6 +164,55 @@ func ReadJson(file string, res interface{}) (interface{}, error) {
return res, nil
}

// ReadJsonWithEnvSub reads JSON config file and maps to a provided interface with environment variable substitution
func ReadJsonWithEnvSub(file string, res interface{}) (interface{}, error) {
envVars := getEnvMap()

f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()

bs, err := io.ReadAll(f)
if err != nil {
return nil, err
}

t, err := template.New("").Parse(string(bs))
if err != nil {
return nil, fmt.Errorf("error parsing template: %v", err)
}

var output bytes.Buffer
// Execute the template, substituting environment variables
err = t.Execute(&output, envVars)
if err != nil {
return nil, fmt.Errorf("error executing template: %v", err)
}

err = json.Unmarshal(output.Bytes(), &res)
if err != nil {
return nil, fmt.Errorf("failed parsing Json file after template was executed, err: %v", err)
}

return res, nil
}

// getEnvMap Convert the output of os.Environ() to a map
func getEnvMap() map[string]string {
envMap := make(map[string]string)

for _, env := range os.Environ() {
parts := strings.SplitN(env, "=", 2)
if len(parts) == 2 {
envMap[parts[0]] = parts[1]
}
}

return envMap
}

// CopyFileContents copies contents of the given src file to the dst file
func CopyFileContents(src, dst string) (err error) {
in, err := os.Open(src)
Expand Down
126 changes: 126 additions & 0 deletions util/file_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package util_test

import (
"crypto/md5"
"encoding/hex"
"io"
"os"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/netbirdio/netbird/util"
)

var _ = Describe("Client", func() {

var (
tmpDir string
)

type TestConfig struct {
SomeMap map[string]string
SomeArray []string
SomeField int
}

BeforeEach(func() {
var err error
tmpDir, err = os.MkdirTemp("", "wiretrustee_util_test_tmp_*")
Expect(err).NotTo(HaveOccurred())
})

AfterEach(func() {
err := os.RemoveAll(tmpDir)
Expect(err).NotTo(HaveOccurred())
})

Describe("Config", func() {
Context("in JSON format", func() {
It("should be written and read successfully", func() {

m := make(map[string]string)
m["key1"] = "value1"
m["key2"] = "value2"

arr := []string{"value1", "value2"}

written := &TestConfig{
SomeMap: m,
SomeArray: arr,
SomeField: 99,
}

err := util.WriteJson(tmpDir+"/testconfig.json", written)
Expect(err).NotTo(HaveOccurred())

read, err := util.ReadJson(tmpDir+"/testconfig.json", &TestConfig{})
Expect(err).NotTo(HaveOccurred())
Expect(read).NotTo(BeNil())
Expect(read.(*TestConfig).SomeMap["key1"]).To(BeEquivalentTo(written.SomeMap["key1"]))
Expect(read.(*TestConfig).SomeMap["key2"]).To(BeEquivalentTo(written.SomeMap["key2"]))
Expect(read.(*TestConfig).SomeArray).To(ContainElements(arr))
Expect(read.(*TestConfig).SomeField).To(BeEquivalentTo(written.SomeField))

})
})
})

Describe("Copying file contents", func() {
Context("from one file to another", func() {
It("should be successful", func() {

src := tmpDir + "/copytest_src"
dst := tmpDir + "/copytest_dst"

err := util.WriteJson(src, []string{"1", "2", "3"})
Expect(err).NotTo(HaveOccurred())

err = util.CopyFileContents(src, dst)
Expect(err).NotTo(HaveOccurred())

hashSrc := md5.New()
hashDst := md5.New()

srcFile, err := os.Open(src)
Expect(err).NotTo(HaveOccurred())

dstFile, err := os.Open(dst)
Expect(err).NotTo(HaveOccurred())

_, err = io.Copy(hashSrc, srcFile)
Expect(err).NotTo(HaveOccurred())

_, err = io.Copy(hashDst, dstFile)
Expect(err).NotTo(HaveOccurred())

err = srcFile.Close()
Expect(err).NotTo(HaveOccurred())

err = dstFile.Close()
Expect(err).NotTo(HaveOccurred())

Expect(hex.EncodeToString(hashSrc.Sum(nil)[:16])).To(BeEquivalentTo(hex.EncodeToString(hashDst.Sum(nil)[:16])))
})
})
})

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

0 comments on commit 8739b2a

Please sign in to comment.