forked from argoproj/argo-cd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
225 lines (206 loc) · 7.22 KB
/
util.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package project
import (
"fmt"
"regexp"
"strings"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
"github.com/argoproj/argo-cd/util/rbac"
)
// GetRoleByName returns the role in a project by the name with its index
func GetRoleByName(proj *v1alpha1.AppProject, name string) (*v1alpha1.ProjectRole, int, error) {
for i, role := range proj.Spec.Roles {
if name == role.Name {
return &role, i, nil
}
}
return nil, -1, fmt.Errorf("role '%s' does not exist in project '%s'", name, proj.Name)
}
// GetJWTToken looks up the index of a JWTToken in a project by the issue at time
func GetJWTToken(proj *v1alpha1.AppProject, roleName string, issuedAt int64) (*v1alpha1.JWTToken, int, error) {
role, _, err := GetRoleByName(proj, roleName)
if err != nil {
return nil, -1, err
}
for i, token := range role.JWTTokens {
if issuedAt == token.IssuedAt {
return &token, i, nil
}
}
return nil, -1, fmt.Errorf("JWT token for role '%s' issued at '%d' does not exist in project '%s'", role.Name, issuedAt, proj.Name)
}
func ValidateProject(p *v1alpha1.AppProject) error {
destKeys := make(map[string]bool)
for _, dest := range p.Spec.Destinations {
key := fmt.Sprintf("%s/%s", dest.Server, dest.Namespace)
if _, ok := destKeys[key]; ok {
return status.Errorf(codes.InvalidArgument, "destination '%s' already added", key)
}
destKeys[key] = true
}
srcRepos := make(map[string]bool)
for _, src := range p.Spec.SourceRepos {
if _, ok := srcRepos[src]; ok {
return status.Errorf(codes.InvalidArgument, "source repository '%s' already added", src)
}
srcRepos[src] = true
}
roleNames := make(map[string]bool)
for _, role := range p.Spec.Roles {
if _, ok := roleNames[role.Name]; ok {
return status.Errorf(codes.AlreadyExists, "role '%s' already exists", role.Name)
}
if err := validateRoleName(role.Name); err != nil {
return err
}
existingPolicies := make(map[string]bool)
for _, policy := range role.Policies {
if _, ok := existingPolicies[policy]; ok {
return status.Errorf(codes.AlreadyExists, "policy '%s' already exists for role '%s'", policy, role.Name)
}
if err := validatePolicy(p.Name, role.Name, policy); err != nil {
return err
}
existingPolicies[policy] = true
}
existingGroups := make(map[string]bool)
for _, group := range role.Groups {
if _, ok := existingGroups[group]; ok {
return status.Errorf(codes.AlreadyExists, "group '%s' already exists for role '%s'", group, role.Name)
}
if err := validateGroupName(group); err != nil {
return err
}
existingGroups[group] = true
}
roleNames[role.Name] = true
}
if err := validatePolicySyntax(p); err != nil {
return err
}
return nil
}
// TODO: refactor to use rbacpolicy.ActionGet, rbacpolicy.ActionCreate, without import cycle
var validActions = map[string]bool{
"get": true,
"create": true,
"update": true,
"delete": true,
"sync": true,
"*": true,
}
func isValidAction(action string) bool {
return validActions[action]
}
func validatePolicy(proj string, role string, policy string) error {
policyComponents := strings.Split(policy, ",")
if len(policyComponents) != 6 || strings.Trim(policyComponents[0], " ") != "p" {
return status.Errorf(codes.InvalidArgument, "invalid policy rule '%s': must be of the form: 'p, sub, res, act, obj, eft'", policy)
}
// subject
subject := strings.Trim(policyComponents[1], " ")
expectedSubject := fmt.Sprintf("proj:%s:%s", proj, role)
if subject != expectedSubject {
return status.Errorf(codes.InvalidArgument, "invalid policy rule '%s': policy subject must be: '%s', not '%s'", policy, expectedSubject, subject)
}
// resource
resource := strings.Trim(policyComponents[2], " ")
if resource != "applications" {
return status.Errorf(codes.InvalidArgument, "invalid policy rule '%s': project resource must be: 'applications', not '%s'", policy, resource)
}
// action
action := strings.Trim(policyComponents[3], " ")
if !isValidAction(action) {
return status.Errorf(codes.InvalidArgument, "invalid policy rule '%s': invalid action '%s'", policy, action)
}
// object
object := strings.Trim(policyComponents[4], " ")
objectRegexp, err := regexp.Compile(fmt.Sprintf(`^%s/[*\w-]+$`, proj))
if err != nil || !objectRegexp.MatchString(object) {
return status.Errorf(codes.InvalidArgument, "invalid policy rule '%s': object must be of form '%s/*' or '%s/<APPNAME>', not '%s'", policy, proj, proj, object)
}
// effect
effect := strings.Trim(policyComponents[5], " ")
if effect != "allow" && effect != "deny" {
return status.Errorf(codes.InvalidArgument, "invalid policy rule '%s': effect must be: 'allow' or 'deny'", policy)
}
return nil
}
var roleNameRegexp = regexp.MustCompile(`^[a-zA-Z0-9]([-_a-zA-Z0-9]*[a-zA-Z0-9])?$`)
func validateRoleName(name string) error {
if !roleNameRegexp.MatchString(name) {
return status.Errorf(codes.InvalidArgument, "invalid role name '%s'. Must consist of alphanumeric characters, '-' or '_', and must start and end with an alphanumeric character", name)
}
return nil
}
var invalidChars = regexp.MustCompile("[,\n\r\t]")
func validateGroupName(name string) error {
if strings.TrimSpace(name) == "" {
return status.Errorf(codes.InvalidArgument, "group '%s' is empty", name)
}
if invalidChars.MatchString(name) {
return status.Errorf(codes.InvalidArgument, "group '%s' contains invalid characters", name)
}
return nil
}
// validatePolicySyntax verifies policy syntax is accepted by casbin
func validatePolicySyntax(p *v1alpha1.AppProject) error {
err := rbac.ValidatePolicy(p.ProjectPoliciesString())
if err != nil {
return status.Errorf(codes.InvalidArgument, "policy syntax error: %s", err.Error())
}
return nil
}
// AddGroupToRole adds an OIDC group to a role
func AddGroupToRole(p *v1alpha1.AppProject, roleName, group string) (bool, error) {
role, roleIndex, err := GetRoleByName(p, roleName)
if err != nil {
return false, err
}
for _, roleGroup := range role.Groups {
if group == roleGroup {
return false, nil
}
}
role.Groups = append(role.Groups, group)
p.Spec.Roles[roleIndex] = *role
return true, nil
}
// RemoveGroupFromRole removes an OIDC group from a role
func RemoveGroupFromRole(p *v1alpha1.AppProject, roleName, group string) (bool, error) {
role, roleIndex, err := GetRoleByName(p, roleName)
if err != nil {
return false, err
}
for i, roleGroup := range role.Groups {
if group == roleGroup {
role.Groups = append(role.Groups[0:i], role.Groups[i:]...)
p.Spec.Roles[roleIndex] = *role
return true, nil
}
}
return false, nil
}
// NormalizePolicies normalizes the policies in the project
func NormalizePolicies(p *v1alpha1.AppProject) {
for i, role := range p.Spec.Roles {
var normalizedPolicies []string
for _, policy := range role.Policies {
normalizedPolicies = append(normalizedPolicies, normalizePolicy(policy))
}
p.Spec.Roles[i].Policies = normalizedPolicies
}
}
func normalizePolicy(policy string) string {
policyComponents := strings.Split(policy, ",")
normalizedPolicy := ""
for _, component := range policyComponents {
if normalizedPolicy == "" {
normalizedPolicy = component
} else {
normalizedPolicy = fmt.Sprintf("%s, %s", normalizedPolicy, strings.Trim(component, " "))
}
}
return normalizedPolicy
}