Ch.10: gRPC Security: TLS, mTLS, and Auth Tokens

Outline

Transcript

0:00 Imagine a made-up team with a Python orders service that calls a payments service over gRPC. Every call carries an auth token. Tests pass, and production's been fine for months. Okay, so where's the problem? Honestly, that sounds like a team doing it right. Two lines nobody looked at twice. The channel is the insecure one somebody copied from local development. And a little client interceptor, the middleware that wraps every call, stamps the token onto it. Oh, no. So the token crosses the network in plain text.

0:31 And if it's a bearer token, whoever copies it can use it. Copy it once, and you're the orders service. And I can already hear the reviewer's answer. It's internal traffic, it never leaves our network. Sure, and that's the trap. Nothing errors, no alert fires, but one compromised box on that network, or a forgotten packet capture, and that token belongs to someone else. So here's the fix: lock the connection, prove both ends, attach the token so gRPC itself can refuse to send it in the clear, and check it on the server.

1:04 So that fix has four parts. Where do you even start? Security feels like one giant topic. The official auth guide has two kinds of credentials, and they answer two questions. First, am I talking to the right server, and can anyone else read this? Second, who is making this particular call? That guide, and every other source we cite, is linked in the description. And I'm guessing those live at different layers. The first one's about the connection, and the second one's about each request. Exactly. The guide calls the first one channel credentials.

1:39 They're attached to the channel, the long-lived connection, and the usual one is TLS. The second is call credentials, attached to each call, and that's usually a token. So a lock on the pipe, and a badge on every request going through it. Oh, I like that. And I think teams get burned assuming the lock or the badge covers both. Take a request from orders. Plain TLS alone doesn't say which caller sent it, and a token alone travels naked. You need both. Then the lock comes first, since anyone watching an unlocked pipe can just copy the badge. So, the lock. TLS is the protocol behind the S in HTTPS.

2:18 It encrypts the connection, and it lets the client confirm the server is who it says it is. Right, and the proof is a certificate. Imagine payments holding a signed document that says, I'm payments dot internal, vouched for by a certificate authority the client trusts. The client checks the signer and the name once, in the handshake, when the connection opens. One check up front, then, and after that every call on the connection is encrypted. What changes in Python? Two lines, basically. On the client, grpc dot ssl channel credentials, then secure channel instead of insecure channel.

2:52 With no arguments, it trusts the usual public certificate authorities. If your company runs a private certificate authority, which internal services often do, you pass that one in. Okay, two lines broke it, two lines start fixing it. Then the server side gets its certificate, plus the private key that goes with it. Yep. Those go into ssl server credentials, then add secure port instead of add insecure port. Java has the same two pieces, TLS server credentials and TLS channel credentials. And now that token on the wire is just gibberish to anyone listening.

3:24 But that lock only proves one side. Back when we split the lock from the badge, you said plain TLS doesn't say who's calling. At the connection level, payments still can't. Yeah. Anyone who can reach the port can connect. So how does payments tell orders from a stranger? Mutual TLS. The client presents its own certificate too, and the server checks it the same way. So it's the same check, just pointed back at the client. Which means orders needs a cert of its own. And its private key, yeah. And the server needs to know which authority signs client certificates, plus a switch that says a client certificate is required.

4:02 In Python, that switch is literally called require client auth. Let's say I flip that switch but forget to give it the authority. Does it just let everyone in? No, Python raises an error right away instead of quietly accepting anyone. In Java the switch is client auth set to require, and you hand it a trust manager yourself, the object that holds the authorities you trust. Oh, good. So it fails at startup, not quietly in production. And once a client's connected, can my handler see which service it is?

4:33 Yep. A Python handler can ask its context for the peer's identity, read from the client's certificate. In Java, an interceptor can read the certificate and pass along something simple, like, the caller is orders. Which, between services, is real identity, with no shared password floating around. So both ends of the connection are proven. But a certificate names a service. What about the user, or the permission, behind one single call? That's the token's job, and it's the same kind of bearer token our orders service was sending. The OAuth standard's own definition: whoever holds it can use it. Like cash.

5:10 Cash on an open pipe. Which is exactly why the lock went on first. So how do I attach one properly? In Python, grpc dot access token call credentials, with the token. It goes into the call's metadata, the same side channel your trace IDs ride in, as an authorization header: the word bearer, then the token. But tokens expire. For example, a token that lasts an hour just dies, and my service keeps running all day. Right, they do. So there's a second option, a small plugin class. gRPC calls it before every single call, so it can hand over a fresh token when the old one's about to run out.

5:50 Oh, a little token vending machine. And I still glue that onto the TLS credentials from before? Pretty much. The vending machine bolts onto the lock: gRPC combines them into one composite credential, so every call on that channel carries the badge automatically. On the Java side, you write a credentials class whose one method runs before each call and adds the header. Nice. So the token can expire at lunch and nobody gets paged. But that every-call badge is what the orders team's interceptor did too.

6:20 It stamped a token on every call. You said gRPC itself could block it. How, when that interceptor puts on the same header? The gRPC interceptors guide answers that one. It says client-side auth could be done with an interceptor, but call credentials are better suited. And the reason shows up in gRPC's own code: a check your interceptor doesn't have. What kind of check? Call credentials get matched against how secure the connection is. In gRPC's core library, which Python sits on, they need a private, tamper-proof connection by default.

6:53 Try them on an insecure channel, and the call fails with an unauthenticated error. Wait, the whole call fails? It doesn't just drop the token? Yep, it fails outright. The token never leaves the process. Let's say someone copies that dev channel config again. The very first call fails loudly, rather than leaking without a sound. Go fails even earlier. Its OAuth credentials require an encrypted connection, so Go refuses to even connect without one. Okay, but plenty of us write Java. Same deal there?

7:24 Java has no blanket rule. Each credential gets told whether the link is encrypted and decides for itself, so a homemade Java credential can still leak over plain text unless you add that check. Even the auth guide only promises that most gRPC implementations block credentials on an unencrypted channel. Most, not all. Fair. But then local development gets annoying. I don't want certificates on my laptop. Python has local channel credentials for that. It's still marked experimental, but it allows call credentials over a local connection, so tests run the real auth path without any certificates.

7:59 Oh, that's great. Then the insecure channel can go away even in tests, and that's one less config to leak into production. Now the other end, since sending's covered. The token lands at payments. Who actually checks it? A server interceptor, same as any check every method needs. It reads the authorization header before the handler runs, and if the token's missing or bad, it rejects the call with the unauthenticated status code. And most of these tokens are JWTs, right? JSON web tokens. Often, yeah. A JWT is three chunks separated by dots: a header, then the claims, which are facts like who it's for and when it expires, and then a signature. And in the usual signed kind, I mean, nothing is encrypted.

8:43 Anyone holding it can read the claims. The signature only proves nobody changed them. Honestly, I didn't realize that for years. I assumed the claims were secret. You're not alone. First check is the signature, then expiry. At or after the expiry time, you reject it. And then the one that's easy to skip, the audience, the field saying which service it's for. Why does the audience matter so much? Take a token minted for a reporting service. If payments skips the audience check, it accepts that token, and a leaked reporting token suddenly works on payments.

9:16 Nothing in the logs looks wrong, because the call just succeeds. Ouch. And I'd bet the issuer needs the same treatment. Yep, the issuer too, so a token from an issuer you don't trust for payments doesn't count, even if it's properly signed. Then hand the caller's identity to the handler. The grpc Java JWT example puts the client ID into the gRPC context, the per-call bag of values the handler can read. So that's who's calling. Being allowed is a different question, though. Orders might be fine on charges and still have no business calling refunds.

9:52 Yeah, and that refusal is permission denied, not unauthenticated. Got it. And none of these tokens come from gRPC, do they? No, something like an OAuth server hands them out. gRPC just carries them. And the gRPC auth guide warns about the sender's side of that audience mistake. Never send a Google-issued token to a non-Google service, because it could be stolen and used to impersonate you to Google. Makes sense. A token should only ever go where it's meant to be used. And how long should one live?

10:23 Short. The OAuth bearer standard recommends an hour or less, so a stolen one stops working soon. So a leak comes with an expiry date. Now, those checks cover the token itself. In production, though, a lot of the risk sits in the setup around it. First, certificates expire. Everything works right up until the day it doesn't, and every new connection fails its handshake. Usually the cert somebody made ages ago, right before they changed teams. With the renewal reminder still sitting in their calendar.

10:56 Classic. So rotation has to be automated, not left to anyone's memory. And then there's the mistake we started with: dev config leaking into production. If you can't drop insecure entirely, make it an explicit, loud, development-only flag, never the default. Yeah. And between rotation and config drift, a lot of teams hand the locking off. If you run on Google Cloud, gRPC also supports ALTS, Google's own version of this lock. Elsewhere, the common route is a service mesh, a proxy next to every service.

11:29 Istio, for example, typically does mutual TLS between those proxies and rotates the certificates for you. Okay, but my first thought is the default. Is plain text actually gone once the mesh is on? Not completely, by default. Istio's mesh-wide default is permissive mode, which accepts both mutual TLS and plain text. It's meant for migrations, and the docs say to switch to strict once everything's on the mesh. Oof. Plain text still getting in, inside the thing you installed to stop plain text, until somebody flips it to strict.

12:02 So if that orders team got one sticky note before shipping, what's on it? Lock the pipe with TLS on every channel. And between services, make it mutual, so the server knows exactly who connected. Attach tokens as call credentials, not a hand-rolled interceptor, so most gRPC stacks won't let them out unencrypted. That orders service? Its token never crosses the wire readable again. And on the server, verify the signature, the expiry, the issuer, and the audience on every call. And if a mesh does the locking, make sure it's on strict.

12:36 Coming up next, changing your API without breaking the clients that already depend on it. Thank goodness, because nobody wants to redeploy every caller just to add one field. Thanks for listening to Learning Podcasts.