-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added warnings and better notifications (#15)
* Added warnings and better notifications * Better docs * Updated demo pic * better docs
- Loading branch information
1 parent
f3b21b8
commit 9f0dd48
Showing
15 changed files
with
332 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package jobs | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/adhocore/gronx" | ||
"github.com/jlucaspains/sharp-cert-manager/models" | ||
"github.com/jlucaspains/sharp-cert-manager/shared" | ||
) | ||
|
||
type Notifier interface { | ||
Notify(result []CertCheckNotification) error | ||
} | ||
|
||
type CheckCertJob struct { | ||
cron string | ||
ticker *time.Ticker | ||
gron gronx.Gronx | ||
siteList []string | ||
running bool | ||
notifier Notifier | ||
level Level | ||
warningDays int | ||
} | ||
|
||
type Level int | ||
|
||
const ( | ||
Info Level = iota | ||
Warning | ||
Error | ||
) | ||
|
||
var levels = map[string]Level{ | ||
"Info": Info, | ||
"Warning": Warning, | ||
"Error": Error, | ||
} | ||
|
||
type CertCheckNotification struct { | ||
Hostname string | ||
IsValid bool | ||
Messages []string | ||
ExpirationWarning bool | ||
} | ||
|
||
func (c *CheckCertJob) Init(schedule string, level string, warningDays int, siteList []string, notifier Notifier) error { | ||
c.gron = gronx.New() | ||
|
||
if schedule == "" || !c.gron.IsValid(schedule) { | ||
log.Printf("A valid cron schedule is required in the format e.g.: * * * * *") | ||
return fmt.Errorf("a valid cron schedule is required") | ||
} | ||
|
||
if notifier == nil { | ||
log.Printf("A valid notifier is required") | ||
return fmt.Errorf("a valid notifier is required") | ||
} | ||
|
||
levelValue, ok := levels[level] | ||
if !ok { | ||
levelValue = Warning | ||
} | ||
|
||
if warningDays <= 0 { | ||
warningDays = 30 | ||
} | ||
|
||
c.cron = schedule | ||
c.siteList = siteList | ||
c.ticker = time.NewTicker(time.Minute) | ||
c.notifier = notifier | ||
c.level = levelValue | ||
c.warningDays = warningDays | ||
|
||
return nil | ||
} | ||
|
||
func (c *CheckCertJob) Start() { | ||
c.running = true | ||
go func() { | ||
for range c.ticker.C { | ||
c.tryExecute() | ||
} | ||
}() | ||
} | ||
|
||
func (c *CheckCertJob) Stop() { | ||
c.running = false | ||
c.ticker.Stop() | ||
} | ||
|
||
func (c *CheckCertJob) tryExecute() { | ||
due, _ := c.gron.IsDue(c.cron, time.Now().Truncate(time.Minute)) | ||
|
||
log.Printf("tryExecute job, isDue: %t", due) | ||
|
||
if due { | ||
c.execute() | ||
} | ||
} | ||
|
||
func (c *CheckCertJob) execute() { | ||
result := []CertCheckNotification{} | ||
for _, url := range c.siteList { | ||
params := models.CertCheckParams{Url: url} | ||
checkStatus, err := shared.CheckCertStatus(params, c.warningDays) | ||
|
||
if err != nil { | ||
log.Printf("Error checking cert status: %s", err) | ||
continue | ||
} | ||
|
||
log.Printf("Cert status for %s: %t", url, checkStatus.IsValid) | ||
|
||
item := c.getNotificationModel(checkStatus) | ||
if c.shouldNotify(item) { | ||
result = append(result, item) | ||
} | ||
} | ||
|
||
c.notifier.Notify(result) | ||
} | ||
|
||
func (c *CheckCertJob) shouldNotify(model CertCheckNotification) bool { | ||
return c.level == Info || !model.IsValid || (c.level == Warning && model.ExpirationWarning) | ||
} | ||
|
||
func (c *CheckCertJob) getNotificationModel(certificate models.CertCheckResult) CertCheckNotification { | ||
result := CertCheckNotification{ | ||
Hostname: certificate.Hostname, | ||
IsValid: certificate.IsValid, | ||
ExpirationWarning: certificate.ExpirationWarning, | ||
Messages: certificate.ValidationIssues, | ||
} | ||
|
||
if certificate.IsValid && result.ExpirationWarning { | ||
// Calculate CertEndDate days from today | ||
days := int(time.Until(certificate.CertEndDate).Hours() / 24) | ||
result.Messages = append(result.Messages, fmt.Sprintf("Certificate expires in %d days", days)) | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.