@@ -25,8 +25,11 @@ import (
2525)
2626
2727var (
28- _ extension.Extension = (* hiveAuthExtension )(nil )
29- _ extensionauth.Server = (* hiveAuthExtension )(nil )
28+ _ extension.Extension = (* hiveAuthExtension )(nil )
29+ _ extensionauth.Server = (* hiveAuthExtension )(nil )
30+ errUnauthorized = errors .New ("unauthorized" )
31+ errMissingAuthorization = errors .New ("missing Authorization header" )
32+ errMissingHiveTargetRef = errors .New ("missing X-Hive-Target-Ref header" )
3033)
3134
3235var _ client.AuthData = (* authData )(nil )
@@ -56,8 +59,8 @@ type hiveAuthExtension struct {
5659 cache * cache.Cache
5760
5861 telemetrySettings component.TelemetrySettings
59- requestDuration metric.Int64Histogram
60- requestCount metric.Int64Counter
62+ requestDuration metric.Int64Histogram
63+ requestCount metric.Int64Counter
6164}
6265
6366func (h * hiveAuthExtension ) Start (_ context.Context , _ component.Host ) error {
@@ -87,8 +90,8 @@ type AuthStatusError struct {
8790 Msg string
8891}
8992
90- func (e * AuthStatusError ) Error () string {
91- return fmt . Sprintf ( "authentication failed: status %d, %s" , e . Code , e . Msg )
93+ func (* AuthStatusError ) Error () string {
94+ return errUnauthorized . Error ( )
9295}
9396
9497func getHeader (h map [string ][]string , headerKey string , metadataKey string ) string {
@@ -139,8 +142,7 @@ type authResult struct {
139142
140143func (h * hiveAuthExtension ) doAuthRequest (ctx context.Context , auth string , targetRef string ) (string , error ) {
141144 h .logger .Debug ("authenticate token for target" ,
142- zap .String ("targetRef" , targetRef ),
143- zap .String ("accessToken" , auth [:10 ]))
145+ zap .String ("targetRef" , targetRef ))
144146
145147 start := time .Now ()
146148 statusLabel := "error"
@@ -198,7 +200,7 @@ func (h *hiveAuthExtension) doAuthRequest(ctx context.Context, auth string, targ
198200 resp .Body .Close ()
199201
200202 select {
201- case <- time .After (retryDelay * time .Duration (attempt + 1 )):
203+ case <- time .After (retryDelay * time .Duration (attempt + 1 )):
202204 // Continue to next attempt.
203205 case <- ctx .Done ():
204206 return "" , ctx .Err ()
@@ -207,12 +209,29 @@ func (h *hiveAuthExtension) doAuthRequest(ctx context.Context, auth string, targ
207209 }
208210
209211 // For non-retryable errors.
210- errMsg := fmt .Sprintf ("authentication failed: received status %s" , resp .Status )
211- h .logger .Warn (errMsg )
212+ body , readErr := io .ReadAll (io .LimitReader (resp .Body , 64 * 1024 ))
212213 resp .Body .Close ()
214+ if readErr != nil {
215+ return "" , fmt .Errorf ("failed to read authentication error response: %w" , readErr )
216+ }
217+
218+ errMsg := strings .TrimSpace (string (body ))
219+ var result struct {
220+ Message string `json:"message"`
221+ }
222+ if json .Unmarshal (body , & result ) == nil && result .Message != "" {
223+ errMsg = result .Message
224+ }
225+ if errMsg == "" {
226+ errMsg = resp .Status
227+ }
228+
229+ h .logger .Warn ("authentication failed" ,
230+ zap .Int ("status" , resp .StatusCode ),
231+ zap .String ("message" , errMsg ))
213232 return "" , & AuthStatusError {
214233 Code : resp .StatusCode ,
215- Msg : "non-retryable error" ,
234+ Msg : errMsg ,
216235 }
217236 }
218237
@@ -226,11 +245,11 @@ func (h *hiveAuthExtension) Authenticate(ctx context.Context, headers map[string
226245 auth := getAuthHeader (headers )
227246 targetRef := getTargetRefHeader (headers )
228247 if auth == "" {
229- return ctx , errors . New ( "No auth provided" )
248+ return ctx , errMissingAuthorization
230249 }
231250
232251 if targetRef == "" {
233- return ctx , errors . New ( "No target ref provided" )
252+ return ctx , errMissingHiveTargetRef
234253 }
235254
236255 cacheKey := fmt .Sprintf ("%s|%s" , auth , targetRef )
@@ -244,7 +263,7 @@ func (h *hiveAuthExtension) Authenticate(ctx context.Context, headers map[string
244263 return client .NewContext (ctx , cl ), nil
245264 }
246265
247- return ctx , res . err
266+ return ctx , errUnauthorized
248267 }
249268
250269 // Deduplicate concurrent calls.
@@ -266,7 +285,7 @@ func (h *hiveAuthExtension) Authenticate(ctx context.Context, headers map[string
266285 return client .NewContext (ctx , cl ), nil
267286 }
268287
269- return ctx , err
288+ return ctx , errUnauthorized
270289}
271290
272291func newHiveAuthExtension (
@@ -289,7 +308,7 @@ func newHiveAuthExtension(
289308 client : & http.Client {
290309 Timeout : c .Timeout ,
291310 },
292- cache : cache .New (500 * time .Second , time .Minute ),
311+ cache : cache .New (500 * time .Second , time .Minute ),
293312 telemetrySettings : telemetrySettings ,
294313 }, nil
295314}
0 commit comments