A workload identity system is only useful if applications can actually use the identities. This module covers how to integrate SPIFFE into applications — from zero-code approaches (SPIFFE Helper) to native library integration (go-spiffe, py-spiffe).
Integration Approaches
There are three ways to consume SPIFFE identities:
- SPIFFE Helper (zero code changes): A sidecar that fetches SVIDs and writes them as certificate files. Your application reads the files like any TLS certificate. Works with any language.
- Native libraries (code integration): Use go-spiffe, py-spiffe, or java-spiffe to call the Workload API directly. Provides automatic rotation callbacks and richer integration.
- Envoy sidecar (proxy-based): Envoy handles mTLS transparently. Your application communicates in plain HTTP; Envoy adds the identity layer. Covered in Module 8.
Using go-spiffe
package main
import (
"context"
"net/http"
"github.com/spiffe/go-spiffe/v2/spiffetls/tlsconfig"
"github.com/spiffe/go-spiffe/v2/workloadapi"
)
func main() {
ctx := context.Background()
// Connect to the Workload API
source, err := workloadapi.NewX509Source(ctx)
if err != nil {
log.Fatal(err)
}
defer source.Close()
// Create an mTLS server
tlsConfig := tlsconfig.MTLSServerConfig(
source, // Serve our SVID
source, // Verify client SVIDs
tlsconfig.AuthorizeAny(), // Or use AuthorizeID for specific SPIFFE IDs
)
server := &http.Server{
Addr: ":8443",
TLSConfig: tlsConfig,
}
server.ListenAndServeTLS("", "") // Certs from SPIRE, not files
}
Using SPIFFE Helper
# spiffe-helper.conf
agent_address = "/run/spire/sockets/agent.sock"
cmd = "/app/server" # Your application command
cert_dir = "/run/spire/certs" # Where to write certificates
svid_file_name = "svid.pem"
svid_key_file_name = "svid_key.pem"
svid_bundle_file_name = "bundle.pem"
renew_signal = "SIGHUP" # Signal to send when certs rotate
# Your app reads /run/spire/certs/svid.pem and svid_key.pem
# When certs rotate, your app gets SIGHUP to reload
Building mTLS Between Microservices
With SVIDs, two services can establish mTLS without manually managing certificates:
# Service A (client) connects to Service B (server):
# 1. Both services get SVIDs from SPIRE via Workload API
# 2. Service A presents its SVID as the client certificate
# 3. Service B presents its SVID as the server certificate
# 4. Both verify the other's SVID against the trust bundle
# 5. Connection established — both identities proven
# No manual certificate generation
# No certificate files to manage
# No expiration alerts — SPIRE handles rotation
Secure gRPC Communication
# gRPC with SPIFFE identity (Go):
# Server:
source, _ := workloadapi.NewX509Source(ctx)
creds := grpc.Creds(credentials.NewTLS(
tlsconfig.MTLSServerConfig(source, source, authorize),
))
server := grpc.NewServer(creds)
# Client:
source, _ := workloadapi.NewX509Source(ctx)
creds := grpc.WithTransportCredentials(credentials.NewTLS(
tlsconfig.MTLSClientConfig(source, source, authorize),
))
conn, _ := grpc.Dial("service-b:8443", creds)