Skip to content

Commit

Permalink
Merge pull request #1515 from breml/fga-online-data-race
Browse files Browse the repository at this point in the history
incusd/auth: fix FGA online data race
  • Loading branch information
stgraber authored Dec 16, 2024
2 parents eb1d537 + 465af0b commit ed2def1
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions internal/server/auth/driver_openfga.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"slices"
"sync"
"time"

openfga "github.com/openfga/go-sdk"
Expand All @@ -26,7 +27,9 @@ type FGA struct {
apiToken string
storeID string

online bool
onlineMu sync.Mutex
online bool

shutdownCtx context.Context
shutdownCancel context.CancelFunc

Expand Down Expand Up @@ -116,7 +119,10 @@ func (f *FGA) load(ctx context.Context, certificateCache *certificate.Cache, opt
logger.Warn("Connection with OpenFGA established")
}

f.onlineMu.Lock()
defer f.onlineMu.Unlock()
f.online = true

return
}

Expand Down Expand Up @@ -276,6 +282,8 @@ func (f *FGA) CheckPermission(ctx context.Context, r *http.Request, object Objec
}

// If offline, return a clear error to the user.
f.onlineMu.Lock()
defer f.onlineMu.Unlock()
if !f.online {
return api.StatusErrorf(http.StatusForbidden, "The authorization server is currently offline, please try again later")
}
Expand Down Expand Up @@ -881,6 +889,8 @@ func (f *FGA) DeleteStorageBucket(ctx context.Context, projectName string, stora
// updateTuples sends an object update to OpenFGA if it's currently online.
func (f *FGA) updateTuples(ctx context.Context, writes []client.ClientTupleKey, deletions []client.ClientTupleKeyWithoutCondition) error {
// If offline, skip updating as a full sync will happen after connection.
f.onlineMu.Lock()
defer f.onlineMu.Unlock()
if !f.online {
return nil
}
Expand Down Expand Up @@ -1118,7 +1128,6 @@ func (f *FGA) GetInstanceAccess(ctx context.Context, projectName string, instanc
Relation: relation,
UserFilters: userFilters,
}).Execute()

if err != nil {
fgaAPIErr, ok := err.(openfga.FgaApiValidationError)
if !ok || fgaAPIErr.ResponseCode() != openfga.ERRORCODE_RELATION_NOT_FOUND {
Expand Down Expand Up @@ -1173,7 +1182,6 @@ func (f *FGA) GetProjectAccess(ctx context.Context, projectName string) (*api.Ac
Relation: relation,
UserFilters: userFilters,
}).Execute()

if err != nil {
fgaAPIErr, ok := err.(openfga.FgaApiValidationError)
if !ok || fgaAPIErr.ResponseCode() != openfga.ERRORCODE_RELATION_NOT_FOUND {
Expand Down

0 comments on commit ed2def1

Please sign in to comment.