Skip to content
14 changes: 14 additions & 0 deletions cmd/fleetctl/fleetctl/gitops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2675,6 +2675,20 @@ func TestGitOpsCustomSettings(t *testing.T) {
}
return ret, nil
}
ds.LabelsByNameFunc = func(ctx context.Context, names []string) (map[string]*fleet.Label, error) {
// for this test, recognize labels A, B and C (as well as the built-in macos 14+ one)
ret := make(map[string]*fleet.Label)
for _, lbl := range names {
id, ok := labelToIDs[lbl]
if ok {
ret[lbl] = &fleet.Label{
ID: id,
Name: lbl,
}
}
}
return ret, nil
}
ds.SetTeamVPPAppsFunc = func(ctx context.Context, teamID *uint, adamIDs []fleet.VPPAppTeam, _ map[string]uint) error {
return nil
}
Expand Down
13 changes: 13 additions & 0 deletions cmd/fleetctl/integrationtest/gitops/software_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,18 @@ func TestGitOpsTeamVPPApps(t *testing.T) {
}
return found, nil
}
ds.LabelsByNameFunc = func(ctx context.Context, names []string) (map[string]*fleet.Label, error) {
found2 := make(map[string]*fleet.Label)
for _, l := range names {
if id, ok := c.expectedLabels[l]; ok {
found2[l] = &fleet.Label{
ID: id,
Name: l,
}
}
}
return found2, nil
}
ds.GetCertificateTemplatesByTeamIDFunc = func(ctx context.Context, teamID uint, options fleet.ListOptions) ([]*fleet.CertificateTemplateResponseSummary, *fleet.PaginationMetadata, error) {
return []*fleet.CertificateTemplateResponseSummary{}, &fleet.PaginationMetadata{}, nil
}
Expand All @@ -565,6 +577,7 @@ func TestGitOpsTeamVPPApps(t *testing.T) {
require.NoError(t, err)
if len(c.expectedLabels) > 0 {
require.True(t, ds.LabelIDsByNameFuncInvoked)
require.True(t, ds.LabelsByNameFuncInvoked)
}

require.Equal(t, c.expectedLabels, found)
Expand Down
2 changes: 1 addition & 1 deletion ee/server/service/maintained_apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (svc *Service) AddFleetMaintainedApp(
}

// validate labels before we do anything else
validatedLabels, err := ValidateSoftwareLabels(ctx, svc, labelsIncludeAny, labelsExcludeAny)
validatedLabels, err := ValidateSoftwareLabels(ctx, svc, teamID, labelsIncludeAny, labelsExcludeAny)
if err != nil {
return 0, ctxerr.Wrap(ctx, err, "validating software labels")
}
Expand Down
10 changes: 5 additions & 5 deletions ee/server/service/software_installers.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (svc *Service) UploadSoftwareInstaller(ctx context.Context, payload *fleet.
}

// validate labels before we do anything else
validatedLabels, err := ValidateSoftwareLabels(ctx, svc, payload.LabelsIncludeAny, payload.LabelsExcludeAny)
validatedLabels, err := ValidateSoftwareLabels(ctx, svc, payload.TeamID, payload.LabelsIncludeAny, payload.LabelsExcludeAny)
if err != nil {
return nil, ctxerr.Wrap(ctx, err, "validating software labels")
}
Expand Down Expand Up @@ -193,7 +193,7 @@ func (svc *Service) UploadSoftwareInstaller(ctx context.Context, payload *fleet.
return addedInstaller, nil
}

func ValidateSoftwareLabels(ctx context.Context, svc fleet.Service, labelsIncludeAny, labelsExcludeAny []string) (*fleet.LabelIdentsWithScope, error) {
func ValidateSoftwareLabels(ctx context.Context, svc fleet.Service, teamID *uint, labelsIncludeAny, labelsExcludeAny []string) (*fleet.LabelIdentsWithScope, error) {
if authctx, ok := authz_ctx.FromContext(ctx); !ok {
return nil, fleet.NewAuthRequiredError("validate software labels: missing authorization context")
} else if !authctx.Checked() {
Expand All @@ -218,7 +218,7 @@ func ValidateSoftwareLabels(ctx context.Context, svc fleet.Service, labelsInclud
return &fleet.LabelIdentsWithScope{}, nil
}

byName, err := svc.BatchValidateLabels(ctx, names)
byName, err := svc.BatchValidateLabels(ctx, teamID, names)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -710,7 +710,7 @@ func ValidateSoftwareLabelsForUpdate(ctx context.Context, svc fleet.Service, exi
return false, nil, nil
}

incoming, err := ValidateSoftwareLabels(ctx, svc, includeAny, excludeAny)
incoming, err := ValidateSoftwareLabels(ctx, svc, existingInstaller.TeamID, includeAny, excludeAny)
if err != nil {
return false, nil, err
}
Expand Down Expand Up @@ -1869,7 +1869,7 @@ func (svc *Service) BatchSetSoftwareInstallers(
}
}
if !dryRun {
validatedLabels, err := ValidateSoftwareLabels(ctx, svc, payload.LabelsIncludeAny, payload.LabelsExcludeAny)
validatedLabels, err := ValidateSoftwareLabels(ctx, svc, teamID, payload.LabelsIncludeAny, payload.LabelsExcludeAny)
if err != nil {
return "", err
}
Expand Down
7 changes: 3 additions & 4 deletions ee/server/service/vpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (svc *Service) BatchAssociateVPPApps(ctx context.Context, teamName string,
}
}

validatedLabels, err := ValidateSoftwareLabels(ctx, svc, payload.LabelsIncludeAny, payload.LabelsExcludeAny)
validatedLabels, err := ValidateSoftwareLabels(ctx, svc, teamID, payload.LabelsIncludeAny, payload.LabelsExcludeAny)
if err != nil {
return nil, ctxerr.Wrap(ctx, err, "validating software labels for batch adding vpp app")
}
Expand Down Expand Up @@ -343,7 +343,6 @@ func (svc *Service) BatchAssociateVPPApps(ctx context.Context, teamName string,
)
}
}

}

return addedApps, nil
Expand Down Expand Up @@ -482,7 +481,7 @@ func (svc *Service) AddAppStoreApp(ctx context.Context, teamID *uint, appID flee
fmt.Sprintf("platform must be one of '%s', '%s', '%s', or '%s'", fleet.IOSPlatform, fleet.IPadOSPlatform, fleet.MacOSPlatform, fleet.AndroidPlatform))
}

validatedLabels, err := ValidateSoftwareLabels(ctx, svc, appID.LabelsIncludeAny, appID.LabelsExcludeAny)
validatedLabels, err := ValidateSoftwareLabels(ctx, svc, teamID, appID.LabelsIncludeAny, appID.LabelsExcludeAny)
if err != nil {
return 0, ctxerr.Wrap(ctx, err, "validating software labels for adding vpp app")
}
Expand Down Expand Up @@ -762,7 +761,7 @@ func (svc *Service) UpdateAppStoreApp(ctx context.Context, titleID uint, teamID
var validatedLabels *fleet.LabelIdentsWithScope
if payload.LabelsExcludeAny != nil || payload.LabelsIncludeAny != nil {
var err error
validatedLabels, err = ValidateSoftwareLabels(ctx, svc, payload.LabelsIncludeAny, payload.LabelsExcludeAny)
validatedLabels, err = ValidateSoftwareLabels(ctx, svc, teamID, payload.LabelsIncludeAny, payload.LabelsExcludeAny)
if err != nil {
return nil, ctxerr.Wrap(ctx, err, "UpdateAppStoreApp: validating software labels")
}
Expand Down
11 changes: 7 additions & 4 deletions server/fleet/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,13 @@ type Service interface {
// ListLabelsForHost returns a slice of labels for a given host
ListLabelsForHost(ctx context.Context, hostID uint) ([]*Label, error)

// BatchValidateLabels validates that each of the provided label names exists. The returned map
// is keyed by label name. Caller must ensure that appropirate authorization checks are
// performed prior to calling this method.
BatchValidateLabels(ctx context.Context, labelNames []string) (map[string]LabelIdent, error)
// BatchValidateLabels validates that each of the provided label names exists,
// and verifies the provided label names belong to the given teamID.
//
// The returned map is keyed by label name.
// Caller must ensure that appropriate authorization checks are performed prior
// to calling this method.
BatchValidateLabels(ctx context.Context, teamID *uint, labelNames []string) (map[string]LabelIdent, error)

// /////////////////////////////////////////////////////////////////////////////
// QueryService
Expand Down
6 changes: 3 additions & 3 deletions server/mock/service/service_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ type ListHostsInLabelFunc func(ctx context.Context, lid uint, opt fleet.HostList

type ListLabelsForHostFunc func(ctx context.Context, hostID uint) ([]*fleet.Label, error)

type BatchValidateLabelsFunc func(ctx context.Context, labelNames []string) (map[string]fleet.LabelIdent, error)
type BatchValidateLabelsFunc func(ctx context.Context, teamID *uint, labelNames []string) (map[string]fleet.LabelIdent, error)

type ApplyQuerySpecsFunc func(ctx context.Context, specs []*fleet.QuerySpec) error

Expand Down Expand Up @@ -2651,11 +2651,11 @@ func (s *Service) ListLabelsForHost(ctx context.Context, hostID uint) ([]*fleet.
return s.ListLabelsForHostFunc(ctx, hostID)
}

func (s *Service) BatchValidateLabels(ctx context.Context, labelNames []string) (map[string]fleet.LabelIdent, error) {
func (s *Service) BatchValidateLabels(ctx context.Context, teamID *uint, labelNames []string) (map[string]fleet.LabelIdent, error) {
s.mu.Lock()
s.BatchValidateLabelsFuncInvoked = true
s.mu.Unlock()
return s.BatchValidateLabelsFunc(ctx, labelNames)
return s.BatchValidateLabelsFunc(ctx, teamID, labelNames)
}

func (s *Service) ApplyQuerySpecs(ctx context.Context, specs []*fleet.QuerySpec) error {
Expand Down
2 changes: 1 addition & 1 deletion server/service/apple_mdm.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ func (svc *Service) NewMDMAppleConfigProfile(ctx context.Context, teamID uint, d
cp.Mobileconfig = data
cp.SecretsUpdatedAt = secretsUpdatedAt

labelMap, err := svc.validateProfileLabels(ctx, labels)
labelMap, err := svc.validateProfileLabels(ctx, &teamID, labels)
if err != nil {
return nil, ctxerr.Wrap(ctx, err, "validating labels")
}
Expand Down
5 changes: 5 additions & 0 deletions server/service/global_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ func (svc Service) NewGlobalPolicy(ctx context.Context, p fleet.PolicyPayload) (
Message: fmt.Sprintf("policy payload verification: %s", err),
})
}

if err := verifyLabelsToAssociate(ctx, svc.ds, nil, append(p.LabelsIncludeAny, p.LabelsExcludeAny...)); err != nil {
return nil, ctxerr.Wrap(ctx, err, "verify labels to associate")
}

Comment thread
lucasmrod marked this conversation as resolved.
policy, err := svc.ds.NewGlobalPolicy(ctx, ptr.Uint(vc.UserID()), p)
if err != nil {
return nil, ctxerr.Wrap(ctx, err, "storing policy")
Expand Down
Loading
Loading