diff --git a/CHANGELOG.md b/CHANGELOG.md index ea1434b7..21d21b9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,13 @@ See also the [v0.107.66 GitHub milestone][ms-v0.107.66]. NOTE: Add new changes BELOW THIS COMMENT. --> + +### Fixed + +- The HTTP API `GET /control/profile` endpoint failing when no users were configured ([#7985]). + +[#7985]: https://github.com/AdguardTeam/AdGuardHome/issues/7985 + diff --git a/internal/home/config.go b/internal/home/config.go index 65452f8f..49dcf4ca 100644 --- a/internal/home/config.go +++ b/internal/home/config.go @@ -629,8 +629,8 @@ func validateBindHosts(conf *configuration) (err error) { } // parseConfig loads configuration from the YAML file, upgrading it if -// necessary. -func parseConfig() (err error) { +// necessary. l must not be nil. +func parseConfig(ctx context.Context, l *slog.Logger) (err error) { // Do the upgrade if necessary. config.fileData, err = readConfigFile() if err != nil { @@ -652,7 +652,7 @@ func parseConfig() (err error) { return err } else if upgraded { confPath := configFilePath() - log.Debug("writing config file %q after config upgrade", confPath) + l.DebugContext(ctx, "writing config file after config upgrade", "path", confPath) err = maybe.WriteFile(confPath, config.fileData, aghos.DefaultPermFile) if err != nil { @@ -666,7 +666,7 @@ func parseConfig() (err error) { return err } - err = validateConfig() + err = validateConfig(ctx, l) if err != nil { return err } @@ -679,8 +679,9 @@ func parseConfig() (err error) { return validateTLSCipherIDs(config.TLS.OverrideTLSCiphers) } -// validateConfig returns error if the configuration is invalid. -func validateConfig() (err error) { +// validateConfig returns error if the configuration is invalid. l must not be +// nil. +func validateConfig(ctx context.Context, l *slog.Logger) (err error) { err = validateBindHosts(config) if err != nil { // Don't wrap the error since it's informative enough as is. @@ -716,6 +717,10 @@ func validateConfig() (err error) { config.Filtering.FiltersUpdateIntervalHours = 24 } + if len(config.Users) == 0 { + l.WarnContext(ctx, "no users in the configuration file; authentication is disabled") + } + return nil } diff --git a/internal/home/home.go b/internal/home/home.go index 9536219d..459474d6 100644 --- a/internal/home/home.go +++ b/internal/home/home.go @@ -178,7 +178,8 @@ func setupContext(ctx context.Context, baseLogger *slog.Logger, opts options) (e return nil } - err = parseConfig() + // TODO(s.chzhen): Consider adding a key prefix. + err = parseConfig(ctx, baseLogger) if err != nil { log.Error("parsing configuration file: %s", err) diff --git a/internal/home/profilehttp.go b/internal/home/profilehttp.go index 41d2c18e..1a89851a 100644 --- a/internal/home/profilehttp.go +++ b/internal/home/profilehttp.go @@ -47,14 +47,10 @@ type profileJSON struct { // handleGetProfile is the handler for GET /control/profile endpoint. func (web *webAPI) handleGetProfile(w http.ResponseWriter, r *http.Request) { var name string - if !web.auth.isGLiNet { - u, ok := webUserFromContext(r.Context()) - if !ok { - w.WriteHeader(http.StatusUnauthorized) - - return - } - + u, ok := webUserFromContext(r.Context()) + // There may be no user in the context if the configuration file defines no + // users. + if ok { name = string(u.Login) }