Skip to content

Commit 6be5938

Browse files
authored
Update extended SSL stats (#184)
1 parent 58241ab commit 6be5938

File tree

2 files changed

+95
-5
lines changed

2 files changed

+95
-5
lines changed

client/nginx.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,21 @@ type HTTPRequests struct {
224224
// SSL represents SSL related stats.
225225
type SSL struct {
226226
Handshakes uint64
227-
HandshakesFailed uint64 `json:"handshakes_failed"`
228-
SessionReuses uint64 `json:"session_reuses"`
227+
HandshakesFailed uint64 `json:"handshakes_failed"`
228+
SessionReuses uint64 `json:"session_reuses"`
229+
NoCommonProtocol uint64 `json:"no_common_protocol"`
230+
NoCommonCipher uint64 `json:"no_common_cipher"`
231+
HandshakeTimeout uint64 `json:"handshake_timeout"`
232+
PeerRejectedCert uint64 `json:"peer_rejected_cert"`
233+
VerifyFailures VerifyFailures `json:"verify_failures"`
234+
}
235+
236+
type VerifyFailures struct {
237+
NoCert uint64 `json:"no_cert"`
238+
ExpiredCert uint64 `json:"expired_cert"`
239+
RevokedCert uint64 `json:"revoked_cert"`
240+
HostnameMismatch uint64 `json:"hostname_mismatch"`
241+
Other uint64 `json:"other"`
229242
}
230243

231244
// ServerZones is map of server zone stats by zone name

client/nginx_test.go

+80-3
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ func TestHaveSameParametersForStream(t *testing.T) {
525525
func TestClientWithCheckAPI(t *testing.T) {
526526
// Create a test server that returns supported API versions
527527
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
528-
_, err := w.Write([]byte(`[4, 5, 6, 7]`))
528+
_, err := w.Write([]byte(`[4, 5, 6, 7, 8, 9]`))
529529
if err != nil {
530530
t.Fatalf("unexpected error: %v", err)
531531
}
@@ -542,7 +542,7 @@ func TestClientWithCheckAPI(t *testing.T) {
542542
}
543543

544544
// Test creating a new client with an unsupported API version on the server
545-
client, err = NewNginxClient(ts.URL, WithAPIVersion(8), WithCheckAPI())
545+
client, err = NewNginxClient(ts.URL, WithAPIVersion(3), WithCheckAPI())
546546
if err == nil {
547547
t.Fatalf("expected error, but got nil")
548548
}
@@ -594,7 +594,7 @@ func TestClientWithHTTPClient(t *testing.T) {
594594
func TestGetStats_NoStreamEndpoint(t *testing.T) {
595595
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
596596
if r.RequestURI == "/" {
597-
_, err := w.Write([]byte(`[4, 5, 6, 7]`))
597+
_, err := w.Write([]byte(`[4, 5, 6, 7, 8, 9]`))
598598
if err != nil {
599599
t.Fatalf("unexpected error: %v", err)
600600
}
@@ -641,3 +641,80 @@ func TestGetStats_NoStreamEndpoint(t *testing.T) {
641641
t.Fatalf("StreamZoneSync: expected %v, actual %v", &StreamZoneSync{}, stats.StreamZoneSync)
642642
}
643643
}
644+
645+
func TestGetStats_SSL(t *testing.T) {
646+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
647+
if r.RequestURI == "/" {
648+
_, err := w.Write([]byte(`[4, 5, 6, 7, 8, 9]`))
649+
if err != nil {
650+
t.Fatalf("unexpected error: %v", err)
651+
}
652+
} else if r.RequestURI == "/8/" {
653+
_, err := w.Write([]byte(`["nginx","processes","connections","slabs","http","resolvers","ssl","workers"]`))
654+
if err != nil {
655+
t.Fatalf("unexpected error: %v", err)
656+
}
657+
} else if strings.HasPrefix(r.RequestURI, "/8/ssl") {
658+
_, err := w.Write([]byte(`{
659+
"handshakes" : 79572,
660+
"handshakes_failed" : 21025,
661+
"session_reuses" : 15762,
662+
"no_common_protocol" : 4,
663+
"no_common_cipher" : 2,
664+
"handshake_timeout" : 0,
665+
"peer_rejected_cert" : 0,
666+
"verify_failures" : {
667+
"no_cert" : 0,
668+
"expired_cert" : 2,
669+
"revoked_cert" : 1,
670+
"hostname_mismatch" : 2,
671+
"other" : 1
672+
}
673+
}`))
674+
if err != nil {
675+
t.Fatalf("unexpected error: %v", err)
676+
}
677+
} else {
678+
_, err := w.Write([]byte(`{}`))
679+
if err != nil {
680+
t.Fatalf("unexpected error: %v", err)
681+
}
682+
}
683+
}))
684+
defer ts.Close()
685+
686+
// Test creating a new client with a supported API version on the server
687+
client, err := NewNginxClient(ts.URL, WithAPIVersion(8), WithCheckAPI())
688+
if err != nil {
689+
t.Fatalf("unexpected error: %v", err)
690+
}
691+
if client == nil {
692+
t.Fatalf("client is nil")
693+
}
694+
695+
stats, err := client.GetStats()
696+
if err != nil {
697+
t.Fatalf("unexpected error: %v", err)
698+
}
699+
700+
testStats := SSL{
701+
Handshakes: 79572,
702+
HandshakesFailed: 21025,
703+
SessionReuses: 15762,
704+
NoCommonProtocol: 4,
705+
NoCommonCipher: 2,
706+
HandshakeTimeout: 0,
707+
PeerRejectedCert: 0,
708+
VerifyFailures: VerifyFailures{
709+
NoCert: 0,
710+
ExpiredCert: 2,
711+
RevokedCert: 1,
712+
HostnameMismatch: 2,
713+
Other: 1,
714+
},
715+
}
716+
717+
if !reflect.DeepEqual(stats.SSL, testStats) {
718+
t.Fatalf("SSL stats: expected %v, actual %v", testStats, stats.SSL)
719+
}
720+
}

0 commit comments

Comments
 (0)