Skip to content

Commit

Permalink
domains: return role provisioned on create
Browse files Browse the repository at this point in the history
Signed-off-by: Arvindh <[email protected]>
  • Loading branch information
arvindh123 committed Dec 26, 2024
1 parent 1fe7708 commit 094827a
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 37 deletions.
2 changes: 1 addition & 1 deletion domains/api/http/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func createDomainEndpoint(svc domains.Service) endpoint.Endpoint {
Tags: req.Tags,
Alias: req.Alias,
}
domain, err := svc.CreateDomain(ctx, session, d)
domain, _, err := svc.CreateDomain(ctx, session, d)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion domains/domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (page DomainsPage) MarshalJSON() ([]byte, error) {

//go:generate mockery --name Service --output=./mocks --filename service.go --quiet --note "Copyright (c) Abstract Machines"
type Service interface {
CreateDomain(ctx context.Context, sesssion authn.Session, d Domain) (Domain, error)
CreateDomain(ctx context.Context, sesssion authn.Session, d Domain) (Domain, []roles.RoleProvision, error)
RetrieveDomain(ctx context.Context, sesssion authn.Session, id string) (Domain, error)
UpdateDomain(ctx context.Context, sesssion authn.Session, id string, d DomainReq) (Domain, error)
EnableDomain(ctx context.Context, sesssion authn.Session, id string) (Domain, error)
Expand Down
15 changes: 9 additions & 6 deletions domains/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/absmach/supermq/domains"
"github.com/absmach/supermq/pkg/events"
"github.com/absmach/supermq/pkg/roles"
)

const (
Expand Down Expand Up @@ -36,16 +37,18 @@ var (

type createDomainEvent struct {
domains.Domain
rolesProvisioned []roles.RoleProvision
}

func (cde createDomainEvent) Encode() (map[string]interface{}, error) {
val := map[string]interface{}{
"operation": domainCreate,
"id": cde.ID,
"alias": cde.Alias,
"status": cde.Status.String(),
"created_at": cde.CreatedAt,
"created_by": cde.CreatedBy,
"operation": domainCreate,
"id": cde.ID,
"alias": cde.Alias,
"status": cde.Status.String(),
"created_at": cde.CreatedAt,
"created_by": cde.CreatedBy,
"roles_provisioned": cde.rolesProvisioned,
}

if cde.Name != "" {
Expand Down
14 changes: 8 additions & 6 deletions domains/events/streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/absmach/supermq/pkg/authn"
"github.com/absmach/supermq/pkg/events"
"github.com/absmach/supermq/pkg/events/store"
"github.com/absmach/supermq/pkg/roles"
rmEvents "github.com/absmach/supermq/pkg/roles/rolemanager/events"
)

Expand Down Expand Up @@ -40,21 +41,22 @@ func NewEventStoreMiddleware(ctx context.Context, svc domains.Service, url strin
}, nil
}

func (es *eventStore) CreateDomain(ctx context.Context, session authn.Session, domain domains.Domain) (domains.Domain, error) {
domain, err := es.svc.CreateDomain(ctx, session, domain)
func (es *eventStore) CreateDomain(ctx context.Context, session authn.Session, domain domains.Domain) (domains.Domain, []roles.RoleProvision, error) {
domain, rps, err := es.svc.CreateDomain(ctx, session, domain)
if err != nil {
return domain, err
return domain, rps, err
}

event := createDomainEvent{
domain,
Domain: domain,
rolesProvisioned: rps,
}

if err := es.Publish(ctx, event); err != nil {
return domain, err
return domain, rps, err
}

return domain, nil
return domain, rps, nil
}

func (es *eventStore) RetrieveDomain(ctx context.Context, session authn.Session, id string) (domains.Domain, error) {
Expand Down
3 changes: 2 additions & 1 deletion domains/middleware/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/absmach/supermq/pkg/authz"
smqauthz "github.com/absmach/supermq/pkg/authz"
"github.com/absmach/supermq/pkg/policies"
"github.com/absmach/supermq/pkg/roles"
rmMW "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
"github.com/absmach/supermq/pkg/svcutil"
)
Expand Down Expand Up @@ -46,7 +47,7 @@ func AuthorizationMiddleware(entityType string, svc domains.Service, authz smqau
}, nil
}

func (am *authorizationMiddleware) CreateDomain(ctx context.Context, session authn.Session, d domains.Domain) (domains.Domain, error) {
func (am *authorizationMiddleware) CreateDomain(ctx context.Context, session authn.Session, d domains.Domain) (domains.Domain, []roles.RoleProvision, error) {
return am.svc.CreateDomain(ctx, session, d)
}

Expand Down
3 changes: 2 additions & 1 deletion domains/middleware/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/absmach/supermq/domains"
"github.com/absmach/supermq/pkg/authn"
"github.com/absmach/supermq/pkg/roles"
rmMW "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
)

Expand All @@ -33,7 +34,7 @@ func LoggingMiddleware(svc domains.Service, logger *slog.Logger) domains.Service
}
}

func (lm *loggingMiddleware) CreateDomain(ctx context.Context, session authn.Session, d domains.Domain) (do domains.Domain, err error) {
func (lm *loggingMiddleware) CreateDomain(ctx context.Context, session authn.Session, d domains.Domain) (do domains.Domain, rps []roles.RoleProvision, err error) {
defer func(begin time.Time) {
args := []any{
slog.String("duration", time.Since(begin).String()),
Expand Down
3 changes: 2 additions & 1 deletion domains/middleware/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/absmach/supermq/domains"
"github.com/absmach/supermq/pkg/authn"
"github.com/absmach/supermq/pkg/roles"
rmMW "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
"github.com/go-kit/kit/metrics"
)
Expand All @@ -36,7 +37,7 @@ func MetricsMiddleware(svc domains.Service, counter metrics.Counter, latency met
}
}

func (ms *metricsMiddleware) CreateDomain(ctx context.Context, session authn.Session, d domains.Domain) (domains.Domain, error) {
func (ms *metricsMiddleware) CreateDomain(ctx context.Context, session authn.Session, d domains.Domain) (domains.Domain, []roles.RoleProvision, error) {
defer func(begin time.Time) {
ms.counter.With("method", "create_domain").Add(1)
ms.latency.With("method", "create_domain").Observe(time.Since(begin).Seconds())
Expand Down
21 changes: 15 additions & 6 deletions domains/mocks/service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 10 additions & 9 deletions domains/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,30 @@ func New(repo Repository, cache Cache, policy policies.Service, idProvider super
}, nil
}

func (svc service) CreateDomain(ctx context.Context, session authn.Session, d Domain) (do Domain, err error) {
func (svc service) CreateDomain(ctx context.Context, session authn.Session, d Domain) (retDo Domain, retRps []roles.RoleProvision, retErr error) {
d.CreatedBy = session.UserID

domainID, err := svc.idProvider.ID()
if err != nil {
return Domain{}, errors.Wrap(svcerr.ErrCreateEntity, err)
return Domain{}, []roles.RoleProvision{}, errors.Wrap(svcerr.ErrCreateEntity, err)
}
d.ID = domainID

if d.Status != DisabledStatus && d.Status != EnabledStatus {
return Domain{}, svcerr.ErrInvalidStatus
return Domain{}, []roles.RoleProvision{}, svcerr.ErrInvalidStatus
}

d.CreatedAt = time.Now()

// Domain is created in repo first, because Roles table have foreign key relation with Domain ID
dom, err := svc.repo.Save(ctx, d)
if err != nil {
return Domain{}, errors.Wrap(svcerr.ErrCreateEntity, err)
return Domain{}, []roles.RoleProvision{}, errors.Wrap(svcerr.ErrCreateEntity, err)
}
defer func() {
if err != nil {
if retErr != nil {
if errRollBack := svc.repo.Delete(ctx, domainID); errRollBack != nil {
err = errors.Wrap(err, errors.Wrap(errRollbackRepo, errRollBack))
retErr = errors.Wrap(retErr, errors.Wrap(errRollbackRepo, errRollBack))
}
}
}()
Expand All @@ -87,11 +87,12 @@ func (svc service) CreateDomain(ctx context.Context, session authn.Session, d Do
},
}

if _, err := svc.AddNewEntitiesRoles(ctx, domainID, session.UserID, []string{domainID}, optionalPolicies, newBuiltInRoleMembers); err != nil {
return Domain{}, errors.Wrap(errCreateDomainPolicy, err)
rps, err := svc.AddNewEntitiesRoles(ctx, domainID, session.UserID, []string{domainID}, optionalPolicies, newBuiltInRoleMembers)
if err != nil {
return Domain{}, []roles.RoleProvision{}, errors.Wrap(errCreateDomainPolicy, err)
}

return dom, nil
return dom, rps, nil
}

func (svc service) RetrieveDomain(ctx context.Context, session authn.Session, id string) (Domain, error) {
Expand Down
4 changes: 2 additions & 2 deletions domains/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ func TestCreateDomain(t *testing.T) {
t.Run(tc.desc, func(t *testing.T) {
repoCall := drepo.On("Save", mock.Anything, mock.Anything).Return(tc.d, tc.saveDomainErr)
repoCall1 := drepo.On("Delete", mock.Anything, mock.Anything).Return(tc.deleteDomainErr)
repoCall2 := drepo.On("AddRoles", mock.Anything, mock.Anything).Return([]roles.Role{}, tc.addRolesErr)
repoCall2 := drepo.On("AddRoles", mock.Anything, mock.Anything).Return([]roles.RoleProvision{}, tc.addRolesErr)
policyCall := policy.On("AddPolicies", mock.Anything, mock.Anything).Return(tc.addPoliciesErr)
policyCall1 := policy.On("DeletePolicies", mock.Anything, mock.Anything).Return(tc.deletePoliciesErr)
_, err := svc.CreateDomain(context.Background(), tc.session, tc.d)
_, _, err := svc.CreateDomain(context.Background(), tc.session, tc.d)
assert.True(t, errors.Contains(err, tc.err))
repoCall.Unset()
repoCall1.Unset()
Expand Down
3 changes: 2 additions & 1 deletion domains/tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/absmach/supermq/domains"
"github.com/absmach/supermq/pkg/authn"
"github.com/absmach/supermq/pkg/roles"
rmTrace "github.com/absmach/supermq/pkg/roles/rolemanager/tracing"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
Expand All @@ -26,7 +27,7 @@ func New(svc domains.Service, tracer trace.Tracer) domains.Service {
return &tracingMiddleware{tracer, svc, rmTrace.NewRoleManagerTracing("domain", svc, tracer)}
}

func (tm *tracingMiddleware) CreateDomain(ctx context.Context, session authn.Session, d domains.Domain) (domains.Domain, error) {
func (tm *tracingMiddleware) CreateDomain(ctx context.Context, session authn.Session, d domains.Domain) (domains.Domain, []roles.RoleProvision, error) {
ctx, span := tm.tracer.Start(ctx, "create_domain", trace.WithAttributes(
attribute.String("name", d.Name),
))
Expand Down
4 changes: 2 additions & 2 deletions pkg/roles/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ type Role struct {

type RoleProvision struct {
Role
OptionalActions []string `json:"-"`
OptionalMembers []string `json:"-"`
OptionalActions []string `json:"optional_actions"`
OptionalMembers []string `json:"optional_members"`
}

type RolePage struct {
Expand Down

0 comments on commit 094827a

Please sign in to comment.