1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2025-12-23 15:37:01 +00:00

Log how many things were loaded for each domain

This patch makes chasquid log how many users, aliases and DKIM keys were
loaded for each domain.

This makes it easier to confirm changes, and troubleshoot problems
related to these per-domain configuration files.
This commit is contained in:
Alberto Bertogli
2024-05-10 11:42:07 +01:00
parent e6a9410377
commit aae0367c60
7 changed files with 45 additions and 24 deletions

View File

@@ -347,7 +347,7 @@ func (v *Resolver) AddDomain(domain string) {
// AddAliasesFile to the resolver. The file will be parsed, and an error
// returned if it does not parse correctly. Note that the file not existing
// does NOT result in an error.
func (v *Resolver) AddAliasesFile(domain, path string) error {
func (v *Resolver) AddAliasesFile(domain, path string) (int, error) {
// We unconditionally add the domain and file on our list.
// Even if the file does not exist now, it may later. This makes it be
// consider when doing Reload.
@@ -360,10 +360,10 @@ func (v *Resolver) AddAliasesFile(domain, path string) error {
aliases, err := v.parseFile(domain, path)
if os.IsNotExist(err) {
return nil
return 0, nil
}
if err != nil {
return err
return 0, err
}
// Add the aliases to the resolver, overriding any previous values.
@@ -373,7 +373,7 @@ func (v *Resolver) AddAliasesFile(domain, path string) error {
}
v.mu.Unlock()
return nil
return len(aliases), nil
}
// AddAliasForTesting adds an alias to the resolver, for testing purposes.

View File

@@ -437,7 +437,7 @@ func TestAddFile(t *testing.T) {
defer os.Remove(fname)
resolver := NewResolver(allUsersExist)
err := resolver.AddAliasesFile("dom", fname)
_, err := resolver.AddAliasesFile("dom", fname)
if err != nil {
t.Fatalf("error adding file: %v", err)
}
@@ -491,11 +491,15 @@ func TestRichFile(t *testing.T) {
resolver := NewResolver(allUsersExist)
resolver.DropChars = "."
resolver.SuffixSep = "+"
err := resolver.AddAliasesFile("dom", fname)
n, err := resolver.AddAliasesFile("dom", fname)
if err != nil {
t.Fatalf("failed to add file: %v", err)
}
if n != 11 {
t.Fatalf("expected 11 aliases, got %d", n)
}
cases := Cases{
{"a@dom", []Recipient{{"b@dom", EMAIL}}, nil},
{"c@dom", []Recipient{{"d@e", EMAIL}, {"f@dom", EMAIL}}, nil},
@@ -539,7 +543,7 @@ func TestManyFiles(t *testing.T) {
resolver := NewResolver(allUsersExist)
for domain, fname := range files {
err := resolver.AddAliasesFile(domain, fname)
_, err := resolver.AddAliasesFile(domain, fname)
if err != nil {
t.Fatalf("failed to add file: %v", err)
}

View File

@@ -133,16 +133,16 @@ func (s *Server) AddDomain(d string) {
}
// AddUserDB adds a userdb file as backend for the domain.
func (s *Server) AddUserDB(domain, f string) error {
func (s *Server) AddUserDB(domain, f string) (int, error) {
// Load the userdb, and register it unconditionally (so reload works even
// if there are errors right now).
udb, err := userdb.Load(f)
s.authr.Register(domain, auth.WrapNoErrorBackend(udb))
return err
return udb.Len(), err
}
// AddAliasesFile adds an aliases file for the given domain.
func (s *Server) AddAliasesFile(domain, f string) error {
func (s *Server) AddAliasesFile(domain, f string) (int, error) {
return s.aliasesR.AddAliasesFile(domain, f)
}

View File

@@ -207,6 +207,13 @@ func (db *DB) Exists(name string) bool {
return present
}
// Len returns the number of users in the database.
func (db *DB) Len() int {
db.mu.Lock()
defer db.mu.Unlock()
return len(db.db.Users)
}
///////////////////////////////////////////////////////////
// Encryption schemes
//