Skip to content

Commit c9b0a78

Browse files
committed
feat: log ping-pong client ID when a ping is received
1 parent 942b23f commit c9b0a78

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

workloads/ping-pong/ping-pong-client/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,6 @@ func ping(client *http.Client, serverAddr string, serverPort int) error {
207207
Scheme: "https",
208208
Host: fmt.Sprintf("%s:%d", serverAddr, serverPort),
209209
}).String())
210-
211210
if err != nil {
212211
return err
213212
}

workloads/ping-pong/ping-pong-server/main.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

192193
func 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+
203219
func 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

219234
func runMetricsUpdateWatcher(env *Env, source *workloadapi.X509Source, ctx context.Context) {

0 commit comments

Comments
 (0)