-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
claim_match.go
93 lines (84 loc) · 2.48 KB
/
claim_match.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package oidcccl
import (
"context"
"encoding/json"
"regexp"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/errors"
)
const claimGroups = "groups"
// extractUsernameFromClaims uses a regex to strip out elements of the value
// corresponding to the token claim claimKey.
//
// Note: to be clear on the nomenclature, the "groups" claim should not be
// confused with the notion of groups in the context of regular expressions.
func extractUsernameFromClaims(
ctx context.Context,
claims map[string]json.RawMessage,
claimKey string,
principalRE *regexp.Regexp,
) (string, error) {
targetClaim, ok := claims[claimKey]
if !ok {
log.Errorf(
ctx, "OIDC: failed to complete authentication: invalid JSON claim key: %s", claimKey,
)
}
if claimKey != claimGroups {
var principal string
if err := json.Unmarshal(targetClaim, &principal); err != nil {
log.Errorf(ctx,
"OIDC: failed to complete authentication: failed to parse value for the claim %s: %v",
claimKey, err,
)
return "", err
}
match := principalRE.FindStringSubmatch(principal)
numGroups := len(match)
if numGroups != 2 {
err := errors.Newf("expected one group in regexp, got %d", numGroups)
log.Errorf(ctx, "OIDC: failed to complete authentication: %v", err)
if log.V(1) {
log.Infof(ctx,
"check OIDC cluster settings: %s, %s",
OIDCPrincipalRegexSettingName, OIDCClaimJSONKeySettingName,
)
}
return "", err
}
return match[1], nil
} else {
// This is the case where the claim key specified is the "groups" claim.
// The first matching principal is selected as the SQL username.
if log.V(1) {
log.Infof(ctx,
"multiple principals in the claim found; selecting first matching principal...",
)
}
var principals []string
if err := json.Unmarshal(targetClaim, &principals); err != nil {
log.Errorf(ctx,
"OIDC: failed to complete authentication: failed to parse value for the claim %s: %v",
claimGroups, err,
)
return "", err
}
var match []string
for _, principal := range principals {
match = principalRE.FindStringSubmatch(principal)
if len(match) == 2 {
return match[1], nil
}
}
// Error when there is not a match.
err := errors.Newf("expected one group in regexp")
log.Errorf(ctx, "OIDC: failed to complete authentication: %v", err)
if log.V(1) {
log.Infof(ctx,
"check OIDC cluster settings: %s, %s",
OIDCPrincipalRegexSettingName, OIDCClaimJSONKeySettingName,
)
}
return "", err
}
}