Skip to content

Commit

Permalink
fix: add resource creation logic to authz
Browse files Browse the repository at this point in the history
  • Loading branch information
nsklikas committed Jul 16, 2024
1 parent a8fb9c3 commit c8e3588
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
79 changes: 79 additions & 0 deletions internal/authorization/resource_manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package authorization

import (
"context"
)

func (a *Authorizer) createTuple(ctx context.Context, resourceID string) error {
user, ok := ctx.Value(USER_CTX).(*User)
if !ok {
// Should we panic?
return nil
}
go func() {
err := a.client.WriteTuple(context.Background(), "user:"+user.ID, CAN_VIEW, resourceID)
if err != nil {
a.logger.Errorf("Failed to create authorization tuple: %s", err)
}
}()
return nil
}

func (a *Authorizer) deleteTuple(ctx context.Context, resourceID string) error {
user, ok := ctx.Value(USER_CTX).(*User)
if !ok {
// Should we panic?
return nil
}
go func() {
err := a.client.DeleteTuple(context.Background(), "user:"+user.ID, CAN_VIEW, resourceID)
if err != nil {
a.logger.Errorf("Failed to create authorization tuple: %s", err)
}
}()
return nil
}

func (a *Authorizer) getResource(resourceID, resourceType string) string {
return resourceType + ":" + resourceID
}

func (a *Authorizer) CreateClient(ctx context.Context, clientID string) error {
return a.createTuple(ctx, a.getResource(clientID, CLIENT_TYPE))
}

func (a *Authorizer) DeleteClient(ctx context.Context, clientID string) error {
return a.deleteTuple(ctx, a.getResource(clientID, CLIENT_TYPE))
}

func (a *Authorizer) CreateIdentity(ctx context.Context, IdentityID string) error {
return a.createTuple(ctx, a.getResource(IdentityID, IDENTITY_TYPE))
}

func (a *Authorizer) DeleteIdentity(ctx context.Context, IdentityID string) error {
return a.deleteTuple(ctx, a.getResource(IdentityID, IDENTITY_TYPE))
}

func (a *Authorizer) CreateProvider(ctx context.Context, providerID string) error {
return a.createTuple(ctx, a.getResource(providerID, PROVIDER_TYPE))
}

func (a *Authorizer) DeleteProvider(ctx context.Context, providerID string) error {
return a.deleteTuple(ctx, a.getResource(providerID, PROVIDER_TYPE))
}

func (a *Authorizer) CreateRule(ctx context.Context, ruleID string) error {
return a.createTuple(ctx, a.getResource(ruleID, RULE_TYPE))
}

func (a *Authorizer) DeleteRule(ctx context.Context, ruleID string) error {
return a.deleteTuple(ctx, a.getResource(ruleID, RULE_TYPE))
}

func (a *Authorizer) CreateSchema(ctx context.Context, schemeID string) error {
return a.createTuple(ctx, a.getResource(schemeID, SCHEME_TYPE))
}

func (a *Authorizer) DeleteSchema(ctx context.Context, schemeID string) error {
return a.deleteTuple(ctx, a.getResource(schemeID, SCHEME_TYPE))
}
3 changes: 3 additions & 0 deletions internal/openfga/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ func (c *Client) WriteTuple(ctx context.Context, user, relation, object string)
ctx, span := c.tracer.Start(ctx, "openfga.Client.WriteTuple")
defer span.End()

ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

r := c.c.Write(ctx)
body := client.ClientWriteRequest{
Writes: []openfga.TupleKey{
Expand Down
1 change: 0 additions & 1 deletion pkg/web/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ type OpenFGAClientInterface interface {
type AuthorizerClientInterface interface {
ListObjects(context.Context, string, string, string) ([]string, error)
Check(context.Context, string, string, string, ...ofga.Tuple) (bool, error)
WriteTuple(ctx context.Context, user, relation, object string) error
}

type O11yConfigInterface interface {
Expand Down

0 comments on commit c8e3588

Please sign in to comment.