2021-04-25 17:24:42 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-04-27 02:30:06 +02:00
|
|
|
"fmt"
|
2021-04-25 17:24:42 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-04-27 02:30:06 +02:00
|
|
|
"strings"
|
2021-04-25 17:24:42 +02:00
|
|
|
"testing"
|
|
|
|
|
2021-06-05 11:13:28 +02:00
|
|
|
"github.com/juanfont/headscale/cmd/headscale/cli"
|
2021-04-25 17:24:42 +02:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"gopkg.in/check.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test(t *testing.T) {
|
|
|
|
check.TestingT(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ = check.Suite(&Suite{})
|
|
|
|
|
|
|
|
type Suite struct{}
|
|
|
|
|
|
|
|
func (s *Suite) SetUpSuite(c *check.C) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TearDownSuite(c *check.C) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-05-15 14:36:13 +02:00
|
|
|
func (*Suite) TestPostgresConfigLoading(c *check.C) {
|
2021-04-25 17:24:42 +02:00
|
|
|
tmpDir, err := ioutil.TempDir("", "headscale")
|
|
|
|
if err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
path, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Symlink the example config file
|
2021-10-20 23:27:59 +02:00
|
|
|
err = os.Symlink(filepath.Clean(path+"/../../config.yaml.postgres.example"), filepath.Join(tmpDir, "config.yaml"))
|
2021-04-25 17:24:42 +02:00
|
|
|
if err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-04-27 02:30:06 +02:00
|
|
|
// Load example config, it should load without validation errors
|
2021-06-05 11:13:28 +02:00
|
|
|
err = cli.LoadConfig(tmpDir)
|
2021-04-27 02:30:06 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-04-25 17:24:42 +02:00
|
|
|
|
|
|
|
// Test that config file was interpreted correctly
|
2021-07-30 17:07:19 +02:00
|
|
|
c.Assert(viper.GetString("server_url"), check.Equals, "http://127.0.0.1:8080")
|
|
|
|
c.Assert(viper.GetString("listen_addr"), check.Equals, "0.0.0.0:8080")
|
2021-04-25 17:24:42 +02:00
|
|
|
c.Assert(viper.GetString("derp_map_path"), check.Equals, "derp.yaml")
|
2021-05-15 14:36:13 +02:00
|
|
|
c.Assert(viper.GetString("db_type"), check.Equals, "postgres")
|
2021-04-25 17:24:42 +02:00
|
|
|
c.Assert(viper.GetString("db_port"), check.Equals, "5432")
|
|
|
|
c.Assert(viper.GetString("tls_letsencrypt_hostname"), check.Equals, "")
|
2021-07-24 00:12:01 +02:00
|
|
|
c.Assert(viper.GetString("tls_letsencrypt_listen"), check.Equals, ":http")
|
2021-08-24 08:10:09 +02:00
|
|
|
c.Assert(viper.GetStringSlice("dns_config.nameservers")[0], check.Equals, "1.1.1.1")
|
2021-04-25 17:24:42 +02:00
|
|
|
}
|
2021-04-27 02:30:06 +02:00
|
|
|
|
2021-05-15 14:36:13 +02:00
|
|
|
func (*Suite) TestSqliteConfigLoading(c *check.C) {
|
|
|
|
tmpDir, err := ioutil.TempDir("", "headscale")
|
|
|
|
if err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
path, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Symlink the example config file
|
2021-10-20 23:27:59 +02:00
|
|
|
err = os.Symlink(filepath.Clean(path+"/../../config.yaml.sqlite.example"), filepath.Join(tmpDir, "config.yaml"))
|
2021-05-15 14:36:13 +02:00
|
|
|
if err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load example config, it should load without validation errors
|
2021-06-05 11:13:28 +02:00
|
|
|
err = cli.LoadConfig(tmpDir)
|
2021-05-15 14:36:13 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
// Test that config file was interpreted correctly
|
2021-07-30 17:07:19 +02:00
|
|
|
c.Assert(viper.GetString("server_url"), check.Equals, "http://127.0.0.1:8080")
|
|
|
|
c.Assert(viper.GetString("listen_addr"), check.Equals, "0.0.0.0:8080")
|
2021-05-15 14:36:13 +02:00
|
|
|
c.Assert(viper.GetString("derp_map_path"), check.Equals, "derp.yaml")
|
|
|
|
c.Assert(viper.GetString("db_type"), check.Equals, "sqlite3")
|
2021-05-18 23:33:18 +02:00
|
|
|
c.Assert(viper.GetString("db_path"), check.Equals, "db.sqlite")
|
2021-05-15 14:36:13 +02:00
|
|
|
c.Assert(viper.GetString("tls_letsencrypt_hostname"), check.Equals, "")
|
2021-07-24 00:12:01 +02:00
|
|
|
c.Assert(viper.GetString("tls_letsencrypt_listen"), check.Equals, ":http")
|
2021-05-15 14:36:13 +02:00
|
|
|
c.Assert(viper.GetString("tls_letsencrypt_challenge_type"), check.Equals, "HTTP-01")
|
2021-08-24 08:10:09 +02:00
|
|
|
c.Assert(viper.GetStringSlice("dns_config.nameservers")[0], check.Equals, "1.1.1.1")
|
2021-05-15 14:36:13 +02:00
|
|
|
}
|
|
|
|
|
2021-08-25 20:03:04 +02:00
|
|
|
func (*Suite) TestDNSConfigLoading(c *check.C) {
|
|
|
|
tmpDir, err := ioutil.TempDir("", "headscale")
|
|
|
|
if err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
path, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Symlink the example config file
|
2021-10-20 23:27:59 +02:00
|
|
|
err = os.Symlink(filepath.Clean(path+"/../../config.yaml.sqlite.example"), filepath.Join(tmpDir, "config.yaml"))
|
2021-08-25 20:03:04 +02:00
|
|
|
if err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load example config, it should load without validation errors
|
|
|
|
err = cli.LoadConfig(tmpDir)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-10-02 11:14:18 +02:00
|
|
|
dnsConfig, baseDomain := cli.GetDNSConfig()
|
2021-08-25 20:03:04 +02:00
|
|
|
|
|
|
|
c.Assert(dnsConfig.Nameservers[0].String(), check.Equals, "1.1.1.1")
|
|
|
|
c.Assert(dnsConfig.Resolvers[0].Addr, check.Equals, "1.1.1.1")
|
2021-10-02 11:14:18 +02:00
|
|
|
c.Assert(dnsConfig.Proxied, check.Equals, true)
|
|
|
|
c.Assert(baseDomain, check.Equals, "example.com")
|
2021-08-25 20:03:04 +02:00
|
|
|
}
|
|
|
|
|
2021-04-27 02:30:06 +02:00
|
|
|
func writeConfig(c *check.C, tmpDir string, configYaml []byte) {
|
|
|
|
// Populate a custom config file
|
|
|
|
configFile := filepath.Join(tmpDir, "config.yaml")
|
|
|
|
err := ioutil.WriteFile(configFile, configYaml, 0644)
|
|
|
|
if err != nil {
|
|
|
|
c.Fatalf("Couldn't write file %s", configFile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*Suite) TestTLSConfigValidation(c *check.C) {
|
|
|
|
tmpDir, err := ioutil.TempDir("", "headscale")
|
|
|
|
if err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
|
|
|
//defer os.RemoveAll(tmpDir)
|
|
|
|
fmt.Println(tmpDir)
|
|
|
|
|
|
|
|
configYaml := []byte("---\ntls_letsencrypt_hostname: \"example.com\"\ntls_letsencrypt_challenge_type: \"\"\ntls_cert_path: \"abc.pem\"")
|
|
|
|
writeConfig(c, tmpDir, configYaml)
|
|
|
|
|
|
|
|
// Check configuration validation errors (1)
|
2021-06-05 11:13:28 +02:00
|
|
|
err = cli.LoadConfig(tmpDir)
|
2021-04-27 02:30:06 +02:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
// check.Matches can not handle multiline strings
|
|
|
|
tmp := strings.ReplaceAll(err.Error(), "\n", "***")
|
|
|
|
c.Assert(tmp, check.Matches, ".*Fatal config error: set either tls_letsencrypt_hostname or tls_cert_path/tls_key_path, not both.*")
|
|
|
|
c.Assert(tmp, check.Matches, ".*Fatal config error: the only supported values for tls_letsencrypt_challenge_type are.*")
|
|
|
|
c.Assert(tmp, check.Matches, ".*Fatal config error: server_url must start with https:// or http://.*")
|
|
|
|
fmt.Println(tmp)
|
|
|
|
|
|
|
|
// Check configuration validation errors (2)
|
2021-07-30 17:07:19 +02:00
|
|
|
configYaml = []byte("---\nserver_url: \"http://127.0.0.1:8080\"\ntls_letsencrypt_hostname: \"example.com\"\ntls_letsencrypt_challenge_type: \"TLS-ALPN-01\"")
|
2021-04-27 02:30:06 +02:00
|
|
|
writeConfig(c, tmpDir, configYaml)
|
2021-06-05 11:13:28 +02:00
|
|
|
err = cli.LoadConfig(tmpDir)
|
2021-07-17 04:02:05 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-04-27 02:30:06 +02:00
|
|
|
}
|