2022-03-13 15:17:18 +01:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2023-12-27 20:56:04 +01:00
|
|
|
"context"
|
2022-03-13 15:17:18 +01:00
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2022-04-15 17:30:12 +02:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-12-27 20:56:04 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-12-14 11:48:12 +01:00
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/util"
|
2022-03-13 15:17:18 +01:00
|
|
|
)
|
|
|
|
|
2022-04-15 17:30:12 +02:00
|
|
|
func TestGetConfig(t *testing.T) {
|
2023-02-07 11:40:05 +01:00
|
|
|
// case 1: new default config has to be generated
|
2023-03-02 13:28:14 +01:00
|
|
|
config, err := UpdateOrCreateConfig(ConfigInput{
|
2023-02-07 11:40:05 +01:00
|
|
|
ConfigPath: filepath.Join(t.TempDir(), "config.json"),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, config.ManagementURL.String(), DefaultManagementURL)
|
|
|
|
assert.Equal(t, config.AdminURL.String(), DefaultAdminURL)
|
|
|
|
|
2022-03-13 15:17:18 +01:00
|
|
|
managementURL := "https://test.management.url:33071"
|
2023-02-07 11:40:05 +01:00
|
|
|
adminURL := "https://app.admin.url:443"
|
2022-03-13 15:17:18 +01:00
|
|
|
path := filepath.Join(t.TempDir(), "config.json")
|
|
|
|
preSharedKey := "preSharedKey"
|
|
|
|
|
2023-02-07 11:40:05 +01:00
|
|
|
// case 2: new config has to be generated
|
2023-03-02 13:28:14 +01:00
|
|
|
config, err = UpdateOrCreateConfig(ConfigInput{
|
2023-01-08 12:57:28 +01:00
|
|
|
ManagementURL: managementURL,
|
|
|
|
AdminURL: adminURL,
|
|
|
|
ConfigPath: path,
|
|
|
|
PreSharedKey: &preSharedKey,
|
|
|
|
})
|
2022-03-13 15:17:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, config.ManagementURL.String(), managementURL)
|
|
|
|
assert.Equal(t, config.PreSharedKey, preSharedKey)
|
|
|
|
|
|
|
|
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
|
|
|
|
t.Errorf("config file was expected to be created under path %s", path)
|
|
|
|
}
|
|
|
|
|
2023-02-07 11:40:05 +01:00
|
|
|
// case 3: existing config -> fetch it
|
2023-03-02 13:28:14 +01:00
|
|
|
config, err = UpdateOrCreateConfig(ConfigInput{
|
2023-01-08 12:57:28 +01:00
|
|
|
ManagementURL: managementURL,
|
|
|
|
AdminURL: adminURL,
|
|
|
|
ConfigPath: path,
|
|
|
|
PreSharedKey: &preSharedKey,
|
|
|
|
})
|
2022-03-13 15:17:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, config.ManagementURL.String(), managementURL)
|
|
|
|
assert.Equal(t, config.PreSharedKey, preSharedKey)
|
|
|
|
|
2023-12-14 11:48:12 +01:00
|
|
|
// case 4: existing config, but new managementURL has been provided -> update config
|
2022-03-13 15:17:18 +01:00
|
|
|
newManagementURL := "https://test.newManagement.url:33071"
|
2023-03-02 13:28:14 +01:00
|
|
|
config, err = UpdateOrCreateConfig(ConfigInput{
|
2023-01-08 12:57:28 +01:00
|
|
|
ManagementURL: newManagementURL,
|
|
|
|
AdminURL: adminURL,
|
|
|
|
ConfigPath: path,
|
|
|
|
PreSharedKey: &preSharedKey,
|
|
|
|
})
|
2022-03-13 15:17:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, config.ManagementURL.String(), newManagementURL)
|
|
|
|
assert.Equal(t, config.PreSharedKey, preSharedKey)
|
|
|
|
|
|
|
|
// read once more to make sure that config file has been updated with the new management URL
|
|
|
|
readConf, err := util.ReadJson(path, config)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.Equal(t, readConf.(*Config).ManagementURL.String(), newManagementURL)
|
|
|
|
}
|
2023-02-23 09:48:43 +01:00
|
|
|
|
|
|
|
func TestHiddenPreSharedKey(t *testing.T) {
|
|
|
|
hidden := "**********"
|
|
|
|
samplePreSharedKey := "mysecretpresharedkey"
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
preSharedKey *string
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{"nil", nil, ""},
|
|
|
|
{"hidden", &hidden, ""},
|
|
|
|
{"filled", &samplePreSharedKey, samplePreSharedKey},
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate default cfg
|
|
|
|
cfgFile := filepath.Join(t.TempDir(), "config.json")
|
2023-03-02 13:28:14 +01:00
|
|
|
_, _ = UpdateOrCreateConfig(ConfigInput{
|
2023-02-23 09:48:43 +01:00
|
|
|
ConfigPath: cfgFile,
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-03-02 13:28:14 +01:00
|
|
|
cfg, err := UpdateOrCreateConfig(ConfigInput{
|
2023-02-23 09:48:43 +01:00
|
|
|
ConfigPath: cfgFile,
|
|
|
|
PreSharedKey: tt.preSharedKey,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to get cfg: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.PreSharedKey != tt.want {
|
|
|
|
t.Fatalf("invalid preshared key: '%s', expected: '%s' ", cfg.PreSharedKey, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-12-27 20:56:04 +01:00
|
|
|
|
|
|
|
func TestUpdateOldManagementURL(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
previousManagementURL string
|
|
|
|
expectedManagementURL string
|
|
|
|
fileShouldNotChange bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Update old management URL with legacy port",
|
|
|
|
previousManagementURL: "https://api.wiretrustee.com:33073",
|
|
|
|
expectedManagementURL: DefaultManagementURL,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Update old management URL",
|
|
|
|
previousManagementURL: oldDefaultManagementURL,
|
|
|
|
expectedManagementURL: DefaultManagementURL,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "No update needed when management URL is up to date",
|
|
|
|
previousManagementURL: DefaultManagementURL,
|
|
|
|
expectedManagementURL: DefaultManagementURL,
|
|
|
|
fileShouldNotChange: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "No update needed when not using cloud management",
|
|
|
|
previousManagementURL: "https://netbird.example.com:33073",
|
|
|
|
expectedManagementURL: "https://netbird.example.com:33073",
|
|
|
|
fileShouldNotChange: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
tempDir := t.TempDir()
|
|
|
|
configPath := filepath.Join(tempDir, "config.json")
|
|
|
|
config, err := UpdateOrCreateConfig(ConfigInput{
|
|
|
|
ManagementURL: tt.previousManagementURL,
|
|
|
|
ConfigPath: configPath,
|
|
|
|
})
|
|
|
|
require.NoError(t, err, "failed to create testing config")
|
|
|
|
previousStats, err := os.Stat(configPath)
|
|
|
|
require.NoError(t, err, "failed to create testing config stats")
|
|
|
|
resultConfig, err := UpdateOldManagementURL(context.TODO(), config, configPath)
|
|
|
|
require.NoError(t, err, "got error when updating old management url")
|
|
|
|
require.Equal(t, tt.expectedManagementURL, resultConfig.ManagementURL.String())
|
|
|
|
newStats, err := os.Stat(configPath)
|
|
|
|
require.NoError(t, err, "failed to create testing config stats")
|
|
|
|
switch tt.fileShouldNotChange {
|
|
|
|
case true:
|
|
|
|
require.Equal(t, previousStats.ModTime(), newStats.ModTime(), "file should not change")
|
|
|
|
case false:
|
|
|
|
require.NotEqual(t, previousStats.ModTime(), newStats.ModTime(), "file should have changed")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|