2019-04-24 21:52:08 +00:00
|
|
|
package notification
|
|
|
|
|
|
|
|
import (
|
2022-12-23 05:06:49 +00:00
|
|
|
"bufio"
|
|
|
|
"context"
|
2019-04-24 21:52:08 +00:00
|
|
|
"fmt"
|
2022-07-18 00:56:09 +00:00
|
|
|
"net/mail"
|
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 04:59:38 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-04-24 21:52:08 +00:00
|
|
|
"time"
|
|
|
|
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
2022-12-23 05:06:49 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/templates"
|
2019-04-24 21:52:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// FileNotifier a notifier to send emails to SMTP servers.
|
|
|
|
type FileNotifier struct {
|
2022-12-23 05:06:49 +00:00
|
|
|
path string
|
|
|
|
append bool
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewFileNotifier create an FileNotifier writing the notification into a file.
|
|
|
|
func NewFileNotifier(configuration schema.FileSystemNotifierConfiguration) *FileNotifier {
|
|
|
|
return &FileNotifier{
|
|
|
|
path: configuration.Filename,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-17 09:53:59 +00:00
|
|
|
// StartupCheck implements the startup check provider interface.
|
2021-11-23 09:45:38 +00:00
|
|
|
func (n *FileNotifier) StartupCheck() (err error) {
|
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 04:59:38 +00:00
|
|
|
dir := filepath.Dir(n.path)
|
2020-04-23 02:01:24 +00:00
|
|
|
if _, err := os.Stat(dir); err != nil {
|
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 04:59:38 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(dir, fileNotifierMode); err != nil {
|
2021-09-17 09:53:59 +00:00
|
|
|
return err
|
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 04:59:38 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-09-17 09:53:59 +00:00
|
|
|
return err
|
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 04:59:38 +00:00
|
|
|
}
|
|
|
|
} else if _, err = os.Stat(n.path); err != nil {
|
2020-04-23 02:01:24 +00:00
|
|
|
if !os.IsNotExist(err) {
|
2021-09-17 09:53:59 +00:00
|
|
|
return err
|
2020-04-23 02:01:24 +00:00
|
|
|
}
|
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 04:59:38 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-02-09 22:07:53 +00:00
|
|
|
return os.WriteFile(n.path, []byte(""), fileNotifierMode)
|
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 04:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
// Send send a identity verification link to a user.
|
2022-12-23 05:06:49 +00:00
|
|
|
func (n *FileNotifier) Send(_ context.Context, recipient mail.Address, subject string, et *templates.EmailTemplate, data any) (err error) {
|
|
|
|
var f *os.File
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2022-12-23 05:06:49 +00:00
|
|
|
var flag int
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case n.append:
|
|
|
|
flag = os.O_APPEND | os.O_CREATE | os.O_WRONLY
|
|
|
|
default:
|
|
|
|
flag = os.O_TRUNC | os.O_CREATE | os.O_WRONLY
|
|
|
|
}
|
|
|
|
|
|
|
|
if f, err = os.OpenFile(n.path, flag, fileNotifierMode); err != nil {
|
|
|
|
return fmt.Errorf("failed to open file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
w := bufio.NewWriter(f)
|
|
|
|
|
|
|
|
if _, err = w.WriteString(fmt.Sprintf(fileNotifierHeader, time.Now(), recipient, subject)); err != nil {
|
|
|
|
return fmt.Errorf("failed to write to the buffer: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = et.Text.Execute(w, data); err != nil {
|
|
|
|
return fmt.Errorf("failed to execute template: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = w.Write(posixDoubleNewLine); err != nil {
|
|
|
|
return fmt.Errorf("failed to write to the buffer: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = w.Flush(); err != nil {
|
|
|
|
return fmt.Errorf("failed to flush buffer: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = f.Sync(); err != nil {
|
|
|
|
return fmt.Errorf("failed to sync the file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|