-
Notifications
You must be signed in to change notification settings - Fork 34
/
service.go
264 lines (232 loc) · 7.25 KB
/
service.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
package resource
import (
"context"
"errors"
"fmt"
"strings"
"github.com/raystack/frontier/core/organization"
"github.com/raystack/frontier/core/authenticate"
"github.com/raystack/frontier/core/project"
"github.com/raystack/frontier/pkg/utils"
"github.com/raystack/frontier/core/relation"
"github.com/raystack/frontier/internal/bootstrap/schema"
)
type RelationService interface {
Create(ctx context.Context, rel relation.Relation) (relation.Relation, error)
CheckPermission(ctx context.Context, rel relation.Relation) (bool, error)
BatchCheckPermission(ctx context.Context, relations []relation.Relation) ([]relation.CheckPair, error)
Delete(ctx context.Context, rel relation.Relation) error
}
type AuthnService interface {
GetPrincipal(ctx context.Context, via ...authenticate.ClientAssertion) (authenticate.Principal, error)
}
type ProjectService interface {
Get(ctx context.Context, idOrName string) (project.Project, error)
}
type OrgService interface {
Get(ctx context.Context, idOrName string) (organization.Organization, error)
}
type Service struct {
repository Repository
configRepository ConfigRepository
relationService RelationService
authnService AuthnService
projectService ProjectService
orgService OrgService
}
func NewService(repository Repository, configRepository ConfigRepository,
relationService RelationService, authnService AuthnService,
projectService ProjectService, orgService OrgService) *Service {
return &Service{
repository: repository,
configRepository: configRepository,
relationService: relationService,
authnService: authnService,
projectService: projectService,
orgService: orgService,
}
}
func (s Service) Get(ctx context.Context, id string) (Resource, error) {
if utils.IsValidUUID(id) {
return s.repository.GetByID(ctx, id)
}
return s.repository.GetByURN(ctx, id)
}
func (s Service) Create(ctx context.Context, res Resource) (Resource, error) {
// TODO(kushsharma): currently we allow users to pass a principal in request which allow
// them to create resource on behalf of other users. Should we only allow this for admins?
principalID := res.PrincipalID
principalType := res.PrincipalType
if strings.TrimSpace(principalID) == "" {
principal, err := s.authnService.GetPrincipal(ctx)
if err != nil {
return Resource{}, err
}
principalID = principal.ID
principalType = principal.Type
}
resourceProject, err := s.projectService.Get(ctx, res.ProjectID)
if err != nil {
return Resource{}, fmt.Errorf("failed to get project: %w", err)
}
newResource, err := s.repository.Create(ctx, Resource{
ID: res.ID,
URN: res.CreateURN(resourceProject.Name),
Name: res.Name,
Title: res.Title,
ProjectID: resourceProject.ID,
NamespaceID: res.NamespaceID,
PrincipalID: principalID,
PrincipalType: principalType,
Metadata: res.Metadata,
})
if err != nil {
return Resource{}, err
}
if err = s.relationService.Delete(ctx, relation.Relation{
Object: relation.Object{
ID: newResource.ID,
Namespace: newResource.NamespaceID,
},
}); err != nil && !errors.Is(err, relation.ErrNotExist) {
return Resource{}, err
}
if err = s.AddProjectToResource(ctx, newResource.ProjectID, newResource); err != nil {
return Resource{}, err
}
if err = s.AddResourceOwner(ctx, newResource); err != nil {
return Resource{}, err
}
return newResource, nil
}
func (s Service) List(ctx context.Context, flt Filter) ([]Resource, error) {
return s.repository.List(ctx, flt)
}
func (s Service) Update(ctx context.Context, resource Resource) (Resource, error) {
return s.repository.Update(ctx, resource)
}
func (s Service) AddProjectToResource(ctx context.Context, projectID string, res Resource) error {
rel := relation.Relation{
Object: relation.Object{
ID: res.ID,
Namespace: res.NamespaceID,
},
Subject: relation.Subject{
ID: projectID,
Namespace: schema.ProjectNamespace,
},
RelationName: schema.ProjectRelationName,
}
if _, err := s.relationService.Create(ctx, rel); err != nil {
return err
}
return nil
}
func (s Service) AddResourceOwner(ctx context.Context, res Resource) error {
if _, err := s.relationService.Create(ctx, relation.Relation{
Object: relation.Object{
ID: res.ID,
Namespace: res.NamespaceID,
},
Subject: relation.Subject{
ID: res.PrincipalID,
Namespace: res.PrincipalType,
},
RelationName: schema.OwnerRelationName,
}); err != nil {
return err
}
return nil
}
func (s Service) CheckAuthz(ctx context.Context, check Check) (bool, error) {
relSubject, err := s.buildRelationSubject(ctx, check.Subject)
if err != nil {
return false, err
}
relObject, err := s.buildRelationObject(ctx, check.Object)
if err != nil {
return false, err
}
return s.relationService.CheckPermission(ctx, relation.Relation{
Subject: relSubject,
Object: relObject,
RelationName: check.Permission,
})
}
func (s Service) buildRelationSubject(ctx context.Context, sub relation.Subject) (relation.Subject, error) {
// use existing if passed in request
if sub.ID != "" && sub.Namespace != "" {
return sub, nil
}
principal, err := s.authnService.GetPrincipal(ctx)
if err != nil {
return relation.Subject{}, err
}
return relation.Subject{
ID: principal.ID,
Namespace: principal.Type,
}, nil
}
func (s Service) buildRelationObject(ctx context.Context, obj relation.Object) (relation.Object, error) {
// a user can pass object name instead of id in the request
// we should convert name to id based on object namespace
if !utils.IsValidUUID(obj.ID) {
if schema.IsSystemNamespace(obj.Namespace) {
if obj.Namespace == schema.ProjectNamespace {
// if object is project, then fetch project by name
project, err := s.projectService.Get(ctx, obj.ID)
if err != nil {
return obj, err
}
obj.ID = project.ID
}
if obj.Namespace == schema.OrganizationNamespace {
// if object is org, then fetch org by name
org, err := s.orgService.Get(ctx, obj.ID)
if err != nil {
return obj, err
}
obj.ID = org.ID
}
} else {
// if not a system namespace it could be a resource, so fetch resource by urn
resource, err := s.Get(ctx, obj.ID)
if err != nil {
return obj, err
}
obj.ID = resource.ID
}
}
return obj, nil
}
func (s Service) BatchCheck(ctx context.Context, checks []Check) ([]relation.CheckPair, error) {
relations := make([]relation.Relation, 0, len(checks))
for _, check := range checks {
// we can parallelize this to speed up the process
relObject, err := s.buildRelationObject(ctx, check.Object)
if err != nil {
return nil, err
}
relSubject, err := s.buildRelationSubject(ctx, check.Subject)
if err != nil {
return nil, err
}
relations = append(relations, relation.Relation{
Subject: relSubject,
Object: relObject,
RelationName: check.Permission,
})
}
return s.relationService.BatchCheckPermission(ctx, relations)
}
func (s Service) Delete(ctx context.Context, namespaceID, id string) error {
if err := s.relationService.Delete(ctx, relation.Relation{
Object: relation.Object{
ID: id,
Namespace: namespaceID,
},
}); err != nil && !errors.Is(err, relation.ErrNotExist) {
return err
}
return s.repository.Delete(ctx, id)
}