This repository has been archived by the owner on May 16, 2024. It is now read-only.
/
api.go
339 lines (305 loc) · 12.7 KB
/
api.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// The api package defines all of our REST API endpoints.
package api
import (
"context"
"errors"
"net/http"
"github.com/benbjohnson/clock"
registry_types "github.com/common-fate/provider-registry-sdk-go/pkg/providerregistrysdk"
"github.com/common-fate/common-fate/accesshandler/pkg/providerregistry"
"github.com/common-fate/common-fate/accesshandler/pkg/psetup"
ahtypes "github.com/common-fate/common-fate/accesshandler/pkg/types"
"github.com/common-fate/common-fate/pkg/access"
"github.com/common-fate/common-fate/pkg/auth"
"github.com/common-fate/common-fate/pkg/cache"
"github.com/common-fate/common-fate/pkg/deploy"
"github.com/common-fate/common-fate/pkg/gconfig"
"github.com/common-fate/common-fate/pkg/gevent"
"github.com/common-fate/common-fate/pkg/handler"
"github.com/common-fate/common-fate/pkg/identity"
"github.com/common-fate/common-fate/pkg/identity/identitysync"
"github.com/common-fate/common-fate/pkg/providersetup"
"github.com/common-fate/common-fate/pkg/rule"
"github.com/common-fate/common-fate/pkg/service/accesssvc"
"github.com/common-fate/common-fate/pkg/service/cachesvc"
"github.com/common-fate/common-fate/pkg/service/cognitosvc"
"github.com/common-fate/common-fate/pkg/service/healthchecksvc"
"github.com/common-fate/common-fate/pkg/service/requestroutersvc"
"github.com/common-fate/common-fate/pkg/service/handlersvc"
"github.com/common-fate/common-fate/pkg/service/internalidentitysvc"
"github.com/common-fate/common-fate/pkg/service/psetupsvc"
"github.com/common-fate/common-fate/pkg/service/rulesvc"
"github.com/common-fate/common-fate/pkg/service/targetsvc"
"github.com/common-fate/common-fate/pkg/service/workflowsvc"
"github.com/common-fate/common-fate/pkg/service/workflowsvc/runtimes/live"
"github.com/common-fate/common-fate/pkg/target"
"github.com/common-fate/common-fate/pkg/types"
"github.com/common-fate/ddb"
"github.com/go-chi/chi/v5"
"go.uber.org/zap"
)
// API holds all of our API endpoint handlers.
// We use a schema-first approach to ensure that the
// API meets our OpenAPI specification.
//
// To add a new endpoint, follow the below steps:
//
// 1. Edit `openapi.yaml` in this repository.
//
// 2. Run `make generate` to update the generated handler code.
// The code is generated into types.gen.go, and the function
// signatures can be found on the ServerInterface interface.
//
// 3. You'll get a compilation error because API no longer meets
// the ServerInterface interface. The missing function will be your
// new endpoint. Implement the function on API, ensuring that the function
// signature matches the ServerInterface interface.
type API struct {
// DB is the DynamoDB client which provides direct storage access.
DB ddb.Storage
DeploymentConfig deploy.DeployConfigReader
// Requests is the service which provides business logic for Access Requests.
Access AccessService
Rules AccessRuleService
ProviderSetup ProviderSetupService
AccessHandlerClient ahtypes.ClientWithResponsesInterface
AdminGroup string
IdentityProvider string
FrontendURL string
Cache CacheService
IdentitySyncer auth.IdentitySyncer
// Set this to nil if cognito is not configured as the IDP for the deployment
Cognito CognitoService
InternalIdentity InternalIdentityService
TargetService TargetService
HandlerService HandlerService
Workflow Workflow
HealthcheckService HealthcheckService
}
//go:generate go run github.com/golang/mock/mockgen -destination=mocks/mock_cognito_service.go -package=mocks . CognitoService
type CognitoService interface {
AdminCreateUser(ctx context.Context, in cognitosvc.CreateUserOpts) (*identity.User, error)
AdminUpdateUserGroups(ctx context.Context, in cognitosvc.UpdateUserGroupsOpts) (*identity.User, error)
}
//go:generate go run github.com/golang/mock/mockgen -destination=mocks/mock_providersetup_service.go -package=mocks . ProviderSetupService
// ProviderSetupService contains business logic for managing the guided provider setup workflows.
type ProviderSetupService interface {
Create(ctx context.Context, providerType string, existingProviders deploy.ProviderMap, r providerregistry.ProviderRegistry) (*providersetup.Setup, error)
CompleteStep(ctx context.Context, setupID string, stepIndex int, body types.ProviderSetupStepCompleteRequest) (*providersetup.Setup, error)
}
//go:generate go run github.com/golang/mock/mockgen -destination=mocks/mock_access_service.go -package=mocks . AccessService
// RequestServices can create Access Requests.
type AccessService interface {
CreateRequests(ctx context.Context, in accesssvc.CreateRequestsOpts) ([]accesssvc.CreateRequestResult, error)
AddReviewAndGrantAccess(ctx context.Context, opts accesssvc.AddReviewOpts) (*accesssvc.AddReviewResult, error)
CancelRequest(ctx context.Context, opts accesssvc.CancelRequestOpts) error
CreateFavorite(ctx context.Context, in accesssvc.CreateFavoriteOpts) (*access.Favorite, error)
UpdateFavorite(ctx context.Context, in accesssvc.UpdateFavoriteOpts) (*access.Favorite, error)
}
//go:generate go run github.com/golang/mock/mockgen -destination=mocks/mock_accessrule_service.go -package=mocks . AccessRuleService
// AccessRuleService can create and get rules
type AccessRuleService interface {
ArchiveAccessRule(ctx context.Context, userID string, in rule.AccessRule) (*rule.AccessRule, error)
CreateAccessRule(ctx context.Context, userID string, in types.CreateAccessRuleRequest) (*rule.AccessRule, error)
LookupRule(ctx context.Context, opts rulesvc.LookupRuleOpts) ([]rulesvc.LookedUpRule, error)
GetRule(ctx context.Context, ID string, user *identity.User, isAdmin bool) (*rule.GetAccessRuleResponse, error)
UpdateRule(ctx context.Context, in *rulesvc.UpdateOpts) (*rule.AccessRule, error)
RequestArguments(ctx context.Context, accessRuleTarget rule.Target) (map[string]types.RequestArgument, error)
}
type CacheService interface {
RefreshCachedProviderArgOptions(ctx context.Context, providerId string, argId string) (bool, []cache.ProviderOption, []cache.ProviderArgGroupOption, error)
LoadCachedProviderArgOptions(ctx context.Context, providerId string, argId string) (bool, []cache.ProviderOption, []cache.ProviderArgGroupOption, error)
}
//go:generate go run github.com/golang/mock/mockgen -destination=mocks/mock_internalidentity_service.go -package=mocks . InternalIdentityService
type InternalIdentityService interface {
UpdateGroup(ctx context.Context, group identity.Group, in types.CreateGroupRequest) (*identity.Group, error)
CreateGroup(ctx context.Context, in types.CreateGroupRequest) (*identity.Group, error)
UpdateUserGroups(ctx context.Context, user identity.User, groups []string) (*identity.User, error)
DeleteGroup(ctx context.Context, group identity.Group) error
}
//go:generate go run github.com/golang/mock/mockgen -destination=mocks/mock_target_service.go -package=mocks . TargetService
type TargetService interface {
CreateGroup(ctx context.Context, targetGroup types.CreateTargetGroupRequest) (*target.Group, error)
CreateRoute(ctx context.Context, group string, req types.CreateTargetGroupLink) (*target.Route, error)
DeleteGroup(ctx context.Context, group *target.Group) error
}
//go:generate go run github.com/golang/mock/mockgen -destination=mocks/mock_handler_service.go -package=mocks . HandlerService
type HandlerService interface {
RegisterHandler(ctx context.Context, req types.RegisterHandlerRequest) (*handler.Handler, error)
DeleteHandler(ctx context.Context, handler *handler.Handler) error
}
//go:generate go run github.com/golang/mock/mockgen -destination=mocks/mock_workflow_service.go -package=mocks . Workflow
type Workflow interface {
Revoke(ctx context.Context, request access.Request, revokerID string, revokerEmail string) (*access.Request, error)
}
//go:generate go run github.com/golang/mock/mockgen -destination=mocks/mock_healthcheck_service.go -package=mocks . HealthcheckService
type HealthcheckService interface {
Check(ctx context.Context) error
}
// API must meet the generated REST API interface.
var _ types.ServerInterface = &API{}
type Opts struct {
Log *zap.SugaredLogger
AccessHandlerClient ahtypes.ClientWithResponsesInterface
ProviderRegistryClient registry_types.ClientWithResponsesInterface
EventSender *gevent.Sender
IdentitySyncer auth.IdentitySyncer
DeploymentConfig deploy.DeployConfigReader
DynamoTable string
PaginationKMSKeyARN string
AdminGroup string
TemplateData psetup.TemplateData
DeploymentSuffix string
CognitoUserPoolID string
IDPType string
AdminGroupID string
StateMachineARN string
FrontendURL string
}
// New creates a new API.
func New(ctx context.Context, opts Opts) (*API, error) {
if opts.Log == nil {
return nil, errors.New("opts.Log must be provided")
}
if opts.AccessHandlerClient == nil {
return nil, errors.New("AccessHandlerClient must be provided")
}
if opts.ProviderRegistryClient == nil {
return nil, errors.New("ProviderRegistryClient must be provided")
}
tokenizer, err := ddb.NewKMSTokenizer(ctx, opts.PaginationKMSKeyARN)
if err != nil {
return nil, err
}
db, err := ddb.New(ctx, opts.DynamoTable, ddb.WithPageTokenizer(tokenizer))
if err != nil {
return nil, err
}
clk := clock.New()
if err != nil {
return nil, err
}
a := API{
DeploymentConfig: opts.DeploymentConfig,
AdminGroup: opts.AdminGroup,
FrontendURL: opts.FrontendURL,
InternalIdentity: &internalidentitysvc.Service{
DB: db,
Clock: clk,
},
Access: &accesssvc.Service{
Clock: clk,
DB: db,
EventPutter: opts.EventSender,
Cache: &cachesvc.Service{
ProviderConfigReader: opts.DeploymentConfig,
DB: db,
AccessHandlerClient: opts.AccessHandlerClient,
},
Rules: &rulesvc.Service{
Clock: clk,
DB: db,
AHClient: opts.AccessHandlerClient,
Cache: &cachesvc.Service{
ProviderConfigReader: opts.DeploymentConfig,
DB: db,
AccessHandlerClient: opts.AccessHandlerClient,
},
},
AHClient: opts.AccessHandlerClient,
Workflow: &workflowsvc.Service{
Runtime: &live.Runtime{
StateMachineARN: opts.StateMachineARN,
AHClient: opts.AccessHandlerClient,
Eventbus: opts.EventSender,
DB: db,
RequestRouter: &requestroutersvc.Service{
DB: db,
},
},
DB: db,
Clk: clk,
Eventbus: opts.EventSender,
},
},
Cache: &cachesvc.Service{
ProviderConfigReader: opts.DeploymentConfig,
DB: db,
AccessHandlerClient: opts.AccessHandlerClient,
},
Rules: &rulesvc.Service{
Clock: clk,
DB: db,
AHClient: opts.AccessHandlerClient,
Cache: &cachesvc.Service{
ProviderConfigReader: opts.DeploymentConfig,
DB: db,
AccessHandlerClient: opts.AccessHandlerClient,
},
},
ProviderSetup: &psetupsvc.Service{
DB: db,
TemplateData: opts.TemplateData,
DeploymentSuffix: opts.DeploymentSuffix,
},
AccessHandlerClient: opts.AccessHandlerClient,
DB: db,
IdentitySyncer: opts.IdentitySyncer,
IdentityProvider: opts.IDPType,
TargetService: &targetsvc.Service{
DB: db,
Clock: clk,
ProviderRegistryClient: opts.ProviderRegistryClient,
},
HandlerService: &handlersvc.Service{
DB: db,
Clock: clk,
},
Workflow: &workflowsvc.Service{
Runtime: &live.Runtime{
StateMachineARN: opts.StateMachineARN,
AHClient: opts.AccessHandlerClient,
Eventbus: opts.EventSender,
DB: db,
RequestRouter: &requestroutersvc.Service{
DB: db,
},
},
DB: db,
Clk: clk,
Eventbus: opts.EventSender,
},
HealthcheckService: &healthchecksvc.Service{
DB: db,
RuntimeGetter: healthchecksvc.DefaultGetter{},
},
}
// only initialise this if cognito is the IDP
if opts.IDPType == identitysync.IDPTypeCognito {
cog := &identitysync.CognitoSync{}
err = cog.Config().Load(ctx, &gconfig.MapLoader{Values: map[string]string{"userPoolId": opts.CognitoUserPoolID}})
if err != nil {
return nil, err
}
err = cog.Init(ctx)
if err != nil {
return nil, err
}
a.Cognito = &cognitosvc.Service{
Clock: clk,
DB: db,
Syncer: opts.IdentitySyncer,
Cognito: cog,
AdminGroupID: opts.AdminGroupID,
}
}
return &a, nil
}
// Handler returns a HTTP handler.
// Hander doesn't add any middleware. It is the caller's
// responsibility to add any middleware.
func (a *API) Handler(r chi.Router) http.Handler {
return types.HandlerWithOptions(a, types.ChiServerOptions{
BaseRouter: r,
})
}