MSP-2243 update audit controller: add new methods
This commit is contained in:
1 parent
158962dcfe
commit
150d61fb18
4 files changed
+79
-3
No files matched your search
+45
-2
@@ -3,11 +3,16 @@ package controller
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"msp-admin-service/domain"
|
||||
)
|
||||
|
||||
type AuditService interface {
|
||||
All(ctx context.Context, limit int, offset int) (*domain.AuditResponse, error)
|
||||
Events(ctx context.Context) ([]domain.AuditEvent, error)
|
||||
SetEvents(ctx context.Context, req []domain.SetAuditEvent) error
|
||||
}
|
||||
|
||||
type Audit struct {
|
||||
@@ -26,11 +31,49 @@ func NewAudit(service AuditService) Audit {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param X-AUTH-ADMIN header string true "Токен администратора"
|
||||
// @Param body body domain.AuditResponse true "Тело запроса"
|
||||
// @Success 200 {object} domain.PageRequest
|
||||
// @Param body body domain.PageRequest true "Тело запроса"
|
||||
// @Success 200 {object} domain.AuditResponse
|
||||
// @Failure 400 {object} domain.GrpcError "Невалидное тело запроса"
|
||||
// @Failure 500 {object} domain.GrpcError
|
||||
// @Router /log/all [POST]
|
||||
func (c Audit) All(ctx context.Context, req domain.PageRequest) (*domain.AuditResponse, error) {
|
||||
return c.service.All(ctx, req.Limit, req.Offset)
|
||||
}
|
||||
|
||||
// Events
|
||||
// @Tags log
|
||||
// @Summary Получение списка логов
|
||||
// @Description Возвращает полный список доступных событий аудита
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param X-AUTH-ADMIN header string true "Токен администратора"
|
||||
// @Param body body domain.AuditResponse true "Тело запроса"
|
||||
// @Success 200 {array} domain.AuditEvent
|
||||
// @Failure 400 {object} domain.GrpcError "Невалидное тело запроса"
|
||||
// @Failure 500 {object} domain.GrpcError
|
||||
// @Router /log/events [POST]
|
||||
func (c Audit) Events(ctx context.Context) ([]domain.AuditEvent, error) {
|
||||
return c.service.Events(ctx)
|
||||
}
|
||||
|
||||
// SetEvents
|
||||
// @Tags log
|
||||
// @Summary Получение списка логов
|
||||
// @Description Всегда возвращает полный список доступных событий аудита
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param X-AUTH-ADMIN header string true "Токен администратора"
|
||||
// @Param body body domain.SetAuditEvent true "Тело запроса"
|
||||
// @Success 200
|
||||
// @Failure 400 {object} domain.GrpcError "Невалидное тело запроса"
|
||||
// @Failure 500 {object} domain.GrpcError
|
||||
// @Router /log/set_events [POST]
|
||||
func (c Audit) SetEvents(ctx context.Context, req []domain.SetAuditEvent) error {
|
||||
err := c.service.SetEvents(ctx, req)
|
||||
switch {
|
||||
case errors.As(err, &domain.UnknownAuditEventError{}):
|
||||
return status.Errorf(codes.InvalidArgument, err.Error())
|
||||
default:
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -20,3 +20,14 @@ type PageRequest struct {
|
||||
Limit int `valid:"required"`
|
||||
Offset int
|
||||
}
|
||||
|
||||
type SetAuditEvent struct {
|
||||
Event string
|
||||
Enabled bool
|
||||
}
|
||||
|
||||
type AuditEvent struct {
|
||||
Event string
|
||||
Name string
|
||||
Enabled bool
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
@@ -15,3 +17,11 @@ var (
|
||||
ErrTokenNotFound = errors.New("token not found")
|
||||
ErrTooManyLoginRequests = errors.New("too many login requests")
|
||||
)
|
||||
|
||||
type UnknownAuditEventError struct {
|
||||
Event string
|
||||
}
|
||||
|
||||
func (e UnknownAuditEventError) Error() string {
|
||||
return fmt.Sprintf("unknown audit event: %s", e.Event)
|
||||
}
|
||||
+13
-1
@@ -23,7 +23,7 @@ func EndpointDescriptors() []cluster.EndpointDescriptor {
|
||||
return endpointDescriptors(Controllers{})
|
||||
}
|
||||
|
||||
func Handler(wrapper endpoint.Wrapper, c Controllers) isp.BackendServiceServer {
|
||||
func Handler(wrapper endpoint.Wrapper, c Controllers) isp.BackendServiceServer { // nolint:ireturn
|
||||
muxer := grpc.NewMux()
|
||||
for _, descriptor := range endpointDescriptors(c) {
|
||||
muxer.Handle(descriptor.Path, wrapper.Endpoint(descriptor.Handler))
|
||||
@@ -148,6 +148,18 @@ func endpointDescriptors(c Controllers) []cluster.EndpointDescriptor {
|
||||
UserAuthRequired: false,
|
||||
Handler: c.Audit.All,
|
||||
},
|
||||
{
|
||||
Path: "admin/log/events",
|
||||
Inner: true,
|
||||
UserAuthRequired: false,
|
||||
Handler: c.Audit.Events,
|
||||
},
|
||||
{
|
||||
Path: "admin/log/set_events",
|
||||
Inner: true,
|
||||
UserAuthRequired: false,
|
||||
Handler: c.Audit.SetEvents,
|
||||
},
|
||||
{
|
||||
Path: "admin/secure/authenticate",
|
||||
Inner: true,
|
||||
|
||||
Reference in new issue
Block a user