-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
821 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package commonPerm | ||
|
||
import ( | ||
"common/auth" | ||
"context" | ||
|
||
"github.com/google/uuid" | ||
) | ||
|
||
type User uuid.UUID | ||
|
||
func (t User) Type() string { return "user" } | ||
func (t User) ID() string { return uuid.UUID(t).String() } | ||
|
||
func UserFromCtx(ctx context.Context) User { | ||
userID := auth.MustGetUserID(ctx) | ||
return User(userID) | ||
} | ||
|
||
type Organization uuid.UUID | ||
|
||
func (p Organization) Type() string { return "organization" } | ||
func (p Organization) ID() string { return uuid.UUID(p).String() } | ||
|
||
func OrganizationFromCtx(ctx context.Context) Organization { | ||
organizationID := auth.MustGetOrganizationID(ctx) | ||
return Organization(organizationID) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
services/property-svc/internal/property-set/perm/permission.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package perm | ||
|
||
import ( | ||
"hwauthz" | ||
|
||
"github.com/google/uuid" | ||
) | ||
|
||
// Types | ||
|
||
type PropertySet uuid.UUID | ||
|
||
func (t PropertySet) Type() string { return "property_set" } | ||
func (t PropertySet) ID() string { return uuid.UUID(t).String() } | ||
|
||
// Direct Relations | ||
|
||
const PropertySetOrganization hwauthz.Relation = "organization" | ||
|
||
// Permissions | ||
|
||
const OrganizationCanUserCreatePropertySet hwauthz.Permission = "create_property_set" | ||
|
||
const ( | ||
PropertySetCanUserGet hwauthz.Permission = "get" | ||
) |
86 changes: 86 additions & 0 deletions
86
...roperty-svc/internal/property-set/projections/spiceDBProjection/set_spicedb_projection.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package spiceDBProjection | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"hwauthz" | ||
"hwauthz/commonPerm" | ||
"hwes" | ||
"hwes/eventstoredb/projections/custom" | ||
"hwutil" | ||
|
||
"property-svc/internal/property-set/aggregate" | ||
|
||
"github.com/EventStore/EventStore-Client-Go/v4/esdb" | ||
"github.com/google/uuid" | ||
zlog "github.com/rs/zerolog/log" | ||
|
||
propertySetEventsV1 "property-svc/internal/property-set/events/v1" | ||
"property-svc/internal/property-set/perm" | ||
) | ||
|
||
type Projection struct { | ||
*custom.CustomProjection | ||
authz hwauthz.AuthZ | ||
} | ||
|
||
func NewProjection(es *esdb.Client, serviceName string, authz hwauthz.AuthZ) *Projection { | ||
subscriptionGroupName := serviceName + "-property-set-spicedb-projection" | ||
p := &Projection{ | ||
CustomProjection: custom.NewCustomProjection( | ||
es, | ||
subscriptionGroupName, | ||
&[]string{aggregate.PropertySetAggregateType + "-"}, | ||
), | ||
authz: authz, | ||
} | ||
p.initEventListeners() | ||
return p | ||
} | ||
|
||
func (p *Projection) initEventListeners() { | ||
p.RegisterEventListener(propertySetEventsV1.PropertySetCreated, p.onPropertySetCreated) | ||
} | ||
|
||
func (p *Projection) onPropertySetCreated(ctx context.Context, evt hwes.Event) (error, *esdb.NackAction) { | ||
log := zlog.Ctx(ctx) | ||
|
||
// Parse Values | ||
var payload propertySetEventsV1.PropertySetCreatedEvent | ||
if err := evt.GetJsonData(&payload); err != nil { | ||
log.Error().Err(err).Msg("unmarshal failed") | ||
return err, hwutil.PtrTo(esdb.NackActionPark) | ||
} | ||
|
||
propertySetID, err := uuid.Parse(payload.ID) | ||
if err != nil { | ||
return err, hwutil.PtrTo(esdb.NackActionPark) | ||
} | ||
|
||
if evt.OrganizationID == nil { | ||
return errors.New("organization is missing from event"), hwutil.PtrTo(esdb.NackActionPark) | ||
} | ||
organizationID := *evt.OrganizationID | ||
|
||
relationship := hwauthz.NewRelationship( | ||
commonPerm.Organization(organizationID), | ||
perm.PropertySetOrganization, | ||
perm.PropertySet(propertySetID), | ||
) | ||
|
||
// add to permission graph | ||
_, err = p.authz. | ||
Create(relationship). | ||
Commit(ctx) | ||
if err != nil { | ||
return fmt.Errorf("could not create spice relationship %s: %w", relationship.DebugString(), err), | ||
hwutil.PtrTo(esdb.NackActionRetry) | ||
} | ||
|
||
log.Debug(). | ||
Str("relationship", relationship.DebugString()). | ||
Msg("spice relationship created") | ||
|
||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.