ipn/{conffile,ipnlocal}: start booting tailscaled from a config file w/ auth key

Updates #1412

Change-Id: Icd880035a31df59797b8379f4af19da5c4c453e2
Co-authored-by: Maisem Ali <maisem@tailscale.com>
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2023-10-16 12:15:03 -07:00
committed by Brad Fitzpatrick
parent 6ca8650c7b
commit 1fc3573446
7 changed files with 126 additions and 10 deletions

View File

@@ -6,6 +6,7 @@
package conffile
import (
"bytes"
"encoding/json"
"fmt"
"os"
@@ -14,7 +15,7 @@ import (
"tailscale.com/ipn"
)
// Config describes a config file
// Config describes a config file.
type Config struct {
Path string // disk path of HuJSON
Raw []byte // raw bytes from disk, in HuJSON form
@@ -29,6 +30,11 @@ type Config struct {
Parsed ipn.ConfigVAlpha
}
// WantRunning reports whether c is non-nil and it's configured to be running.
func (c *Config) WantRunning() bool {
return c != nil && !c.Parsed.Enabled.EqualBool(false)
}
// Load reads and parses the config file at the provided path on disk.
func Load(path string) (*Config, error) {
var c Config
@@ -58,9 +64,14 @@ func Load(path string) (*Config, error) {
}
c.Version = ver.Version
err = json.Unmarshal(c.Std, &c.Parsed)
jd := json.NewDecoder(bytes.NewReader(c.Std))
jd.DisallowUnknownFields()
err = jd.Decode(&c.Parsed)
if err != nil {
return nil, fmt.Errorf("error parsing config file %s: %w", path, err)
}
if jd.More() {
return nil, fmt.Errorf("error parsing config file %s: trailing data after JSON object", path)
}
return &c, nil
}