fleet/server/service/status.go
Jahziel Villasana-Espinoza c4161518c0
feat: send back forbidden when live queries are disabled (#14804)
# Checklist for submitter

If some of the following don't apply, delete the relevant line.

- [x] Changes file added for user-visible changes in `changes/` or
`orbit/changes/`.
See [Changes
files](https://fleetdm.com/docs/contributing/committing-changes#changes-files)
for more information.
- [x] Added/updated tests
- [x] Manual QA for all new/changed functionality
2023-10-31 08:58:36 -04:00

64 lines
1.7 KiB
Go

package service
import (
"context"
"github.com/fleetdm/fleet/v4/server/contexts/ctxerr"
"github.com/fleetdm/fleet/v4/server/fleet"
)
////////////////////////////////////////////////////////////////////////////////
// Status Result Store
////////////////////////////////////////////////////////////////////////////////
type statusResponse struct {
Err error `json:"error,omitempty"`
}
func (m statusResponse) error() error { return m.Err }
func statusResultStoreEndpoint(ctx context.Context, req interface{}, svc fleet.Service) (errorer, error) {
var resp statusResponse
if err := svc.StatusResultStore(ctx); err != nil {
resp.Err = err
}
return resp, nil
}
func (svc *Service) StatusResultStore(ctx context.Context) error {
if err := svc.authz.Authorize(ctx, &fleet.AppConfig{}, fleet.ActionRead); err != nil {
return err
}
return svc.resultStore.HealthCheck()
}
////////////////////////////////////////////////////////////////////////////////
// Status Live Query
////////////////////////////////////////////////////////////////////////////////
func statusLiveQueryEndpoint(ctx context.Context, req interface{}, svc fleet.Service) (errorer, error) {
var resp statusResponse
if err := svc.StatusLiveQuery(ctx); err != nil {
resp.Err = err
}
return resp, nil
}
func (svc *Service) StatusLiveQuery(ctx context.Context) error {
if err := svc.authz.Authorize(ctx, &fleet.AppConfig{}, fleet.ActionRead); err != nil {
return err
}
cfg, err := svc.ds.AppConfig(ctx)
if err != nil {
return ctxerr.Wrap(ctx, err, "retrieve app config")
}
if cfg.ServerSettings.LiveQueryDisabled {
return ctxerr.Wrap(ctx, fleet.NewPermissionError("disabled by administrator"))
}
return svc.StatusResultStore(ctx)
}