@@ -146,7 +146,33 @@ func (f *fga) StopService(ctx context.Context) error {
146
146
147
147
// ApplyPatch is called when an applicable server patch is run, this triggers a model re-upload.
148
148
func (f * fga ) ApplyPatch (ctx context.Context , name string ) error {
149
- // Upload a new model.
149
+ if name == "auth_openfga_viewer" {
150
+ // Add the public access permission if not set.
151
+ resp , err := f .client .Check (ctx ).Body (client.ClientCheckRequest {
152
+ User : "user:*" ,
153
+ Relation : "authenticated" ,
154
+ Object : ObjectServer ().String (),
155
+ }).Execute ()
156
+ if err != nil {
157
+ return err
158
+ }
159
+
160
+ if ! resp .GetAllowed () {
161
+ err = f .sendTuples (ctx , []client.ClientTupleKey {
162
+ {User : "user:*" , Relation : "authenticated" , Object : ObjectServer ().String ()},
163
+ }, nil )
164
+ if err != nil {
165
+ return err
166
+ }
167
+
168
+ // Attempt to clear the former version of this permission.
169
+ _ = f .sendTuples (ctx , nil , []client.ClientTupleKeyWithoutCondition {
170
+ {User : "user:*" , Relation : "viewer" , Object : ObjectServer ().String ()},
171
+ })
172
+ }
173
+ }
174
+
175
+ // Always refresh the model.
150
176
logger .Info ("Refreshing the OpenFGA model" )
151
177
return f .refreshModel (ctx )
152
178
}
@@ -176,10 +202,20 @@ func (f *fga) connect(ctx context.Context, certificateCache *certificate.Cache,
176
202
// Check if we need to upload an initial model.
177
203
if readModelResponse .AuthorizationModel == nil {
178
204
logger .Info ("Upload initial OpenFGA model" )
205
+
206
+ // Upload the model itself.
179
207
err := f .refreshModel (ctx )
180
208
if err != nil {
181
209
return fmt .Errorf ("Failed to load initial model: %w" , err )
182
210
}
211
+
212
+ // Allow basic authenticated access.
213
+ err = f .sendTuples (ctx , []client.ClientTupleKey {
214
+ {User : "user:*" , Relation : "authenticated" , Object : ObjectServer ().String ()},
215
+ }, nil )
216
+ if err != nil {
217
+ return err
218
+ }
183
219
}
184
220
185
221
if opts .resourcesFunc != nil {
@@ -830,6 +866,7 @@ func (f *fga) DeleteStorageBucket(ctx context.Context, projectName string, stora
830
866
return f .updateTuples (ctx , nil , deletions )
831
867
}
832
868
869
+ // updateTuples sends an object update to OpenFGA if it's currently online.
833
870
func (f * fga ) updateTuples (ctx context.Context , writes []client.ClientTupleKey , deletions []client.ClientTupleKeyWithoutCondition ) error {
834
871
// If offline, skip updating as a full sync will happen after connection.
835
872
if ! f .online {
@@ -840,6 +877,11 @@ func (f *fga) updateTuples(ctx context.Context, writes []client.ClientTupleKey,
840
877
return nil
841
878
}
842
879
880
+ return f .sendTuples (ctx , writes , deletions )
881
+ }
882
+
883
+ // sendTuples directly sends the write/deletion tuples to OpenFGA.
884
+ func (f * fga ) sendTuples (ctx context.Context , writes []client.ClientTupleKey , deletions []client.ClientTupleKeyWithoutCondition ) error {
843
885
ctx , cancel := context .WithTimeout (ctx , 10 * time .Second )
844
886
defer cancel ()
845
887
@@ -916,43 +958,10 @@ func (f *fga) projectObjects(ctx context.Context, projectName string) ([]string,
916
958
return allObjects , nil
917
959
}
918
960
919
- func (f * fga ) applyPatches (ctx context.Context ) ([]client. ClientTupleKey , []client. ClientTupleKeyWithoutCondition , error ) {
961
+ func (f * fga ) syncResources (ctx context.Context , resources Resources ) error {
920
962
var writes []client.ClientTupleKey
921
963
var deletions []client.ClientTupleKeyWithoutCondition
922
964
923
- // Add the public access permission if not set.
924
- resp , err := f .client .Check (ctx ).Body (client.ClientCheckRequest {
925
- User : "user:*" ,
926
- Relation : "authenticated" ,
927
- Object : ObjectServer ().String (),
928
- }).Execute ()
929
- if err != nil {
930
- return nil , nil , err
931
- }
932
-
933
- if ! resp .GetAllowed () {
934
- writes = append (writes , client.ClientTupleKey {
935
- User : "user:*" ,
936
- Relation : "authenticated" ,
937
- Object : ObjectServer ().String (),
938
- })
939
-
940
- // Attempt to clear the former version of this permission.
941
- _ = f .updateTuples (ctx , nil , []client.ClientTupleKeyWithoutCondition {
942
- {User : "user:*" , Relation : "viewer" , Object : ObjectServer ().String ()},
943
- })
944
- }
945
-
946
- return writes , deletions , nil
947
- }
948
-
949
- func (f * fga ) syncResources (ctx context.Context , resources Resources ) error {
950
- // Apply model patches.
951
- writes , deletions , err := f .applyPatches (ctx )
952
- if err != nil {
953
- return err
954
- }
955
-
956
965
// Helper function for diffing local objects with those in OpenFGA. These are appended to the writes and deletions
957
966
// slices as appropriate. If the given relation is relationProject, we need to construct a project object for the
958
967
// "user" field. The project is calculated from the object we are inspecting.
0 commit comments