-
Notifications
You must be signed in to change notification settings - Fork 124
/
resolver.go
255 lines (223 loc) · 8.61 KB
/
resolver.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package graphql
// This file will always be generated when running gqlgen.
// werrors imports in the resolvers are due to https://github.com/99designs/gqlgen/issues/1171.
import (
"context"
"fmt"
"strings"
"github.com/99designs/gqlgen/graphql"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/model"
"github.com/evergreen-ci/evergreen/rest/data"
restModel "github.com/evergreen-ci/evergreen/rest/model"
"github.com/evergreen-ci/gimlet"
"github.com/evergreen-ci/utility"
)
const (
CreateProjectMutation = "CreateProject"
CopyProjectMutation = "CopyProject"
DeleteProjectMutation = "DeleteProject"
SetLastRevisionMutation = "SetLastRevision"
)
type Resolver struct {
sc data.Connector
}
func New(apiURL string) Config {
dbConnector := &data.DBConnector{URL: apiURL}
c := Config{
Resolvers: &Resolver{
sc: dbConnector,
},
}
c.Directives.RequireDistroAccess = func(ctx context.Context, obj interface{}, next graphql.Resolver, access DistroSettingsAccess) (interface{}, error) {
user := mustHaveUser(ctx)
// If directive is checking for create permissions, no distro ID is required.
if access == DistroSettingsAccessCreate {
if userHasDistroCreatePermission(user) {
return next(ctx)
}
return nil, Forbidden.Send(ctx, fmt.Sprintf("user '%s' does not have create distro permissions", user.Username()))
}
args, isStringMap := obj.(map[string]interface{})
if !isStringMap {
return nil, ResourceNotFound.Send(ctx, "distro not specified")
}
distroId, hasDistroId := args["distroId"].(string)
if !hasDistroId {
name, hasName := args["name"].(string)
if !hasName {
return nil, ResourceNotFound.Send(ctx, "Distro not specified")
}
distroId = name
}
var requiredLevel int
if access == DistroSettingsAccessAdmin {
requiredLevel = evergreen.DistroSettingsAdmin.Value
} else if access == DistroSettingsAccessEdit {
requiredLevel = evergreen.DistroSettingsEdit.Value
} else if access == DistroSettingsAccessView {
requiredLevel = evergreen.DistroSettingsView.Value
} else {
return nil, Forbidden.Send(ctx, "Permission not specified")
}
if userHasDistroPermission(user, distroId, requiredLevel) {
return next(ctx)
}
return nil, Forbidden.Send(ctx, fmt.Sprintf("user '%s' does not have permission to access settings for the distro '%s'", user.Username(), distroId))
}
c.Directives.RequireProjectAdmin = func(ctx context.Context, obj interface{}, next graphql.Resolver) (interface{}, error) {
// Allow if user is superuser.
user := mustHaveUser(ctx)
opts := gimlet.PermissionOpts{
Resource: evergreen.SuperUserPermissionsID,
ResourceType: evergreen.SuperUserResourceType,
Permission: evergreen.PermissionProjectCreate,
RequiredLevel: evergreen.ProjectCreate.Value,
}
if user.HasPermission(opts) {
return next(ctx)
}
operationContext := graphql.GetOperationContext(ctx).OperationName
if operationContext == CreateProjectMutation {
canCreate, err := user.HasProjectCreatePermission()
if err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("checking user permissions: %s", err.Error()))
}
if canCreate {
return next(ctx)
}
}
getPermissionOpts := func(projectId string) gimlet.PermissionOpts {
return gimlet.PermissionOpts{
Resource: projectId,
ResourceType: evergreen.ProjectResourceType,
Permission: evergreen.PermissionProjectSettings,
RequiredLevel: evergreen.ProjectSettingsEdit.Value,
}
}
args, isStringMap := obj.(map[string]interface{})
if !isStringMap {
return nil, ResourceNotFound.Send(ctx, "Project not specified")
}
if operationContext == CopyProjectMutation {
projectIdToCopy, ok := args["project"].(map[string]interface{})["projectIdToCopy"].(string)
if !ok {
return nil, InternalServerError.Send(ctx, "finding projectIdToCopy for copy project operation")
}
opts := getPermissionOpts(projectIdToCopy)
if user.HasPermission(opts) {
return next(ctx)
}
}
if operationContext == DeleteProjectMutation {
projectId, ok := args["projectId"].(string)
if !ok {
return nil, InternalServerError.Send(ctx, "finding projectId for delete project operation")
}
opts := getPermissionOpts(projectId)
if user.HasPermission(opts) {
return next(ctx)
}
}
if operationContext == SetLastRevisionMutation {
projectIdentifier, ok := args["opts"].(map[string]interface{})["projectIdentifier"].(string)
if !ok {
return nil, InternalServerError.Send(ctx, "finding projectIdentifier for set last revision operation")
}
project, err := model.FindBranchProjectRef(projectIdentifier)
if err != nil {
return nil, InternalServerError.Send(ctx, fmt.Sprintf("finding project '%s': %s", projectIdentifier, err.Error()))
}
if project == nil {
return nil, ResourceNotFound.Send(ctx, fmt.Sprintf("project '%s' not found", projectIdentifier))
}
opts := getPermissionOpts(project.Id)
if user.HasPermission(opts) {
return next(ctx)
}
}
return nil, Forbidden.Send(ctx, fmt.Sprintf("user %s does not have permission to access the %s resolver", user.Username(), operationContext))
}
c.Directives.RequireProjectAccess = func(ctx context.Context, obj interface{}, next graphql.Resolver, permission ProjectPermission, access AccessLevel) (interface{}, error) {
user := mustHaveUser(ctx)
args, isMap := obj.(map[string]interface{})
if !isMap {
return nil, InternalServerError.Send(ctx, "converting args into map")
}
requiredPermission, requiredLevel, err := getProjectPermissionLevel(permission, access)
if err != nil {
return nil, InputValidationError.Send(ctx, fmt.Sprintf("invalid permission and access level configuration: %s", err.Error()))
}
paramsMap, err := data.BuildProjectParameterMapForGraphQL(args)
if err != nil {
return nil, InputValidationError.Send(ctx, err.Error())
}
projectId, statusCode, err := data.GetProjectIdFromParams(ctx, paramsMap)
if err != nil {
return nil, mapHTTPStatusToGqlError(ctx, statusCode, err)
}
opts := gimlet.PermissionOpts{
Resource: projectId,
ResourceType: evergreen.ProjectResourceType,
Permission: requiredPermission,
RequiredLevel: requiredLevel,
}
if user.HasPermission(opts) {
return next(ctx)
}
return nil, Forbidden.Send(ctx, fmt.Sprintf("user '%s' does not have permission to access '%s' for the project '%s'", user.Username(), strings.ToLower(permission.String()), projectId))
}
c.Directives.RequireProjectSettingsAccess = func(ctx context.Context, obj interface{}, next graphql.Resolver) (res interface{}, err error) {
user := mustHaveUser(ctx)
projectSettings, isProjectSettings := obj.(*restModel.APIProjectSettings)
if !isProjectSettings {
return nil, InternalServerError.Send(ctx, "project not valid")
}
projectRef := projectSettings.ProjectRef
projectId := utility.FromStringPtr(projectRef.Id)
if projectId == "" {
return nil, ResourceNotFound.Send(ctx, "project not specified")
}
opts := gimlet.PermissionOpts{
Resource: projectId,
ResourceType: evergreen.ProjectResourceType,
Permission: evergreen.PermissionProjectSettings,
RequiredLevel: evergreen.ProjectSettingsView.Value,
}
if user.HasPermission(opts) {
return next(ctx)
}
return nil, Forbidden.Send(ctx, fmt.Sprintf("user does not have permission to access the field '%s' for project with ID '%s'", graphql.GetFieldContext(ctx).Path(), projectId))
}
c.Directives.RequireCommitQueueItemOwner = func(ctx context.Context, obj interface{}, next graphql.Resolver) (interface{}, error) {
usr := mustHaveUser(ctx)
args, isStringMap := obj.(map[string]interface{})
if !isStringMap {
return nil, InternalServerError.Send(ctx, "converting mutation args into map")
}
commitQueueId, hasCommitQueueId := args["commitQueueId"].(string)
if !hasCommitQueueId {
return nil, InputValidationError.Send(ctx, "commit queue id was not provided")
}
issue, hasIssue := args["issue"].(string)
if !hasIssue {
return nil, InputValidationError.Send(ctx, "issue was not provided")
}
project, err := data.FindProjectById(commitQueueId, true, false)
if err != nil {
return nil, InternalServerError.Send(ctx, err.Error())
}
if err = data.CheckCanRemoveCommitQueueItem(ctx, dbConnector, usr, project, issue); err != nil {
gimletErr, ok := err.(gimlet.ErrorResponse)
if ok {
return nil, mapHTTPStatusToGqlError(ctx, gimletErr.StatusCode, err)
}
return nil, InternalServerError.Send(ctx, err.Error())
}
return next(ctx)
}
c.Directives.RedactSecrets = func(ctx context.Context, obj interface{}, next graphql.Resolver) (res interface{}, err error) {
return next(ctx)
}
return c
}