Skip to content

Commit 2769a24

Browse files
authored
Merge pull request #1469 from stgraber/main
Fix TPM fd leaks and OpenFGA patching issue
2 parents 9fd7ac6 + 66baacf commit 2769a24

File tree

3 files changed

+45
-55
lines changed

3 files changed

+45
-55
lines changed

cmd/incusd/patches.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -686,25 +686,6 @@ func patchMoveBackupsInstances(name string, d *Daemon) error {
686686
}
687687

688688
func patchGenericAuthorization(name string, d *Daemon) error {
689-
// Only run authorization patches on the leader.
690-
isLeader := false
691-
692-
leaderAddress, err := d.gateway.LeaderAddress()
693-
if err != nil {
694-
if !errors.Is(err, cluster.ErrNodeIsNotClustered) {
695-
return err
696-
}
697-
698-
isLeader = true
699-
} else if leaderAddress == d.localConfig.ClusterAddress() {
700-
isLeader = true
701-
}
702-
703-
// If clustered and not running on a leader, skip the resource update.
704-
if !isLeader {
705-
return nil
706-
}
707-
708689
return d.authorizer.ApplyPatch(d.shutdownCtx, name)
709690
}
710691

internal/server/auth/driver_openfga.go

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,33 @@ func (f *fga) StopService(ctx context.Context) error {
146146

147147
// ApplyPatch is called when an applicable server patch is run, this triggers a model re-upload.
148148
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.
150176
logger.Info("Refreshing the OpenFGA model")
151177
return f.refreshModel(ctx)
152178
}
@@ -176,10 +202,20 @@ func (f *fga) connect(ctx context.Context, certificateCache *certificate.Cache,
176202
// Check if we need to upload an initial model.
177203
if readModelResponse.AuthorizationModel == nil {
178204
logger.Info("Upload initial OpenFGA model")
205+
206+
// Upload the model itself.
179207
err := f.refreshModel(ctx)
180208
if err != nil {
181209
return fmt.Errorf("Failed to load initial model: %w", err)
182210
}
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+
}
183219
}
184220

185221
if opts.resourcesFunc != nil {
@@ -830,6 +866,7 @@ func (f *fga) DeleteStorageBucket(ctx context.Context, projectName string, stora
830866
return f.updateTuples(ctx, nil, deletions)
831867
}
832868

869+
// updateTuples sends an object update to OpenFGA if it's currently online.
833870
func (f *fga) updateTuples(ctx context.Context, writes []client.ClientTupleKey, deletions []client.ClientTupleKeyWithoutCondition) error {
834871
// If offline, skip updating as a full sync will happen after connection.
835872
if !f.online {
@@ -840,6 +877,11 @@ func (f *fga) updateTuples(ctx context.Context, writes []client.ClientTupleKey,
840877
return nil
841878
}
842879

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 {
843885
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
844886
defer cancel()
845887

@@ -916,43 +958,10 @@ func (f *fga) projectObjects(ctx context.Context, projectName string) ([]string,
916958
return allObjects, nil
917959
}
918960

919-
func (f *fga) applyPatches(ctx context.Context) ([]client.ClientTupleKey, []client.ClientTupleKeyWithoutCondition, error) {
961+
func (f *fga) syncResources(ctx context.Context, resources Resources) error {
920962
var writes []client.ClientTupleKey
921963
var deletions []client.ClientTupleKeyWithoutCondition
922964

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-
956965
// Helper function for diffing local objects with those in OpenFGA. These are appended to the writes and deletions
957966
// slices as appropriate. If the given relation is relationProject, we need to construct a project object for the
958967
// "user" field. The project is calculated from the object we are inspecting.

internal/server/instance/drivers/driver_qemu.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4870,7 +4870,7 @@ func (d *qemu) addTPMDeviceConfig(cfg *[]cfgSection, tpmConfig []deviceConfig.Ru
48704870
}
48714871
}
48724872

4873-
fd, err := unix.Open(socketPath, unix.O_PATH, 0)
4873+
fd, err := unix.Open(socketPath, unix.O_PATH|unix.O_CLOEXEC, 0)
48744874
if err != nil {
48754875
return err
48764876
}

0 commit comments

Comments
 (0)