0Pricing
gRPC & High Performance APIs · Lekcja

Mutual TLS (mTLS) do uwierzytelniania usług

Zabezpiecz połączenia gRPC między usługami za pomocą mutual TLS, w którym zarówno klient, jak i serwer przedstawiają certyfikaty, aby kryptograficznie potwierdzić swoją tożsamość.

Mutual TLS (mTLS) do uwierzytelniania usług to bezpłatna lekcja gRPC & High Performance APIs na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej gRPC & High Performance APIs, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs gRPC & High Performance APIs zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

Beyond One-Way TLS

Standard TLS authenticates only the server to the client. In a zero-trust network, the server also needs to verify who is calling.

Mutual TLS (mTLS) makes both sides present certificates.

How mTLS Works

During the handshake:

  • The server sends its certificate (as in normal TLS)
  • The server then requests the client's certificate
  • The client presents its cert and proves it holds the private key
  • Each side validates the other against a trusted CA

The Role of the CA

A Certificate Authority (CA) signs both client and server certs. Each peer trusts the CA, so any cert signed by it is accepted. In service meshes an internal CA issues short-lived certs automatically.

Generating Certificates

For a test setup you create a CA, then sign a server cert and a client cert with it. Tools like openssl or cfssl produce the key/cert pairs.

openssl req -x509 -newkey rsa:4096 -keyout ca.key -out ca.crt -days 365 -nodes

Server Side in Go

Configure the server's tls.Config to load its cert and require client certs verified against the CA pool.

cfg := &tls.Config{
  Certificates: []tls.Certificate{serverCert},
  ClientCAs:    caPool,
  ClientAuth:   tls.RequireAndVerifyClientCert,
}
creds := credentials.NewTLS(cfg)

Wiring the Server

Pass the TLS credentials when constructing the gRPC server so every connection is mutually authenticated.

s := grpc.NewServer(grpc.Creds(creds))

Client Side in Go

The client presents its own certificate and trusts the CA to validate the server.

cfg := &tls.Config{
  Certificates: []tls.Certificate{clientCert},
  RootCAs:      caPool,
}
conn, _ := grpc.Dial(addr, grpc.WithTransportCredentials(credentials.NewTLS(cfg)))

Reading the Peer Identity

Once connected, the server can read the client's certificate from the connection's peer info and use the subject or SAN as an authenticated identity.

p, _ := peer.FromContext(ctx)
tlsInfo := p.AuthInfo.(credentials.TLSInfo)
name := tlsInfo.State.PeerCertificates[0].Subject.CommonName

Certificate Rotation

Certs expire. Production systems rotate them frequently using short lifetimes (hours/days). A sidecar or mesh control plane reloads new certs without restarting the service.

mTLS in Service Meshes

Meshes like Istio or Linkerd automate mTLS entirely: sidecar proxies handle the handshake, issue certs, and rotate them, so application code stays unchanged.

Common Pitfalls

Watch out for:

  • Clock skew breaking cert validity checks
  • Wrong CA pool causing handshake failures
  • Mismatched SAN/hostname errors
  • Forgetting RequireAndVerifyClientCert (downgrades to one-way TLS)

Quick Check

Test your mTLS understanding.

Recap

You learned mutual TLS for gRPC:

  • mTLS authenticates both client and server
  • A shared CA signs and validates certificates
  • Set RequireAndVerifyClientCert on the server, present a client cert on the dial
  • Read peer identity from the verified certificate
  • Rotate certs often; meshes automate the whole flow

Często zadawane pytania

Czy lekcja „Mutual TLS (mTLS) do uwierzytelniania usług” jest bezpłatna?

Tak — pełny tekst „Mutual TLS (mTLS) do uwierzytelniania usług” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu gRPC & High Performance APIs, przejdź na CoddyKit PRO. Kurs gRPC & High Performance APIs zawiera 4 lekcji w sumie.

Co nauczysz się w „Mutual TLS (mTLS) do uwierzytelniania usług”?

Zabezpiecz połączenia gRPC między usługami za pomocą mutual TLS, w którym zarówno klient, jak i serwer przedstawiają certyfikaty, aby kryptograficznie potwierdzić swoją tożsamość. Ćwiczysz gRPC & High Performance APIs z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć gRPC & High Performance APIs?

Nie wymagamy żadnego doświadczenia. gRPC & High Performance APIs w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Mutual TLS (mTLS) do uwierzytelniania usług”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji gRPC & High Performance APIs?

Tak. Każda lekcja gRPC & High Performance APIs zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. TLS/SSL dla gRPC
  2. Uwierzytelnianie i autoryzacja
  3. Interceptory na potrzeby bezpieczeństwa
  4. Mutual TLS (mTLS) do uwierzytelniania usług
← Powrót do gRPC & High Performance APIs