logpolicy: fix config initialization bug (#5010)

If ConfigFromFile cannot find the configuration file,
we must not initialize it with NewConfig.
Instead, we need it to fail validation so that it eventually writes
a newly constructed configuration file.
Otherwise, new tailscale instances will never be able store a persistent
log config and start with a new config file upon every bootup.

Signed-off-by: Joe Tsai <joetsai@digital-static.net>
pull/5014/head
Joe Tsai 2 years ago committed by GitHub
parent d6817d0f22
commit fe3426b4c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -112,6 +112,8 @@ func NewConfig(collection string) *Config {
// and that the PrivateID and PublicID pair are sensible. // and that the PrivateID and PublicID pair are sensible.
func (c *Config) Validate(collection string) error { func (c *Config) Validate(collection string) error {
switch { switch {
case c == nil:
return errors.New("config is nil")
case c.Collection != collection: case c.Collection != collection:
return fmt.Errorf("config collection %q does not match %q", c.Collection, collection) return fmt.Errorf("config collection %q does not match %q", c.Collection, collection)
case c.PrivateID.IsZero(): case c.PrivateID.IsZero():
@ -509,11 +511,10 @@ func New(collection string) *Policy {
newc, err := ConfigFromFile(cfgPath) newc, err := ConfigFromFile(cfgPath)
if err != nil { if err != nil {
earlyLogf("logpolicy.ConfigFromFile %v: %v", cfgPath, err) earlyLogf("logpolicy.ConfigFromFile %v: %v", cfgPath, err)
newc = NewConfig(collection)
} }
if err := newc.Validate(collection); err != nil { if err := newc.Validate(collection); err != nil {
earlyLogf("logpolicy.Config.Validate for %q: %v", cfgPath, err) earlyLogf("logpolicy.Config.Validate for %v: %v", cfgPath, err)
newc := NewConfig(collection) newc = NewConfig(collection)
if err := newc.Save(cfgPath); err != nil { if err := newc.Save(cfgPath); err != nil {
earlyLogf("logpolicy.Config.Save for %v: %v", cfgPath, err) earlyLogf("logpolicy.Config.Save for %v: %v", cfgPath, err)
} }

Loading…
Cancel
Save