Skip to content

incusd/auth: fix FGA online data race #1515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading