Skip to content

Commit

Permalink
updates users, transformers services
Browse files Browse the repository at this point in the history
  • Loading branch information
nickzelei committed Dec 13, 2024
1 parent 0e0676c commit 2ed2760
Show file tree
Hide file tree
Showing 12 changed files with 360 additions and 220 deletions.
5 changes: 2 additions & 3 deletions backend/internal/cmds/mgmt/serve/connect/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,10 +548,9 @@ func serve(ctx context.Context) error {
db,
tfwfmgr,
connectionService,
useraccountService,
sqlmanager,
jobhookService,
rbacclient,
userdataclient,
)
api.Handle(
mgmtv1alpha1connect.NewJobServiceHandler(
Expand Down Expand Up @@ -605,7 +604,7 @@ func serve(ctx context.Context) error {
PresidioDefaultLanguage: getPresidioDefaultLanguage(),
IsAuthEnabled: isAuthEnabled,
IsNeosyncCloud: ncloudlicense.IsValid(),
}, anonymizerMeter, useraccountService, presAnalyzeClient, presAnonClient, db)
}, anonymizerMeter, userdataclient, useraccountService, presAnalyzeClient, presAnonClient, db)
api.Handle(
mgmtv1alpha1connect.NewAnonymizationServiceHandler(
anonymizationService,
Expand Down
12 changes: 12 additions & 0 deletions backend/internal/ee/rbac/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ func (a AccountAction) String() string {
return string(a)
}

type AccountMemberAction string

const (
AccountMemberAction_Invite AccountMemberAction = "invite"
AccountMemberAction_Delete AccountMemberAction = "delete"
AccountMemberAction_View AccountMemberAction = "view"
)

func (a AccountMemberAction) String() string {
return string(a)
}

type ConnectionAction string

const (
Expand Down
54 changes: 54 additions & 0 deletions backend/internal/ee/rbac/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/casbin/casbin/v2"
mgmtv1alpha1 "github.com/nucleuscloud/neosync/backend/gen/go/protos/mgmt/v1alpha1"
nucleuserrors "github.com/nucleuscloud/neosync/backend/internal/errors"
)

type Rbac struct {
Expand All @@ -26,8 +27,11 @@ type Db interface {

type EntityEnforcer interface {
Job(ctx context.Context, user EntityString, account EntityString, job EntityString, action JobAction) (bool, error)
EnforceJob(ctx context.Context, user EntityString, account EntityString, job EntityString, action JobAction) error
Connection(ctx context.Context, user EntityString, account EntityString, connection EntityString, action ConnectionAction) (bool, error)
EnforceConnection(ctx context.Context, user EntityString, account EntityString, connection EntityString, action ConnectionAction) error
Account(ctx context.Context, user EntityString, account EntityString, action AccountAction) (bool, error)
EnforceAccount(ctx context.Context, user EntityString, account EntityString, action AccountAction) error
}

// Initialize default policies for existing accounts at startup
Expand Down Expand Up @@ -145,6 +149,23 @@ func (r *Rbac) Job(
return r.e.Enforce(user.String(), account.String(), job.String(), action.String())
}

func (r *Rbac) EnforceJob(
ctx context.Context,
user EntityString,
account EntityString,
job EntityString,
action JobAction,
) error {
ok, err := r.Job(ctx, user, account, job, action)
if err != nil {
return err
}
if !ok {
return nucleuserrors.NewForbidden(fmt.Sprintf("user does not have permission to %s job", action))
}
return nil
}

func (r *Rbac) Connection(
ctx context.Context,
user EntityString,
Expand All @@ -155,6 +176,23 @@ func (r *Rbac) Connection(
return r.e.Enforce(user.String(), account.String(), connection.String(), action.String())
}

func (r *Rbac) EnforceConnection(
ctx context.Context,
user EntityString,
account EntityString,
connection EntityString,
action ConnectionAction,
) error {
ok, err := r.Connection(ctx, user, account, connection, action)
if err != nil {
return err
}
if !ok {
return nucleuserrors.NewForbidden(fmt.Sprintf("user does not have permission to %s connection", action))
}
return nil
}

func (r *Rbac) Account(
ctx context.Context,
user EntityString,
Expand All @@ -164,6 +202,22 @@ func (r *Rbac) Account(
return r.e.Enforce(user.String(), account.String(), account.String(), action.String())
}

func (r *Rbac) EnforceAccount(
ctx context.Context,
user EntityString,
account EntityString,
action AccountAction,
) error {
ok, err := r.Account(ctx, user, account, action)
if err != nil {
return err
}
if !ok {
return nucleuserrors.NewForbidden(fmt.Sprintf("user does not have permission to %s account", action))
}
return nil
}

func toRoleName(role mgmtv1alpha1.AccountRole) (string, error) {
switch role {
case mgmtv1alpha1.AccountRole_ACCOUNT_ROLE_ADMIN:
Expand Down
60 changes: 25 additions & 35 deletions backend/internal/userdata/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ type UserEntityEnforcer struct {
}

type DomainEntity interface {
GetId() string
Identifier
GetAccountId() string
}
type DomainEntityImpl struct {
Expand All @@ -98,6 +98,10 @@ type DomainEntityImpl struct {
isWild bool
}

type Identifier interface {
GetId() string
}

func (j *DomainEntityImpl) GetId() string {
return j.id
}
Expand Down Expand Up @@ -127,51 +131,37 @@ func NewDbDomainEntity(accountId, id pgtype.UUID) DomainEntity {
}
}

func (u *UserEntityEnforcer) Job(ctx context.Context, job DomainEntity, action rbac.JobAction) (bool, error) {
if err := u.enforceAccountAccess(ctx, job.GetAccountId()); err != nil {
return false, err
type IdentifierImpl struct {
id string
}

func NewIdentifier(id string) Identifier {
return &IdentifierImpl{
id: id,
}
return u.enforcer.Job(ctx, u.user, rbac.NewAccountIdEntity(job.GetAccountId()), rbac.NewJobIdEntity(job.GetId()), action)
}

func (i *IdentifierImpl) GetId() string {
return i.id
}

func (u *UserEntityEnforcer) EnforceJob(ctx context.Context, job DomainEntity, action rbac.JobAction) error {
ok, err := u.Job(ctx, job, action)
if err != nil {
if err := u.enforceAccountAccess(ctx, job.GetAccountId()); err != nil {
return err
}
if !ok {
return nucleuserrors.NewForbidden(fmt.Sprintf("user does not have permission to %s job", action))
}
return nil
}
func (u *UserEntityEnforcer) Connection(ctx context.Context, connection DomainEntity, action rbac.ConnectionAction) (bool, error) {
if err := u.enforceAccountAccess(ctx, connection.GetAccountId()); err != nil {
return false, err
}
return u.enforcer.Connection(ctx, u.user, rbac.NewAccountIdEntity(connection.GetAccountId()), rbac.NewConnectionIdEntity(connection.GetId()), action)
return u.enforcer.EnforceJob(ctx, u.user, rbac.NewAccountIdEntity(job.GetAccountId()), rbac.NewJobIdEntity(job.GetId()), action)
}

func (u *UserEntityEnforcer) EnforceConnection(ctx context.Context, connection DomainEntity, action rbac.ConnectionAction) error {
ok, err := u.Connection(ctx, connection, action)
if err != nil {
if err := u.enforceAccountAccess(ctx, connection.GetAccountId()); err != nil {
return err
}
if !ok {
return nucleuserrors.NewForbidden(fmt.Sprintf("user does not have permission to %s connection", action))
}
return nil
return u.enforcer.EnforceConnection(ctx, u.user, rbac.NewAccountIdEntity(connection.GetAccountId()), rbac.NewConnectionIdEntity(connection.GetId()), action)
}
func (u *UserEntityEnforcer) Account(ctx context.Context, account *mgmtv1alpha1.UserAccount, action rbac.AccountAction) (bool, error) {

func (u *UserEntityEnforcer) EnforceAccount(ctx context.Context, account Identifier, action rbac.AccountAction) error {
if err := u.enforceAccountAccess(ctx, account.GetId()); err != nil {
return false, err
}
return u.enforcer.Account(ctx, u.user, rbac.NewAccountIdEntity(account.GetId()), action)
}
func (u *UserEntityEnforcer) EnforceAccount(ctx context.Context, account *mgmtv1alpha1.UserAccount, action rbac.AccountAction) error {
ok, err := u.Account(ctx, account, action)
if err != nil {
return err
}
if !ok {
return nucleuserrors.NewForbidden(fmt.Sprintf("user does not have permission to %s account", action))
}
return nil
return u.enforcer.EnforceAccount(ctx, u.user, rbac.NewAccountIdEntity(account.GetId()), action)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"connectrpc.com/connect"
mgmtv1alpha1 "github.com/nucleuscloud/neosync/backend/gen/go/protos/mgmt/v1alpha1"
"github.com/nucleuscloud/neosync/backend/gen/go/protos/mgmt/v1alpha1/mgmtv1alpha1connect"
"github.com/nucleuscloud/neosync/backend/internal/ee/rbac"
nucleuserrors "github.com/nucleuscloud/neosync/backend/internal/errors"
"github.com/nucleuscloud/neosync/backend/internal/userdata"
presidioapi "github.com/nucleuscloud/neosync/internal/ee/presidio"
)

Expand All @@ -26,7 +28,11 @@ func (s *Service) GetTransformPiiEntities(
if s.entityclient == nil {
return nil, nucleuserrors.NewInternalError("entity service is enabled but client was nil.")
}
_, err := s.verifyUserInAccount(ctx, req.Msg.GetAccountId())
user, err := s.userdataclient.GetUser(ctx)
if err != nil {
return nil, err
}
err = user.EnforceJob(ctx, userdata.NewWildcardDomainEntity(req.Msg.GetAccountId()), rbac.JobAction_View)
if err != nil {
return nil, err
}
Expand Down
20 changes: 10 additions & 10 deletions backend/services/mgmt/v1alpha1/transformers-service/service.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package v1alpha1_transformersservice

import (
"github.com/nucleuscloud/neosync/backend/gen/go/protos/mgmt/v1alpha1/mgmtv1alpha1connect"
"github.com/nucleuscloud/neosync/backend/internal/neosyncdb"
"github.com/nucleuscloud/neosync/backend/internal/userdata"
presidioapi "github.com/nucleuscloud/neosync/internal/ee/presidio"
)

type Service struct {
cfg *Config
db *neosyncdb.NeosyncDb
useraccountService mgmtv1alpha1connect.UserAccountServiceClient
entityclient presidioapi.EntityInterface
cfg *Config
db *neosyncdb.NeosyncDb
entityclient presidioapi.EntityInterface
userdataclient userdata.Client
}

type Config struct {
Expand All @@ -21,13 +21,13 @@ type Config struct {
func New(
cfg *Config,
db *neosyncdb.NeosyncDb,
useraccountService mgmtv1alpha1connect.UserAccountServiceClient,
recognizerclient presidioapi.EntityInterface,
userdataclient userdata.Client,
) *Service {
return &Service{
cfg: cfg,
db: db,
useraccountService: useraccountService,
entityclient: recognizerclient,
cfg: cfg,
db: db,
entityclient: recognizerclient,
userdataclient: userdataclient,
}
}

This file was deleted.

Loading

0 comments on commit 2ed2760

Please sign in to comment.