@@ -15,6 +15,7 @@ import (
1515 "github.com/prometheus/client_golang/prometheus/promhttp"
1616 "github.com/spiffe/go-spiffe/v2/spiffeid"
1717 "github.com/spiffe/go-spiffe/v2/spiffetls/tlsconfig"
18+ "github.com/spiffe/go-spiffe/v2/svid/x509svid"
1819 "github.com/spiffe/go-spiffe/v2/workloadapi"
1920)
2021
@@ -191,15 +192,30 @@ func metricsWrapper(next http.HandlerFunc) http.HandlerFunc {
191192
192193func handler (w http.ResponseWriter , r * http.Request ) {
193194 w .Header ().Set ("Content-Type" , "text/plain" )
195+ clientID , err := getClientID (r )
196+ if err != nil {
197+ slog .Warn ("Unable to determine client SPIFFE ID" , "error" , err )
198+ http .Error (w , "Unable to determine client SPIFFE ID" , http .StatusUnauthorized )
199+ return
200+ }
201+ slog .Info ("Received ping" , "client.id" , clientID .String ())
194202 w .WriteHeader (http .StatusOK )
195- _ , err : = w .Write ([]byte ("...pong" ))
203+ _ , err = w .Write ([]byte ("...pong" ))
196204 if err != nil {
197205 handlerErrors .Inc ()
198206 slog .Error ("Error writing response" , "error" , err )
199207 return
200208 }
201209}
202210
211+ // getClientID returns the SPIFFE ID of the client.
212+ func getClientID (r * http.Request ) (spiffeid.ID , error ) {
213+ if r .TLS == nil || len (r .TLS .PeerCertificates ) == 0 {
214+ return spiffeid.ID {}, fmt .Errorf ("no peer certificates" )
215+ }
216+ return x509svid .IDFromCert (r .TLS .PeerCertificates [0 ])
217+ }
218+
203219func runMetrics (env * Env , mux * http.ServeMux ) {
204220 if env .MetricsEnabled {
205221 // Expose metrics endpoint in both the mTLS server and a default HTTP server
@@ -213,7 +229,6 @@ func runMetrics(env *Env, mux *http.ServeMux) {
213229 }()
214230
215231 }
216-
217232}
218233
219234func runMetricsUpdateWatcher (env * Env , source * workloadapi.X509Source , ctx context.Context ) {
0 commit comments