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.
-->
### 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.
-->

View File

@ -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
}

View File

@ -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)

View File

@ -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)
}