Browse Source

Initial commit

master
Teran McKinney 2 years ago
commit
934d8cb097
  1. 25
      host_check.go
  2. 21
      host_check_statsd.go
  3. 15
      host_check_test.go
  4. 52
      main.go
  5. 28
      test.sh

25
host_check.go

@ -0,0 +1,25 @@
package main
import (
"crypto/tls"
"time"
)
func hostCheck(host string) (secondsTillExpiry int, err error) {
conn, err := tls.Dial("tcp", host, nil)
if err != nil {
return
}
// secondsTillExpiry gets initalized to 0.
var seconds int
for _, chain := range conn.ConnectionState().VerifiedChains {
for _, certificate := range chain {
seconds = int(certificate.NotAfter.Sub(time.Now()).Seconds())
if secondsTillExpiry == 0 || secondsTillExpiry > seconds {
secondsTillExpiry = seconds
}
}
}
return
}

21
host_check_statsd.go

@ -0,0 +1,21 @@
package main
import (
"gopkg.in/alexcesaro/statsd.v2"
)
func hostCheckStatsd(host string) (secondsTillExpiry int, err error) {
s, err := statsd.New(statsd.Prefix("sslexpiry.hostCheck"))
if err != nil {
// Since we are calling for the sake of statsd, this should fail.
return
}
defer s.Close()
defer s.NewTiming().Send("timetocheck." + host)
secondsTillExpiry, err = hostCheck(host)
if err != nil {
return
}
s.Gauge("expiry."+host, secondsTillExpiry)
return
}

15
host_check_test.go

@ -0,0 +1,15 @@
package main
import (
"log"
"testing"
)
func TestHostCheck(t *testing.T) {
seconds, err := hostCheck("go-beyond.org:443")
if err != nil {
t.Errorf("Got error when we should not have: %s", err.Error())
} else {
log.Printf("Seconds left: %d", seconds)
}
}

52
main.go

@ -0,0 +1,52 @@
package main
import (
"fmt"
"os"
)
func print_list(list []string) {
for _, item := range list {
fmt.Println(item)
}
}
func usage() {
fmt.Fprintln(os.Stderr, "Usage: sslexpirystatsd <command> [argument]")
fmt.Fprintln(os.Stderr, "Command: check <host:port> (Example: github.com:443)")
fmt.Fprintln(os.Stderr, "Command: checkstatsd <host:port> (Example: github.com:443)")
os.Exit(2)
}
func fatal_error(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func exactly_arguments(arguments int) {
if len(os.Args) != arguments {
usage()
}
}
func main() {
if len(os.Args) <= 1 {
usage()
}
switch os.Args[1] {
case "check":
exactly_arguments(3)
seconds, err := hostCheck(os.Args[2])
fatal_error(err)
fmt.Printf("%s expires in %d seconds.\n", os.Args[2], seconds)
case "checkstatsd":
exactly_arguments(3)
seconds, err := hostCheckStatsd(os.Args[2])
fatal_error(err)
fmt.Printf("%s expires in %d seconds.\n", os.Args[2], seconds)
default:
usage()
}
}

28
test.sh

@ -0,0 +1,28 @@
#!/usr/bin/env bash
set -eE
shellcheck "$0"
# Before we build...
go fmt
go doc
go test
go build
# strip -s decensor
cleanup() {
echo "Cleaning up."
}
trap fail $(seq 1 64)
fail() {
echo "FAIL: $1"
cleanup
exit 1
}
echo Success
Loading…
Cancel
Save