-
Notifications
You must be signed in to change notification settings - Fork 0
/
fga.go
133 lines (121 loc) · 3.25 KB
/
fga.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
package main
import (
"context"
"fmt"
openfga "github.com/openfga/go-sdk"
"github.com/paulosuzart/fgamanager/db"
"log"
"strings"
"time"
)
func create(ctx context.Context, tupleKey string) {
keyParts := strings.Split(tupleKey, " ")
if len(keyParts) != 3 {
log.Printf("Unable to create tuple %v", tupleKey)
return
}
user := keyParts[0]
relation := keyParts[1]
object := keyParts[2]
key := openfga.NewTupleKey(user, relation, object)
tuple := openfga.NewWriteRequestWrites([]openfga.TupleKey{*key})
err := fga.write(ctx, tuple)
if err != nil {
log.Printf("Error writing tuple: %v", err)
}
}
func deleteMarked(ctx context.Context) {
for {
results := db.Repository.GetMarkedForDeletion()
if results != nil {
for _, tuple := range results {
deleteTuple := openfga.TupleKeyWithoutCondition{
User: tuple.UserType + ":" + tuple.UserId,
Relation: tuple.Relation,
Object: tuple.ObjectType + ":" + tuple.ObjectId,
}
deletes := []openfga.TupleKeyWithoutCondition{deleteTuple}
resp, err := fga.delete(ctx, deletes)
if err != nil && resp.StatusCode != 200 {
log.Printf("Error deleting tuples %v: %v", err, resp)
}
if resp.StatusCode == 400 {
log.Printf("Mark tuple as stale %v", deleteTuple)
db.MarkStale(tuple.TupleKey)
}
}
}
time.Sleep(10 * time.Second)
}
}
func read(ctx context.Context, watchUpdatesChan chan WatchUpdate) {
for {
var lastWatchUpdate *WatchUpdate
token := db.GetContinuationToken(fgaClient.GetConfig().ApiUrl, fgaClient.GetStoreId())
request := fgaClient.OpenFgaApi.ReadChanges(ctx).PageSize(50)
if token != nil {
request = request.ContinuationToken(*token)
}
resp, _, err := request.Execute()
if err != nil {
log.Printf("Failure on change fetch: %v", err)
errStr := fmt.Sprintf("%v", err)
if lastWatchUpdate != nil {
lastWatchUpdate.WatchEnabled = "Error"
watchUpdatesChan <- *lastWatchUpdate
} else {
lastWatchUpdate = &WatchUpdate{
Token: &errStr,
Writes: -1,
Deletes: -1,
WatchEnabled: "true",
}
watchUpdatesChan <- *lastWatchUpdate
continue
}
}
writes := 0
deletes := 0
err = db.Transact(func() {
db.UpsertConnection(db.Connection{
ApiUrl: fgaClient.GetConfig().ApiUrl,
StoreId: fgaClient.GetStoreId(),
ContinuationToken: resp.GetContinuationToken(),
LastSync: time.Now(),
})
for _, c := range resp.GetChanges() {
db.Repository.ApplyChange(c)
if c.GetOperation() == openfga.WRITE {
writes++
} else if c.GetOperation() == openfga.DELETE {
deletes++
}
}
})
if err != nil {
log.Printf("Failure on change fetch: %v", err)
errStr := fmt.Sprintf("%v", err)
if lastWatchUpdate != nil {
lastWatchUpdate.WatchEnabled = "Error"
watchUpdatesChan <- *lastWatchUpdate
} else {
lastWatchUpdate = &WatchUpdate{
Token: &errStr,
Writes: -1,
Deletes: -1,
WatchEnabled: "true",
}
watchUpdatesChan <- *lastWatchUpdate
continue
}
}
lastWatchUpdate = &WatchUpdate{
Token: token,
Writes: writes,
Deletes: deletes,
WatchEnabled: "true",
}
watchUpdatesChan <- *lastWatchUpdate
time.Sleep(2 * time.Second)
}
}