Skip to content

Commit

Permalink
fix: audit logs handler fields and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Chief-Rishab committed Jul 24, 2023
1 parent b5df1ce commit 251ffa0
Show file tree
Hide file tree
Showing 2 changed files with 381 additions and 2 deletions.
22 changes: 20 additions & 2 deletions internal/api/v1beta1/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ type AuditService interface {
func (h Handler) ListOrganizationAuditLogs(ctx context.Context, request *shieldv1beta1.ListOrganizationAuditLogsRequest) (*shieldv1beta1.ListOrganizationAuditLogsResponse, error) {
logger := grpczap.Extract(ctx)

if request.GetOrgId() == "" {
return nil, grpcBadBodyError
}

var logs []*shieldv1beta1.AuditLog
logList, err := h.auditService.List(ctx, audit.Filter{
OrgID: request.GetOrgId(),
Expand All @@ -42,8 +46,14 @@ func (h Handler) ListOrganizationAuditLogs(ctx context.Context, request *shieldv

func (h Handler) CreateOrganizationAuditLogs(ctx context.Context, request *shieldv1beta1.CreateOrganizationAuditLogsRequest) (*shieldv1beta1.CreateOrganizationAuditLogsResponse, error) {
logger := grpczap.Extract(ctx)
if request.GetOrgId() == "" || request.GetLogs() == nil {
return nil, grpcBadBodyError
}

for _, log := range request.GetLogs() {
if log.Source == "" || log.Action == "" {
return nil, grpcBadBodyError
}
if err := h.auditService.Create(ctx, &audit.Log{
ID: log.GetId(),
OrgID: request.GetOrgId(),
Expand All @@ -53,16 +63,18 @@ func (h Handler) CreateOrganizationAuditLogs(ctx context.Context, request *shiel
CreatedAt: log.CreatedAt.AsTime(),
Actor: audit.Actor{
ID: log.GetActor().GetId(),
Type: log.GetActor().GetType(),
Name: log.GetActor().GetName(),
},
Target: audit.Target{
ID: log.GetTarget().GetId(),
Type: log.GetTarget().GetType(),
Name: log.GetTarget().GetName(),
},
Metadata: log.Context,
}); err != nil {
logger.Error(err.Error())
return nil, err
return nil, grpcInternalServerError
}
}
return &shieldv1beta1.CreateOrganizationAuditLogsResponse{}, nil
Expand All @@ -71,10 +83,14 @@ func (h Handler) CreateOrganizationAuditLogs(ctx context.Context, request *shiel
func (h Handler) GetOrganizationAuditLog(ctx context.Context, request *shieldv1beta1.GetOrganizationAuditLogRequest) (*shieldv1beta1.GetOrganizationAuditLogResponse, error) {
logger := grpczap.Extract(ctx)

if request.OrgId == "" || request.GetId() == "" {
return nil, grpcBadBodyError
}

log, err := h.auditService.GetByID(ctx, request.GetId())
if err != nil {
logger.Error(err.Error())
return nil, err
return nil, grpcInternalServerError
}

return &shieldv1beta1.GetOrganizationAuditLogResponse{
Expand All @@ -91,10 +107,12 @@ func transformAuditLogToPB(log audit.Log) *shieldv1beta1.AuditLog {
Actor: &shieldv1beta1.AuditLogActor{
Id: log.Actor.ID,
Name: log.Actor.Name,
Type: log.Actor.Type,
},
Target: &shieldv1beta1.AuditLogTarget{
Id: log.Target.ID,
Name: log.Target.Name,
Type: log.Target.Type,
},
Context: log.Metadata,
}
Expand Down
Loading

0 comments on commit 251ffa0

Please sign in to comment.