Skip to content

Commit

Permalink
feat: add policy CRUD warpper
Browse files Browse the repository at this point in the history
Signed-off-by: 1998-felix <[email protected]>
  • Loading branch information
felixgateru committed Aug 29, 2024
1 parent a462d17 commit 4bf0eb3
Show file tree
Hide file tree
Showing 17 changed files with 958 additions and 515 deletions.
5 changes: 3 additions & 2 deletions bootstrap/events/producer/streams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/absmach/magistrala/pkg/errors"
svcerr "github.com/absmach/magistrala/pkg/errors/service"
"github.com/absmach/magistrala/pkg/events/store"
policymocks "github.com/absmach/magistrala/pkg/policy/mocks"
mgsdk "github.com/absmach/magistrala/pkg/sdk/go"
sdkmocks "github.com/absmach/magistrala/pkg/sdk/mocks"
"github.com/absmach/magistrala/pkg/uuid"
Expand Down Expand Up @@ -88,14 +89,14 @@ type testVariable struct {
svc bootstrap.Service
boot *mocks.ConfigRepository
auth *authmocks.AuthServiceClient
policy *authmocks.PolicyServiceClient
policy *policymocks.PolicyService
sdk *sdkmocks.SDK
}

func newTestVariable(t *testing.T, redisURL string) testVariable {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthServiceClient)
policy := new(authmocks.PolicyServiceClient)
policy := new(policymocks.PolicyService)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
svc := bootstrap.New(auth, policy, boot, sdk, encKey, idp)
Expand Down
9 changes: 5 additions & 4 deletions bootstrap/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/absmach/magistrala/pkg/errors"
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
svcerr "github.com/absmach/magistrala/pkg/errors/service"
"github.com/absmach/magistrala/pkg/policy"
mgsdk "github.com/absmach/magistrala/pkg/sdk/go"
)

Expand Down Expand Up @@ -120,20 +121,20 @@ type ConfigReader interface {

type bootstrapService struct {
auth magistrala.AuthServiceClient
policy magistrala.PolicyServiceClient
policy policy.PolicyService
configs ConfigRepository
sdk mgsdk.SDK
encKey []byte
idProvider magistrala.IDProvider
}

// New returns new Bootstrap service.
func New(auth magistrala.AuthServiceClient, policy magistrala.PolicyServiceClient, configs ConfigRepository, sdk mgsdk.SDK, encKey []byte, idp magistrala.IDProvider) Service {
func New(auth magistrala.AuthServiceClient, policyService policy.PolicyService, configs ConfigRepository, sdk mgsdk.SDK, encKey []byte, idp magistrala.IDProvider) Service {
return &bootstrapService{
configs: configs,
sdk: sdk,
auth: auth,
policy: policy,
policy: policyService,
encKey: encKey,
idProvider: idp,
}
Expand Down Expand Up @@ -313,7 +314,7 @@ func (bs bootstrapService) listClientIDs(ctx context.Context, userID string) ([]
if err != nil {
return nil, errors.Wrap(svcerr.ErrNotFound, err)
}
return tids.Policies, nil
return tids, nil
}

func (bs bootstrapService) checkSuperAdmin(ctx context.Context, userID string) error {
Expand Down
49 changes: 25 additions & 24 deletions bootstrap/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/absmach/magistrala/internal/testsutil"
"github.com/absmach/magistrala/pkg/errors"
svcerr "github.com/absmach/magistrala/pkg/errors/service"
policymocks "github.com/absmach/magistrala/pkg/policy/mocks"
mgsdk "github.com/absmach/magistrala/pkg/sdk/go"
sdkmocks "github.com/absmach/magistrala/pkg/sdk/mocks"
"github.com/absmach/magistrala/pkg/uuid"
Expand Down Expand Up @@ -77,7 +78,7 @@ func enc(in []byte) ([]byte, error) {
func TestAdd(t *testing.T) {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthServiceClient)
policy := new(authmocks.PolicyServiceClient)
policy := new(policymocks.PolicyService)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
svc := bootstrap.New(auth, policy, boot, sdk, encKey, idp)
Expand Down Expand Up @@ -213,7 +214,7 @@ func TestAdd(t *testing.T) {
func TestView(t *testing.T) {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthServiceClient)
policy := new(authmocks.PolicyServiceClient)
policy := new(policymocks.PolicyService)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
svc := bootstrap.New(auth, policy, boot, sdk, encKey, idp)
Expand Down Expand Up @@ -312,7 +313,7 @@ func TestView(t *testing.T) {
func TestUpdate(t *testing.T) {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthServiceClient)
policy := new(authmocks.PolicyServiceClient)
policy := new(policymocks.PolicyService)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
svc := bootstrap.New(auth, policy, boot, sdk, encKey, idp)
Expand Down Expand Up @@ -404,7 +405,7 @@ func TestUpdate(t *testing.T) {
func TestUpdateCert(t *testing.T) {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthServiceClient)
policy := new(authmocks.PolicyServiceClient)
policy := new(policymocks.PolicyService)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
svc := bootstrap.New(auth, policy, boot, sdk, encKey, idp)
Expand Down Expand Up @@ -520,7 +521,7 @@ func TestUpdateCert(t *testing.T) {
func TestUpdateConnections(t *testing.T) {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthServiceClient)
policy := new(authmocks.PolicyServiceClient)
policy := new(policymocks.PolicyService)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
svc := bootstrap.New(auth, policy, boot, sdk, encKey, idp)
Expand Down Expand Up @@ -640,7 +641,7 @@ func TestUpdateConnections(t *testing.T) {
func TestList(t *testing.T) {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthServiceClient)
policy := new(authmocks.PolicyServiceClient)
policy := new(policymocks.PolicyService)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
svc := bootstrap.New(auth, policy, boot, sdk, encKey, idp)
Expand Down Expand Up @@ -670,7 +671,7 @@ func TestList(t *testing.T) {
domainAdminAuthRes *magistrala.AuthorizeRes
superAdmiAuthErr error
domainAdmiAuthErr error
listObjectsResponse *magistrala.ListObjectsRes
listObjectsResponse []string
authorizeErr error
identifyErr error
listObjectsErr error
Expand All @@ -690,7 +691,7 @@ func TestList(t *testing.T) {
userID: validID,
domainID: domainID,
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: true},
listObjectsResponse: &magistrala.ListObjectsRes{},
listObjectsResponse: []string{},
offset: 0,
limit: 10,
err: nil,
Expand All @@ -703,7 +704,7 @@ func TestList(t *testing.T) {
userID: validID,
domainID: domainID,
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
listObjectsResponse: &magistrala.ListObjectsRes{},
listObjectsResponse: []string{},
offset: 0,
limit: 10,
err: nil,
Expand All @@ -722,7 +723,7 @@ func TestList(t *testing.T) {
domainID: domainID,
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
domainAdminAuthRes: &magistrala.AuthorizeRes{Authorized: true},
listObjectsResponse: &magistrala.ListObjectsRes{},
listObjectsResponse: []string{},
offset: 0,
limit: 10,
err: nil,
Expand All @@ -736,7 +737,7 @@ func TestList(t *testing.T) {
domainID: domainID,
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
domainAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
listObjectsResponse: &magistrala.ListObjectsRes{},
listObjectsResponse: []string{},
offset: 0,
limit: 10,
err: nil,
Expand All @@ -755,7 +756,7 @@ func TestList(t *testing.T) {
domainID: domainID,
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
domainAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
listObjectsResponse: &magistrala.ListObjectsRes{Policies: []string{"test", "test"}},
listObjectsResponse: []string{"test", "test"},
offset: 0,
limit: 10,
err: nil,
Expand Down Expand Up @@ -809,7 +810,7 @@ func TestList(t *testing.T) {
domainID: domainID,
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
domainAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
listObjectsResponse: &magistrala.ListObjectsRes{Policies: []string{"test", "test"}},
listObjectsResponse: []string{"test", "test"},
offset: 0,
limit: 100,
err: nil,
Expand Down Expand Up @@ -890,7 +891,7 @@ func TestList(t *testing.T) {
domainID: domainID,
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
domainAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
listObjectsResponse: &magistrala.ListObjectsRes{Policies: []string{"test", "test"}},
listObjectsResponse: []string{"test", "test"},
offset: 95,
limit: 10,
err: nil,
Expand Down Expand Up @@ -945,7 +946,7 @@ func TestList(t *testing.T) {
domainID: domainID,
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
domainAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
listObjectsResponse: &magistrala.ListObjectsRes{Policies: []string{"test", "test"}},
listObjectsResponse: []string{"test", "test"},
offset: 35,
limit: 20,
err: nil,
Expand All @@ -961,7 +962,7 @@ func TestList(t *testing.T) {
domainID: domainID,
superAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
domainAdminAuthRes: &magistrala.AuthorizeRes{Authorized: false},
listObjectsResponse: &magistrala.ListObjectsRes{},
listObjectsResponse: []string{},
listObjectsErr: svcerr.ErrNotFound,
err: svcerr.ErrNotFound,
},
Expand Down Expand Up @@ -1007,7 +1008,7 @@ func TestList(t *testing.T) {
func TestRemove(t *testing.T) {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthServiceClient)
policy := new(authmocks.PolicyServiceClient)
policy := new(policymocks.PolicyService)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
svc := bootstrap.New(auth, policy, boot, sdk, encKey, idp)
Expand Down Expand Up @@ -1096,7 +1097,7 @@ func TestRemove(t *testing.T) {
func TestBootstrap(t *testing.T) {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthServiceClient)
policy := new(authmocks.PolicyServiceClient)
policy := new(policymocks.PolicyService)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
svc := bootstrap.New(auth, policy, boot, sdk, encKey, idp)
Expand Down Expand Up @@ -1169,7 +1170,7 @@ func TestBootstrap(t *testing.T) {
func TestChangeState(t *testing.T) {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthServiceClient)
policy := new(authmocks.PolicyServiceClient)
policy := new(policymocks.PolicyService)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
svc := bootstrap.New(auth, policy, boot, sdk, encKey, idp)
Expand Down Expand Up @@ -1275,7 +1276,7 @@ func TestChangeState(t *testing.T) {
func TestUpdateChannelHandler(t *testing.T) {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthServiceClient)
policy := new(authmocks.PolicyServiceClient)
policy := new(policymocks.PolicyService)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
svc := bootstrap.New(auth, policy, boot, sdk, encKey, idp)
Expand Down Expand Up @@ -1314,7 +1315,7 @@ func TestUpdateChannelHandler(t *testing.T) {
func TestRemoveChannelHandler(t *testing.T) {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthServiceClient)
policy := new(authmocks.PolicyServiceClient)
policy := new(policymocks.PolicyService)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
svc := bootstrap.New(auth, policy, boot, sdk, encKey, idp)
Expand Down Expand Up @@ -1347,7 +1348,7 @@ func TestRemoveChannelHandler(t *testing.T) {
func TestRemoveConfigHandler(t *testing.T) {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthServiceClient)
policy := new(authmocks.PolicyServiceClient)
policy := new(policymocks.PolicyService)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
svc := bootstrap.New(auth, policy, boot, sdk, encKey, idp)
Expand Down Expand Up @@ -1380,7 +1381,7 @@ func TestRemoveConfigHandler(t *testing.T) {
func TestConnectThingsHandler(t *testing.T) {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthServiceClient)
policy := new(authmocks.PolicyServiceClient)
policy := new(policymocks.PolicyService)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
svc := bootstrap.New(auth, policy, boot, sdk, encKey, idp)
Expand Down Expand Up @@ -1416,7 +1417,7 @@ func TestConnectThingsHandler(t *testing.T) {
func TestDisconnectThingsHandler(t *testing.T) {
boot := new(mocks.ConfigRepository)
auth := new(authmocks.AuthServiceClient)
policy := new(authmocks.PolicyServiceClient)
policy := new(policymocks.PolicyService)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
svc := bootstrap.New(auth, policy, boot, sdk, encKey, idp)
Expand Down
5 changes: 4 additions & 1 deletion cmd/bootstrap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/absmach/magistrala/bootstrap/events/producer"
bootstrappg "github.com/absmach/magistrala/bootstrap/postgres"
"github.com/absmach/magistrala/bootstrap/tracing"
mgpolicy "github.com/absmach/magistrala/internal/policy"
mglog "github.com/absmach/magistrala/logger"
"github.com/absmach/magistrala/pkg/events"
"github.com/absmach/magistrala/pkg/events/store"
Expand Down Expand Up @@ -192,7 +193,9 @@ func newService(ctx context.Context, authClient magistrala.AuthServiceClient, po
sdk := mgsdk.NewSDK(config)
idp := uuid.New()

svc := bootstrap.New(authClient, policyClient, repoConfig, sdk, []byte(cfg.EncKey), idp)
policyService := mgpolicy.NewService(policyClient)

svc := bootstrap.New(authClient, policyService, repoConfig, sdk, []byte(cfg.EncKey), idp)

publisher, err := store.NewPublisher(ctx, cfg.ESURL, streamID)
if err != nil {
Expand Down
7 changes: 5 additions & 2 deletions cmd/things/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
gevents "github.com/absmach/magistrala/internal/groups/events"
gpostgres "github.com/absmach/magistrala/internal/groups/postgres"
gtracing "github.com/absmach/magistrala/internal/groups/tracing"
mgpolicy "github.com/absmach/magistrala/internal/policy"
mglog "github.com/absmach/magistrala/logger"
"github.com/absmach/magistrala/pkg/groups"
"github.com/absmach/magistrala/pkg/grpcclient"
Expand Down Expand Up @@ -242,8 +243,10 @@ func newService(ctx context.Context, db *sqlx.DB, dbConfig pgclient.Config, auth

thingCache := thcache.NewCache(cacheClient, keyDuration)

csvc := things.NewService(authClient, policyClient, cRepo, gRepo, thingCache, idp)
gsvc := mggroups.NewService(gRepo, idp, authClient, policyClient)
policyService := mgpolicy.NewService(policyClient)

csvc := things.NewService(authClient, policyService, cRepo, gRepo, thingCache, idp)
gsvc := mggroups.NewService(gRepo, idp, authClient, policyService)

csvc, err := thevents.NewEventStoreMiddleware(ctx, csvc, esURL)
if err != nil {
Expand Down
18 changes: 11 additions & 7 deletions cmd/users/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
gevents "github.com/absmach/magistrala/internal/groups/events"
gpostgres "github.com/absmach/magistrala/internal/groups/postgres"
gtracing "github.com/absmach/magistrala/internal/groups/tracing"
mgpolicy "github.com/absmach/magistrala/internal/policy"
mglog "github.com/absmach/magistrala/logger"
mgclients "github.com/absmach/magistrala/pkg/clients"
svcerr "github.com/absmach/magistrala/pkg/errors/service"
Expand All @@ -31,6 +32,7 @@ import (
jaegerclient "github.com/absmach/magistrala/pkg/jaeger"
"github.com/absmach/magistrala/pkg/oauth2"
googleoauth "github.com/absmach/magistrala/pkg/oauth2/google"
"github.com/absmach/magistrala/pkg/policy"
"github.com/absmach/magistrala/pkg/postgres"
pgclient "github.com/absmach/magistrala/pkg/postgres"
"github.com/absmach/magistrala/pkg/prometheus"
Expand Down Expand Up @@ -230,8 +232,10 @@ func newService(ctx context.Context, authClient magistrala.AuthServiceClient, po
logger.Error(fmt.Sprintf("failed to configure e-mailing util: %s", err.Error()))
}

csvc := users.NewService(cRepo, authClient, policyClient, emailerClient, hsr, idp, c.SelfRegister)
gsvc := mggroups.NewService(gRepo, idp, authClient, policyClient)
policyService := mgpolicy.NewService(policyClient)

csvc := users.NewService(cRepo, authClient, policyService, emailerClient, hsr, idp, c.SelfRegister)
gsvc := mggroups.NewService(gRepo, idp, authClient, policyService)

csvc, err = uevents.NewEventStoreMiddleware(ctx, csvc, c.ESURL)
if err != nil {
Expand All @@ -256,11 +260,11 @@ func newService(ctx context.Context, authClient magistrala.AuthServiceClient, po
if err != nil {
logger.Error(fmt.Sprintf("failed to create admin client: %s", err))
}
if err := createAdminPolicy(ctx, clientID, authClient, policyClient); err != nil {
if err := createAdminPolicy(ctx, clientID, authClient, policyService); err != nil {
return nil, nil, err
}

users.NewDeleteHandler(ctx, cRepo, policyClient, c.DeleteInterval, c.DeleteAfter, logger)
users.NewDeleteHandler(ctx, cRepo, policyService, c.DeleteInterval, c.DeleteAfter, logger)

return csvc, gsvc, err
}
Expand Down Expand Up @@ -305,7 +309,7 @@ func createAdmin(ctx context.Context, c config, crepo clientspg.Repository, hsr
return client.ID, nil
}

func createAdminPolicy(ctx context.Context, clientID string, authClient magistrala.AuthServiceClient, policyClient magistrala.PolicyServiceClient) error {
func createAdminPolicy(ctx context.Context, clientID string, authClient magistrala.AuthServiceClient, policyService policy.PolicyService) error {
res, err := authClient.Authorize(ctx, &magistrala.AuthorizeReq{
SubjectType: authSvc.UserType,
Subject: clientID,
Expand All @@ -314,7 +318,7 @@ func createAdminPolicy(ctx context.Context, clientID string, authClient magistra
ObjectType: authSvc.PlatformType,
})
if err != nil || !res.Authorized {
addPolicyRes, err := policyClient.AddPolicy(ctx, &magistrala.AddPolicyReq{
addPolicyRes, err := policyService.AddPolicy(ctx, &magistrala.AddPolicyReq{
SubjectType: authSvc.UserType,
Subject: clientID,
Relation: authSvc.AdministratorRelation,
Expand All @@ -324,7 +328,7 @@ func createAdminPolicy(ctx context.Context, clientID string, authClient magistra
if err != nil {
return err
}
if !addPolicyRes.Added {
if !addPolicyRes {
return svcerr.ErrAuthorization
}
}
Expand Down
Loading

0 comments on commit 4bf0eb3

Please sign in to comment.