diff --git a/ipn/auditlog/extension.go b/ipn/auditlog/extension.go index e403f145d..eedfec58c 100644 --- a/ipn/auditlog/extension.go +++ b/ipn/auditlog/extension.go @@ -37,6 +37,11 @@ func init() { type extension struct { logf logger.Logf + // sb is the backend, consulted for its state directory when the + // store is created. The directory is not known yet when the + // extension is constructed. + sb ipnext.SafeBackend + // store is the log store shared by all loggers. // It is created when the first logger is started. store lazy.SyncValue[LogStore] @@ -53,8 +58,11 @@ type extension struct { // newExtension is an [ipnext.NewExtensionFn] that creates a new audit log extension. // It is registered with [ipnext.RegisterExtension] if the package is imported. -func newExtension(logf logger.Logf, _ ipnext.SafeBackend) (ipnext.Extension, error) { - return &extension{logf: logger.WithPrefix(logf, featureName+": ")}, nil +func newExtension(logf logger.Logf, sb ipnext.SafeBackend) (ipnext.Extension, error) { + return &extension{ + logf: logger.WithPrefix(logf, featureName+": "), + sb: sb, + }, nil } // Name implements [ipnext.Extension]. @@ -86,7 +94,7 @@ func (e *extension) startNewLogger(cc controlclient.Client, profileID ipn.Profil // Create a new log store if this is the first logger. // Otherwise, get the existing log store. store, err := e.store.GetErr(func() (LogStore, error) { - return newDefaultLogStore(e.logf) + return newDefaultLogStore(e.logf, e.sb.TailscaleVarRoot()) }) if err != nil { return nil, fmt.Errorf("failed to create audit log store: %w", err) diff --git a/ipn/auditlog/store.go b/ipn/auditlog/store.go index 07c971772..10075e5ef 100644 --- a/ipn/auditlog/store.go +++ b/ipn/auditlog/store.go @@ -41,9 +41,20 @@ func DefaultStoreFilePath() (string, error) { } } -// newDefaultLogStore returns a new [LogStore] for the current platform. -func newDefaultLogStore(logf logger.Logf) (LogStore, error) { - path, err := storeFilePath.GetErr(DefaultStoreFilePath) +// newDefaultLogStore returns a new [LogStore] for the current platform. Its +// file is the path set with [SetStoreFilePath] if any, else audit-log.json in +// varRoot (the backend's state directory) if that is known, else the platform +// default from [DefaultStoreFilePath]. For the Windows service the state +// directory is the platform default's directory anyway; the difference is for +// a tailscaled run by a user with their own --statedir, which cannot write +// under %ProgramData%. +func newDefaultLogStore(logf logger.Logf, varRoot string) (LogStore, error) { + path, err := storeFilePath.GetErr(func() (string, error) { + if varRoot != "" { + return filepath.Join(varRoot, "audit-log.json"), nil + } + return DefaultStoreFilePath() + }) if err != nil { // This indicates that the auditlog package was not omitted from the build // on a platform without a default store path and that [SetStoreFilePath]