forked from gravitational/teleport
/
join_gitlab.go
177 lines (162 loc) · 5.34 KB
/
join_gitlab.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Teleport
* Copyright (C) 2023 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package auth
import (
"context"
"regexp"
"strings"
"github.com/gravitational/trace"
"github.com/sirupsen/logrus"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/lib/gitlab"
)
type gitlabIDTokenValidator interface {
Validate(
ctx context.Context, domain string, token string,
) (*gitlab.IDTokenClaims, error)
}
func (a *Server) checkGitLabJoinRequest(ctx context.Context, req *types.RegisterUsingTokenRequest) (*gitlab.IDTokenClaims, error) {
if req.IDToken == "" {
return nil, trace.BadParameter("IDToken not provided for gitlab join request")
}
pt, err := a.GetToken(ctx, req.Token)
if err != nil {
return nil, trace.Wrap(err)
}
token, ok := pt.(*types.ProvisionTokenV2)
if !ok {
return nil, trace.BadParameter("gitlab join method only supports ProvisionTokenV2, '%T' was provided", pt)
}
claims, err := a.gitlabIDTokenValidator.Validate(
ctx, token.Spec.GitLab.Domain, req.IDToken,
)
if err != nil {
return nil, trace.Wrap(err)
}
log.WithFields(logrus.Fields{
"claims": claims,
"token": pt.GetName(),
}).Info("GitLab CI run trying to join cluster")
return claims, trace.Wrap(checkGitLabAllowRules(token, claims))
}
// joinRuleGlobMatch is used when comparing some rule fields from a
// ProvisionToken against a claim from a token. It allows simple pattern
// matching:
// - '*' matches zero or more characters.
// - '?' matches any single character.
// It returns true if a match is detected.
func joinRuleGlobMatch(want string, got string) (bool, error) {
if want == "" {
return true, nil
}
return globMatch(want, got)
}
// globMatch performs simple a simple glob-style match test on a string.
// - '*' matches zero or more characters.
// - '?' matches any single character.
// It returns true if a match is detected.
func globMatch(pattern, str string) (bool, error) {
pattern = regexp.QuoteMeta(pattern)
pattern = strings.ReplaceAll(pattern, `\*`, ".*")
pattern = strings.ReplaceAll(pattern, `\?`, ".")
pattern = "^" + pattern + "$"
matched, err := regexp.MatchString(pattern, str)
return matched, trace.Wrap(err)
}
func checkGitLabAllowRules(token *types.ProvisionTokenV2, claims *gitlab.IDTokenClaims) error {
// Helper for comparing a BoolOption with GitLabs string bool.
// Returns true if OK - returns false if not OK
boolEqual := func(want *types.BoolOption, got string) bool {
if want == nil {
return true
}
return (want.Value && got == "true") || (!want.Value && got == "false")
}
// If a single rule passes, accept the IDToken
for i, rule := range token.Spec.GitLab.Allow {
// Please consider keeping these field validators in the same order they
// are defined within the ProvisionTokenSpecV2GitLab proto spec.
subMatches, err := joinRuleGlobMatch(rule.Sub, claims.Sub)
if err != nil {
return trace.Wrap(err, "evaluating rule (%d) sub match", i)
}
if !subMatches {
continue
}
refMatches, err := joinRuleGlobMatch(rule.Ref, claims.Ref)
if err != nil {
return trace.Wrap(err, "evaluating rule (%d) ref match", i)
}
if !refMatches {
continue
}
if rule.RefType != "" && claims.RefType != rule.RefType {
continue
}
namespacePathMatches, err := joinRuleGlobMatch(rule.NamespacePath, claims.NamespacePath)
if err != nil {
return trace.Wrap(err, "evaluating rule (%d) namespace_path match", i)
}
if !namespacePathMatches {
continue
}
projectPathMatches, err := joinRuleGlobMatch(rule.ProjectPath, claims.ProjectPath)
if err != nil {
return trace.Wrap(err, "evaluating rule (%d) project_path match", i)
}
if !projectPathMatches {
continue
}
if rule.PipelineSource != "" && claims.PipelineSource != rule.PipelineSource {
continue
}
if rule.Environment != "" && claims.Environment != rule.Environment {
continue
}
if rule.UserLogin != "" && claims.UserLogin != rule.UserLogin {
continue
}
if rule.UserID != "" && claims.UserID != rule.UserID {
continue
}
if rule.UserEmail != "" && claims.UserEmail != rule.UserEmail {
continue
}
if !boolEqual(rule.RefProtected, claims.RefProtected) {
continue
}
if !boolEqual(rule.EnvironmentProtected, claims.EnvironmentProtected) {
continue
}
if rule.CIConfigSHA != "" && claims.CIConfigSHA != rule.CIConfigSHA {
continue
}
if rule.CIConfigRefURI != "" && claims.CIConfigRefURI != rule.CIConfigRefURI {
continue
}
if rule.DeploymentTier != "" && claims.DeploymentTier != rule.DeploymentTier {
continue
}
if rule.ProjectVisibility != "" && claims.ProjectVisibility != rule.ProjectVisibility {
continue
}
// All provided rules met.
return nil
}
return trace.AccessDenied("id token claims did not match any allow rules")
}