authz.go84 lines, 2.9 KB
| 1 | package mtls |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/tls" |
| 6 | "fmt" |
| 7 | "log/slog" |
| 8 | "net/http" |
| 9 | "slices" |
| 10 | "strings" |
| 11 | |
| 12 | "github.com/fiston-user/payments/internal/platform/apperr" |
| 13 | "github.com/fiston-user/payments/internal/platform/httpx" |
| 14 | "github.com/fiston-user/payments/internal/platform/logging" |
| 15 | "google.golang.org/grpc" |
| 16 | "google.golang.org/grpc/credentials" |
| 17 | "google.golang.org/grpc/peer" |
| 18 | ) |
| 19 | |
| 20 | // Peer returns the name of the service at the other end of a connection: the |
| 21 | // one name in the certificate it showed, which our CA vouched for. It's "" |
| 22 | // when there's no verified certificate. |
| 23 | func Peer(cs *tls.ConnectionState) string { |
| 24 | if cs == nil || len(cs.VerifiedChains) == 0 || len(cs.VerifiedChains[0][0].DNSNames) != 1 { |
| 25 | return "" |
| 26 | } |
| 27 | return cs.VerifiedChains[0][0].DNSNames[0] |
| 28 | } |
| 29 | |
| 30 | type peerKey struct{} |
| 31 | |
| 32 | // withPeer returns a copy of ctx that carries the calling service's name, for |
| 33 | // PeerFrom, and for every line logged with it. |
| 34 | func withPeer(ctx context.Context, name string) context.Context { |
| 35 | ctx = context.WithValue(ctx, peerKey{}, name) |
| 36 | return logging.With(ctx, slog.String("peer", name)) |
| 37 | } |
| 38 | |
| 39 | // PeerFrom returns the name of the service that made the request, as Allow or |
| 40 | // Authorize stored it in ctx. |
| 41 | func PeerFrom(ctx context.Context) string { |
| 42 | name, _ := ctx.Value(peerKey{}).(string) |
| 43 | return name |
| 44 | } |
| 45 | |
| 46 | // Allow is middleware that only lets the named services through. Any other |
| 47 | // caller gets a 403, even with a certificate from our CA, and so does a |
| 48 | // request that didn't come over mTLS at all. |
| 49 | func Allow(names ...string) httpx.Middleware { |
| 50 | return func(next http.Handler) http.Handler { |
| 51 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 52 | name := Peer(r.TLS) |
| 53 | if !slices.Contains(names, name) { |
| 54 | // Misconfigured, or someone probing: either way, worth an alert. |
| 55 | slog.WarnContext(r.Context(), "caller refused", "peer", name, "path", r.URL.Path) |
| 56 | httpx.Error(w, r, fmt.Errorf("%w: %q may not call this", apperr.ErrForbidden, name)) |
| 57 | return |
| 58 | } |
| 59 | next.ServeHTTP(w, r.WithContext(withPeer(r.Context(), name))) |
| 60 | }) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // Authorize is Allow, for gRPC. callers lists who may call each service, by |
| 65 | // its full name, like wallet.v1.HoldService: a service that isn't listed |
| 66 | // can't be called at all. |
| 67 | func Authorize(callers map[string][]string) grpc.UnaryServerInterceptor { |
| 68 | return func( |
| 69 | ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler, |
| 70 | ) (any, error) { |
| 71 | var name string |
| 72 | if p, ok := peer.FromContext(ctx); ok { |
| 73 | if t, ok := p.AuthInfo.(credentials.TLSInfo); ok { |
| 74 | name = Peer(&t.State) |
| 75 | } |
| 76 | } |
| 77 | service, _, _ := strings.Cut(strings.TrimPrefix(info.FullMethod, "/"), "/") |
| 78 | if !slices.Contains(callers[service], name) { |
| 79 | slog.WarnContext(ctx, "caller refused", "peer", name, "method", info.FullMethod) |
| 80 | return nil, fmt.Errorf("%w: %q may not call %s", apperr.ErrForbidden, name, service) |
| 81 | } |
| 82 | return handler(withPeer(ctx, name), req) |
| 83 | } |
| 84 | } |