1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2026-10-28 15:02:28 +00:00

chasquid: Return error on inaccessible private key file

In commit 3f9d0c0762, we added a new possible private file. If the file
exists but there's an error reading it (common for permission issues),
currently we just move on to the next file, and if it doesn't exist we
may give up with that error.

That can "hide" the access problems making them harder to debug.

To help with those kinds of errors, this patch updates the logic to
return an error if a private key exists but is inaccessible.

It also adds an unit test for this function to cover the different
scenarios, and updates the docs to match.
This commit is contained in:
Alberto Bertogli
2026-09-27 20:35:48 +01:00
parent f22d2289af
commit e417d67f7c
7 changed files with 96 additions and 13 deletions
+12 -7
View File
@@ -6,6 +6,7 @@ package main
import (
"context"
"errors"
"flag"
"fmt"
"net"
@@ -284,24 +285,28 @@ func loadCert(name, dir string, s *smtpsrv.Server) {
func findCertKey(dir string) (string, error) {
keyFiles := []string{
// Lego (NixOS ACME module).
"key.pem",
// Letsencrypt Certbot.
"privkey.pem",
// Lego (NixOS ACME module).
"key.pem",
}
var err error
for _, name := range keyFiles {
keyPath := filepath.Join(dir, name)
_, err = os.Stat(keyPath)
_, err := os.Stat(keyPath)
if err == nil {
return keyPath, nil
}
if !errors.Is(err, os.ErrNotExist) {
// If the file does not exist, we move on to try the next file.
// But if it exists but it's inaccessible, return the error so
// users can debug it more clearly.
return "", err
}
}
return "", err
return "", fmt.Errorf(
"no private key found in %q (tried %v)", dir, keyFiles)
}
// Helper to load a single domain configuration into the server.