Skip to content

Commit e749c7e

Browse files
authored
add support for iam-runtime relationship events (#231)
Adds support for using the iam-runtime to create relationships, while still maintaining support using the legacy permissions client. Signed-off-by: Mike Mason <[email protected]>
1 parent 818bb30 commit e749c7e

File tree

1 file changed

+60
-13
lines changed

1 file changed

+60
-13
lines changed

entx/template/event_hooks.tmpl

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77

88
{{ $genPackage := base $.Config.Package }}
99

10-
import "go.infratographer.com/permissions-api/pkg/permissions"
10+
import (
11+
"github.com/metal-toolbox/iam-runtime/pkg/iam/runtime/authorization"
12+
"github.com/metal-toolbox/iam-runtime-contrib/iamruntime"
13+
"go.infratographer.com/permissions-api/pkg/permissions"
14+
)
1115

1216
{{- range $node := $.Nodes }}
1317
{{- if $nodeAnnotation := $node.Annotations.INFRA9_EVENTHOOKS }}
@@ -19,7 +23,7 @@
1923
return hook.{{ $node.Name }}Func(func(ctx context.Context, m *generated.{{ $node.Name }}Mutation) (ent.Value, error) {
2024
var err error
2125
additionalSubjects := []gidx.PrefixedID{}
22-
relationships := []events.AuthRelationshipRelation{}
26+
relationships := []*authorization.Relationship{}
2327

2428
objID, ok := m.{{ $node.ID.MutationGet }}()
2529
if !ok {
@@ -56,19 +60,19 @@
5660
additionalSubjects = append(additionalSubjects, {{ $f.Name }})
5761

5862
{{- if $annotation.AdditionalSubjectRelation }}
59-
relationships = append(relationships, events.AuthRelationshipRelation{
63+
relationships = append(relationships, &authorization.Relationship{
6064
Relation: "{{ $annotation.AdditionalSubjectRelation }}",
61-
SubjectID: {{ $f.Name }},
65+
SubjectId: {{ $f.Name }}.String(),
6266
})
6367
{{- end }}
6468
}
6569
{{- else }}
6670
additionalSubjects = append(additionalSubjects, {{ $f.Name }})
6771

6872
{{- if $annotation.AdditionalSubjectRelation }}
69-
relationships = append(relationships, events.AuthRelationshipRelation{
73+
relationships = append(relationships, &authorization.Relationship{
7074
Relation: "{{ $annotation.AdditionalSubjectRelation }}",
71-
SubjectID: {{ $f.Name }},
75+
SubjectId: {{ $f.Name }}.String(),
7276
})
7377
{{- end }}
7478
{{- end }}
@@ -132,7 +136,7 @@
132136
}
133137

134138
if len(relationships) != 0 && m.Op().Is(ent.OpCreate) {
135-
if err := permissions.CreateAuthRelationships(ctx, "{{ $nodeAnnotation.SubjectName }}", objID, relationships...); err != nil {
139+
if err := createAuthRelationships(ctx, "{{ $nodeAnnotation.SubjectName }}", objID, relationships...); err != nil {
136140
return nil, fmt.Errorf("relationship request failed with error: %w", err)
137141
}
138142
}
@@ -151,7 +155,7 @@
151155
func(next ent.Mutator) ent.Mutator {
152156
return hook.{{ $node.Name }}Func(func(ctx context.Context, m *generated.{{ $node.Name }}Mutation) (ent.Value, error) {
153157
additionalSubjects := []gidx.PrefixedID{}
154-
relationships := []events.AuthRelationshipRelation{}
158+
relationships := []*authorization.Relationship{}
155159

156160
objID, ok := m.{{ $node.ID.MutationGet }}()
157161
if !ok {
@@ -172,19 +176,19 @@
172176
additionalSubjects = append(additionalSubjects, dbObj.{{ $f.MutationGet }})
173177

174178
{{- if $annotation.AdditionalSubjectRelation }}
175-
relationships = append(relationships, events.AuthRelationshipRelation{
179+
relationships = append(relationships, &authorization.Relationship{
176180
Relation: "{{ $annotation.AdditionalSubjectRelation }}",
177-
SubjectID: dbObj.{{ $f.MutationGet }},
181+
SubjectId: dbObj.{{ $f.MutationGet }}.String(),
178182
})
179183
{{- end }}
180184
}
181185
{{- else }}
182186
additionalSubjects = append(additionalSubjects, dbObj.{{ $f.MutationGet }})
183187

184188
{{- if $annotation.AdditionalSubjectRelation }}
185-
relationships = append(relationships, events.AuthRelationshipRelation{
189+
relationships = append(relationships, &authorization.Relationship{
186190
Relation: "{{ $annotation.AdditionalSubjectRelation }}",
187-
SubjectID: dbObj.{{ $f.MutationGet }},
191+
SubjectId: dbObj.{{ $f.MutationGet }}.String(),
188192
})
189193
{{- end }}
190194
{{- end }}
@@ -199,7 +203,7 @@
199203
}
200204

201205
if len(relationships) != 0 {
202-
if err := permissions.DeleteAuthRelationships(ctx, "{{ $nodeAnnotation.SubjectName }}", objID, relationships...); err != nil {
206+
if err := deleteAuthRelationships(ctx, "{{ $nodeAnnotation.SubjectName }}", objID, relationships...); err != nil {
203207
return nil, fmt.Errorf("relationship request failed with error: %w", err)
204208
}
205209
}
@@ -248,5 +252,48 @@
248252
}
249253
}
250254

255+
func createAuthRelationships(ctx context.Context, resourceType string, resourceID gidx.PrefixedID, relationships ...*authorization.Relationship) error {
256+
request := &authorization.CreateRelationshipsRequest{
257+
ResourceId: resourceID.String(),
258+
Relationships: relationships,
259+
}
260+
261+
if _, err := iamruntime.ContextCreateRelationships(ctx, request); err == nil || !errors.Is(err, iamruntime.ErrRuntimeNotFound) {
262+
return err
263+
}
264+
265+
eventRelationships := make([]events.AuthRelationshipRelation, len(request.Relationships))
266+
267+
for i, rel := range request.Relationships {
268+
eventRelationships[i] = events.AuthRelationshipRelation{
269+
Relation: rel.Relation,
270+
SubjectID: gidx.PrefixedID(rel.SubjectId),
271+
}
272+
}
273+
274+
return permissions.CreateAuthRelationships(ctx, resourceType, gidx.PrefixedID(request.ResourceId), eventRelationships...)
275+
}
276+
277+
func deleteAuthRelationships(ctx context.Context, resourceType string, resourceID gidx.PrefixedID, relationships ...*authorization.Relationship) error {
278+
request := &authorization.DeleteRelationshipsRequest{
279+
ResourceId: resourceID.String(),
280+
Relationships: relationships,
281+
}
282+
283+
if _, err := iamruntime.ContextDeleteRelationships(ctx, request); err == nil || !errors.Is(err, iamruntime.ErrRuntimeNotFound) {
284+
return err
285+
}
286+
287+
eventRelationships := make([]events.AuthRelationshipRelation, len(request.Relationships))
288+
289+
for i, rel := range request.Relationships {
290+
eventRelationships[i] = events.AuthRelationshipRelation{
291+
Relation: rel.Relation,
292+
SubjectID: gidx.PrefixedID(rel.SubjectId),
293+
}
294+
}
295+
296+
return permissions.DeleteAuthRelationships(ctx, resourceType, gidx.PrefixedID(request.ResourceId), eventRelationships...)
297+
}
251298

252299
{{ end }}

0 commit comments

Comments
 (0)