2019-04-24 21:52:08 +00:00
|
|
|
package notification
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
[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"
|
|
|
|
|
2019-12-24 02:14:52 +00:00
|
|
|
"github.com/authelia/authelia/internal/configuration/schema"
|
2019-04-24 21:52:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// FileNotifier a notifier to send emails to SMTP servers.
|
|
|
|
type FileNotifier struct {
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFileNotifier create an FileNotifier writing the notification into a file.
|
|
|
|
func NewFileNotifier(configuration schema.FileSystemNotifierConfiguration) *FileNotifier {
|
|
|
|
return &FileNotifier{
|
|
|
|
path: configuration.Filename,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[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
|
|
|
// StartupCheck checks the file provider can write to the specified file
|
|
|
|
func (n *FileNotifier) StartupCheck() (ok bool, err error) {
|
|
|
|
ok = true
|
|
|
|
dir := filepath.Dir(n.path)
|
|
|
|
if _, err = os.Stat(dir); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(dir, fileNotifierMode); err != nil {
|
|
|
|
ok = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = ioutil.WriteFile(n.path, []byte(""), fileNotifierMode); err != nil {
|
|
|
|
ok = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ok = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else if _, err = os.Stat(n.path); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
if err = ioutil.WriteFile(n.path, []byte(""), fileNotifierMode); err != nil {
|
|
|
|
ok = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ok = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
// Send send a identity verification link to a user.
|
2019-12-30 02:03:51 +00:00
|
|
|
func (n *FileNotifier) Send(recipient, subject, body string) error {
|
2019-04-24 21:52:08 +00:00
|
|
|
content := fmt.Sprintf("Date: %s\nRecipient: %s\nSubject: %s\nBody: %s", time.Now(), recipient, subject, body)
|
|
|
|
|
[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
|
|
|
err := ioutil.WriteFile(n.path, []byte(content), fileNotifierMode)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|