Use same code to produce $SUMMARY and email subject

This commit is contained in:
Andrew Ayer 2023-02-19 08:48:30 -05:00
parent 152f4341d6
commit a242f6be26
4 changed files with 17 additions and 17 deletions

View File

@ -83,7 +83,7 @@ func (cert *discoveredCert) save() error {
func (cert *discoveredCert) Environ() []string { func (cert *discoveredCert) Environ() []string {
env := []string{ env := []string{
"EVENT=discovered_cert", "EVENT=discovered_cert",
"SUMMARY=certificate discovered for " + cert.WatchItem.String(), "SUMMARY=" + cert.Summary(),
"CERT_PARSEABLE=yes", // backwards compat with pre-0.15.0; not documented "CERT_PARSEABLE=yes", // backwards compat with pre-0.15.0; not documented
"LOG_URI=" + cert.LogEntry.Log.URL, "LOG_URI=" + cert.LogEntry.Log.URL,
"ENTRY_INDEX=" + fmt.Sprint(cert.LogEntry.Index), "ENTRY_INDEX=" + fmt.Sprint(cert.LogEntry.Index),
@ -165,6 +165,6 @@ func (cert *discoveredCert) Text() string {
return text.String() return text.String()
} }
func (cert *discoveredCert) EmailSubject() string { func (cert *discoveredCert) Summary() string {
return fmt.Sprintf("[certspotter] Certificate Discovered for %s", cert.WatchItem) return fmt.Sprintf("Certificate Discovered for %s", cert.WatchItem)
} }

View File

@ -107,33 +107,33 @@ func (e *backlogEvent) Backlog() uint64 {
func (e *staleSTHEvent) Environ() []string { func (e *staleSTHEvent) Environ() []string {
return []string{ return []string{
"EVENT=error", "EVENT=error",
"SUMMARY=" + e.Summary(),
"TEXT_FILENAME=" + e.TextPath, "TEXT_FILENAME=" + e.TextPath,
"SUMMARY=" + fmt.Sprintf("unable to contact %s since %s", e.Log.URL, e.LastSuccess),
} }
} }
func (e *backlogEvent) Environ() []string { func (e *backlogEvent) Environ() []string {
return []string{ return []string{
"EVENT=error", "EVENT=error",
"SUMMARY=" + e.Summary(),
"TEXT_FILENAME=" + e.TextPath, "TEXT_FILENAME=" + e.TextPath,
"SUMMARY=" + fmt.Sprintf("backlog of size %d from %s", e.Backlog(), e.Log.URL),
} }
} }
func (e *staleLogListEvent) Environ() []string { func (e *staleLogListEvent) Environ() []string {
return []string{ return []string{
"EVENT=error", "EVENT=error",
"SUMMARY=" + e.Summary(),
"TEXT_FILENAME=" + e.TextPath, "TEXT_FILENAME=" + e.TextPath,
"SUMMARY=" + fmt.Sprintf("unable to retrieve log list since %s: %s", e.LastSuccess, e.LastError),
} }
} }
func (e *staleSTHEvent) EmailSubject() string { func (e *staleSTHEvent) Summary() string {
return fmt.Sprintf("[certspotter] Unable to contact %s since %s", e.Log.URL, e.LastSuccess) return fmt.Sprintf("Unable to contact %s since %s", e.Log.URL, e.LastSuccess)
} }
func (e *backlogEvent) EmailSubject() string { func (e *backlogEvent) Summary() string {
return fmt.Sprintf("[certspotter] Backlog of size %d from %s", e.Backlog(), e.Log.URL) return fmt.Sprintf("Backlog of size %d from %s", e.Backlog(), e.Log.URL)
} }
func (e *staleLogListEvent) EmailSubject() string { func (e *staleLogListEvent) Summary() string {
return fmt.Sprintf("[certspotter] Unable to retrieve log list since %s", e.LastSuccess) return fmt.Sprintf("Unable to retrieve log list since %s", e.LastSuccess)
} }
func (e *staleSTHEvent) Text() string { func (e *staleSTHEvent) Text() string {

View File

@ -44,7 +44,7 @@ func (malformed *malformedLogEntry) save() error {
func (malformed *malformedLogEntry) Environ() []string { func (malformed *malformedLogEntry) Environ() []string {
return []string{ return []string{
"EVENT=malformed_cert", "EVENT=malformed_cert",
"SUMMARY=" + fmt.Sprintf("unable to parse entry %d in %s", malformed.Entry.Index, malformed.Entry.Log.URL), "SUMMARY=" + malformed.Summary(),
"LOG_URI=" + malformed.Entry.Log.URL, "LOG_URI=" + malformed.Entry.Log.URL,
"ENTRY_INDEX=" + fmt.Sprint(malformed.Entry.Index), "ENTRY_INDEX=" + fmt.Sprint(malformed.Entry.Index),
"LEAF_HASH=" + malformed.Entry.LeafHash.Base64String(), "LEAF_HASH=" + malformed.Entry.LeafHash.Base64String(),
@ -67,6 +67,6 @@ func (malformed *malformedLogEntry) Text() string {
return text.String() return text.String()
} }
func (malformed *malformedLogEntry) EmailSubject() string { func (malformed *malformedLogEntry) Summary() string {
return fmt.Sprintf("[certspotter] Unable to Parse Entry %d in %s", malformed.Entry.Index, malformed.Entry.Log.URL) return fmt.Sprintf("Unable to Parse Entry %d in %s", malformed.Entry.Index, malformed.Entry.Log.URL)
} }

View File

@ -23,7 +23,7 @@ var stdoutMu sync.Mutex
type notification interface { type notification interface {
Environ() []string Environ() []string
EmailSubject() string Summary() string
Text() string Text() string
} }
@ -58,7 +58,7 @@ func sendEmail(ctx context.Context, to []string, notif notification) error {
stderr := new(bytes.Buffer) stderr := new(bytes.Buffer)
fmt.Fprintf(stdin, "To: %s\n", strings.Join(to, ", ")) fmt.Fprintf(stdin, "To: %s\n", strings.Join(to, ", "))
fmt.Fprintf(stdin, "Subject: %s\n", notif.EmailSubject()) fmt.Fprintf(stdin, "Subject: [certspotter] %s\n", notif.Summary())
fmt.Fprintf(stdin, "Mime-Version: 1.0\n") fmt.Fprintf(stdin, "Mime-Version: 1.0\n")
fmt.Fprintf(stdin, "Content-Type: text/plain; charset=US-ASCII\n") fmt.Fprintf(stdin, "Content-Type: text/plain; charset=US-ASCII\n")
fmt.Fprintf(stdin, "X-Mailer: certspotter\n") fmt.Fprintf(stdin, "X-Mailer: certspotter\n")