Skip to content

Commit

Permalink
Fix cert parsing issue, additional logging
Browse files Browse the repository at this point in the history
  • Loading branch information
nateinaction committed Jul 13, 2024
1 parent 6212226 commit 9cef22e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.vscode
bin/
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ fmt:
-v ~/.cache/golangci-lint/$(GOLANGCI_LINT_VERSION):/root/.cache \
-w /app \
golangci/golangci-lint:$(GOLANGCI_LINT_VERSION) golangci-lint run --fix

.PHONY: build
build:
GOOS=linux GOARCH=arm CGO_ENABLED=0 go build -o bin/ ./...
20 changes: 13 additions & 7 deletions internal/certmanager/certmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package certmanager
import (
"context"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"os"
Expand All @@ -25,11 +26,11 @@ func NewCertManager(ssl *sslpaths.SSLPaths) *CertManager {
const (
certDirPerms = 0o755
certFilePerms = 0o644
closeToExpire = -7 * 24 * time.Hour
week = -7 * 24 * time.Hour
)

var (
ErrExpiringSoon = errors.New("connection error")
ErrExpiringSoon = errors.New("cert is expiring soon")
ErrDoesNotExist = errors.New("cert does not exist")
)

Expand All @@ -49,14 +50,19 @@ func (c *CertManager) CheckCert() error {
return fmt.Errorf("failed to read cert file: %w", err)
}

cert, err := x509.ParseCertificate(b)
pBlock, _ := pem.Decode(b)
if pBlock == nil {
return errors.New("failed to decode cert file")
}

cert, err := x509.ParseCertificate(pBlock.Bytes)
if err != nil {
return fmt.Errorf("failed to parse cert: %w", err)
return fmt.Errorf("failed to parse cert block: %w", err)
}

renewIfAfter := time.Now().Add(closeToExpire)
if cert.NotAfter.After(renewIfAfter) {
return ErrExpiringSoon
remainingTime := time.Until(cert.NotAfter)
if remainingTime < week {
return fmt.Errorf("cert expriring in %s: %w", remainingTime.String(), ErrExpiringSoon)
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion internal/tailscale/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tailscale
import (
"context"
"fmt"
"strings"

ts "tailscale.com/client/tailscale"
)
Expand All @@ -16,7 +17,7 @@ func GetDomain(ctx context.Context) (string, error) {
return "", fmt.Errorf("failed to get status: %w", err)
}

return statusResp.Self.DNSName, nil
return strings.TrimSuffix(statusResp.Self.DNSName, "."), nil
}

// CertPair generates the cert pair for the given domain
Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func main() {

if err := certManager.CheckCert(); err != nil {
if errors.Is(err, certmanager.ErrDoesNotExist) || errors.Is(err, certmanager.ErrExpiringSoon) {
slog.Info("cert is missing or expiring soon, generating new cert", "reason", err)
slog.Warn("cert is missing or expiring soon, generating new cert", "reason", err)

if err := pikvm.SetFSReadWrite(); err != nil {
slog.Error("failed filesystem mode change", "error", err)
Expand All @@ -39,10 +39,11 @@ func main() {

genCert(ctx, certManager)
} else {
slog.Error("failed to check cert", "error", err)
slog.Error("failed to check cert", "error", err, "cert_path", ssl.GetCertPath())
}
}

slog.Info("sleeping", "duration", timeToSleep)
time.Sleep(timeToSleep)
}
}
Expand Down

0 comments on commit 9cef22e

Please sign in to comment.