Skip to content

Instantly share code, notes, and snippets.

@gsquire
Last active December 22, 2016 03:59
Show Gist options
  • Save gsquire/82372f9be7282e646feb3e1592720e1b to your computer and use it in GitHub Desktop.
Save gsquire/82372f9be7282e646feb3e1592720e1b to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"math"
"net/http"
"os"
"os/exec"
"time"
)
const (
hoursInDay = 24
shouldUpdateDays = 1
)
func main() {
if len(os.Args) != 2 {
log.Fatal("must supply an HTTPS URL...")
}
resp, err := http.Get(os.Args[1])
if err != nil {
log.Fatalf("error performing HTTPS request: %s\n", err)
}
if resp.TLS == nil {
log.Fatal("did not supply an HTTPS URL...")
}
for _, v := range resp.TLS.PeerCertificates {
// A client certificate is not a certificate authority, so check only those.
if !v.IsCA {
duration := time.Since(v.NotAfter)
expiresInDays := math.Abs(duration.Hours() / hoursInDay)
if int(expiresInDays) == shouldUpdateDays {
out, err := exec.Command("certbot", "renew").Output()
if err != nil {
log.Fatalf("error running certificate renew: %s\n", err)
}
fmt.Println(string(out))
} else {
fmt.Println("no update needed...")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment