Skip to content

Commit ba98beb

Browse files
author
Sankeerth
authored
chore: add error logs for oauthv2 errors (#4608)
1 parent ef35f90 commit ba98beb

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

router/transformer/transformer_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -1152,7 +1152,7 @@ var oauthv2ProxyTestCases = []oauthv2ProxyTcs{
11521152
},
11531153
RespBodys: map[int64]string{},
11541154
RespContentType: "text/plain; charset=utf-8",
1155-
ProxyRequestResponseBody: `reading response body post RoundTrip: unexpected EOF`, // not full error message
1155+
ProxyRequestResponseBody: `reading response body post roundTrip: unexpected EOF`, // not full error message
11561156
ProxyRequestStatusCode: 500,
11571157
RespStatusCodes: map[int64]int{},
11581158
},
@@ -1208,8 +1208,8 @@ var oauthv2ProxyTestCases = []oauthv2ProxyTcs{
12081208
},
12091209
RespBodys: map[int64]string{},
12101210
RespContentType: "text/plain; charset=utf-8",
1211-
// Originally Response Body will look like this "Post \"http://<TF_SERVER>/v1/destinations/salesforce_oauth/proxy\": getting auth error category: LB cannot send to transformer"
1212-
ProxyRequestResponseBody: `getting auth error category: LB cannot send to transformer`,
1211+
// Originally Response Body will look like this "Post \"http://<TF_SERVER>/v1/destinations/salesforce_oauth/proxy\": getting auth error category post roundTrip: LB cannot send to transformer"
1212+
ProxyRequestResponseBody: `getting auth error category post roundTrip: LB cannot send to transformer`,
12131213
ProxyRequestStatusCode: 500,
12141214
RespStatusCodes: map[int64]int{},
12151215
},
@@ -1259,8 +1259,8 @@ var oauthv2ProxyTestCases = []oauthv2ProxyTcs{
12591259
},
12601260
RespBodys: map[int64]string{},
12611261
RespContentType: "text/plain; charset=utf-8",
1262-
// Originally Response Body will look like this "Post \"http://<TF_SERVER>/v1/destinations/salesforce_oauth/proxy\": getting auth error category: LB cannot send to transformer"
1263-
ProxyRequestResponseBody: `getting auth error category: LB cannot send to transformer`,
1262+
// Originally Response Body will look like this "Post \"http://<TF_SERVER>/v1/destinations/salesforce_oauth/proxy\": getting auth error category post roundTrip: LB cannot send to transformer"
1263+
ProxyRequestResponseBody: `getting auth error category post roundTrip: LB cannot send to transformer`,
12641264
ProxyRequestStatusCode: 500,
12651265
RespStatusCodes: map[int64]int{},
12661266
},

services/oauth/v2/http/transport.go

+35-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package v2
33
import (
44
"bytes"
55
"encoding/json"
6+
"errors"
67
"fmt"
78
"io"
89
"net/http"
@@ -94,7 +95,7 @@ func (t *OAuthTransport) preRoundTrip(rts *roundTripState) *http.Response {
9495
}
9596
body, err := io.ReadAll(rts.req.Body)
9697
if err != nil {
97-
t.log.Errorn("reading request body",
98+
t.log.Errorn("[preRoundTrip] reading request body",
9899
obskit.DestinationID(rts.destination.ID),
99100
obskit.WorkspaceID(rts.destination.WorkspaceID),
100101
obskit.DestinationType(rts.destination.DefinitionName),
@@ -106,7 +107,11 @@ func (t *OAuthTransport) preRoundTrip(rts *roundTripState) *http.Response {
106107
rts.req = rts.req.WithContext(cntx.CtxWithSecret(rts.req.Context(), authResponse.Account.Secret))
107108
err = t.Augmenter.Augment(rts.req, body, authResponse.Account.Secret)
108109
if err != nil {
109-
t.log.Debugn("augmenting the secret",
110+
t.log.Errorn("[preRoundTrip] secret augmentation",
111+
obskit.DestinationID(rts.destination.ID),
112+
obskit.WorkspaceID(rts.destination.WorkspaceID),
113+
obskit.DestinationType(rts.destination.DefinitionName),
114+
logger.NewStringField("flow", string(t.flow)),
110115
logger.NewErrorField(err))
111116
return httpResponseCreator(http.StatusInternalServerError, []byte(fmt.Errorf("augmenting the secret pre roundTrip: %w", err).Error()))
112117
}
@@ -127,7 +132,14 @@ func (t *OAuthTransport) preRoundTrip(rts *roundTripState) *http.Response {
127132
func (t *OAuthTransport) postRoundTrip(rts *roundTripState) (*http.Response, error) {
128133
respData, err := io.ReadAll(rts.res.Body)
129134
if err != nil {
130-
return nil, fmt.Errorf("reading response body post RoundTrip: %w", err)
135+
t.log.Errorn("[postRoundTrip] reading response body",
136+
obskit.DestinationID(rts.destination.ID),
137+
obskit.WorkspaceID(rts.destination.WorkspaceID),
138+
obskit.DestinationType(rts.destination.DefinitionName),
139+
logger.NewStringField("flow", string(t.flow)),
140+
logger.NewErrorField(err),
141+
)
142+
return nil, fmt.Errorf("reading response body post roundTrip: %w", err)
131143
}
132144
interceptorResp := oauth.OAuthInterceptorResponse{}
133145
// internal function
@@ -142,14 +154,26 @@ func (t *OAuthTransport) postRoundTrip(rts *roundTripState) (*http.Response, err
142154
}
143155
authErrorCategory, err := t.getAuthErrorCategory(respData)
144156
if err != nil {
145-
return nil, fmt.Errorf("getting auth error category: %s", string(respData))
157+
t.log.Errorn("[postRoundTrip] get authErrorCategory",
158+
obskit.DestinationID(rts.destination.ID),
159+
obskit.WorkspaceID(rts.destination.WorkspaceID),
160+
obskit.DestinationType(rts.destination.DefinitionName),
161+
logger.NewStringField("flow", string(t.flow)),
162+
logger.NewErrorField(errors.New(string(respData))),
163+
)
164+
return nil, fmt.Errorf("getting auth error category post roundTrip: %s", string(respData))
146165
}
147166
if authErrorCategory == common.CategoryRefreshToken {
148167
// since same token that was used to make the http call needs to be refreshed, we need the current token information
149168
var oldSecret json.RawMessage
150169
oldSecret, ok := cntx.SecretFromCtx(rts.req.Context())
151170
if !ok {
152-
return nil, fmt.Errorf("getting secret from context")
171+
t.log.Errorn("[postRoundTrip] get secret from context",
172+
obskit.DestinationID(rts.destination.ID),
173+
obskit.WorkspaceID(rts.destination.WorkspaceID),
174+
obskit.DestinationType(rts.destination.DefinitionName),
175+
logger.NewStringField("flow", string(t.flow)))
176+
return nil, fmt.Errorf("getting secret from context post roundTrip")
153177
}
154178
rts.refreshTokenParams.Secret = oldSecret
155179
rts.refreshTokenParams.Destination = rts.destination
@@ -212,9 +236,11 @@ func (t *OAuthTransport) fireTimerStats(statName string, tags stats.Tags, startT
212236
func (t *OAuthTransport) RoundTrip(req *http.Request) (*http.Response, error) {
213237
destination, ok := cntx.DestInfoFromCtx(req.Context())
214238
if !ok {
215-
return httpResponseCreator(http.StatusInternalServerError, []byte("the consent data is not of destinationInfo type")), nil
239+
t.log.Errorn("destinationInfo is not present in request context", logger.NewStringField("flow", string(t.flow)))
240+
return httpResponseCreator(http.StatusInternalServerError, []byte("request context data is not of destinationInfo type")), nil
216241
}
217242
if destination == nil {
243+
t.log.Errorn("nil destination info in request context", logger.NewStringField("flow", string(t.flow)))
218244
return httpResponseCreator(http.StatusInternalServerError, []byte("no destination found in context of the request")), nil
219245
}
220246

@@ -242,7 +268,9 @@ func (t *OAuthTransport) RoundTrip(req *http.Request) (*http.Response, error) {
242268
obskit.DestinationID(rts.destination.ID),
243269
obskit.WorkspaceID(rts.destination.WorkspaceID),
244270
obskit.DestinationType(rts.destination.DefinitionName),
245-
logger.NewStringField("flow", string(t.flow)))
271+
logger.NewStringField("flow", string(t.flow)),
272+
logger.NewErrorField(err),
273+
)
246274
return httpResponseCreator(http.StatusInternalServerError, []byte(err.Error())), nil
247275
}
248276
rts.refreshTokenParams = &oauth.RefreshTokenParams{

0 commit comments

Comments
 (0)