CI / Lint, test and scan (push) Successful in 1m41s
Restructure the daemon to follow the project's Go coding standards and
expand its features.
Layout and tooling
- cmd/uberbringer/main.go is now thin; all logic lives under internal/.
- Module path is git.anomalous.dev/57_Wolve/uberbringer, go 1.26, every
dependency upgraded, vendor/ removed.
- staticcheck, govulncheck and goimports pinned as go tools; add a
Makefile, CI workflow, goreleaser v2 config and Alpine Dockerfile.
Features
- Background print queue with job status (GET /api/v1/jobs/:id), send
retries with backoff, and graceful shutdown.
- New {"elements": [...]} job format on POST /api/v1/print. Breaking:
replaces {"data": [...]}, and the response is now JSON 202.
- In-house ESC/POS library replacing justinmichaelvieira/escpos: sizes
1-8, Font B, CODE39/CODE128, QR, banded raster images, DLE EOT status.
- UTF-8 text transcoded to CP437 (configurable) with word-aware wrapping.
- Images from URL or base64, scaled and dithered; URL fetches refuse
internal addresses (SSRF guard).
- Fiber v3 HTTP server, TypeID (UUIDv7) job and request IDs, /healthz
with live printer status, `secret` and `token` subcommands.
Fixes
- Jobs are validated before anything is sent; a failed printer dial no
longer panics.
- Request logs never contain tokens; Sentry no longer receives the
Authorization header or print contents.
- Rate limiting is per client behind nginx (X-Real-IP).
- install.sh writes the [printer] section, runs the service as a
dedicated user and uses a current nginx TLS config.
config.toml is no longer tracked; start from configs/config.example.toml.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
105 lines
2.7 KiB
Go
105 lines
2.7 KiB
Go
package uberbringer
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/base64"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
)
|
|
|
|
func runCommand(t *testing.T, args ...string) (string, error) {
|
|
t.Helper()
|
|
cmd := newRootCommand(BuildInfo{Version: "1.2.3", Commit: "abc123"})
|
|
var out bytes.Buffer
|
|
cmd.SetOut(&out)
|
|
cmd.SetArgs(args)
|
|
err := cmd.Execute()
|
|
return out.String(), err
|
|
}
|
|
|
|
func TestVersionCommand(t *testing.T) {
|
|
out, err := runCommand(t, "version")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(out, "Überbringer | Version: 1.2.3 Build: abc123") {
|
|
t.Errorf("version output = %q", out)
|
|
}
|
|
}
|
|
|
|
func TestSecretCommand(t *testing.T) {
|
|
out, err := runCommand(t, "secret")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
raw, err := base64.StdEncoding.DecodeString(strings.TrimSpace(out))
|
|
if err != nil || len(raw) != secretBytes {
|
|
t.Errorf("secret %q decodes to %d bytes, %v", out, len(raw), err)
|
|
}
|
|
again, _ := runCommand(t, "secret")
|
|
if again == out {
|
|
t.Error("two secrets were identical")
|
|
}
|
|
}
|
|
|
|
func TestTokenCommand(t *testing.T) {
|
|
secret, _ := runCommand(t, "secret")
|
|
secret = strings.TrimSpace(secret)
|
|
path := filepath.Join(t.TempDir(), "config.toml")
|
|
body := "[api]\nsecret = \"" + secret + "\"\n[printer]\naddress = \"192.168.1.245\"\n"
|
|
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
out, err := runCommand(t, "token", "-c", path, "--subject", "kitchen", "--ttl", "1h")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
claims := &jwt.RegisteredClaims{}
|
|
_, err = jwt.ParseWithClaims(strings.TrimSpace(out), claims, func(*jwt.Token) (any, error) {
|
|
return []byte(secret), nil
|
|
}, jwt.WithValidMethods([]string{"HS256"}))
|
|
if err != nil {
|
|
t.Fatalf("token does not verify: %v", err)
|
|
}
|
|
if claims.Subject != "kitchen" || claims.ExpiresAt == nil {
|
|
t.Errorf("claims = %+v", claims)
|
|
}
|
|
if left := time.Until(claims.ExpiresAt.Time); left < 59*time.Minute || left > time.Hour {
|
|
t.Errorf("expires in %v, want about 1h", left)
|
|
}
|
|
}
|
|
|
|
func TestTokenCommandNoExpiryByDefault(t *testing.T) {
|
|
token, err := signToken([]byte("0123456789abcdef0123456789abcdef"), "x", 0, time.Now())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
claims := &jwt.RegisteredClaims{}
|
|
if _, _, err := jwt.NewParser().ParseUnverified(token, claims); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if claims.ExpiresAt != nil {
|
|
t.Errorf("ExpiresAt = %v, want none", claims.ExpiresAt)
|
|
}
|
|
}
|
|
|
|
func TestServeRejectsBadConfig(t *testing.T) {
|
|
_, err := runCommand(t, "-c", filepath.Join(t.TempDir(), "missing.toml"))
|
|
if err == nil {
|
|
t.Fatal("serve with a missing config must fail")
|
|
}
|
|
}
|
|
|
|
func TestNewRequestID(t *testing.T) {
|
|
id := newRequestID()
|
|
if !strings.HasPrefix(id, "req_") || len(id) != len("req_")+26 {
|
|
t.Errorf("request ID %q is not a req_ TypeID", id)
|
|
}
|
|
}
|