Skip to content

Commit 0fcd92f

Browse files
committed
Minor fix to help testing
1 parent 094fde3 commit 0fcd92f

File tree

3 files changed

+51
-51
lines changed

3 files changed

+51
-51
lines changed

cmd/headscale/cli/utils.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cli
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"io"
78
"log"
@@ -20,6 +21,50 @@ type ErrorOutput struct {
2021
Error string
2122
}
2223

24+
func LoadConfig(path string) error {
25+
viper.SetConfigName("config")
26+
if path == "" {
27+
viper.AddConfigPath("/etc/headscale/")
28+
viper.AddConfigPath("$HOME/.headscale")
29+
viper.AddConfigPath(".")
30+
} else {
31+
// For testing
32+
viper.AddConfigPath(path)
33+
}
34+
viper.AutomaticEnv()
35+
36+
viper.SetDefault("tls_letsencrypt_cache_dir", "/var/www/.cache")
37+
viper.SetDefault("tls_letsencrypt_challenge_type", "HTTP-01")
38+
39+
err := viper.ReadInConfig()
40+
if err != nil {
41+
return fmt.Errorf("Fatal error reading config file: %s \n", err)
42+
}
43+
44+
// Collect any validation errors and return them all at once
45+
var errorText string
46+
if (viper.GetString("tls_letsencrypt_hostname") != "") && ((viper.GetString("tls_cert_path") != "") || (viper.GetString("tls_key_path") != "")) {
47+
errorText += "Fatal config error: set either tls_letsencrypt_hostname or tls_cert_path/tls_key_path, not both\n"
48+
}
49+
50+
if (viper.GetString("tls_letsencrypt_hostname") != "") && (viper.GetString("tls_letsencrypt_challenge_type") == "TLS-ALPN-01") && (!strings.HasSuffix(viper.GetString("listen_addr"), ":443")) {
51+
errorText += "Fatal config error: when using tls_letsencrypt_hostname with TLS-ALPN-01 as challenge type, listen_addr must end in :443\n"
52+
}
53+
54+
if (viper.GetString("tls_letsencrypt_challenge_type") != "HTTP-01") && (viper.GetString("tls_letsencrypt_challenge_type") != "TLS-ALPN-01") {
55+
errorText += "Fatal config error: the only supported values for tls_letsencrypt_challenge_type are HTTP-01 and TLS-ALPN-01\n"
56+
}
57+
58+
if !strings.HasPrefix(viper.GetString("server_url"), "http://") && !strings.HasPrefix(viper.GetString("server_url"), "https://") {
59+
errorText += "Fatal config error: server_url must start with https:// or http://\n"
60+
}
61+
if errorText != "" {
62+
return errors.New(strings.TrimSuffix(errorText, "\n"))
63+
} else {
64+
return nil
65+
}
66+
}
67+
2368
func absPath(path string) string {
2469
// If a relative path is provided, prefix it with the the directory where
2570
// the config file was found.

cmd/headscale/headscale.go

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package main
22

33
import (
4-
"errors"
54
"fmt"
65
"log"
76
"os"
87
"strings"
98

109
"github.com/juanfont/headscale/cmd/headscale/cli"
1110
"github.com/spf13/cobra"
12-
"github.com/spf13/viper"
1311
)
1412

1513
var version = "dev"
@@ -38,52 +36,8 @@ Juan Font Alonso <[email protected]> - 2021
3836
https://gitlab.com/juanfont/headscale`,
3937
}
4038

41-
func loadConfig(path string) error {
42-
viper.SetConfigName("config")
43-
if path == "" {
44-
viper.AddConfigPath("/etc/headscale/")
45-
viper.AddConfigPath("$HOME/.headscale")
46-
viper.AddConfigPath(".")
47-
} else {
48-
// For testing
49-
viper.AddConfigPath(path)
50-
}
51-
viper.AutomaticEnv()
52-
53-
viper.SetDefault("tls_letsencrypt_cache_dir", "/var/www/.cache")
54-
viper.SetDefault("tls_letsencrypt_challenge_type", "HTTP-01")
55-
56-
err := viper.ReadInConfig()
57-
if err != nil {
58-
return fmt.Errorf("Fatal error reading config file: %s \n", err)
59-
}
60-
61-
// Collect any validation errors and return them all at once
62-
var errorText string
63-
if (viper.GetString("tls_letsencrypt_hostname") != "") && ((viper.GetString("tls_cert_path") != "") || (viper.GetString("tls_key_path") != "")) {
64-
errorText += "Fatal config error: set either tls_letsencrypt_hostname or tls_cert_path/tls_key_path, not both\n"
65-
}
66-
67-
if (viper.GetString("tls_letsencrypt_hostname") != "") && (viper.GetString("tls_letsencrypt_challenge_type") == "TLS-ALPN-01") && (!strings.HasSuffix(viper.GetString("listen_addr"), ":443")) {
68-
errorText += "Fatal config error: when using tls_letsencrypt_hostname with TLS-ALPN-01 as challenge type, listen_addr must end in :443\n"
69-
}
70-
71-
if (viper.GetString("tls_letsencrypt_challenge_type") != "HTTP-01") && (viper.GetString("tls_letsencrypt_challenge_type") != "TLS-ALPN-01") {
72-
errorText += "Fatal config error: the only supported values for tls_letsencrypt_challenge_type are HTTP-01 and TLS-ALPN-01\n"
73-
}
74-
75-
if !strings.HasPrefix(viper.GetString("server_url"), "http://") && !strings.HasPrefix(viper.GetString("server_url"), "https://") {
76-
errorText += "Fatal config error: server_url must start with https:// or http://\n"
77-
}
78-
if errorText != "" {
79-
return errors.New(strings.TrimSuffix(errorText, "\n"))
80-
} else {
81-
return nil
82-
}
83-
}
84-
8539
func main() {
86-
err := loadConfig("")
40+
err := cli.LoadConfig("")
8741
if err != nil {
8842
log.Fatalf(err.Error())
8943
}

cmd/headscale/headscale_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99
"testing"
1010

11+
"github.com/juanfont/headscale/cmd/headscale/cli"
1112
"github.com/spf13/viper"
1213
"gopkg.in/check.v1"
1314
)
@@ -46,7 +47,7 @@ func (*Suite) TestPostgresConfigLoading(c *check.C) {
4647
}
4748

4849
// Load example config, it should load without validation errors
49-
err = loadConfig(tmpDir)
50+
err = cli.LoadConfig(tmpDir)
5051
c.Assert(err, check.IsNil)
5152

5253
// Test that config file was interpreted correctly
@@ -78,7 +79,7 @@ func (*Suite) TestSqliteConfigLoading(c *check.C) {
7879
}
7980

8081
// Load example config, it should load without validation errors
81-
err = loadConfig(tmpDir)
82+
err = cli.LoadConfig(tmpDir)
8283
c.Assert(err, check.IsNil)
8384

8485
// Test that config file was interpreted correctly
@@ -112,7 +113,7 @@ func (*Suite) TestTLSConfigValidation(c *check.C) {
112113
writeConfig(c, tmpDir, configYaml)
113114

114115
// Check configuration validation errors (1)
115-
err = loadConfig(tmpDir)
116+
err = cli.LoadConfig(tmpDir)
116117
c.Assert(err, check.NotNil)
117118
// check.Matches can not handle multiline strings
118119
tmp := strings.ReplaceAll(err.Error(), "\n", "***")
@@ -124,7 +125,7 @@ func (*Suite) TestTLSConfigValidation(c *check.C) {
124125
// Check configuration validation errors (2)
125126
configYaml = []byte("---\nserver_url: \"http://127.0.0.1:8000\"\ntls_letsencrypt_hostname: \"example.com\"\ntls_letsencrypt_challenge_type: \"TLS-ALPN-01\"")
126127
writeConfig(c, tmpDir, configYaml)
127-
err = loadConfig(tmpDir)
128+
err = cli.LoadConfig(tmpDir)
128129
c.Assert(err, check.NotNil)
129130
c.Assert(err, check.ErrorMatches, "Fatal config error: when using tls_letsencrypt_hostname with TLS-ALPN-01 as challenge type, listen_addr must end in :443.*")
130131
}

0 commit comments

Comments
 (0)