From 2f84190faa4920048741b1a6f5c2ac1618c5efdc Mon Sep 17 00:00:00 2001 From: Piyush Dixit <79581397+PiyushDixit96@users.noreply.github.com> Date: Fri, 11 Apr 2025 10:16:03 +0530 Subject: [PATCH] feat: Add support for both .yml and .yaml files and prioritize .yml for users file (#3789) --- docs/guide/authentication.md | 6 +++++- main.go | 34 +++++++++++++++++++++------------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/docs/guide/authentication.md b/docs/guide/authentication.md index c5caaefb..abb68c04 100644 --- a/docs/guide/authentication.md +++ b/docs/guide/authentication.md @@ -10,7 +10,11 @@ If you do not have an authentication solution, then Dozzle has a simple file-bas ## File-Based User Management -Dozzle supports multi-user authentication by setting `--auth-provider` to `simple`. In this mode, Dozzle will try to read `/data/users.yml`. +**Dozzle** supports multi-user authentication by setting `--auth-provider` to `simple`. In this mode, Dozzle will attempt to read the users file from `/data/`, prioritizing `users.yml` over `users.yaml` if both files are present. If only one of the files exists, it will be used. The log will indicate which file is being read (e.g., `Reading users.yml file`). + +### Example file paths: +- `/data/users.yml` +- `/data/users.yaml` The content of the file looks like: diff --git a/main.go b/main.go index 5f774f35..fc654c3e 100644 --- a/main.go +++ b/main.go @@ -129,6 +129,14 @@ func main() { log.Debug().Msg("shut down complete") } +func fileExists(filename string) bool { + _, err := os.Stat(filename) + if os.IsNotExist(err) { + return false + } + return err == nil +} + func createServer(args cli.Args, hostService web.HostService) *http.Server { _, dev := os.LookupEnv("DEV") @@ -141,21 +149,21 @@ func createServer(args cli.Args, hostService web.HostService) *http.Server { } else if args.AuthProvider == "simple" { log.Debug().Msg("Using simple authentication") provider = web.SIMPLE + + userFilePath := "./data/users.yml" + if !fileExists(userFilePath) { + userFilePath = "./data/users.yaml" + if !fileExists(userFilePath) { + log.Fatal().Msg("No users.yaml or users.yml file found.") + } + } - path, err := filepath.Abs("./data/users.yml") - if err != nil { - log.Fatal().Err(err).Msg("Could not get absolute path") - } - if _, err := os.Stat(path); os.IsNotExist(err) { - log.Fatal().Msg("users.yml file does not exist") - } + log.Debug().Msgf("Reading %s file", filepath.Base(userFilePath)) - log.Debug().Str("path", path).Msg("Reading users.yml file") - - db, err := auth.ReadUsersFromFile(path) - if err != nil { - log.Fatal().Err(err).Msg("Could not read users.yml file") - } + db, err := auth.ReadUsersFromFile(userFilePath) + if err != nil { + log.Fatal().Err(err).Msgf("Could not read users file: %s", userFilePath) + } log.Debug().Int("users", len(db.Users)).Msg("Loaded users") ttl := time.Duration(0)