Skip to content

Commit

Permalink
feat: logging fail user fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
FemiNoviaLina committed Apr 18, 2024
1 parent f0509b2 commit a9ccd37
Show file tree
Hide file tree
Showing 15 changed files with 93 additions and 120 deletions.
35 changes: 15 additions & 20 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"github.com/goto/shield/pkg/db"

"github.com/goto/salt/log"
grpczap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
"github.com/pkg/profile"
"google.golang.org/grpc/codes"
)
Expand Down Expand Up @@ -94,26 +93,24 @@ func StartServer(logger *log.Zap, cfg *config.Shield) error {
schemaMigrationConfig := schema.NewSchemaMigrationConfig(cfg.App.DefaultSystemEmail)

//
serviceLogger := grpczap.Extract(ctx).Sugar()

activityRepository := postgres.NewActivityRepository(dbClient)
activityService := activity.NewService(activityRepository)

userRepository := postgres.NewUserRepository(dbClient)
userService := user.NewService(serviceLogger, userRepository, activityService)
userService := user.NewService(logger, userRepository, activityService)

actionRepository := postgres.NewActionRepository(dbClient)
actionService := action.NewService(serviceLogger, actionRepository, userService, activityService)
actionService := action.NewService(logger, actionRepository, userService, activityService)

roleRepository := postgres.NewRoleRepository(dbClient)
roleService := role.NewService(serviceLogger, roleRepository, userService, activityService)
roleService := role.NewService(logger, roleRepository, userService, activityService)

policyPGRepository := postgres.NewPolicyRepository(dbClient)
policySpiceRepository := spicedb.NewPolicyRepository(spiceDBClient)
policyService := policy.NewService(serviceLogger, policyPGRepository, userService, activityService)
policyService := policy.NewService(logger, policyPGRepository, userService, activityService)

namespaceRepository := postgres.NewNamespaceRepository(dbClient)
namespaceService := namespace.NewService(serviceLogger, namespaceRepository, userService, activityService)
namespaceService := namespace.NewService(logger, namespaceRepository, userService, activityService)

s := schema.NewSchemaMigrationService(
blob.NewSchemaConfigRepository(resourceBlobFS),
Expand Down Expand Up @@ -173,42 +170,40 @@ func BuildAPIDependencies(
dbc *db.Client,
sdb *spicedb.SpiceDB,
) (api.Deps, error) {
serviceLogger := grpczap.Extract(ctx).Sugar()

activityRepository := postgres.NewActivityRepository(dbc)
activityService := activity.NewService(activityRepository)

userRepository := postgres.NewUserRepository(dbc)
userService := user.NewService(serviceLogger, userRepository, activityService)
userService := user.NewService(logger, userRepository, activityService)

actionRepository := postgres.NewActionRepository(dbc)
actionService := action.NewService(serviceLogger, actionRepository, userService, activityService)
actionService := action.NewService(logger, actionRepository, userService, activityService)

namespaceRepository := postgres.NewNamespaceRepository(dbc)
namespaceService := namespace.NewService(serviceLogger, namespaceRepository, userService, activityService)
namespaceService := namespace.NewService(logger, namespaceRepository, userService, activityService)

roleRepository := postgres.NewRoleRepository(dbc)
roleService := role.NewService(serviceLogger, roleRepository, userService, activityService)
roleService := role.NewService(logger, roleRepository, userService, activityService)

relationPGRepository := postgres.NewRelationRepository(dbc)
relationSpiceRepository := spicedb.NewRelationRepository(sdb)
relationService := relation.NewService(serviceLogger, relationPGRepository, relationSpiceRepository, userService, activityService)
relationService := relation.NewService(logger, relationPGRepository, relationSpiceRepository, userService, activityService)

groupRepository := postgres.NewGroupRepository(dbc)
groupService := group.NewService(serviceLogger, groupRepository, relationService, userService, activityService)
groupService := group.NewService(logger, groupRepository, relationService, userService, activityService)

organizationRepository := postgres.NewOrganizationRepository(dbc)
organizationService := organization.NewService(serviceLogger, organizationRepository, relationService, userService, activityService)
organizationService := organization.NewService(logger, organizationRepository, relationService, userService, activityService)

projectRepository := postgres.NewProjectRepository(dbc)
projectService := project.NewService(serviceLogger, projectRepository, relationService, userService, activityService)
projectService := project.NewService(logger, projectRepository, relationService, userService, activityService)

policyPGRepository := postgres.NewPolicyRepository(dbc)
policyService := policy.NewService(serviceLogger, policyPGRepository, userService, activityService)
policyService := policy.NewService(logger, policyPGRepository, userService, activityService)

resourcePGRepository := postgres.NewResourceRepository(dbc)
resourceService := resource.NewService(
serviceLogger, resourcePGRepository, resourceBlobRepository, relationService, userService, projectService, organizationService, groupService, activityService)
logger, resourcePGRepository, resourceBlobRepository, relationService, userService, projectService, organizationService, groupService, activityService)

relationAdapter := adapter.NewRelation(groupService, userService, relationService)

Expand Down
14 changes: 7 additions & 7 deletions core/action/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"context"
"fmt"

"github.com/goto/salt/log"
"github.com/goto/shield/core/user"
pkgctx "github.com/goto/shield/pkg/context"
"go.uber.org/zap"
)

const (
Expand All @@ -23,13 +23,13 @@ type ActivityService interface {
}

type Service struct {
logger *zap.SugaredLogger
logger log.Logger
repository Repository
userService UserService
activityService ActivityService
}

func NewService(logger *zap.SugaredLogger, repository Repository, userService UserService, activityService ActivityService) *Service {
func NewService(logger log.Logger, repository Repository, userService UserService, activityService ActivityService) *Service {
return &Service{
logger: logger,
repository: repository,
Expand All @@ -45,7 +45,7 @@ func (s Service) Get(ctx context.Context, id string) (Action, error) {
func (s Service) Create(ctx context.Context, action Action) (Action, error) {
currentUser, err := s.userService.FetchCurrentUser(ctx)
if err != nil {
return Action{}, fmt.Errorf("%w: %s", user.ErrInvalidEmail, err.Error())
s.logger.Error(fmt.Sprintf("%s: %s", user.ErrInvalidEmail.Error(), err.Error()))
}

newAction, err := s.repository.Create(ctx, action)
Expand All @@ -57,7 +57,7 @@ func (s Service) Create(ctx context.Context, action Action) (Action, error) {
ctx := pkgctx.WithoutCancel(ctx)
actionLogData := newAction.ToActionLogData()
if err := s.activityService.Log(ctx, auditKeyActionCreate, currentUser.ID, actionLogData); err != nil {
s.logger.Errorf("%s: %s", ErrLogActivity.Error(), err.Error())
s.logger.Error(fmt.Sprintf("%s: %s", ErrLogActivity.Error(), err.Error()))
}
}()

Expand All @@ -71,7 +71,7 @@ func (s Service) List(ctx context.Context) ([]Action, error) {
func (s Service) Update(ctx context.Context, id string, action Action) (Action, error) {
currentUser, err := s.userService.FetchCurrentUser(ctx)
if err != nil {
return Action{}, fmt.Errorf("%w: %s", user.ErrInvalidEmail, err.Error())
s.logger.Error(fmt.Sprintf("%s: %s", user.ErrInvalidEmail.Error(), err.Error()))
}

updatedAction, err := s.repository.Update(ctx, Action{
Expand All @@ -87,7 +87,7 @@ func (s Service) Update(ctx context.Context, id string, action Action) (Action,
ctx := pkgctx.WithoutCancel(ctx)
actionLogData := updatedAction.ToActionLogData()
if err := s.activityService.Log(ctx, auditKeyActionUpdate, currentUser.ID, actionLogData); err != nil {
s.logger.Errorf("%s: %s", ErrLogActivity.Error(), err.Error())
s.logger.Error(fmt.Sprintf("%s: %s", ErrLogActivity.Error(), err.Error()))
}
}()

Expand Down
12 changes: 6 additions & 6 deletions core/group/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strings"

"github.com/goto/salt/log"
"github.com/goto/shield/core/action"
"github.com/goto/shield/core/namespace"
"github.com/goto/shield/core/relation"
Expand All @@ -13,7 +14,6 @@ import (
pkgctx "github.com/goto/shield/pkg/context"
"github.com/goto/shield/pkg/str"
"github.com/goto/shield/pkg/uuid"
"go.uber.org/zap"
)

const (
Expand All @@ -38,14 +38,14 @@ type ActivityService interface {
}

type Service struct {
logger *zap.SugaredLogger
logger log.Logger
repository Repository
relationService RelationService
userService UserService
activityService ActivityService
}

func NewService(logger *zap.SugaredLogger, repository Repository, relationService RelationService, userService UserService, activityService ActivityService) *Service {
func NewService(logger log.Logger, repository Repository, relationService RelationService, userService UserService, activityService ActivityService) *Service {
return &Service{
logger: logger,
repository: repository,
Expand Down Expand Up @@ -74,7 +74,7 @@ func (s Service) Create(ctx context.Context, grp Group) (Group, error) {
ctx := pkgctx.WithoutCancel(ctx)
groupLogData := newGroup.ToGroupLogData()
if err := s.activityService.Log(ctx, auditKeyGroupCreate, currentUser.ID, groupLogData); err != nil {
s.logger.Errorf("%s: %s", ErrLogActivity.Error(), err.Error())
s.logger.Error(fmt.Sprintf("%s: %s", ErrLogActivity.Error(), err.Error()))
}
}()

Expand Down Expand Up @@ -103,7 +103,7 @@ func (s Service) List(ctx context.Context, flt Filter) ([]Group, error) {
func (s Service) Update(ctx context.Context, grp Group) (Group, error) {
currentUser, err := s.userService.FetchCurrentUser(ctx)
if err != nil {
return Group{}, fmt.Errorf("%w: %s", user.ErrInvalidEmail, err.Error())
s.logger.Error(fmt.Sprintf("%s: %s", user.ErrInvalidEmail.Error(), err.Error()))
}

if strings.TrimSpace(grp.ID) != "" {
Expand All @@ -119,7 +119,7 @@ func (s Service) Update(ctx context.Context, grp Group) (Group, error) {
ctx := pkgctx.WithoutCancel(ctx)
groupLogData := updatedGroup.ToGroupLogData()
if err := s.activityService.Log(ctx, auditKeyGroupUpdate, currentUser.ID, groupLogData); err != nil {
s.logger.Errorf("%s: %s", ErrLogActivity.Error(), err.Error())
s.logger.Error(fmt.Sprintf("%s: %s", ErrLogActivity.Error(), err.Error()))
}
}()

Expand Down
14 changes: 7 additions & 7 deletions core/namespace/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"context"
"fmt"

"github.com/goto/salt/log"
"github.com/goto/shield/core/user"
pkgctx "github.com/goto/shield/pkg/context"
"go.uber.org/zap"
)

const (
Expand All @@ -23,13 +23,13 @@ type ActivityService interface {
}

type Service struct {
logger *zap.SugaredLogger
logger log.Logger
repository Repository
userService UserService
activityService ActivityService
}

func NewService(logger *zap.SugaredLogger, repository Repository, userService UserService, activityService ActivityService) *Service {
func NewService(logger log.Logger, repository Repository, userService UserService, activityService ActivityService) *Service {
return &Service{
logger: logger,
repository: repository,
Expand All @@ -45,7 +45,7 @@ func (s Service) Get(ctx context.Context, id string) (Namespace, error) {
func (s Service) Create(ctx context.Context, ns Namespace) (Namespace, error) {
currentUser, err := s.userService.FetchCurrentUser(ctx)
if err != nil {
return Namespace{}, fmt.Errorf("%w: %s", user.ErrInvalidEmail, err.Error())
s.logger.Error(fmt.Sprintf("%s: %s", user.ErrInvalidEmail.Error(), err.Error()))
}

newNamespace, err := s.repository.Create(ctx, ns)
Expand All @@ -57,7 +57,7 @@ func (s Service) Create(ctx context.Context, ns Namespace) (Namespace, error) {
ctx := pkgctx.WithoutCancel(ctx)
namespaceLogData := newNamespace.ToNameSpaceLogData()
if err := s.activityService.Log(ctx, auditKeyNamespaceCreate, currentUser.ID, namespaceLogData); err != nil {
s.logger.Errorf("%s: %s", ErrLogActivity.Error(), err.Error())
s.logger.Error(fmt.Sprintf("%s: %s", ErrLogActivity.Error(), err.Error()))
}
}()

Expand All @@ -71,7 +71,7 @@ func (s Service) List(ctx context.Context) ([]Namespace, error) {
func (s Service) Update(ctx context.Context, ns Namespace) (Namespace, error) {
currentUser, err := s.userService.FetchCurrentUser(ctx)
if err != nil {
return Namespace{}, fmt.Errorf("%w: %s", user.ErrInvalidEmail, err.Error())
s.logger.Error(fmt.Sprintf("%s: %s", user.ErrInvalidEmail.Error(), err.Error()))
}

updatedNamespace, err := s.repository.Update(ctx, ns)
Expand All @@ -83,7 +83,7 @@ func (s Service) Update(ctx context.Context, ns Namespace) (Namespace, error) {
ctx := pkgctx.WithoutCancel(ctx)
namespaceLogData := updatedNamespace.ToNameSpaceLogData()
if err := s.activityService.Log(ctx, auditKeyNamespaceUpdate, currentUser.ID, namespaceLogData); err != nil {
s.logger.Errorf("%s: %s", ErrLogActivity.Error(), err.Error())
s.logger.Error(fmt.Sprintf("%s: %s", ErrLogActivity.Error(), err.Error()))
}
}()

Expand Down
19 changes: 11 additions & 8 deletions core/organization/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"context"
"fmt"

"github.com/goto/salt/log"
"github.com/goto/shield/core/action"
"github.com/goto/shield/core/namespace"
"github.com/goto/shield/core/relation"
"github.com/goto/shield/core/user"
"github.com/goto/shield/internal/schema"
pkgctx "github.com/goto/shield/pkg/context"
"github.com/goto/shield/pkg/uuid"
"go.uber.org/zap"
)

const (
Expand All @@ -36,14 +36,14 @@ type ActivityService interface {
}

type Service struct {
logger *zap.SugaredLogger
logger log.Logger
repository Repository
relationService RelationService
userService UserService
activityService ActivityService
}

func NewService(logger *zap.SugaredLogger, repository Repository, relationService RelationService, userService UserService, activityService ActivityService) *Service {
func NewService(logger log.Logger, repository Repository, relationService RelationService, userService UserService, activityService ActivityService) *Service {
return &Service{
logger: logger,
repository: repository,
Expand Down Expand Up @@ -83,7 +83,7 @@ func (s Service) Create(ctx context.Context, org Organization) (Organization, er
ctx := pkgctx.WithoutCancel(ctx)
organizationLogData := newOrg.ToOrganizationLogData()
if err := s.activityService.Log(ctx, auditKeyOrganizationCreate, currentUser.ID, organizationLogData); err != nil {
s.logger.Errorf("%s: %s", ErrLogActivity.Error(), err.Error())
s.logger.Error(fmt.Sprintf("%s: %s", ErrLogActivity.Error(), err.Error()))
}
}()

Expand All @@ -97,14 +97,17 @@ func (s Service) List(ctx context.Context) ([]Organization, error) {
func (s Service) Update(ctx context.Context, org Organization) (Organization, error) {
currentUser, err := s.userService.FetchCurrentUser(ctx)
if err != nil {
return Organization{}, fmt.Errorf("%w: %s", user.ErrInvalidEmail, err.Error())
s.logger.Error(fmt.Sprintf("%s: %s", user.ErrInvalidEmail.Error(), err.Error()))
}

var updatedOrg Organization

if org.ID != "" {
return s.repository.UpdateByID(ctx, org)
updatedOrg, err = s.repository.UpdateByID(ctx, org)
} else {
updatedOrg, err = s.repository.UpdateBySlug(ctx, org)
}

updatedOrg, err := s.repository.UpdateBySlug(ctx, org)
if err != nil {
return Organization{}, err
}
Expand All @@ -113,7 +116,7 @@ func (s Service) Update(ctx context.Context, org Organization) (Organization, er
ctx := pkgctx.WithoutCancel(ctx)
organizationLogData := updatedOrg.ToOrganizationLogData()
if err := s.activityService.Log(ctx, auditKeyOrganizationUpdate, currentUser.ID, organizationLogData); err != nil {
s.logger.Errorf("%s: %s", ErrLogActivity.Error(), err.Error())
s.logger.Error(fmt.Sprintf("%s: %s", ErrLogActivity.Error(), err.Error()))
}
}()

Expand Down
Loading

0 comments on commit a9ccd37

Please sign in to comment.