2016-10-03 03:14:35 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
|
|
|
|
2021-11-22 14:13:26 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/ctxerr"
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/viewer"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
2016-10-03 03:14:35 +00:00
|
|
|
)
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (svc *Service) ApplyLabelSpecs(ctx context.Context, specs []*fleet.LabelSpec) error {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Label{}, fleet.ActionWrite); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-07 22:12:32 +00:00
|
|
|
for _, spec := range specs {
|
2021-06-06 22:07:29 +00:00
|
|
|
if spec.LabelMembershipType == fleet.LabelMembershipTypeDynamic && len(spec.Hosts) > 0 {
|
2021-11-22 14:13:26 +00:00
|
|
|
return ctxerr.Errorf(ctx, "label %s is declared as dynamic but contains `hosts` key", spec.Name)
|
2020-04-07 22:12:32 +00:00
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
if spec.LabelMembershipType == fleet.LabelMembershipTypeManual && spec.Hosts == nil {
|
2020-04-07 22:12:32 +00:00
|
|
|
// Hosts list doesn't need to contain anything, but it should at least not be nil.
|
2021-11-22 14:13:26 +00:00
|
|
|
return ctxerr.Errorf(ctx, "label %s is declared as manual but contains no `hosts key`", spec.Name)
|
2020-04-07 22:12:32 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-14 12:11:07 +00:00
|
|
|
return svc.ds.ApplyLabelSpecs(ctx, specs)
|
2018-01-10 19:38:20 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (svc *Service) GetLabelSpecs(ctx context.Context) ([]*fleet.LabelSpec, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Label{}, fleet.ActionRead); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
return svc.ds.GetLabelSpecs(ctx)
|
2018-01-10 19:38:20 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (svc *Service) GetLabelSpec(ctx context.Context, name string) (*fleet.LabelSpec, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Label{}, fleet.ActionRead); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
return svc.ds.GetLabelSpec(ctx, name)
|
2018-05-08 01:54:29 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (svc *Service) NewLabel(ctx context.Context, p fleet.LabelPayload) (*fleet.Label, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Label{}, fleet.ActionWrite); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
label := &fleet.Label{}
|
2018-06-18 17:09:08 +00:00
|
|
|
|
|
|
|
if p.Name == nil {
|
2021-06-06 22:07:29 +00:00
|
|
|
return nil, fleet.NewInvalidArgumentError("name", "missing required argument")
|
2018-06-18 17:09:08 +00:00
|
|
|
}
|
|
|
|
label.Name = *p.Name
|
|
|
|
|
|
|
|
if p.Query == nil {
|
2021-06-06 22:07:29 +00:00
|
|
|
return nil, fleet.NewInvalidArgumentError("query", "missing required argument")
|
2018-06-18 17:09:08 +00:00
|
|
|
}
|
|
|
|
label.Query = *p.Query
|
|
|
|
|
|
|
|
if p.Platform != nil {
|
|
|
|
label.Platform = *p.Platform
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Description != nil {
|
|
|
|
label.Description = *p.Description
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
label, err := svc.ds.NewLabel(ctx, label)
|
2018-06-18 17:09:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return label, nil
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (svc *Service) ModifyLabel(ctx context.Context, id uint, payload fleet.ModifyLabelPayload) (*fleet.Label, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Label{}, fleet.ActionWrite); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
label, err := svc.ds.Label(ctx, id)
|
2018-06-18 17:09:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if payload.Name != nil {
|
|
|
|
label.Name = *payload.Name
|
|
|
|
}
|
|
|
|
if payload.Description != nil {
|
|
|
|
label.Description = *payload.Description
|
|
|
|
}
|
2021-09-14 12:11:07 +00:00
|
|
|
return svc.ds.SaveLabel(ctx, label)
|
2018-06-18 17:09:08 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (svc *Service) ListLabels(ctx context.Context, opt fleet.ListOptions) ([]*fleet.Label, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Label{}, fleet.ActionRead); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-04 01:53:43 +00:00
|
|
|
vc, ok := viewer.FromContext(ctx)
|
|
|
|
if !ok {
|
2021-06-06 22:07:29 +00:00
|
|
|
return nil, fleet.ErrNoContext
|
2021-06-04 01:53:43 +00:00
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
filter := fleet.TeamFilter{User: vc.User, IncludeObserver: true}
|
2021-06-03 23:24:15 +00:00
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
return svc.ds.ListLabels(ctx, filter, opt)
|
2016-10-03 03:14:35 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (svc *Service) GetLabel(ctx context.Context, id uint) (*fleet.Label, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Label{}, fleet.ActionRead); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
return svc.ds.Label(ctx, id)
|
2016-10-03 03:14:35 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 23:24:15 +00:00
|
|
|
func (svc *Service) DeleteLabel(ctx context.Context, name string) error {
|
2021-06-06 22:07:29 +00:00
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Label{}, fleet.ActionWrite); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
return svc.ds.DeleteLabel(ctx, name)
|
2016-10-03 03:14:35 +00:00
|
|
|
}
|
2017-01-16 22:57:05 +00:00
|
|
|
|
2021-06-03 23:24:15 +00:00
|
|
|
func (svc *Service) DeleteLabelByID(ctx context.Context, id uint) error {
|
2021-06-06 22:07:29 +00:00
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Label{}, fleet.ActionWrite); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
label, err := svc.ds.Label(ctx, id)
|
2018-06-25 20:56:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-14 12:11:07 +00:00
|
|
|
return svc.ds.DeleteLabel(ctx, label.Name)
|
2018-06-25 20:56:59 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (svc *Service) ListHostsInLabel(ctx context.Context, lid uint, opt fleet.HostListOptions) ([]*fleet.Host, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Label{}, fleet.ActionRead); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-04 01:53:43 +00:00
|
|
|
vc, ok := viewer.FromContext(ctx)
|
|
|
|
if !ok {
|
2021-06-06 22:07:29 +00:00
|
|
|
return nil, fleet.ErrNoContext
|
2021-06-04 01:53:43 +00:00
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
filter := fleet.TeamFilter{User: vc.User, IncludeObserver: true}
|
2020-03-30 02:19:54 +00:00
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
return svc.ds.ListHostsInLabel(ctx, filter, lid, opt)
|
2020-04-22 20:54:32 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (svc *Service) ListLabelsForHost(ctx context.Context, hid uint) ([]*fleet.Label, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Label{}, fleet.ActionRead); err != nil {
|
2017-01-16 22:57:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-03 23:24:15 +00:00
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
return svc.ds.ListLabelsForHost(ctx, hid)
|
2017-01-16 22:57:05 +00:00
|
|
|
}
|