Skip to content

Commit 02561f7

Browse files
committed
[TT-14494] improve error logging for JWKS URL handling - visor comments
1 parent 7cbe1bd commit 02561f7

2 files changed

Lines changed: 17 additions & 52 deletions

File tree

gateway/log_helpers.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"net"
77
"net/http"
88
"net/url"
9-
"strings"
109
"syscall"
1110

1211
"github.com/sirupsen/logrus"
@@ -77,30 +76,23 @@ func (gw *Gateway) logJWKError(logger *logrus.Entry, jwkURL string, err error) {
7776
return
7877
}
7978

80-
errStr := err.Error()
81-
8279
// content/JSON errors
8380
var syntaxErr *json.SyntaxError
8481
var unmarshalErr *json.UnmarshalTypeError
8582

86-
if errors.As(err, &syntaxErr) || errors.As(err, &unmarshalErr) || strings.Contains(errStr, "invalid character") {
83+
if errors.As(err, &syntaxErr) || errors.As(err, &unmarshalErr) {
8784
logger.WithError(err).Errorf("Invalid JWKS retrieved from endpoint: %s", jwkURL)
8885
return
8986
}
9087

9188
// network errors
9289
var urlErr *url.Error
93-
var netOpErr *net.OpError
94-
var dnsErr *net.DNSError
95-
96-
isNetworkError := errors.As(err, &urlErr) ||
97-
errors.As(err, &dnsErr) ||
98-
(errors.As(err, &netOpErr) && errors.Is(netOpErr, syscall.ECONNREFUSED)) ||
99-
strings.Contains(errStr, "dial tcp") ||
100-
strings.Contains(errStr, "no such host") ||
101-
strings.Contains(errStr, "connection refused")
90+
var netErr net.Error
10291

103-
if isNetworkError {
92+
// errors.As(err, &netErr) catches any error that implements the net.Error interface.
93+
// This covers DNS errors, timeouts, connection refused, dial errors, etc.
94+
// errors.Is(err, syscall.ECONNREFUSED) catches underlying system call errors specifically.
95+
if errors.As(err, &urlErr) || errors.As(err, &netErr) || errors.Is(err, syscall.ECONNREFUSED) {
10496
logger.WithError(err).Errorf("JWKS endpoint resolution failed: invalid or unreachable host %s", jwkURL)
10597
return
10698
}

gateway/log_helpers_test.go

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -155,18 +155,11 @@ func TestGatewayLogJWKError(t *testing.T) {
155155
name string
156156
err error
157157
expectedLog string
158-
shouldLog bool
159158
}{
160-
{
161-
name: "No error (nil)",
162-
err: nil,
163-
shouldLog: false,
164-
},
165159
{
166160
name: "JSON Syntax Error",
167161
err: &json.SyntaxError{},
168162
expectedLog: "Invalid JWKS retrieved from endpoint: " + testURL,
169-
shouldLog: true,
170163
},
171164
{
172165
name: "JSON Unmarshal Type Error",
@@ -175,67 +168,47 @@ func TestGatewayLogJWKError(t *testing.T) {
175168
Type: reflect.TypeOf(""),
176169
},
177170
expectedLog: "Invalid JWKS retrieved from endpoint: " + testURL,
178-
shouldLog: true,
179-
},
180-
{
181-
name: "String error containing 'invalid character'",
182-
err: errors.New("invalid character 'x' looking for beginning of value"),
183-
expectedLog: "Invalid JWKS retrieved from endpoint: " + testURL,
184-
shouldLog: true,
185171
},
186172
{
187-
name: "URL Error type",
173+
name: "URL Error",
188174
err: &url.Error{Op: "Get", URL: testURL, Err: errors.New("timeout")},
189175
expectedLog: "JWKS endpoint resolution failed: invalid or unreachable host " + testURL,
190-
shouldLog: true,
191176
},
192177
{
193-
name: "Net DNS Error",
194-
err: &net.DNSError{Err: "no such host", Name: "example.com"},
178+
name: "Net DNS Error (implements net.Error)",
179+
err: &net.DNSError{Err: "no such host", Name: "example.com", IsNotFound: true},
195180
expectedLog: "JWKS endpoint resolution failed: invalid or unreachable host " + testURL,
196-
shouldLog: true,
197181
},
198182
{
199-
name: "Net OpError (Connection Refused)",
200-
err: &net.OpError{Op: "dial", Err: syscall.ECONNREFUSED},
183+
name: "Net OpError (implements net.Error)",
184+
err: &net.OpError{Op: "dial", Net: "tcp", Err: errors.New("timeout")},
201185
expectedLog: "JWKS endpoint resolution failed: invalid or unreachable host " + testURL,
202-
shouldLog: true,
203186
},
204187
{
205-
name: "String error containing 'dial tcp'",
206-
err: errors.New("dial tcp: lookup failed"),
188+
name: "Syscall Connection Refused",
189+
err: syscall.ECONNREFUSED,
207190
expectedLog: "JWKS endpoint resolution failed: invalid or unreachable host " + testURL,
208-
shouldLog: true,
209191
},
210192
{
211-
name: "String error containing 'no such host'",
212-
err: errors.New("Get: no such host"),
193+
name: "Wrapped Connection Refused",
194+
err: &net.OpError{Op: "dial", Err: syscall.ECONNREFUSED},
213195
expectedLog: "JWKS endpoint resolution failed: invalid or unreachable host " + testURL,
214-
shouldLog: true,
215196
},
216197
{
217-
name: "Generic/Fallback Error",
218-
err: errors.New("unknown internal server error"),
198+
name: "Generic Error",
199+
err: errors.New("something random"),
219200
expectedLog: "Failed to fetch or decode JWKs from " + testURL,
220-
shouldLog: true,
221201
},
222202
}
223203

224204
for _, tc := range tests {
225205
t.Run(tc.name, func(t *testing.T) {
226206
hook.Reset()
227-
228207
gw.logJWKError(entry, testURL, tc.err)
229208

230-
if !tc.shouldLog {
231-
assert.Empty(t, hook.Entries)
232-
return
233-
}
234-
235209
assert.Len(t, hook.Entries, 1)
236210
assert.Equal(t, logrus.ErrorLevel, hook.LastEntry().Level)
237211
assert.Equal(t, tc.expectedLog, hook.LastEntry().Message)
238-
assert.Equal(t, tc.err, hook.LastEntry().Data["error"])
239212
})
240213
}
241214
}

0 commit comments

Comments
 (0)