1
0
mirror of https://github.com/amir20/dozzle.git synced 2025-12-21 13:23:07 +01:00

feat: Add support for both .yml and .yaml files and prioritize .yml for users file (#3789)

This commit is contained in:
Piyush Dixit
2025-04-11 10:16:03 +05:30
committed by GitHub
parent ebcedb2a67
commit 2f84190faa
2 changed files with 26 additions and 14 deletions

View File

@@ -10,7 +10,11 @@ If you do not have an authentication solution, then Dozzle has a simple file-bas
## File-Based User Management ## 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: The content of the file looks like:

34
main.go
View File

@@ -129,6 +129,14 @@ func main() {
log.Debug().Msg("shut down complete") 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 { func createServer(args cli.Args, hostService web.HostService) *http.Server {
_, dev := os.LookupEnv("DEV") _, dev := os.LookupEnv("DEV")
@@ -141,21 +149,21 @@ func createServer(args cli.Args, hostService web.HostService) *http.Server {
} else if args.AuthProvider == "simple" { } else if args.AuthProvider == "simple" {
log.Debug().Msg("Using simple authentication") log.Debug().Msg("Using simple authentication")
provider = web.SIMPLE 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") log.Debug().Msgf("Reading %s file", filepath.Base(userFilePath))
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().Str("path", path).Msg("Reading users.yml file") db, err := auth.ReadUsersFromFile(userFilePath)
if err != nil {
db, err := auth.ReadUsersFromFile(path) log.Fatal().Err(err).Msgf("Could not read users file: %s", userFilePath)
if err != nil { }
log.Fatal().Err(err).Msg("Could not read users.yml file")
}
log.Debug().Int("users", len(db.Users)).Msg("Loaded users") log.Debug().Int("users", len(db.Users)).Msg("Loaded users")
ttl := time.Duration(0) ttl := time.Duration(0)