package service import ( "bytes" "context" "crypto/tls" "encoding/base64" "fmt" "net/http" "net/http/httptest" "os" "strings" "sync/atomic" "testing" "github.com/fleetdm/fleet/v4/server/authz" "github.com/fleetdm/fleet/v4/server/config" "github.com/fleetdm/fleet/v4/server/contexts/license" "github.com/fleetdm/fleet/v4/server/contexts/viewer" "github.com/fleetdm/fleet/v4/server/fleet" "github.com/fleetdm/fleet/v4/server/mock" nanodep_mock "github.com/fleetdm/fleet/v4/server/mock/nanodep" nanomdm_mock "github.com/fleetdm/fleet/v4/server/mock/nanomdm" "github.com/fleetdm/fleet/v4/server/ptr" "github.com/fleetdm/fleet/v4/server/test" kitlog "github.com/go-kit/kit/log" "github.com/google/uuid" nanodep_client "github.com/micromdm/nanodep/client" "github.com/micromdm/nanomdm/mdm" nanomdm_pushsvc "github.com/micromdm/nanomdm/push/service" "github.com/stretchr/testify/require" ) func setupAppleMDMService(t *testing.T) (fleet.Service, context.Context, *mock.Store) { ds := new(mock.Store) cfg := config.TestConfig() cfg.MDMApple.Enable = true ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch { case strings.Contains(r.URL.Path, "/server/devices"): _, err := w.Write([]byte("{}")) require.NoError(t, err) return case strings.Contains(r.URL.Path, "/session"): _, err := w.Write([]byte(`{"auth_session_token": "yoo"}`)) require.NoError(t, err) return } })) mdmStorage := &nanomdm_mock.Storage{} depStorage := &nanodep_mock.Storage{} pushFactory, _ := newMockAPNSPushProviderFactory() pusher := nanomdm_pushsvc.New( mdmStorage, mdmStorage, pushFactory, NewNanoMDMLogger(kitlog.NewJSONLogger(os.Stdout)), ) opts := &TestServerOpts{ FleetConfig: &cfg, MDMStorage: mdmStorage, DEPStorage: depStorage, MDMPusher: pusher, License: &fleet.LicenseInfo{Tier: fleet.TierPremium}, } svc, ctx := newTestServiceWithConfig(t, ds, cfg, nil, nil, opts) mdmStorage.EnqueueCommandFunc = func(ctx context.Context, id []string, cmd *mdm.Command) (map[string]error, error) { return nil, nil } mdmStorage.RetrievePushInfoFunc = func(ctx context.Context, tokens []string) (map[string]*mdm.Push, error) { res := make(map[string]*mdm.Push, len(tokens)) for _, t := range tokens { res[t] = &mdm.Push{ PushMagic: "", Token: []byte(t), Topic: "", } } return res, nil } mdmStorage.RetrievePushCertFunc = func(ctx context.Context, topic string) (*tls.Certificate, string, error) { cert, err := tls.LoadX509KeyPair("testdata/server.pem", "testdata/server.key") return &cert, "", err } mdmStorage.IsPushCertStaleFunc = func(ctx context.Context, topic string, staleToken string) (bool, error) { return false, nil } depStorage.RetrieveAuthTokensFunc = func(ctx context.Context, name string) (*nanodep_client.OAuth1Tokens, error) { return &nanodep_client.OAuth1Tokens{}, nil } depStorage.RetrieveConfigFunc = func(context.Context, string) (*nanodep_client.Config, error) { return &nanodep_client.Config{ BaseURL: ts.URL, }, nil } ds.AppConfigFunc = func(ctx context.Context) (*fleet.AppConfig, error) { return &fleet.AppConfig{ OrgInfo: fleet.OrgInfo{ OrgName: "Foo Inc.", }, ServerSettings: fleet.ServerSettings{ ServerURL: "https://foo.example.com", }, }, nil } ds.GetMDMAppleEnrollmentProfileByTokenFunc = func(ctx context.Context, token string) (*fleet.MDMAppleEnrollmentProfile, error) { return nil, nil } ds.NewMDMAppleEnrollmentProfileFunc = func(ctx context.Context, enrollmentPayload fleet.MDMAppleEnrollmentProfilePayload) (*fleet.MDMAppleEnrollmentProfile, error) { return &fleet.MDMAppleEnrollmentProfile{ ID: 1, Token: "foo", Type: fleet.MDMAppleEnrollmentTypeManual, EnrollmentURL: "https://foo.example.com?token=foo", }, nil } ds.GetMDMAppleEnrollmentProfileByTokenFunc = func(ctx context.Context, token string) (*fleet.MDMAppleEnrollmentProfile, error) { return nil, nil } ds.ListMDMAppleEnrollmentProfilesFunc = func(ctx context.Context) ([]*fleet.MDMAppleEnrollmentProfile, error) { return nil, nil } ds.GetMDMAppleCommandResultsFunc = func(ctx context.Context, commandUUID string) (map[string]*fleet.MDMAppleCommandResult, error) { return nil, nil } ds.NewMDMAppleInstallerFunc = func(ctx context.Context, name string, size int64, manifest string, installer []byte, urlToken string) (*fleet.MDMAppleInstaller, error) { return nil, nil } ds.MDMAppleInstallerFunc = func(ctx context.Context, token string) (*fleet.MDMAppleInstaller, error) { return nil, nil } ds.MDMAppleInstallerDetailsByIDFunc = func(ctx context.Context, id uint) (*fleet.MDMAppleInstaller, error) { return nil, nil } ds.DeleteMDMAppleInstallerFunc = func(ctx context.Context, id uint) error { return nil } ds.MDMAppleInstallerDetailsByTokenFunc = func(ctx context.Context, token string) (*fleet.MDMAppleInstaller, error) { return nil, nil } ds.ListMDMAppleInstallersFunc = func(ctx context.Context) ([]fleet.MDMAppleInstaller, error) { return nil, nil } ds.MDMAppleListDevicesFunc = func(ctx context.Context) ([]fleet.MDMAppleDevice, error) { return nil, nil } ds.GetNanoMDMEnrollmentStatusFunc = func(ctx context.Context, hostUUID string) (bool, error) { return false, nil } return svc, ctx, ds } func TestAppleMDMAuthorization(t *testing.T) { svc, ctx, _ := setupAppleMDMService(t) checkAuthErr := func(t *testing.T, err error, shouldFailWithAuth bool) { t.Helper() if shouldFailWithAuth { require.Error(t, err) require.Contains(t, err.Error(), authz.ForbiddenErrorMessage) } else { require.NoError(t, err) } } testAuthdMethods := func(t *testing.T, user *fleet.User, shouldFailWithAuth bool) { ctx := test.UserContext(ctx, user) _, err := svc.NewMDMAppleEnrollmentProfile(ctx, fleet.MDMAppleEnrollmentProfilePayload{}) checkAuthErr(t, err, shouldFailWithAuth) _, err = svc.ListMDMAppleEnrollmentProfiles(ctx) checkAuthErr(t, err, shouldFailWithAuth) _, err = svc.GetMDMAppleCommandResults(ctx, "foo") checkAuthErr(t, err, shouldFailWithAuth) _, err = svc.UploadMDMAppleInstaller(ctx, "foo", 3, bytes.NewReader([]byte("foo"))) checkAuthErr(t, err, shouldFailWithAuth) _, err = svc.GetMDMAppleInstallerByID(ctx, 42) checkAuthErr(t, err, shouldFailWithAuth) err = svc.DeleteMDMAppleInstaller(ctx, 42) checkAuthErr(t, err, shouldFailWithAuth) _, err = svc.ListMDMAppleInstallers(ctx) checkAuthErr(t, err, shouldFailWithAuth) _, err = svc.ListMDMAppleDevices(ctx) checkAuthErr(t, err, shouldFailWithAuth) _, err = svc.ListMDMAppleDEPDevices(ctx) checkAuthErr(t, err, shouldFailWithAuth) _, _, err = svc.EnqueueMDMAppleCommand(ctx, &fleet.MDMAppleCommand{Command: &mdm.Command{}}, nil, false) checkAuthErr(t, err, shouldFailWithAuth) } // Only global admins can access the endpoints. testAuthdMethods(t, test.UserAdmin, false) // All other users should not have access to the endpoints. for _, user := range []*fleet.User{ test.UserNoRoles, test.UserMaintainer, test.UserObserver, test.UserTeamAdminTeam1, } { testAuthdMethods(t, user, true) } // Token authenticated endpoints can be accessed by anyone. ctx = test.UserContext(ctx, test.UserNoRoles) _, err := svc.GetMDMAppleInstallerByToken(ctx, "foo") require.NoError(t, err) _, err = svc.GetMDMAppleEnrollmentProfileByToken(ctx, "foo") require.NoError(t, err) _, err = svc.GetMDMAppleInstallerDetailsByToken(ctx, "foo") require.NoError(t, err) // Generating a new key pair does not actually make any changes to fleet, or expose any // information. The user must configure fleet with the new key pair and restart the server. _, err = svc.NewMDMAppleDEPKeyPair(ctx) require.NoError(t, err) // Must be device-authenticated, should fail _, err = svc.GetDeviceMDMAppleEnrollmentProfile(ctx) checkAuthErr(t, err, true) // works with device-authenticated context ctx = test.HostContext(context.Background(), &fleet.Host{}) _, err = svc.GetDeviceMDMAppleEnrollmentProfile(ctx) require.NoError(t, err) } func TestMDMAppleEnrollURL(t *testing.T) { svc := Service{} cases := []struct { appConfig *fleet.AppConfig expectedURL string }{ { appConfig: &fleet.AppConfig{ ServerSettings: fleet.ServerSettings{ ServerURL: "https://foo.example.com", }, }, expectedURL: "https://foo.example.com/api/mdm/apple/enroll?token=tok", }, { appConfig: &fleet.AppConfig{ ServerSettings: fleet.ServerSettings{ ServerURL: "https://foo.example.com/", }, }, expectedURL: "https://foo.example.com/api/mdm/apple/enroll?token=tok", }, } for _, tt := range cases { enrollURL, err := svc.mdmAppleEnrollURL("tok", tt.appConfig) require.NoError(t, err) require.Equal(t, tt.expectedURL, enrollURL) } } func TestMDMAppleConfigProfileAuthz(t *testing.T) { svc, ctx, ds := setupAppleMDMService(t) testCases := []struct { name string user *fleet.User shouldFailGlobal bool shouldFailTeam bool }{ { "global admin", &fleet.User{GlobalRole: ptr.String(fleet.RoleAdmin)}, false, false, }, { "global maintainer", &fleet.User{GlobalRole: ptr.String(fleet.RoleMaintainer)}, false, false, }, { "global observer", &fleet.User{GlobalRole: ptr.String(fleet.RoleObserver)}, true, true, }, { "team admin, belongs to team", &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 1}, Role: fleet.RoleAdmin}}}, true, false, }, { "team admin, DOES NOT belong to team", &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 2}, Role: fleet.RoleAdmin}}}, true, true, }, { "team maintainer, belongs to team", &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 1}, Role: fleet.RoleMaintainer}}}, true, false, }, { "team maintainer, DOES NOT belong to team", &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 2}, Role: fleet.RoleMaintainer}}}, true, true, }, { "team observer, belongs to team", &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 1}, Role: fleet.RoleObserver}}}, true, true, }, { "team observer, DOES NOT belong to team", &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 2}, Role: fleet.RoleObserver}}}, true, true, }, { "user no roles", &fleet.User{ID: 1337}, true, true, }, } ds.NewMDMAppleConfigProfileFunc = func(ctx context.Context, cp fleet.MDMAppleConfigProfile) (*fleet.MDMAppleConfigProfile, error) { return &cp, nil } ds.ListMDMAppleConfigProfilesFunc = func(ctx context.Context, teamID *uint) ([]*fleet.MDMAppleConfigProfile, error) { return nil, nil } ds.NewActivityFunc = func(context.Context, *fleet.User, fleet.ActivityDetails) error { return nil } mockGetFuncWithTeamID := func(teamID uint) mock.GetMDMAppleConfigProfileFunc { return func(ctx context.Context, profileID uint) (*fleet.MDMAppleConfigProfile, error) { require.Equal(t, uint(42), profileID) return &fleet.MDMAppleConfigProfile{TeamID: &teamID}, nil } } mockDeleteFuncWithTeamID := func(teamID uint) mock.DeleteMDMAppleConfigProfileFunc { return func(ctx context.Context, profileID uint) error { require.Equal(t, uint(42), profileID) return nil } } mockTeamFuncWithUser := func(u *fleet.User) mock.TeamFunc { return func(ctx context.Context, teamID uint) (*fleet.Team, error) { if len(u.Teams) > 0 { for _, t := range u.Teams { if t.ID == teamID { return &fleet.Team{ID: teamID, Users: []fleet.TeamUser{{User: *u, Role: t.Role}}}, nil } } } return &fleet.Team{}, nil } } checkShouldFail := func(err error, shouldFail bool) { if !shouldFail { require.NoError(t, err) } else { require.Error(t, err) require.Contains(t, err.Error(), authz.ForbiddenErrorMessage) } } mcBytes := mcBytesForTest("Foo", "Bar", "UUID") for _, tt := range testCases { ctx := viewer.NewContext(ctx, viewer.Viewer{User: tt.user}) ds.TeamFunc = mockTeamFuncWithUser(tt.user) t.Run(tt.name, func(t *testing.T) { // test authz create new profile (no team) _, err := svc.NewMDMAppleConfigProfile(ctx, 0, bytes.NewReader(mcBytes), int64(len(mcBytes))) checkShouldFail(err, tt.shouldFailGlobal) // test authz create new profile (team 1) _, err = svc.NewMDMAppleConfigProfile(ctx, 1, bytes.NewReader(mcBytes), int64(len(mcBytes))) checkShouldFail(err, tt.shouldFailTeam) // test authz list profiles (no team) _, err = svc.ListMDMAppleConfigProfiles(ctx, 0) checkShouldFail(err, tt.shouldFailGlobal) // test authz list profiles (team 1) _, err = svc.ListMDMAppleConfigProfiles(ctx, 1) checkShouldFail(err, tt.shouldFailTeam) // test authz get config profile (no team) ds.GetMDMAppleConfigProfileFunc = mockGetFuncWithTeamID(0) _, err = svc.GetMDMAppleConfigProfile(ctx, 42) checkShouldFail(err, tt.shouldFailGlobal) // test authz delete config profile (no team) ds.DeleteMDMAppleConfigProfileFunc = mockDeleteFuncWithTeamID(0) err = svc.DeleteMDMAppleConfigProfile(ctx, 42) checkShouldFail(err, tt.shouldFailGlobal) // test authz get config profile (team 1) ds.GetMDMAppleConfigProfileFunc = mockGetFuncWithTeamID(1) _, err = svc.GetMDMAppleConfigProfile(ctx, 42) checkShouldFail(err, tt.shouldFailTeam) // test authz delete config profile (team 1) ds.DeleteMDMAppleConfigProfileFunc = mockDeleteFuncWithTeamID(1) err = svc.DeleteMDMAppleConfigProfile(ctx, 42) checkShouldFail(err, tt.shouldFailTeam) }) } } func TestNewMDMAppleConfigProfile(t *testing.T) { svc, ctx, ds := setupAppleMDMService(t) ctx = viewer.NewContext(ctx, viewer.Viewer{User: &fleet.User{GlobalRole: ptr.String(fleet.RoleAdmin)}}) mcBytes := mcBytesForTest("Foo", "Bar", "UUID") r := bytes.NewReader(mcBytes) ds.NewMDMAppleConfigProfileFunc = func(ctx context.Context, cp fleet.MDMAppleConfigProfile) (*fleet.MDMAppleConfigProfile, error) { require.Equal(t, "Foo", cp.Name) require.Equal(t, "Bar", cp.Identifier) require.Equal(t, mcBytes, []byte(cp.Mobileconfig)) cp.ProfileID = 1 return &cp, nil } ds.NewActivityFunc = func(context.Context, *fleet.User, fleet.ActivityDetails) error { return nil } cp, err := svc.NewMDMAppleConfigProfile(ctx, 0, r, r.Size()) require.NoError(t, err) require.Equal(t, "Foo", cp.Name) require.Equal(t, "Bar", cp.Identifier) require.Equal(t, mcBytes, []byte(cp.Mobileconfig)) } func mcBytesForTest(name, identifier, uuid string) []byte { return []byte(fmt.Sprintf(` PayloadContent PayloadDisplayName %s PayloadIdentifier %s PayloadType Configuration PayloadUUID %s PayloadVersion 1 `, name, identifier, uuid)) } func TestHostDetailsMDMProfiles(t *testing.T) { svc, ctx, ds := setupAppleMDMService(t) ctx = viewer.NewContext(ctx, viewer.Viewer{User: &fleet.User{GlobalRole: ptr.String(fleet.RoleAdmin)}}) expected := []fleet.HostMDMAppleProfile{ {HostUUID: "H057-UU1D-1337", Name: "NAME-5", ProfileID: uint(5), CommandUUID: "CMD-UU1D-5", Status: &fleet.MDMAppleDeliveryPending, OperationType: fleet.MDMAppleOperationTypeInstall, Detail: ""}, {HostUUID: "H057-UU1D-1337", Name: "NAME-9", ProfileID: uint(8), CommandUUID: "CMD-UU1D-8", Status: &fleet.MDMAppleDeliveryApplied, OperationType: fleet.MDMAppleOperationTypeInstall, Detail: ""}, {HostUUID: "H057-UU1D-1337", Name: "NAME-13", ProfileID: uint(13), CommandUUID: "CMD-UU1D-13", Status: &fleet.MDMAppleDeliveryFailed, OperationType: fleet.MDMAppleOperationTypeRemove, Detail: "Error removing profile"}, } expectedByProfileID := make(map[uint]fleet.HostMDMAppleProfile) for _, ep := range expected { expectedByProfileID[ep.ProfileID] = ep } ds.GetHostMDMProfilesFunc = func(ctx context.Context, hostUUID string) ([]fleet.HostMDMAppleProfile, error) { if hostUUID == "H057-UU1D-1337" { return expected, nil } return []fleet.HostMDMAppleProfile{}, nil } ds.HostFunc = func(ctx context.Context, hostID uint) (*fleet.Host, error) { if hostID == uint(42) { return &fleet.Host{ID: uint(42), UUID: "H057-UU1D-1337"}, nil } return &fleet.Host{ID: hostID, UUID: "WR0N6-UU1D"}, nil } ds.HostByIdentifierFunc = func(ctx context.Context, identifier string) (*fleet.Host, error) { if identifier == "h0571d3n71f13r" { return &fleet.Host{ID: uint(42), UUID: "H057-UU1D-1337"}, nil } return &fleet.Host{ID: uint(21), UUID: "WR0N6-UU1D"}, nil } ds.LoadHostSoftwareFunc = func(ctx context.Context, host *fleet.Host, includeCVEScores bool) error { return nil } ds.ListLabelsForHostFunc = func(ctx context.Context, hid uint) ([]*fleet.Label, error) { return nil, nil } ds.ListPacksForHostFunc = func(ctx context.Context, hid uint) (packs []*fleet.Pack, err error) { return nil, nil } ds.ListHostBatteriesFunc = func(ctx context.Context, id uint) ([]*fleet.HostBattery, error) { return nil, nil } ds.ListPoliciesForHostFunc = func(ctx context.Context, host *fleet.Host) ([]*fleet.HostPolicy, error) { return nil, nil } expectedNilSlice := []fleet.HostMDMAppleProfile(nil) expectedEmptySlice := []fleet.HostMDMAppleProfile{} cases := []struct { name string mdmEnabled bool hostID *uint hostIdentifier *string expected *[]fleet.HostMDMAppleProfile }{ { name: "TestGetHostMDMProfilesOK", mdmEnabled: true, hostID: ptr.Uint(42), hostIdentifier: nil, expected: &expected, }, { name: "TestGetHostMDMProfilesEmpty", mdmEnabled: true, hostID: ptr.Uint(21), hostIdentifier: nil, expected: &expectedEmptySlice, }, { name: "TestGetHostMDMProfilesNil", mdmEnabled: false, hostID: ptr.Uint(42), hostIdentifier: nil, expected: &expectedNilSlice, }, { name: "TestHostByIdentifierMDMProfilesOK", mdmEnabled: true, hostID: nil, hostIdentifier: ptr.String("h0571d3n71f13r"), expected: &expected, }, { name: "TestHostByIdentifierMDMProfilesNil", mdmEnabled: false, hostID: nil, hostIdentifier: ptr.String("h0571d3n71f13r"), expected: &expectedNilSlice, }, { name: "TestHostByIdentifierMDMProfilesEmpty", mdmEnabled: true, hostID: nil, hostIdentifier: ptr.String("4n07h3r1d3n71f13r"), expected: &expectedEmptySlice, }, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { ds.AppConfigFunc = func(context.Context) (*fleet.AppConfig, error) { return &fleet.AppConfig{MDM: fleet.MDM{EnabledAndConfigured: c.mdmEnabled}}, nil } ds.AppConfigFuncInvoked = false ds.HostFuncInvoked = false ds.HostByIdentifierFuncInvoked = false ds.GetHostMDMProfilesFuncInvoked = false var gotHost *fleet.HostDetail if c.hostID != nil { h, err := svc.GetHost(ctx, *c.hostID, fleet.HostDetailOptions{}) require.NoError(t, err) require.True(t, ds.HostFuncInvoked) gotHost = h } if c.hostIdentifier != nil { h, err := svc.HostByIdentifier(ctx, *c.hostIdentifier, fleet.HostDetailOptions{}) require.NoError(t, err) require.True(t, ds.HostByIdentifierFuncInvoked) gotHost = h } require.NotNil(t, gotHost) require.True(t, ds.AppConfigFuncInvoked) if !c.mdmEnabled { require.Equal(t, gotHost.MDM.Profiles, c.expected) return } require.True(t, ds.GetHostMDMProfilesFuncInvoked) require.NotNil(t, gotHost.MDM.Profiles) require.ElementsMatch(t, *c.expected, *gotHost.MDM.Profiles) }) } } func TestAppleMDMEnrollmentProfile(t *testing.T) { svc, ctx, _ := setupAppleMDMService(t) // Only global admins can create enrollment profiles. ctx = test.UserContext(ctx, test.UserAdmin) _, err := svc.NewMDMAppleEnrollmentProfile(ctx, fleet.MDMAppleEnrollmentProfilePayload{}) require.NoError(t, err) // All other users should not have access to the endpoints. for _, user := range []*fleet.User{ test.UserNoRoles, test.UserMaintainer, test.UserObserver, test.UserTeamAdminTeam1, } { ctx := test.UserContext(ctx, user) _, err := svc.NewMDMAppleEnrollmentProfile(ctx, fleet.MDMAppleEnrollmentProfilePayload{}) require.Error(t, err) require.Contains(t, err.Error(), authz.ForbiddenErrorMessage) } } func TestMDMCommandAuthz(t *testing.T) { svc, ctx, ds := setupAppleMDMService(t) ds.HostLiteFunc = func(ctx context.Context, hostID uint) (*fleet.Host, error) { switch hostID { case 1: return &fleet.Host{UUID: "test-host-team-1", TeamID: ptr.Uint(1)}, nil default: return &fleet.Host{UUID: "test-host-no-team"}, nil } } ds.GetHostMDMCheckinInfoFunc = func(ctx context.Context, hostUUID string) (*fleet.HostMDMCheckinInfo, error) { return &fleet.HostMDMCheckinInfo{}, nil } ds.NewActivityFunc = func(context.Context, *fleet.User, fleet.ActivityDetails) error { return nil } var mdmEnabled atomic.Bool ds.GetNanoMDMEnrollmentStatusFunc = func(ctx context.Context, hostUUID string) (bool, error) { // This function is called twice during EnqueueMDMAppleCommandRemoveEnrollmentProfile. // It first is called to check that the device is enrolled as a pre-condition to enqueueing the // command. It is called second time after the command has been enqueued to check whether // the device was successfully unenrolled. // // For each test run, the bool should be initialized to true to simulate an existing device // that is initially enrolled to Fleet's MDM. return mdmEnabled.Swap(!mdmEnabled.Load()), nil } testCases := []struct { name string user *fleet.User shouldFailGlobal bool shouldFailTeam bool }{ { "global admin", &fleet.User{GlobalRole: ptr.String(fleet.RoleAdmin)}, false, false, }, { "global maintainer", &fleet.User{GlobalRole: ptr.String(fleet.RoleMaintainer)}, false, false, }, { "global observer", &fleet.User{GlobalRole: ptr.String(fleet.RoleObserver)}, true, true, }, { "team admin, belongs to team", &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 1}, Role: fleet.RoleAdmin}}}, true, false, }, { "team admin, DOES NOT belong to team", &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 2}, Role: fleet.RoleAdmin}}}, true, true, }, { "team maintainer, belongs to team", &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 1}, Role: fleet.RoleMaintainer}}}, true, false, }, { "team maintainer, DOES NOT belong to team", &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 2}, Role: fleet.RoleMaintainer}}}, true, true, }, { "team observer, belongs to team", &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 1}, Role: fleet.RoleObserver}}}, true, true, }, { "team observer, DOES NOT belong to team", &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 2}, Role: fleet.RoleObserver}}}, true, true, }, { "user no roles", &fleet.User{ID: 1337}, true, true, }, } for _, tt := range testCases { t.Run(tt.name, func(t *testing.T) { ctx := viewer.NewContext(ctx, viewer.Viewer{User: tt.user}) mdmEnabled.Store(true) err := svc.EnqueueMDMAppleCommandRemoveEnrollmentProfile(ctx, 42) // global host if !tt.shouldFailGlobal { require.NoError(t, err) } else { require.Error(t, err) require.Contains(t, err.Error(), authz.ForbiddenErrorMessage) } mdmEnabled.Store(true) err = svc.EnqueueMDMAppleCommandRemoveEnrollmentProfile(ctx, 1) // host belongs to team 1 if !tt.shouldFailTeam { require.NoError(t, err) } else { require.Error(t, err) require.Contains(t, err.Error(), authz.ForbiddenErrorMessage) } }) } } func TestMDMAuthenticate(t *testing.T) { ds := new(mock.Store) svc := MDMAppleCheckinAndCommandService{ds: ds} ctx := context.Background() uuid, serial, model := "ABC-DEF-GHI", "XYZABC", "MacBookPro 16,1" ds.IngestMDMAppleDeviceFromCheckinFunc = func(ctx context.Context, mdmHost fleet.MDMAppleHostDetails) error { require.Equal(t, uuid, mdmHost.UDID) require.Equal(t, serial, mdmHost.SerialNumber) require.Equal(t, model, mdmHost.Model) return nil } ds.GetHostMDMCheckinInfoFunc = func(ct context.Context, hostUUID string) (*fleet.HostMDMCheckinInfo, error) { require.Equal(t, uuid, hostUUID) return &fleet.HostMDMCheckinInfo{HardwareSerial: serial, DisplayName: fmt.Sprintf("%s (%s)", model, serial), InstalledFromDEP: false}, nil } ds.NewActivityFunc = func(ctx context.Context, user *fleet.User, activity fleet.ActivityDetails) error { a, ok := activity.(*fleet.ActivityTypeMDMEnrolled) require.True(t, ok) require.Nil(t, user) require.Equal(t, "mdm_enrolled", activity.ActivityName()) require.Equal(t, serial, a.HostSerial) require.Equal(t, a.HostDisplayName, fmt.Sprintf("%s (%s)", model, serial)) require.False(t, a.InstalledFromDEP) return nil } err := svc.Authenticate( &mdm.Request{Context: ctx}, &mdm.Authenticate{ Enrollment: mdm.Enrollment{ UDID: uuid, }, SerialNumber: serial, Model: model, }, ) require.NoError(t, err) require.True(t, ds.IngestMDMAppleDeviceFromCheckinFuncInvoked) require.True(t, ds.GetHostMDMCheckinInfoFuncInvoked) require.True(t, ds.NewActivityFuncInvoked) } func TestMDMCheckout(t *testing.T) { ds := new(mock.Store) svc := MDMAppleCheckinAndCommandService{ds: ds} ctx := context.Background() uuid, serial, installedFromDEP, displayName := "ABC-DEF-GHI", "XYZABC", true, "Test's MacBook" ds.UpdateHostTablesOnMDMUnenrollFunc = func(ctx context.Context, hostUUID string) error { require.Equal(t, uuid, hostUUID) return nil } ds.GetHostMDMCheckinInfoFunc = func(ct context.Context, hostUUID string) (*fleet.HostMDMCheckinInfo, error) { require.Equal(t, uuid, hostUUID) return &fleet.HostMDMCheckinInfo{ HardwareSerial: serial, DisplayName: displayName, InstalledFromDEP: installedFromDEP, }, nil } ds.NewActivityFunc = func(ctx context.Context, user *fleet.User, activity fleet.ActivityDetails) error { a, ok := activity.(*fleet.ActivityTypeMDMUnenrolled) require.True(t, ok) require.Nil(t, user) require.Equal(t, "mdm_unenrolled", activity.ActivityName()) require.Equal(t, serial, a.HostSerial) require.Equal(t, displayName, a.HostDisplayName) require.True(t, a.InstalledFromDEP) return nil } err := svc.CheckOut( &mdm.Request{Context: ctx}, &mdm.CheckOut{ Enrollment: mdm.Enrollment{ UDID: uuid, }, }, ) require.NoError(t, err) require.True(t, ds.UpdateHostTablesOnMDMUnenrollFuncInvoked) require.True(t, ds.GetHostMDMCheckinInfoFuncInvoked) require.True(t, ds.NewActivityFuncInvoked) } func TestMDMCommandAndReportResultsProfileHandling(t *testing.T) { ds := new(mock.Store) svc := MDMAppleCheckinAndCommandService{ds: ds} ctx := context.Background() hostUUID := "ABC-DEF-GHI" commandUUID := "COMMAND-UUID" cases := []struct { status string requestType string errors []mdm.ErrorChain want *fleet.HostMDMAppleProfile }{ { status: "Acknowledged", requestType: "InstallProfile", errors: nil, want: &fleet.HostMDMAppleProfile{ Status: &fleet.MDMAppleDeliveryApplied, Detail: "", OperationType: fleet.MDMAppleOperationTypeInstall, }, }, { status: "Acknowledged", requestType: "RemoveProfile", errors: nil, want: &fleet.HostMDMAppleProfile{ Status: &fleet.MDMAppleDeliveryApplied, Detail: "", OperationType: fleet.MDMAppleOperationTypeRemove, }, }, { status: "Error", requestType: "InstallProfile", errors: []mdm.ErrorChain{ {ErrorCode: 123, ErrorDomain: "testDomain", USEnglishDescription: "testMessage"}, }, want: &fleet.HostMDMAppleProfile{ Status: &fleet.MDMAppleDeliveryFailed, Detail: "testDomain (123): testMessage\n", OperationType: fleet.MDMAppleOperationTypeInstall, }, }, { status: "Error", requestType: "RemoveProfile", errors: []mdm.ErrorChain{ {ErrorCode: 123, ErrorDomain: "testDomain", USEnglishDescription: "testMessage"}, {ErrorCode: 321, ErrorDomain: "domainTest", USEnglishDescription: "messageTest"}, }, want: &fleet.HostMDMAppleProfile{ Status: &fleet.MDMAppleDeliveryFailed, Detail: "testDomain (123): testMessage\ndomainTest (321): messageTest\n", OperationType: fleet.MDMAppleOperationTypeRemove, }, }, { status: "Error", requestType: "RemoveProfile", errors: nil, want: &fleet.HostMDMAppleProfile{ Status: &fleet.MDMAppleDeliveryFailed, Detail: "", OperationType: fleet.MDMAppleOperationTypeRemove, }, }, } for _, c := range cases { ds.GetMDMAppleCommandRequestTypeFunc = func(ctx context.Context, targetCmd string) (string, error) { require.Equal(t, commandUUID, targetCmd) return c.requestType, nil } ds.UpdateHostMDMAppleProfileFunc = func(ctx context.Context, profile *fleet.HostMDMAppleProfile) error { c.want.CommandUUID = commandUUID c.want.HostUUID = hostUUID require.Equal(t, c.want, profile) return nil } _, err := svc.CommandAndReportResults( &mdm.Request{Context: ctx}, &mdm.CommandResults{ Enrollment: mdm.Enrollment{UDID: hostUUID}, CommandUUID: commandUUID, Status: c.status, RequestType: c.requestType, ErrorChain: c.errors, }, ) require.NoError(t, err) require.True(t, ds.GetMDMAppleCommandRequestTypeFuncInvoked) require.True(t, ds.UpdateHostMDMAppleProfileFuncInvoked) } } func TestMDMBatchSetAppleProfiles(t *testing.T) { svc, ctx, ds := setupAppleMDMService(t) ds.TeamByNameFunc = func(ctx context.Context, name string) (*fleet.Team, error) { return &fleet.Team{ID: 1, Name: name}, nil } ds.TeamFunc = func(ctx context.Context, id uint) (*fleet.Team, error) { return &fleet.Team{ID: id, Name: "team"}, nil } ds.BatchSetMDMAppleProfilesFunc = func(ctx context.Context, teamID *uint, profiles []*fleet.MDMAppleConfigProfile) error { return nil } ds.NewActivityFunc = func(ctx context.Context, user *fleet.User, activity fleet.ActivityDetails) error { return nil } testCases := []struct { name string user *fleet.User premium bool teamID *uint teamName *string profiles [][]byte wantErr string }{ { "global admin", &fleet.User{GlobalRole: ptr.String(fleet.RoleAdmin)}, false, nil, nil, nil, "", }, { "global admin, team", &fleet.User{GlobalRole: ptr.String(fleet.RoleAdmin)}, true, ptr.Uint(1), nil, nil, "", }, { "global maintainer", &fleet.User{GlobalRole: ptr.String(fleet.RoleMaintainer)}, false, nil, nil, nil, "", }, { "global maintainer, team", &fleet.User{GlobalRole: ptr.String(fleet.RoleMaintainer)}, true, ptr.Uint(1), nil, nil, "", }, { "global observer", &fleet.User{GlobalRole: ptr.String(fleet.RoleObserver)}, false, nil, nil, nil, authz.ForbiddenErrorMessage, }, { "team admin, DOES belong to team", &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 1}, Role: fleet.RoleAdmin}}}, true, ptr.Uint(1), nil, nil, "", }, { "team admin, DOES belong to team by name", &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 1}, Role: fleet.RoleAdmin}}}, true, nil, ptr.String("team"), nil, "", }, { "team admin, DOES NOT belong to team", &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 2}, Role: fleet.RoleAdmin}}}, true, ptr.Uint(1), nil, nil, authz.ForbiddenErrorMessage, }, { "team admin, DOES NOT belong to team by name", &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 2}, Role: fleet.RoleAdmin}}}, true, nil, ptr.String("team"), nil, authz.ForbiddenErrorMessage, }, { "team maintainer, DOES belong to team", &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 1}, Role: fleet.RoleMaintainer}}}, true, ptr.Uint(1), nil, nil, "", }, { "team maintainer, DOES NOT belong to team", &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 2}, Role: fleet.RoleMaintainer}}}, true, ptr.Uint(1), nil, nil, authz.ForbiddenErrorMessage, }, { "team observer, DOES belong to team", &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 1}, Role: fleet.RoleObserver}}}, true, ptr.Uint(1), nil, nil, authz.ForbiddenErrorMessage, }, { "team observer, DOES NOT belong to team", &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 2}, Role: fleet.RoleObserver}}}, true, ptr.Uint(1), nil, nil, authz.ForbiddenErrorMessage, }, { "user no roles", &fleet.User{ID: 1337}, false, nil, nil, nil, authz.ForbiddenErrorMessage, }, { "team id with free license", &fleet.User{GlobalRole: ptr.String(fleet.RoleAdmin)}, false, ptr.Uint(1), nil, nil, ErrMissingLicense.Error(), }, { "team name with free license", &fleet.User{GlobalRole: ptr.String(fleet.RoleAdmin)}, false, nil, ptr.String("team"), nil, ErrMissingLicense.Error(), }, { "team id and name specified", &fleet.User{GlobalRole: ptr.String(fleet.RoleAdmin)}, true, ptr.Uint(1), ptr.String("team"), nil, "cannot specify both team_id and team_name", }, { "duplicate profile name", &fleet.User{GlobalRole: ptr.String(fleet.RoleAdmin)}, true, ptr.Uint(1), nil, [][]byte{ mobileconfigForTest("N1", "I1"), mobileconfigForTest("N1", "I2"), }, `More than one configuration profile have the same name (PayloadDisplayName): "N1"`, }, { "duplicate profile identifier", &fleet.User{GlobalRole: ptr.String(fleet.RoleAdmin)}, true, ptr.Uint(1), nil, [][]byte{ mobileconfigForTest("N1", "I1"), mobileconfigForTest("N2", "I2"), mobileconfigForTest("N3", "I1"), }, `More than one configuration profile have the same identifier (PayloadIdentifier): "I1"`, }, { "no duplicates", &fleet.User{GlobalRole: ptr.String(fleet.RoleAdmin)}, false, nil, nil, [][]byte{ mobileconfigForTest("N1", "I1"), mobileconfigForTest("N2", "I2"), mobileconfigForTest("N3", "I3"), }, ``, }, } for _, tt := range testCases { t.Run(tt.name, func(t *testing.T) { defer func() { ds.BatchSetMDMAppleProfilesFuncInvoked = false }() // prepare the context with the user and license ctx := viewer.NewContext(ctx, viewer.Viewer{User: tt.user}) tier := fleet.TierFree if tt.premium { tier = fleet.TierPremium } ctx = license.NewContext(ctx, &fleet.LicenseInfo{Tier: tier}) err := svc.BatchSetMDMAppleProfiles(ctx, tt.teamID, tt.teamName, tt.profiles, false) if tt.wantErr == "" { require.NoError(t, err) require.True(t, ds.BatchSetMDMAppleProfilesFuncInvoked) return } require.Error(t, err) require.ErrorContains(t, err, tt.wantErr) require.False(t, ds.BatchSetMDMAppleProfilesFuncInvoked) }) } } func TestMDMAppleCommander(t *testing.T) { ctx := context.Background() mdmStorage := &nanomdm_mock.Storage{} pushFactory, _ := newMockAPNSPushProviderFactory() pusher := nanomdm_pushsvc.New( mdmStorage, mdmStorage, pushFactory, NewNanoMDMLogger(kitlog.NewJSONLogger(os.Stdout)), ) cmdr := NewMDMAppleCommander(mdmStorage, pusher) // TODO(roberto): there's a data race in the mock when more // than one host ID is provided because the pusher uses one // goroutine per uuid to send the commands hostUUIDs := []string{"A"} payloadName := "com.foo.bar" payloadIdentifier := "com-foo-bar" mc := mobileconfigForTest(payloadName, payloadIdentifier) mdmStorage.EnqueueCommandFunc = func(ctx context.Context, id []string, cmd *mdm.Command) (map[string]error, error) { require.NotNil(t, cmd) require.Equal(t, cmd.Command.RequestType, "InstallProfile") require.Contains(t, string(cmd.Raw), base64.StdEncoding.EncodeToString(mc)) return nil, nil } mdmStorage.RetrievePushInfoFunc = func(p0 context.Context, targetUUIDs []string) (map[string]*mdm.Push, error) { require.ElementsMatch(t, hostUUIDs, targetUUIDs) pushes := make(map[string]*mdm.Push, len(targetUUIDs)) for _, uuid := range targetUUIDs { pushes[uuid] = &mdm.Push{ PushMagic: "magic" + uuid, Token: []byte("token" + uuid), Topic: "topic" + uuid, } } return pushes, nil } mdmStorage.RetrievePushCertFunc = func(ctx context.Context, topic string) (*tls.Certificate, string, error) { cert, err := tls.LoadX509KeyPair("testdata/server.pem", "testdata/server.key") return &cert, "", err } mdmStorage.IsPushCertStaleFunc = func(ctx context.Context, topic string, staleToken string) (bool, error) { return false, nil } cmdUUID := uuid.New().String() err := cmdr.InstallProfile(ctx, hostUUIDs, mc, cmdUUID) require.NotEmpty(t, cmdUUID) require.NoError(t, err) require.True(t, mdmStorage.EnqueueCommandFuncInvoked) mdmStorage.EnqueueCommandFuncInvoked = false require.True(t, mdmStorage.RetrievePushInfoFuncInvoked) mdmStorage.RetrievePushInfoFuncInvoked = false mdmStorage.EnqueueCommandFunc = func(ctx context.Context, id []string, cmd *mdm.Command) (map[string]error, error) { require.NotNil(t, cmd) require.Equal(t, "RemoveProfile", cmd.Command.RequestType) require.Contains(t, string(cmd.Raw), payloadIdentifier) return nil, nil } cmdUUID = uuid.New().String() err = cmdr.RemoveProfile(ctx, hostUUIDs, payloadIdentifier, cmdUUID) require.True(t, mdmStorage.EnqueueCommandFuncInvoked) mdmStorage.EnqueueCommandFuncInvoked = false require.True(t, mdmStorage.RetrievePushInfoFuncInvoked) mdmStorage.RetrievePushInfoFuncInvoked = false require.NotEmpty(t, cmdUUID) require.NoError(t, err) } func TestMDMAppleReconcileProfiles(t *testing.T) { ctx := context.Background() mdmStorage := &nanomdm_mock.Storage{} ds := new(mock.Store) pushFactory, _ := newMockAPNSPushProviderFactory() pusher := nanomdm_pushsvc.New( mdmStorage, mdmStorage, pushFactory, NewNanoMDMLogger(kitlog.NewNopLogger()), ) cmdr := NewMDMAppleCommander(mdmStorage, pusher) hostUUID := "ABC-DEF" contents1 := []byte("test-content-1") contents1Base64 := base64.StdEncoding.EncodeToString(contents1) contents2 := []byte("test-content-2") contents2Base64 := base64.StdEncoding.EncodeToString(contents2) ds.ListMDMAppleProfilesToInstallFunc = func(ctx context.Context) ([]*fleet.MDMAppleProfilePayload, error) { return []*fleet.MDMAppleProfilePayload{ {ProfileID: 1, ProfileIdentifier: "com.add.profile", HostUUID: hostUUID}, {ProfileID: 2, ProfileIdentifier: "com.add.profile.two", HostUUID: hostUUID}, }, nil } ds.ListMDMAppleProfilesToRemoveFunc = func(ctx context.Context) ([]*fleet.MDMAppleProfilePayload, error) { return []*fleet.MDMAppleProfilePayload{ {ProfileID: 3, ProfileIdentifier: "com.remove.profile", HostUUID: hostUUID}, }, nil } ds.GetMDMAppleProfilesContentsFunc = func(ctx context.Context, profileIDs []uint) (map[uint]fleet.Mobileconfig, error) { require.ElementsMatch(t, []uint{1, 2}, profileIDs) return map[uint]fleet.Mobileconfig{ 1: contents1, 2: contents2, }, nil } mdmStorage.EnqueueCommandFunc = func(ctx context.Context, id []string, cmd *mdm.Command) (map[string]error, error) { require.NotNil(t, cmd) require.ElementsMatch(t, []string{hostUUID}, id) switch cmd.Command.RequestType { case "InstallProfile": if !strings.Contains(string(cmd.Raw), contents1Base64) && !strings.Contains(string(cmd.Raw), contents2Base64) { require.Failf(t, "profile contents don't match", "expected to contain %s or %s but got %s", contents1Base64, contents2Base64, string(cmd.Raw)) } case "RemoveProfile": require.Contains(t, string(cmd.Raw), "com.remove.profile") } return nil, nil } mdmStorage.RetrievePushInfoFunc = func(ctx context.Context, tokens []string) (map[string]*mdm.Push, error) { res := make(map[string]*mdm.Push, len(tokens)) for _, t := range tokens { res[t] = &mdm.Push{ PushMagic: "", Token: []byte(t), Topic: "", } } return res, nil } mdmStorage.RetrievePushCertFunc = func(ctx context.Context, topic string) (*tls.Certificate, string, error) { cert, err := tls.LoadX509KeyPair("testdata/server.pem", "testdata/server.key") return &cert, "", err } mdmStorage.IsPushCertStaleFunc = func(ctx context.Context, topic string, staleToken string) (bool, error) { return false, nil } ds.BulkUpsertMDMAppleHostProfilesFunc = func(ctx context.Context, payload []*fleet.MDMAppleBulkUpsertHostProfilePayload) error { // TODO: this function is called a second time with a non-empty slice // if there are any errors, test this scenario if len(payload) == 0 { return nil } require.ElementsMatch(t, []*fleet.MDMAppleBulkUpsertHostProfilePayload{ { ProfileID: 1, ProfileIdentifier: "com.add.profile", HostUUID: hostUUID, OperationType: fleet.MDMAppleOperationTypeInstall, Status: &fleet.MDMAppleDeliveryPending, CommandUUID: payload[0].CommandUUID, }, { ProfileID: 2, ProfileIdentifier: "com.add.profile.two", HostUUID: hostUUID, OperationType: fleet.MDMAppleOperationTypeInstall, Status: &fleet.MDMAppleDeliveryPending, CommandUUID: payload[1].CommandUUID, }, { ProfileID: 3, ProfileIdentifier: "com.remove.profile", HostUUID: hostUUID, OperationType: fleet.MDMAppleOperationTypeRemove, Status: &fleet.MDMAppleDeliveryPending, CommandUUID: payload[2].CommandUUID, }, }, payload) return nil } // TODO(roberto): there's a data race in the mock when more // than one host ID is provided because the pusher uses one // goroutine per uuid to send the commands err := ReconcileProfiles(ctx, ds, cmdr, kitlog.NewNopLogger()) require.NoError(t, err) require.True(t, ds.ListMDMAppleProfilesToInstallFuncInvoked) require.True(t, ds.ListMDMAppleProfilesToRemoveFuncInvoked) require.True(t, ds.GetMDMAppleProfilesContentsFuncInvoked) require.True(t, ds.BulkUpsertMDMAppleHostProfilesFuncInvoked) } func mobileconfigForTest(name, identifier string) []byte { return []byte(fmt.Sprintf(` PayloadContent PayloadDisplayName %s PayloadIdentifier %s PayloadType Configuration PayloadUUID %s PayloadVersion 1 `, name, identifier, uuid.New().String())) }