2021-08-24 20:24:52 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-11-24 17:16:42 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-08-24 20:24:52 +00:00
|
|
|
|
2022-02-16 15:33:56 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/authz"
|
2021-11-22 14:13:26 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/ctxerr"
|
2022-12-09 18:23:08 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/license"
|
2021-11-24 17:16:42 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/viewer"
|
2021-08-24 20:24:52 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
2021-11-24 17:16:42 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/ptr"
|
2021-08-24 20:24:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Add
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type globalPolicyRequest struct {
|
2021-11-24 17:16:42 +00:00
|
|
|
QueryID *uint `json:"query_id"`
|
|
|
|
Query string `json:"query"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Resolution string `json:"resolution"`
|
2021-12-10 16:55:49 +00:00
|
|
|
Platform string `json:"platform"`
|
2022-12-09 18:23:08 +00:00
|
|
|
Critical bool `json:"critical" premium:"true"`
|
2021-08-24 20:24:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type globalPolicyResponse struct {
|
|
|
|
Policy *fleet.Policy `json:"policy,omitempty"`
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r globalPolicyResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func globalPolicyEndpoint(ctx context.Context, request interface{}, svc fleet.Service) (interface{}, error) {
|
|
|
|
req := request.(*globalPolicyRequest)
|
2021-11-24 17:16:42 +00:00
|
|
|
resp, err := svc.NewGlobalPolicy(ctx, fleet.PolicyPayload{
|
|
|
|
QueryID: req.QueryID,
|
|
|
|
Query: req.Query,
|
|
|
|
Name: req.Name,
|
|
|
|
Description: req.Description,
|
|
|
|
Resolution: req.Resolution,
|
2021-12-10 16:55:49 +00:00
|
|
|
Platform: req.Platform,
|
2022-12-09 18:23:08 +00:00
|
|
|
Critical: req.Critical,
|
2021-11-24 17:16:42 +00:00
|
|
|
})
|
2021-08-24 20:24:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return globalPolicyResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return globalPolicyResponse{Policy: resp}, nil
|
|
|
|
}
|
|
|
|
|
2021-11-24 17:16:42 +00:00
|
|
|
func (svc Service) NewGlobalPolicy(ctx context.Context, p fleet.PolicyPayload) (*fleet.Policy, error) {
|
2021-08-24 20:24:52 +00:00
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Policy{}, fleet.ActionWrite); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-24 17:16:42 +00:00
|
|
|
vc, ok := viewer.FromContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("user must be authenticated to create team policies")
|
|
|
|
}
|
|
|
|
if err := p.Verify(); err != nil {
|
2022-09-19 17:53:44 +00:00
|
|
|
return nil, ctxerr.Wrap(ctx, &fleet.BadRequestError{
|
|
|
|
Message: fmt.Sprintf("policy payload verification: %s", err),
|
2022-01-19 19:07:58 +00:00
|
|
|
})
|
2021-11-24 17:16:42 +00:00
|
|
|
}
|
|
|
|
policy, err := svc.ds.NewGlobalPolicy(ctx, ptr.Uint(vc.UserID()), p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ctxerr.Wrap(ctx, err, "storing policy")
|
|
|
|
}
|
2022-02-16 15:33:56 +00:00
|
|
|
// Note: Issue #4191 proposes that we move to SQL transactions for actions so that we can
|
|
|
|
// rollback an action in the event of an error writing the associated activity
|
|
|
|
if err := svc.ds.NewActivity(
|
|
|
|
ctx,
|
|
|
|
authz.UserFromContext(ctx),
|
2022-12-23 16:05:16 +00:00
|
|
|
fleet.ActivityTypeCreatedPolicy{
|
|
|
|
ID: policy.ID,
|
|
|
|
Name: policy.Name,
|
|
|
|
},
|
2022-02-16 15:33:56 +00:00
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-24 17:16:42 +00:00
|
|
|
return policy, nil
|
2021-08-24 20:24:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// List
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type listGlobalPoliciesResponse struct {
|
|
|
|
Policies []*fleet.Policy `json:"policies,omitempty"`
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r listGlobalPoliciesResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func listGlobalPoliciesEndpoint(ctx context.Context, _ interface{}, svc fleet.Service) (interface{}, error) {
|
|
|
|
resp, err := svc.ListGlobalPolicies(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return listGlobalPoliciesResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return listGlobalPoliciesResponse{Policies: resp}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svc Service) ListGlobalPolicies(ctx context.Context) ([]*fleet.Policy, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Policy{}, fleet.ActionRead); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
return svc.ds.ListGlobalPolicies(ctx)
|
2021-08-24 20:24:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Get by id
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type getPolicyByIDRequest struct {
|
|
|
|
PolicyID uint `url:"policy_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type getPolicyByIDResponse struct {
|
|
|
|
Policy *fleet.Policy `json:"policy"`
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r getPolicyByIDResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func getPolicyByIDEndpoint(ctx context.Context, request interface{}, svc fleet.Service) (interface{}, error) {
|
|
|
|
req := request.(*getPolicyByIDRequest)
|
|
|
|
policy, err := svc.GetPolicyByIDQueries(ctx, req.PolicyID)
|
|
|
|
if err != nil {
|
|
|
|
return getPolicyByIDResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return getPolicyByIDResponse{Policy: policy}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svc Service) GetPolicyByIDQueries(ctx context.Context, policyID uint) (*fleet.Policy, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Policy{}, fleet.ActionRead); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
policy, err := svc.ds.Policy(ctx, policyID)
|
2021-08-24 20:24:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return policy, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Delete
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type deleteGlobalPoliciesRequest struct {
|
|
|
|
IDs []uint `json:"ids"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type deleteGlobalPoliciesResponse struct {
|
|
|
|
Deleted []uint `json:"deleted,omitempty"`
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r deleteGlobalPoliciesResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func deleteGlobalPoliciesEndpoint(ctx context.Context, request interface{}, svc fleet.Service) (interface{}, error) {
|
|
|
|
req := request.(*deleteGlobalPoliciesRequest)
|
|
|
|
resp, err := svc.DeleteGlobalPolicies(ctx, req.IDs)
|
|
|
|
if err != nil {
|
|
|
|
return deleteGlobalPoliciesResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return deleteGlobalPoliciesResponse{Deleted: resp}, nil
|
|
|
|
}
|
|
|
|
|
2021-12-23 21:26:55 +00:00
|
|
|
// DeleteGlobalPolicies deletes the given policies from the database.
|
|
|
|
// It also deletes the given ids from the failing policies webhook configuration.
|
2021-08-24 20:24:52 +00:00
|
|
|
func (svc Service) DeleteGlobalPolicies(ctx context.Context, ids []uint) ([]uint, error) {
|
2022-02-16 15:33:56 +00:00
|
|
|
// First check if authorized to read policies
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Policy{}, fleet.ActionRead); err != nil {
|
2021-08-24 20:24:52 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-24 17:16:42 +00:00
|
|
|
if len(ids) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2022-02-16 15:33:56 +00:00
|
|
|
policiesByID, err := svc.ds.PoliciesByID(ctx, ids)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ctxerr.Wrap(ctx, err, "getting policies by ID")
|
|
|
|
}
|
|
|
|
// Then check if authorized to write policies
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Policy{}, fleet.ActionWrite); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, policy := range policiesByID {
|
|
|
|
if policy.PolicyData.TeamID != nil {
|
|
|
|
return nil, authz.ForbiddenWithInternal(
|
|
|
|
"attempting to delete policy that belongs to team",
|
|
|
|
authz.UserFromContext(ctx),
|
|
|
|
policy,
|
|
|
|
fleet.ActionWrite,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2021-12-23 21:26:55 +00:00
|
|
|
if err := svc.removeGlobalPoliciesFromWebhookConfig(ctx, ids); err != nil {
|
|
|
|
return nil, ctxerr.Wrap(ctx, err, "removing global policies from webhook config")
|
|
|
|
}
|
2022-02-16 15:33:56 +00:00
|
|
|
deletedIDs, err := svc.ds.DeleteGlobalPolicies(ctx, ids)
|
2021-11-24 17:16:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-02-16 15:33:56 +00:00
|
|
|
|
|
|
|
// Note: Issue #4191 proposes that we move to SQL transactions for actions so that we can
|
|
|
|
// rollback an action in the event of an error writing the associated activity
|
|
|
|
for _, id := range deletedIDs {
|
|
|
|
if err := svc.ds.NewActivity(
|
|
|
|
ctx,
|
|
|
|
authz.UserFromContext(ctx),
|
2022-12-23 16:05:16 +00:00
|
|
|
fleet.ActivityTypeDeletedPolicy{
|
|
|
|
ID: id,
|
|
|
|
Name: policiesByID[id].Name,
|
|
|
|
},
|
2022-02-16 15:33:56 +00:00
|
|
|
); err != nil {
|
|
|
|
return nil, ctxerr.Wrap(ctx, err, "adding new activity for deleted policy")
|
|
|
|
}
|
|
|
|
}
|
2021-11-24 17:16:42 +00:00
|
|
|
return ids, nil
|
|
|
|
}
|
2021-08-24 20:24:52 +00:00
|
|
|
|
2021-12-23 21:26:55 +00:00
|
|
|
func (svc Service) removeGlobalPoliciesFromWebhookConfig(ctx context.Context, ids []uint) error {
|
|
|
|
ac, err := svc.ds.AppConfig(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
idSet := make(map[uint]struct{})
|
|
|
|
for _, id := range ids {
|
|
|
|
idSet[id] = struct{}{}
|
|
|
|
}
|
|
|
|
n := 0
|
|
|
|
policyIDs := ac.WebhookSettings.FailingPoliciesWebhook.PolicyIDs
|
|
|
|
origLen := len(policyIDs)
|
|
|
|
for i := range policyIDs {
|
|
|
|
if _, ok := idSet[policyIDs[i]]; !ok {
|
|
|
|
policyIDs[n] = policyIDs[i]
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if n == origLen {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ac.WebhookSettings.FailingPoliciesWebhook.PolicyIDs = policyIDs[:n]
|
|
|
|
if err := svc.ds.SaveAppConfig(ctx, ac); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-24 17:16:42 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Modify
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type modifyGlobalPolicyRequest struct {
|
|
|
|
PolicyID uint `url:"policy_id"`
|
|
|
|
fleet.ModifyPolicyPayload
|
|
|
|
}
|
|
|
|
|
|
|
|
type modifyGlobalPolicyResponse struct {
|
|
|
|
Policy *fleet.Policy `json:"policy,omitempty"`
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r modifyGlobalPolicyResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func modifyGlobalPolicyEndpoint(ctx context.Context, request interface{}, svc fleet.Service) (interface{}, error) {
|
|
|
|
req := request.(*modifyGlobalPolicyRequest)
|
|
|
|
resp, err := svc.ModifyGlobalPolicy(ctx, req.PolicyID, req.ModifyPolicyPayload)
|
|
|
|
if err != nil {
|
|
|
|
return modifyGlobalPolicyResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return modifyGlobalPolicyResponse{Policy: resp}, nil
|
|
|
|
}
|
|
|
|
|
2022-02-22 18:42:03 +00:00
|
|
|
func (svc *Service) ModifyGlobalPolicy(ctx context.Context, id uint, p fleet.ModifyPolicyPayload) (*fleet.Policy, error) {
|
2021-11-24 17:16:42 +00:00
|
|
|
return svc.modifyPolicy(ctx, nil, id, p)
|
2021-08-24 20:24:52 +00:00
|
|
|
}
|
2021-10-15 10:34:11 +00:00
|
|
|
|
2022-12-16 21:00:54 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Reset automation
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type resetAutomationRequest struct {
|
|
|
|
TeamIDs []uint `json:"team_ids" premium:"true"`
|
|
|
|
PolicyIDs []uint `json:"policy_ids"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type resetAutomationResponse struct {
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r resetAutomationResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func resetAutomationEndpoint(ctx context.Context, request interface{}, svc fleet.Service) (interface{}, error) {
|
|
|
|
req := request.(*resetAutomationRequest)
|
|
|
|
err := svc.ResetAutomation(ctx, req.TeamIDs, req.PolicyIDs)
|
|
|
|
return resetAutomationResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svc *Service) ResetAutomation(ctx context.Context, teamIDs, policyIDs []uint) error {
|
|
|
|
ac, err := svc.ds.AppConfig(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
allAutoPolicies := automationPolicies(ac.WebhookSettings.FailingPoliciesWebhook, ac.Integrations.Jira, ac.Integrations.Zendesk)
|
|
|
|
pIDs := make(map[uint]struct{})
|
|
|
|
for _, id := range policyIDs {
|
|
|
|
pIDs[id] = struct{}{}
|
|
|
|
}
|
|
|
|
for _, teamID := range teamIDs {
|
|
|
|
p1, p2, err := svc.ds.ListTeamPolicies(ctx, teamID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, p := range p1 {
|
|
|
|
pIDs[p.ID] = struct{}{}
|
|
|
|
}
|
|
|
|
for _, p := range p2 {
|
|
|
|
pIDs[p.ID] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hasGlobal := false
|
|
|
|
tIDs := make(map[uint]struct{})
|
|
|
|
for id := range pIDs {
|
|
|
|
p, err := svc.ds.Policy(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if p.TeamID == nil {
|
|
|
|
hasGlobal = true
|
|
|
|
} else {
|
|
|
|
tIDs[*p.TeamID] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for id := range tIDs {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Team{ID: id}, fleet.ActionWrite); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t, err := svc.ds.Team(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for pID := range teamAutomationPolicies(t.Config.WebhookSettings.FailingPoliciesWebhook, t.Config.Integrations.Jira, t.Config.Integrations.Zendesk) {
|
|
|
|
allAutoPolicies[pID] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if hasGlobal {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.AppConfig{}, fleet.ActionWrite); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(tIDs) == 0 && !hasGlobal {
|
|
|
|
svc.authz.SkipAuthorization(ctx)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for id := range pIDs {
|
|
|
|
if _, ok := allAutoPolicies[id]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := svc.ds.IncreasePolicyAutomationIteration(ctx, id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func automationPolicies(wh fleet.FailingPoliciesWebhookSettings, ji []*fleet.JiraIntegration, zi []*fleet.ZendeskIntegration) map[uint]struct{} {
|
|
|
|
enabled := wh.Enable
|
|
|
|
for _, j := range ji {
|
|
|
|
if j.EnableFailingPolicies {
|
|
|
|
enabled = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, z := range zi {
|
|
|
|
if z.EnableFailingPolicies {
|
|
|
|
enabled = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pols := make(map[uint]struct{}, len(wh.PolicyIDs))
|
|
|
|
if !enabled {
|
|
|
|
return pols
|
|
|
|
}
|
|
|
|
for _, pid := range wh.PolicyIDs {
|
|
|
|
pols[pid] = struct{}{}
|
|
|
|
}
|
|
|
|
return pols
|
|
|
|
}
|
|
|
|
|
|
|
|
func teamAutomationPolicies(wh fleet.FailingPoliciesWebhookSettings, ji []*fleet.TeamJiraIntegration, zi []*fleet.TeamZendeskIntegration) map[uint]struct{} {
|
|
|
|
enabled := wh.Enable
|
|
|
|
for _, j := range ji {
|
|
|
|
if j.EnableFailingPolicies {
|
|
|
|
enabled = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, z := range zi {
|
|
|
|
if z.EnableFailingPolicies {
|
|
|
|
enabled = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pols := make(map[uint]struct{}, len(wh.PolicyIDs))
|
|
|
|
if !enabled {
|
|
|
|
return pols
|
|
|
|
}
|
|
|
|
for _, pid := range wh.PolicyIDs {
|
|
|
|
pols[pid] = struct{}{}
|
|
|
|
}
|
|
|
|
return pols
|
|
|
|
}
|
|
|
|
|
2021-10-15 10:34:11 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Apply Spec
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type applyPolicySpecsRequest struct {
|
|
|
|
Specs []*fleet.PolicySpec `json:"specs"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type applyPolicySpecsResponse struct {
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r applyPolicySpecsResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func applyPolicySpecsEndpoint(ctx context.Context, request interface{}, svc fleet.Service) (interface{}, error) {
|
|
|
|
req := request.(*applyPolicySpecsRequest)
|
|
|
|
err := svc.ApplyPolicySpecs(ctx, req.Specs)
|
|
|
|
if err != nil {
|
|
|
|
return applyPolicySpecsResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return applyPolicySpecsResponse{}, nil
|
|
|
|
}
|
|
|
|
|
2022-02-16 15:33:56 +00:00
|
|
|
// TODO: add tests for activities?
|
2022-02-22 18:42:03 +00:00
|
|
|
func (svc *Service) ApplyPolicySpecs(ctx context.Context, policies []*fleet.PolicySpec) error {
|
2021-10-15 10:34:11 +00:00
|
|
|
checkGlobalPolicyAuth := false
|
|
|
|
for _, policy := range policies {
|
2021-11-24 17:16:42 +00:00
|
|
|
if err := policy.Verify(); err != nil {
|
2022-09-19 17:53:44 +00:00
|
|
|
return ctxerr.Wrap(ctx, &fleet.BadRequestError{
|
|
|
|
Message: fmt.Sprintf("policy spec payload verification: %s", err),
|
2022-01-19 19:07:58 +00:00
|
|
|
})
|
2021-11-24 17:16:42 +00:00
|
|
|
}
|
2021-10-15 10:34:11 +00:00
|
|
|
if policy.Team != "" {
|
|
|
|
team, err := svc.ds.TeamByName(ctx, policy.Team)
|
|
|
|
if err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return ctxerr.Wrap(ctx, err, "getting team by name")
|
2021-10-15 10:34:11 +00:00
|
|
|
}
|
2021-11-24 17:16:42 +00:00
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Policy{
|
|
|
|
PolicyData: fleet.PolicyData{
|
|
|
|
TeamID: &team.ID,
|
|
|
|
},
|
|
|
|
}, fleet.ActionWrite); err != nil {
|
2021-10-15 10:34:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-11-24 17:16:42 +00:00
|
|
|
} else {
|
|
|
|
checkGlobalPolicyAuth = true
|
2021-10-15 10:34:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if checkGlobalPolicyAuth {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Policy{}, fleet.ActionWrite); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-11-24 17:16:42 +00:00
|
|
|
vc, ok := viewer.FromContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("user must be authenticated to apply policies")
|
|
|
|
}
|
2022-12-09 18:23:08 +00:00
|
|
|
if !license.IsPremium(ctx) {
|
|
|
|
for i := range policies {
|
|
|
|
policies[i].Critical = false
|
|
|
|
}
|
|
|
|
}
|
2021-11-24 17:16:42 +00:00
|
|
|
if err := svc.ds.ApplyPolicySpecs(ctx, vc.UserID(), policies); err != nil {
|
|
|
|
return ctxerr.Wrap(ctx, err, "applying policy specs")
|
|
|
|
}
|
2022-02-16 15:33:56 +00:00
|
|
|
// Note: Issue #4191 proposes that we move to SQL transactions for actions so that we can
|
|
|
|
// rollback an action in the event of an error writing the associated activity
|
|
|
|
return svc.ds.NewActivity(
|
|
|
|
ctx,
|
|
|
|
authz.UserFromContext(ctx),
|
2022-12-23 16:05:16 +00:00
|
|
|
fleet.ActivityTypeAppliedSpecPolicy{
|
|
|
|
Policies: policies,
|
|
|
|
},
|
2022-02-16 15:33:56 +00:00
|
|
|
)
|
2021-10-15 10:34:11 +00:00
|
|
|
}
|