Skip to content

Commit 465a789

Browse files
Merge branch 'main' into laboratory-add-built-in-docs-component
2 parents 3bb70a9 + 3a0e777 commit 465a789

26 files changed

Lines changed: 1101 additions & 175 deletions

File tree

docker/configs/otel-collector/extension-hiveauth/extension.go

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ import (
2525
)
2626

2727
var (
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

3235
var _ 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

6366
func (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

9497
func getHeader(h map[string][]string, headerKey string, metadataKey string) string {
@@ -139,8 +142,7 @@ type authResult struct {
139142

140143
func (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

272291
func 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
}

integration-tests/testkit/flow.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
AddMetricAlertRuleInput,
77
AnswerOrganizationTransferRequestInput,
88
AssignMemberRoleInput,
9+
ConfirmScimManagementForMemberInput,
910
CreateContractInput,
1011
CreateMemberRoleInput,
1112
CreateOrganizationAccessTokenInput,
@@ -2247,6 +2248,38 @@ export function createOIDCIntegration(
22472248
});
22482249
}
22492250

2251+
export function confirmSCIMManagementForMember(
2252+
input: ConfirmScimManagementForMemberInput,
2253+
authToken: string,
2254+
) {
2255+
return execute({
2256+
document: graphql(`
2257+
mutation TestKit_ConfirmSCIMManagementForMember(
2258+
$input: ConfirmSCIMManagementForMemberInput!
2259+
) {
2260+
confirmSCIMManagementForMember(input: $input) {
2261+
ok {
2262+
confirmedMember {
2263+
id
2264+
user {
2265+
id
2266+
provisionInfo {
2267+
provisioningStatus
2268+
}
2269+
}
2270+
}
2271+
}
2272+
error {
2273+
message
2274+
}
2275+
}
2276+
}
2277+
`),
2278+
variables: { input },
2279+
authToken,
2280+
});
2281+
}
2282+
22502283
export function updateOIDCIntegration(input: UpdateOidcIntegrationInput, authToken: string) {
22512284
return execute({
22522285
document: graphql(`

0 commit comments

Comments
 (0)