2022-09-28 09:01:17 +02:00
|
|
|
|
package event
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// AuditLog optionally logs security events.
|
|
|
|
|
var AuditLog Logger
|
|
|
|
|
var AuditPrefix = "audit: "
|
2022-10-10 16:34:07 +02:00
|
|
|
|
var AuditMessageSep = " › "
|
2022-09-28 09:01:17 +02:00
|
|
|
|
|
|
|
|
|
// Format formats an audit log event.
|
|
|
|
|
func Format(ev []string, args ...interface{}) string {
|
2022-10-10 16:34:07 +02:00
|
|
|
|
return fmt.Sprintf(strings.Join(ev, AuditMessageSep), args...)
|
2022-09-28 09:01:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Audit optionally reports security-relevant events.
|
|
|
|
|
func Audit(level logrus.Level, ev []string, args ...interface{}) {
|
2022-10-10 16:34:07 +02:00
|
|
|
|
// Skip if empty.
|
|
|
|
|
if len(ev) == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Format log message.
|
|
|
|
|
message := Format(ev, args...)
|
|
|
|
|
|
|
|
|
|
// Show log message if AuditLog is specified.
|
|
|
|
|
if AuditLog != nil {
|
|
|
|
|
AuditLog.Log(level, AuditPrefix+message)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Publish event if log level is info or higher.
|
|
|
|
|
if level <= logrus.InfoLevel {
|
|
|
|
|
Publish(
|
|
|
|
|
"audit."+level.String(),
|
|
|
|
|
Data{
|
|
|
|
|
"time": TimeStamp(),
|
|
|
|
|
"level": level.String(),
|
|
|
|
|
"message": message,
|
|
|
|
|
},
|
|
|
|
|
)
|
2022-09-28 09:01:17 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func AuditErr(ev []string, args ...interface{}) {
|
|
|
|
|
Audit(logrus.ErrorLevel, ev, args...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func AuditWarn(ev []string, args ...interface{}) {
|
|
|
|
|
Audit(logrus.WarnLevel, ev, args...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func AuditInfo(ev []string, args ...interface{}) {
|
|
|
|
|
Audit(logrus.InfoLevel, ev, args...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func AuditDebug(ev []string, args ...interface{}) {
|
|
|
|
|
Audit(logrus.DebugLevel, ev, args...)
|
|
|
|
|
}
|