Files
mtusi2026lab7/infra/tls/gateway_test.go
T

123 lines
4.0 KiB
Go

package main
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/json"
"encoding/pem"
"math/big"
"net/url"
"os"
"path/filepath"
"testing"
"time"
)
type authority struct {
cert *x509.Certificate
key *ecdsa.PrivateKey
pem []byte
}
func root(t *testing.T) authority {
t.Helper()
key, e := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if e != nil {
t.Fatal(e)
}
template := &x509.Certificate{SerialNumber: big.NewInt(time.Now().UnixNano()), Subject: pkix.Name{CommonName: "Synthetic test CA"}, NotBefore: time.Now().Add(-time.Hour), NotAfter: time.Now().Add(time.Hour), IsCA: true, BasicConstraintsValid: true, KeyUsage: x509.KeyUsageCertSign}
der, e := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key)
if e != nil {
t.Fatal(e)
}
cert, e := x509.ParseCertificate(der)
if e != nil {
t.Fatal(e)
}
return authority{cert, key, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der})}
}
func client(t *testing.T, ca authority, role string, expired bool) *x509.Certificate {
t.Helper()
key, e := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if e != nil {
t.Fatal(e)
}
identity, _ := url.Parse("spiffe://messenger/" + role)
expiry := time.Now().Add(time.Minute)
if expired {
expiry = time.Now().Add(-time.Minute)
}
template := &x509.Certificate{SerialNumber: big.NewInt(time.Now().UnixNano()), Subject: pkix.Name{CommonName: role}, NotBefore: time.Now().Add(-time.Hour), NotAfter: expiry, URIs: []*url.URL{identity}, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}, KeyUsage: x509.KeyUsageDigitalSignature}
der, e := x509.CreateCertificate(rand.Reader, template, ca.cert, &key.PublicKey, ca.key)
if e != nil {
t.Fatal(e)
}
cert, e := x509.ParseCertificate(der)
if e != nil {
t.Fatal(e)
}
return cert
}
func TestClientAuthorizationUsesCurrentChainIdentityExpiryAndRevocation(t *testing.T) {
ca := root(t)
foreign := root(t)
directory := t.TempDir()
conf := Config{Trust: filepath.Join(directory, "ca.pem"), Revoked: filepath.Join(directory, "revoked.json")}
if e := os.WriteFile(conf.Trust, ca.pem, 0600); e != nil {
t.Fatal(e)
}
if e := os.WriteFile(conf.Revoked, []byte(`{}`), 0600); e != nil {
t.Fatal(e)
}
boundary := Boundary{Allow: map[string][]string{"spiffe://messenger/lb": {"* /"}}}
good := client(t, ca, "lb", false)
state := func(leaf *x509.Certificate, issuer authority) tls.ConnectionState {
return tls.ConnectionState{PeerCertificates: []*x509.Certificate{leaf, issuer.cert}, VerifiedChains: [][]*x509.Certificate{{leaf, issuer.cert}}}
}
cases := []struct {
name string
state tls.ConnectionState
allowed bool
}{
{"authorized", state(good, ca), true},
{"no_certificate", tls.ConnectionState{}, false},
{"trusted_but_wrong_role", state(client(t, ca, "worker", false), ca), false},
{"foreign_authority", state(client(t, foreign, "lb", false), foreign), false},
{"expired", state(client(t, ca, "lb", true), ca), false},
}
for _, test := range cases {
t.Run(test.name, func(t *testing.T) {
_, e := peer(conf, test.state, boundary)
if (e == nil) != test.allowed {
t.Fatalf("authorization mismatch: allowed=%v error=%v", test.allowed, e)
}
})
}
raw, _ := json.Marshal(map[string]bool{good.SerialNumber.Text(16): true})
if e := os.WriteFile(conf.Revoked, raw, 0600); e != nil {
t.Fatal(e)
}
if _, e := peer(conf, state(good, ca), boundary); e == nil {
t.Fatal("revoked client accepted on previously authenticated connection")
}
if e := os.WriteFile(conf.Revoked, []byte(`{}`), 0600); e != nil {
t.Fatal(e)
}
if e := os.WriteFile(conf.Trust, foreign.pem, 0600); e != nil {
t.Fatal(e)
}
if _, e := peer(conf, state(good, ca), boundary); e == nil {
t.Fatal("retired authority accepted from cached verified chain")
}
if e := os.Remove(conf.Revoked); e != nil {
t.Fatal(e)
}
if _, e := peer(conf, state(good, ca), boundary); e == nil {
t.Fatal("missing revocation policy did not fail closed")
}
}