mondash-checkgpg/gpg.go

154 lines
3.8 KiB
Go
Raw Normal View History

2020-10-17 15:43:28 +00:00
package main
import (
"context"
"fmt"
"net/http"
"net/url"
"strings"
2020-10-17 15:43:28 +00:00
"time"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/armor"
"golang.org/x/crypto/openpgp/packet"
mondash "github.com/Luzifer/mondash/client"
)
func getKeyFromKeyserver(ctx context.Context, keyID string) (*openpgp.Entity, error) {
uri, err := url.Parse(cfg.KeyServer)
if err != nil {
return nil, errors.Wrap(err, "parse keyserver lookup url")
}
params := url.Values{
"op": []string{"get"},
"search": []string{keyID},
}
uri.RawQuery = params.Encode()
return getKeyFromURL(ctx, uri.String())
}
func getKeyFromURL(ctx context.Context, keyURL string) (*openpgp.Entity, error) {
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, keyURL, nil)
2020-10-17 15:43:28 +00:00
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, errors.Wrap(err, "execute http request")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errors.Errorf("http status %d", resp.StatusCode)
2020-10-17 15:43:28 +00:00
}
block, err := armor.Decode(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "parse armored key")
}
ent, err := openpgp.ReadEntity(packet.NewReader(block.Body))
if err != nil {
return nil, errors.Wrap(err, "parse entity")
}
return ent, nil
}
func processKey(ctx context.Context, key string) (keyID, message string, status mondash.Status) {
var (
e *openpgp.Entity
err error
logger = log.WithField("key", key)
)
2020-10-17 15:43:28 +00:00
switch {
case strings.HasPrefix(key, "0x"):
if e, err = getKeyFromKeyserver(ctx, key); err != nil {
return "", "Key retrieval failed", mondash.StatusUnknown
}
case strings.HasPrefix(key, "http"):
if e, err = getKeyFromURL(ctx, key); err != nil {
return "", "Key retrieval failed: " + err.Error(), mondash.StatusUnknown
}
default:
return "", "Unexpected key source", mondash.StatusUnknown
2020-10-17 15:43:28 +00:00
}
keyID = fmt.Sprintf("0x%016X", e.PrimaryKey.KeyId)
2020-10-17 15:43:28 +00:00
if l := len(e.Revocations); l > 0 {
return keyID, fmt.Sprintf("Key has %d revocation signature(s)", l), mondash.StatusCritical
2020-10-17 15:43:28 +00:00
}
var expiry *time.Time
for n, id := range e.Identities {
logger.Debugf("%s %#v", n, id)
var idSelfSigExpiry *time.Time
2020-10-17 15:43:28 +00:00
for _, sig := range id.Signatures {
if sig.KeyLifetimeSecs == nil || sig.IssuerKeyId != &e.PrimaryKey.KeyId {
2020-10-17 15:43:28 +00:00
continue
}
idSigExpiry := e.PrimaryKey.CreationTime.Add(time.Duration(*sig.KeyLifetimeSecs) * time.Second)
logger.WithField("id", n).Debugf("Sig: Identity expires: %s", idSigExpiry)
if idSelfSigExpiry == nil || idSigExpiry.After(*idSelfSigExpiry) {
idSelfSigExpiry = &idSigExpiry
2020-10-17 15:43:28 +00:00
}
}
2020-10-17 15:43:28 +00:00
if idSelfSigExpiry == nil {
continue
}
if s := checkExpiry(*idSelfSigExpiry); s != mondash.StatusOK {
return keyID, fmt.Sprintf("Identity signature for %q has key-expiry in %dh", n, time.Until(*idSelfSigExpiry)/time.Hour), s
}
if expiry == nil || expiry.After(*idSelfSigExpiry) {
expiry = idSelfSigExpiry
2020-10-17 15:43:28 +00:00
}
}
for _, sk := range e.Subkeys {
if sk.Sig.KeyLifetimeSecs == nil {
continue
}
skExp := sk.PublicKey.CreationTime.Add(time.Duration(*sk.Sig.KeyLifetimeSecs) * time.Second)
logger.Debugf("Subkey signature expires: %s", skExp)
if s := checkExpiry(skExp); s != mondash.StatusOK {
return keyID, fmt.Sprintf("Subkey signature has key-expiry in %dh", time.Until(skExp)/time.Hour), s
2020-10-17 15:43:28 +00:00
}
if expiry == nil || expiry.After(skExp) {
expiry = &skExp
}
}
if expiry != nil {
return keyID, fmt.Sprintf("Key looks good (expires in %dh)", time.Until(*expiry)/time.Hour), mondash.StatusOK
2020-10-17 15:43:28 +00:00
}
return keyID, "Key looks good (does not expire)", mondash.StatusOK
2020-10-17 15:43:28 +00:00
}
func checkExpiry(ex time.Time) mondash.Status {
switch {
case time.Until(ex) < cfg.CritAt:
return mondash.StatusCritical
case time.Until(ex) < cfg.WarnAt:
return mondash.StatusWarning
default:
return mondash.StatusOK
}
}