Skip to content

Commit 226f717

Browse files
committed
Fix issue deleting DDM profiles with secret variables.
1 parent ad6d473 commit 226f717

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

server/service/apple_mdm.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -903,22 +903,27 @@ func (svc *Service) DeleteMDMAppleDeclaration(ctx context.Context, declUUID stri
903903
return ctxerr.Wrap(ctx, err)
904904
}
905905

906-
if _, ok := mdm_types.FleetReservedProfileNames()[decl.Name]; ok {
907-
return &fleet.BadRequestError{
908-
Message: "profiles managed by Fleet can't be deleted using this endpoint.",
909-
InternalErr: fmt.Errorf("deleting profile %s is not allowed because it's managed by Fleet", decl.Name),
906+
// Check if the declaration contains a secret variable. If it does, this means that the declaration
907+
// has been provided by the user and can be deleted. We don't need to validate that it is a Fleet declaration.
908+
hasSecretVariable := len(fleet.ContainsPrefixVars(string(decl.RawJSON), fleet.ServerSecretPrefix)) > 0
909+
if !hasSecretVariable {
910+
if _, ok := mdm_types.FleetReservedProfileNames()[decl.Name]; ok {
911+
return &fleet.BadRequestError{
912+
Message: "profiles managed by Fleet can't be deleted using this endpoint.",
913+
InternalErr: fmt.Errorf("deleting profile %s is not allowed because it's managed by Fleet", decl.Name),
914+
}
910915
}
911-
}
912916

913-
// TODO: refine our approach to deleting restricted/forbidden types of declarations so that we
914-
// can check that Fleet-managed aren't being deleted; this can be addressed once we add support
915-
// for more types of declarations
916-
var d fleet.MDMAppleRawDeclaration
917-
if err := json.Unmarshal(decl.RawJSON, &d); err != nil {
918-
return ctxerr.Wrap(ctx, err, "unmarshalling declaration")
919-
}
920-
if err := d.ValidateUserProvided(); err != nil {
921-
return ctxerr.Wrap(ctx, &fleet.BadRequestError{Message: err.Error()})
917+
// TODO: refine our approach to deleting restricted/forbidden types of declarations so that we
918+
// can check that Fleet-managed aren't being deleted; this can be addressed once we add support
919+
// for more types of declarations
920+
var d fleet.MDMAppleRawDeclaration
921+
if err := json.Unmarshal(decl.RawJSON, &d); err != nil {
922+
return ctxerr.Wrap(ctx, err, "unmarshalling declaration")
923+
}
924+
if err := d.ValidateUserProvided(); err != nil {
925+
return ctxerr.Wrap(ctx, &fleet.BadRequestError{Message: err.Error()})
926+
}
922927
}
923928

924929
var teamName string

server/service/integration_mdm_ddm_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,15 +598,19 @@ WHERE name = ?`
598598
return decl
599599
}
600600
nameToIdentifier := make(map[string]string, 3)
601+
nameToUUID := make(map[string]string, 3)
601602
decl := getDeclaration(t, "N0")
602603
nameToIdentifier["N0"] = decl.Identifier
604+
nameToUUID["N0"] = decl.DeclarationUUID
603605
decl = getDeclaration(t, "N1")
604606
assert.NotContains(t, string(decl.RawJSON), myBash)
605607
assert.Contains(t, string(decl.RawJSON), "$"+fleet.ServerSecretPrefix+"BASH")
606608
nameToIdentifier["N1"] = decl.Identifier
609+
nameToUUID["N1"] = decl.DeclarationUUID
607610
decl = getDeclaration(t, "N2")
608611
assert.Equal(t, string(decl.RawJSON), "${"+fleet.ServerSecretPrefix+"PROFILE}")
609612
nameToIdentifier["N2"] = decl.Identifier
613+
nameToUUID["N2"] = decl.DeclarationUUID
610614

611615
// trigger a profile sync
612616
s.awaitTriggerProfileSchedule(t)
@@ -641,6 +645,13 @@ WHERE name = ?`
641645
require.NoError(t, err)
642646
require.NoError(t, json.NewDecoder(r.Body).Decode(&gotParsed))
643647
assert.EqualValues(t, `{"DataAssetReference":"com.fleet.asset.bash","ServiceType":"com.apple.bash2"}`, gotParsed.Payload)
648+
649+
// Delete the profiles
650+
s.Do("DELETE", "/api/latest/fleet/configuration_profiles/"+nameToUUID["N0"], nil, http.StatusOK)
651+
s.Do("DELETE", "/api/latest/fleet/configuration_profiles/"+nameToUUID["N1"], nil, http.StatusOK)
652+
s.Do("DELETE", "/api/latest/fleet/configuration_profiles/"+nameToUUID["N2"], nil, http.StatusOK)
653+
s.DoJSON("GET", "/api/latest/fleet/mdm/profiles", &listMDMConfigProfilesRequest{}, http.StatusOK, &resp)
654+
require.Empty(t, resp.Profiles)
644655
}
645656

646657
func (s *integrationMDMTestSuite) TestAppleDDMReconciliation() {

0 commit comments

Comments
 (0)