Skip to content

Commit

Permalink
add audit events
Browse files Browse the repository at this point in the history
  • Loading branch information
rawdaGastan committed Jan 23, 2025
1 parent d591ab0 commit c71e7b2
Show file tree
Hide file tree
Showing 15 changed files with 869 additions and 1 deletion.
35 changes: 35 additions & 0 deletions server/app/admin_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ func (a *App) SetPricesHandler(req *http.Request) (interface{}, Response) {
a.config.PricesPerMonth.PublicIP = input.PublicIP
}

if err := a.logVMsPriceUpdate(req, a.config.PricesPerMonth); err != nil {
log.Error().Err(err).Send()
return nil, InternalServerError(errors.New(internalServerErrorMsg))
}

return ResponseMsg{
Message: "New prices are set",
Data: nil,
Expand Down Expand Up @@ -314,6 +319,11 @@ func (a *App) DeleteAllDeploymentsHandler(req *http.Request) (interface{}, Respo
}
}

if err := a.logAllDeploymentsDelete(req); err != nil {
log.Error().Err(err).Send()
return nil, InternalServerError(errors.New(internalServerErrorMsg))
}

return ResponseMsg{
Message: "Deployments are deleted successfully",
}, Ok()
Expand Down Expand Up @@ -416,6 +426,11 @@ func (a *App) UpdateMaintenanceHandler(req *http.Request) (interface{}, Response
return nil, InternalServerError(errors.New(internalServerErrorMsg))
}

if err := a.logMaintenanceUpdate(req, input.ON); err != nil {
log.Error().Err(err).Send()
return nil, InternalServerError(errors.New(internalServerErrorMsg))
}

return ResponseMsg{
Message: "Maintenance is updated successfully",
Data: nil,
Expand Down Expand Up @@ -477,6 +492,11 @@ func (a *App) SetAdminHandler(req *http.Request) (interface{}, Response) {
return nil, InternalServerError(errors.New(internalServerErrorMsg))
}

if err := a.logAdminSet(req, user.ID.String(), input.Admin); err != nil {
log.Error().Err(err).Send()
return nil, InternalServerError(errors.New(internalServerErrorMsg))
}

return ResponseMsg{
Message: "User is updated successfully",
}, Ok()
Expand Down Expand Up @@ -537,6 +557,11 @@ func (a *App) CreateNewAnnouncementHandler(req *http.Request) (interface{}, Resp
}
}

if err := a.logAnnouncementCreate(req, adminAnnouncement.Subject); err != nil {
log.Error().Err(err).Send()
return nil, InternalServerError(errors.New(internalServerErrorMsg))
}

return ResponseMsg{
Message: "new announcement is sent successfully",
}, Created()
Expand Down Expand Up @@ -598,6 +623,11 @@ func (a *App) SendEmailHandler(req *http.Request) (interface{}, Response) {
return nil, InternalServerError(errors.New(internalServerErrorMsg))
}

if err := a.logEmailSent(req, user.ID.String(), emailUser.Subject); err != nil {
log.Error().Err(err).Send()
return nil, InternalServerError(errors.New(internalServerErrorMsg))
}

return ResponseMsg{
Message: "new email is sent successfully",
}, Created()
Expand Down Expand Up @@ -636,6 +666,11 @@ func (a *App) UpdateNextLaunchHandler(req *http.Request) (interface{}, Response)
return nil, InternalServerError(errors.New(internalServerErrorMsg))
}

if err := a.logNextLaunchUpdate(req, input.Launched); err != nil {
log.Error().Err(err).Send()
return nil, InternalServerError(errors.New(internalServerErrorMsg))
}

return ResponseMsg{
Message: "Next Launch is updated successfully",
Data: nil,
Expand Down
3 changes: 3 additions & 0 deletions server/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func (a *App) registerHandlers() {
invoiceRouter := authRouter.PathPrefix("/invoice").Subrouter()
cardRouter := userRouter.PathPrefix("/card").Subrouter()
logRouter := userRouter.PathPrefix("/log").Subrouter()
eventRouter := userRouter.PathPrefix("/event").Subrouter()
notificationRouter := authRouter.PathPrefix("/notification").Subrouter()
vmRouter := authRouter.PathPrefix("/vm").Subrouter()
k8sRouter := authRouter.PathPrefix("/k8s").Subrouter()
Expand Down Expand Up @@ -159,6 +160,8 @@ func (a *App) registerHandlers() {

logRouter.HandleFunc("", WrapFunc(a.ListLogsHandler)).Methods("GET", "OPTIONS")

eventRouter.HandleFunc("", WrapFunc(a.ListEventsHandler)).Methods("GET", "OPTIONS")

invoiceRouter.HandleFunc("", WrapFunc(a.ListInvoicesHandler)).Methods("GET", "OPTIONS")
invoiceRouter.HandleFunc("/{id}", WrapFunc(a.GetInvoiceHandler)).Methods("GET", "OPTIONS")
invoiceRouter.HandleFunc("/download/{id}", WrapFunc(a.DownloadInvoiceHandler)).Methods("GET", "OPTIONS")
Expand Down
34 changes: 34 additions & 0 deletions server/app/audit_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,37 @@ func (a *App) ListLogsHandler(req *http.Request) (interface{}, Response) {
Data: logs,
}, Ok()
}

// Example endpoint: List user's events
// @Summary List user's events
// @Description List user's events
// @Tags Audit
// @Accept json
// @Produce json
// @Security BearerAuth
// @Success 200 {object} []models.AuditEvent
// @Failure 400 {object} Response
// @Failure 401 {object} Response
// @Failure 404 {object} Response
// @Failure 500 {object} Response
// @Router /user/event [get]
func (a *App) ListEventsHandler(req *http.Request) (interface{}, Response) {
userID := req.Context().Value(middlewares.UserIDKey("UserID")).(string)

events, err := a.db.GetUserEvents(userID)
if err == gorm.ErrRecordNotFound || len(events) == 0 {
return ResponseMsg{
Message: "no events found",
Data: events,
}, Ok()
}
if err != nil {
log.Error().Err(err).Send()
return nil, InternalServerError(errors.New(internalServerErrorMsg))
}

return ResponseMsg{
Message: "Events are found",
Data: events,
}, Ok()
}
Loading

0 comments on commit c71e7b2

Please sign in to comment.