all: fix control profile

This commit is contained in:
Stanislav Chzhen
2025-08-25 16:59:06 +03:00
parent 5f591bda61
commit 799e8b49a1
4 changed files with 24 additions and 15 deletions

View File

@@ -17,6 +17,13 @@ See also the [v0.107.66 GitHub milestone][ms-v0.107.66].
NOTE: Add new changes BELOW THIS COMMENT. 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
<!-- <!--
NOTE: Add new changes ABOVE THIS COMMENT. NOTE: Add new changes ABOVE THIS COMMENT.
--> -->

View File

@@ -629,8 +629,8 @@ func validateBindHosts(conf *configuration) (err error) {
} }
// parseConfig loads configuration from the YAML file, upgrading it if // parseConfig loads configuration from the YAML file, upgrading it if
// necessary. // necessary. l must not be nil.
func parseConfig() (err error) { func parseConfig(ctx context.Context, l *slog.Logger) (err error) {
// Do the upgrade if necessary. // Do the upgrade if necessary.
config.fileData, err = readConfigFile() config.fileData, err = readConfigFile()
if err != nil { if err != nil {
@@ -652,7 +652,7 @@ func parseConfig() (err error) {
return err return err
} else if upgraded { } else if upgraded {
confPath := configFilePath() 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) err = maybe.WriteFile(confPath, config.fileData, aghos.DefaultPermFile)
if err != nil { if err != nil {
@@ -666,7 +666,7 @@ func parseConfig() (err error) {
return err return err
} }
err = validateConfig() err = validateConfig(ctx, l)
if err != nil { if err != nil {
return err return err
} }
@@ -679,8 +679,9 @@ func parseConfig() (err error) {
return validateTLSCipherIDs(config.TLS.OverrideTLSCiphers) return validateTLSCipherIDs(config.TLS.OverrideTLSCiphers)
} }
// validateConfig returns error if the configuration is invalid. // validateConfig returns error if the configuration is invalid. l must not be
func validateConfig() (err error) { // nil.
func validateConfig(ctx context.Context, l *slog.Logger) (err error) {
err = validateBindHosts(config) err = validateBindHosts(config)
if err != nil { if err != nil {
// Don't wrap the error since it's informative enough as is. // Don't wrap the error since it's informative enough as is.
@@ -716,6 +717,10 @@ func validateConfig() (err error) {
config.Filtering.FiltersUpdateIntervalHours = 24 config.Filtering.FiltersUpdateIntervalHours = 24
} }
if len(config.Users) == 0 {
l.WarnContext(ctx, "no users in the configuration file; authentication is disabled")
}
return nil return nil
} }

View File

@@ -178,7 +178,8 @@ func setupContext(ctx context.Context, baseLogger *slog.Logger, opts options) (e
return nil return nil
} }
err = parseConfig() // TODO(s.chzhen): Consider adding a key prefix.
err = parseConfig(ctx, baseLogger)
if err != nil { if err != nil {
log.Error("parsing configuration file: %s", err) log.Error("parsing configuration file: %s", err)

View File

@@ -47,14 +47,10 @@ type profileJSON struct {
// handleGetProfile is the handler for GET /control/profile endpoint. // handleGetProfile is the handler for GET /control/profile endpoint.
func (web *webAPI) handleGetProfile(w http.ResponseWriter, r *http.Request) { func (web *webAPI) handleGetProfile(w http.ResponseWriter, r *http.Request) {
var name string var name string
if !web.auth.isGLiNet { u, ok := webUserFromContext(r.Context())
u, ok := webUserFromContext(r.Context()) // There may be no user in the context if the configuration file defines no
if !ok { // users.
w.WriteHeader(http.StatusUnauthorized) if ok {
return
}
name = string(u.Login) name = string(u.Login)
} }