2023-05-15 20:00:52 +00:00
package service
import (
"context"
2024-06-28 15:09:22 +00:00
"database/sql"
2024-11-18 22:44:25 +00:00
"errors"
2023-05-15 20:00:52 +00:00
"fmt"
"testing"
"time"
2024-11-18 22:44:25 +00:00
"github.com/fleetdm/fleet/v4/pkg/optjson"
2023-05-15 20:00:52 +00:00
"github.com/fleetdm/fleet/v4/server/fleet"
"github.com/fleetdm/fleet/v4/server/mock"
"github.com/fleetdm/fleet/v4/server/ptr"
2023-05-18 17:21:54 +00:00
"github.com/fleetdm/fleet/v4/server/test"
2024-07-02 16:32:49 +00:00
"github.com/stretchr/testify/assert"
2023-05-15 20:00:52 +00:00
"github.com/stretchr/testify/require"
)
func TestGetFleetDesktopSummary ( t * testing . T ) {
t . Run ( "free implementation" , func ( t * testing . T ) {
ds := new ( mock . Store )
svc , ctx := newTestService ( t , ds , nil , nil )
sum , err := svc . GetFleetDesktopSummary ( ctx )
require . ErrorIs ( t , err , fleet . ErrMissingLicense )
require . Empty ( t , sum )
} )
2023-05-18 17:21:54 +00:00
t . Run ( "different app config values for managed host" , func ( t * testing . T ) {
2023-05-15 20:00:52 +00:00
ds := new ( mock . Store )
license := & fleet . LicenseInfo { Tier : fleet . TierPremium , Expiration : time . Now ( ) . Add ( 24 * time . Hour ) }
svc , ctx := newTestService ( t , ds , nil , nil , & TestServerOpts { License : license , SkipCreateTestUsers : true } )
ds . FailingPoliciesCountFunc = func ( ctx context . Context , host * fleet . Host ) ( uint , error ) {
return uint ( 1 ) , nil
}
2024-07-02 16:32:49 +00:00
const expectedPlatform = "darwin"
ds . HasSelfServiceSoftwareInstallersFunc = func ( ctx context . Context , platform string , teamID * uint ) ( bool , error ) {
assert . Equal ( t , expectedPlatform , platform )
return true , nil
}
2023-05-15 20:00:52 +00:00
cases := [ ] struct {
2023-05-23 18:01:04 +00:00
mdm fleet . MDM
depAssigned bool
out fleet . DesktopNotifications
2023-05-15 20:00:52 +00:00
} {
{
mdm : fleet . MDM {
EnabledAndConfigured : true ,
MacOSMigration : fleet . MacOSMigration {
Enable : true ,
} ,
} ,
2023-05-23 18:01:04 +00:00
depAssigned : true ,
2023-05-15 20:00:52 +00:00
out : fleet . DesktopNotifications {
2023-05-18 17:21:54 +00:00
NeedsMDMMigration : true ,
RenewEnrollmentProfile : false ,
2023-05-15 20:00:52 +00:00
} ,
} ,
2023-05-23 18:01:04 +00:00
{
mdm : fleet . MDM {
EnabledAndConfigured : true ,
MacOSMigration : fleet . MacOSMigration {
Enable : true ,
} ,
} ,
depAssigned : false ,
out : fleet . DesktopNotifications {
NeedsMDMMigration : false ,
RenewEnrollmentProfile : false ,
} ,
} ,
2023-05-15 20:00:52 +00:00
{
mdm : fleet . MDM {
EnabledAndConfigured : false ,
MacOSMigration : fleet . MacOSMigration {
Enable : true ,
} ,
} ,
2023-05-23 18:01:04 +00:00
depAssigned : true ,
2023-05-15 20:00:52 +00:00
out : fleet . DesktopNotifications {
2023-05-18 17:21:54 +00:00
NeedsMDMMigration : false ,
RenewEnrollmentProfile : false ,
2023-05-15 20:00:52 +00:00
} ,
} ,
{
mdm : fleet . MDM {
EnabledAndConfigured : true ,
MacOSMigration : fleet . MacOSMigration {
Enable : false ,
} ,
} ,
2023-05-23 18:01:04 +00:00
depAssigned : true ,
2023-05-15 20:00:52 +00:00
out : fleet . DesktopNotifications {
2023-05-18 17:21:54 +00:00
NeedsMDMMigration : false ,
RenewEnrollmentProfile : false ,
2023-05-15 20:00:52 +00:00
} ,
} ,
{
mdm : fleet . MDM {
EnabledAndConfigured : false ,
MacOSMigration : fleet . MacOSMigration {
Enable : false ,
} ,
} ,
2023-05-23 18:01:04 +00:00
depAssigned : true ,
2023-05-15 20:00:52 +00:00
out : fleet . DesktopNotifications {
2023-05-18 17:21:54 +00:00
NeedsMDMMigration : false ,
RenewEnrollmentProfile : false ,
2023-05-15 20:00:52 +00:00
} ,
} ,
}
for _ , c := range cases {
2023-09-13 18:59:35 +00:00
c := c
2023-05-15 20:00:52 +00:00
ds . AppConfigFunc = func ( ctx context . Context ) ( * fleet . AppConfig , error ) {
appCfg := fleet . AppConfig { }
appCfg . MDM = c . mdm
return & appCfg , nil
}
2024-06-14 18:01:12 +00:00
ds . IsHostConnectedToFleetMDMFunc = func ( ctx context . Context , host * fleet . Host ) ( bool , error ) {
return false , nil
}
2024-06-28 15:09:22 +00:00
ds . GetHostMDMFunc = func ( ctx context . Context , hostID uint ) ( * fleet . HostMDM , error ) {
return & fleet . HostMDM {
2024-04-08 14:10:29 +00:00
IsServer : false ,
InstalledFromDep : true ,
Enrolled : true ,
Name : fleet . WellKnownMDMIntune ,
DEPProfileAssignStatus : ptr . String ( string ( fleet . DEPAssignProfileResponseSuccess ) ) ,
2024-06-28 15:09:22 +00:00
} , nil
}
ctx := test . HostContext ( ctx , & fleet . Host {
OsqueryHostID : ptr . String ( "test" ) ,
DEPAssignedToFleet : & c . depAssigned ,
2024-07-02 16:32:49 +00:00
Platform : expectedPlatform ,
2024-06-28 15:09:22 +00:00
} )
2023-05-15 20:00:52 +00:00
sum , err := svc . GetFleetDesktopSummary ( ctx )
require . NoError ( t , err )
require . Equal ( t , c . out , sum . Notifications , fmt . Sprintf ( "enabled_and_configured: %t | macos_migration.enable: %t" , c . mdm . EnabledAndConfigured , c . mdm . MacOSMigration . Enable ) )
require . EqualValues ( t , 1 , * sum . FailingPolicies )
2024-07-02 16:32:49 +00:00
assert . Equal ( t , ptr . Bool ( true ) , sum . SelfService )
2023-05-15 20:00:52 +00:00
}
} )
2023-05-18 17:21:54 +00:00
t . Run ( "different app config values for unmanaged host" , func ( t * testing . T ) {
ds := new ( mock . Store )
license := & fleet . LicenseInfo { Tier : fleet . TierPremium , Expiration : time . Now ( ) . Add ( 24 * time . Hour ) }
svc , ctx := newTestService ( t , ds , nil , nil , & TestServerOpts { License : license , SkipCreateTestUsers : true } )
ds . FailingPoliciesCountFunc = func ( ctx context . Context , host * fleet . Host ) ( uint , error ) {
return uint ( 1 ) , nil
}
2024-07-02 16:32:49 +00:00
ds . HasSelfServiceSoftwareInstallersFunc = func ( ctx context . Context , platform string , teamID * uint ) ( bool , error ) {
return true , nil
}
2023-05-18 17:21:54 +00:00
cases := [ ] struct {
2023-05-23 18:01:04 +00:00
mdm fleet . MDM
depAssigned bool
out fleet . DesktopNotifications
2023-05-18 17:21:54 +00:00
} {
{
mdm : fleet . MDM {
EnabledAndConfigured : true ,
MacOSMigration : fleet . MacOSMigration {
Enable : true ,
} ,
} ,
2023-05-23 18:01:04 +00:00
depAssigned : true ,
2023-05-18 17:21:54 +00:00
out : fleet . DesktopNotifications {
NeedsMDMMigration : false ,
RenewEnrollmentProfile : true ,
} ,
} ,
{
mdm : fleet . MDM {
EnabledAndConfigured : false ,
MacOSMigration : fleet . MacOSMigration {
Enable : true ,
} ,
} ,
2023-05-23 18:01:04 +00:00
depAssigned : true ,
2023-05-18 17:21:54 +00:00
out : fleet . DesktopNotifications {
NeedsMDMMigration : false ,
RenewEnrollmentProfile : false ,
} ,
} ,
{
mdm : fleet . MDM {
EnabledAndConfigured : true ,
MacOSMigration : fleet . MacOSMigration {
Enable : false ,
} ,
} ,
2023-05-23 18:01:04 +00:00
depAssigned : true ,
2023-05-18 17:21:54 +00:00
out : fleet . DesktopNotifications {
NeedsMDMMigration : false ,
RenewEnrollmentProfile : false ,
} ,
} ,
{
mdm : fleet . MDM {
EnabledAndConfigured : false ,
MacOSMigration : fleet . MacOSMigration {
Enable : false ,
} ,
} ,
2023-05-23 18:01:04 +00:00
depAssigned : true ,
2023-05-18 17:21:54 +00:00
out : fleet . DesktopNotifications {
NeedsMDMMigration : false ,
RenewEnrollmentProfile : false ,
} ,
} ,
}
2024-06-28 15:09:22 +00:00
mdmInfo := & fleet . HostMDM {
IsServer : false ,
InstalledFromDep : true ,
Enrolled : false ,
Name : fleet . WellKnownMDMFleet ,
DEPProfileAssignStatus : ptr . String ( string ( fleet . DEPAssignProfileResponseSuccess ) ) ,
}
2023-05-18 17:21:54 +00:00
for _ , c := range cases {
2023-09-13 18:59:35 +00:00
c := c
2023-05-18 17:21:54 +00:00
ds . AppConfigFunc = func ( ctx context . Context ) ( * fleet . AppConfig , error ) {
appCfg := fleet . AppConfig { }
appCfg . MDM = c . mdm
return & appCfg , nil
}
2024-06-14 18:01:12 +00:00
ds . IsHostConnectedToFleetMDMFunc = func ( ctx context . Context , host * fleet . Host ) ( bool , error ) {
return false , nil
}
2023-05-18 17:21:54 +00:00
2024-06-28 15:09:22 +00:00
ds . GetHostMDMFunc = func ( ctx context . Context , hostID uint ) ( * fleet . HostMDM , error ) {
return mdmInfo , nil
}
2023-05-18 17:21:54 +00:00
ctx = test . HostContext ( ctx , & fleet . Host {
2023-05-23 18:01:04 +00:00
OsqueryHostID : ptr . String ( "test" ) ,
DEPAssignedToFleet : & c . depAssigned ,
2024-06-28 15:09:22 +00:00
} )
2023-05-18 17:21:54 +00:00
sum , err := svc . GetFleetDesktopSummary ( ctx )
require . NoError ( t , err )
require . Equal ( t , c . out , sum . Notifications , fmt . Sprintf ( "enabled_and_configured: %t | macos_migration.enable: %t" , c . mdm . EnabledAndConfigured , c . mdm . MacOSMigration . Enable ) )
require . EqualValues ( t , 1 , * sum . FailingPolicies )
}
} )
2023-05-15 20:00:52 +00:00
t . Run ( "different host attributes" , func ( t * testing . T ) {
ds := new ( mock . Store )
license := & fleet . LicenseInfo { Tier : fleet . TierPremium , Expiration : time . Now ( ) . Add ( 24 * time . Hour ) }
svc , ctx := newTestService ( t , ds , nil , nil , & TestServerOpts { License : license , SkipCreateTestUsers : true } )
// context without a host
sum , err := svc . GetFleetDesktopSummary ( ctx )
require . Empty ( t , sum )
var authErr * fleet . AuthRequiredError
require . ErrorAs ( t , err , & authErr )
ds . FailingPoliciesCountFunc = func ( ctx context . Context , host * fleet . Host ) ( uint , error ) {
return uint ( 1 ) , nil
}
ds . AppConfigFunc = func ( ctx context . Context ) ( * fleet . AppConfig , error ) {
appCfg := fleet . AppConfig { }
appCfg . MDM . EnabledAndConfigured = true
appCfg . MDM . MacOSMigration . Enable = true
return & appCfg , nil
}
2024-07-02 16:32:49 +00:00
ds . HasSelfServiceSoftwareInstallersFunc = func ( ctx context . Context , platform string , teamID * uint ) ( bool , error ) {
return false , nil
}
2023-05-15 20:00:52 +00:00
cases := [ ] struct {
2024-06-28 15:09:22 +00:00
name string
host * fleet . Host
hostMDM * fleet . HostMDM
err error
out fleet . DesktopNotifications
2023-05-15 20:00:52 +00:00
} {
{
name : "not enrolled into osquery" ,
host : & fleet . Host { OsqueryHostID : nil } ,
err : nil ,
out : fleet . DesktopNotifications {
2023-05-18 17:21:54 +00:00
NeedsMDMMigration : false ,
RenewEnrollmentProfile : false ,
2023-05-15 20:00:52 +00:00
} ,
} ,
{
name : "manually enrolled into another MDM" ,
host : & fleet . Host {
2023-05-23 18:01:04 +00:00
OsqueryHostID : ptr . String ( "test" ) ,
DEPAssignedToFleet : ptr . Bool ( false ) ,
2024-06-28 15:09:22 +00:00
} ,
hostMDM : & fleet . HostMDM {
IsServer : false ,
InstalledFromDep : false ,
Enrolled : true ,
Name : fleet . WellKnownMDMIntune ,
DEPProfileAssignStatus : ptr . String ( string ( fleet . DEPAssignProfileResponseSuccess ) ) ,
} ,
2023-05-15 20:00:52 +00:00
err : nil ,
out : fleet . DesktopNotifications {
2023-05-18 17:21:54 +00:00
NeedsMDMMigration : false ,
RenewEnrollmentProfile : false ,
2023-05-15 20:00:52 +00:00
} ,
} ,
{
name : "DEP capable, but already unenrolled" ,
host : & fleet . Host {
2023-05-23 18:01:04 +00:00
DEPAssignedToFleet : ptr . Bool ( true ) ,
OsqueryHostID : ptr . String ( "test" ) ,
2024-06-28 15:09:22 +00:00
} ,
hostMDM : & fleet . HostMDM {
IsServer : false ,
InstalledFromDep : true ,
Enrolled : false ,
Name : fleet . WellKnownMDMFleet ,
DEPProfileAssignStatus : ptr . String ( string ( fleet . DEPAssignProfileResponseSuccess ) ) ,
} ,
2023-05-15 20:00:52 +00:00
err : nil ,
out : fleet . DesktopNotifications {
2023-05-18 17:21:54 +00:00
NeedsMDMMigration : false ,
RenewEnrollmentProfile : true ,
2023-05-15 20:00:52 +00:00
} ,
} ,
{
name : "DEP capable, but enrolled into Fleet" ,
host : & fleet . Host {
2023-05-23 18:01:04 +00:00
DEPAssignedToFleet : ptr . Bool ( true ) ,
OsqueryHostID : ptr . String ( "test" ) ,
2024-06-28 15:09:22 +00:00
} ,
hostMDM : & fleet . HostMDM {
IsServer : false ,
InstalledFromDep : true ,
Enrolled : true ,
Name : fleet . WellKnownMDMFleet ,
DEPProfileAssignStatus : ptr . String ( string ( fleet . DEPAssignProfileResponseSuccess ) ) ,
} ,
2024-04-08 14:10:29 +00:00
err : nil ,
out : fleet . DesktopNotifications {
NeedsMDMMigration : false ,
RenewEnrollmentProfile : false ,
} ,
} ,
{
name : "failed ADE assignment status" ,
host : & fleet . Host {
DEPAssignedToFleet : ptr . Bool ( true ) ,
OsqueryHostID : ptr . String ( "test" ) ,
2024-06-28 15:09:22 +00:00
} ,
hostMDM : & fleet . HostMDM {
IsServer : false ,
InstalledFromDep : true ,
Enrolled : true ,
Name : fleet . WellKnownMDMIntune ,
DEPProfileAssignStatus : ptr . String ( string ( fleet . DEPAssignProfileResponseFailed ) ) ,
} ,
2024-04-08 14:10:29 +00:00
err : nil ,
out : fleet . DesktopNotifications {
NeedsMDMMigration : false ,
RenewEnrollmentProfile : false ,
} ,
} ,
{
name : "not accessible ADE assignment status" ,
host : & fleet . Host {
DEPAssignedToFleet : ptr . Bool ( true ) ,
OsqueryHostID : ptr . String ( "test" ) ,
2024-06-28 15:09:22 +00:00
} ,
hostMDM : & fleet . HostMDM {
IsServer : false ,
InstalledFromDep : true ,
Enrolled : true ,
Name : fleet . WellKnownMDMIntune ,
DEPProfileAssignStatus : ptr . String ( string ( fleet . DEPAssignProfileResponseNotAccessible ) ) ,
} ,
2024-04-08 14:10:29 +00:00
err : nil ,
out : fleet . DesktopNotifications {
NeedsMDMMigration : false ,
RenewEnrollmentProfile : false ,
} ,
} ,
{
name : "empty ADE assignment status" ,
host : & fleet . Host {
DEPAssignedToFleet : ptr . Bool ( true ) ,
OsqueryHostID : ptr . String ( "test" ) ,
2024-06-28 15:09:22 +00:00
} ,
hostMDM : & fleet . HostMDM {
IsServer : false ,
InstalledFromDep : true ,
Enrolled : true ,
Name : fleet . WellKnownMDMIntune ,
DEPProfileAssignStatus : ptr . String ( "" ) ,
} ,
2024-04-08 14:10:29 +00:00
err : nil ,
out : fleet . DesktopNotifications {
NeedsMDMMigration : false ,
RenewEnrollmentProfile : false ,
} ,
} ,
{
name : "nil ADE assignment status" ,
host : & fleet . Host {
DEPAssignedToFleet : ptr . Bool ( true ) ,
OsqueryHostID : ptr . String ( "test" ) ,
2024-06-28 15:09:22 +00:00
} ,
hostMDM : & fleet . HostMDM {
IsServer : false ,
InstalledFromDep : true ,
Enrolled : true ,
Name : fleet . WellKnownMDMIntune ,
DEPProfileAssignStatus : nil ,
} ,
2023-05-15 20:00:52 +00:00
err : nil ,
out : fleet . DesktopNotifications {
2023-05-18 17:21:54 +00:00
NeedsMDMMigration : false ,
RenewEnrollmentProfile : false ,
2023-05-15 20:00:52 +00:00
} ,
} ,
{
name : "all conditions met" ,
host : & fleet . Host {
2023-05-23 18:01:04 +00:00
DEPAssignedToFleet : ptr . Bool ( true ) ,
OsqueryHostID : ptr . String ( "test" ) ,
2024-06-28 15:09:22 +00:00
} ,
hostMDM : & fleet . HostMDM {
IsServer : false ,
InstalledFromDep : true ,
Enrolled : true ,
Name : fleet . WellKnownMDMIntune ,
DEPProfileAssignStatus : ptr . String ( string ( fleet . DEPAssignProfileResponseSuccess ) ) ,
} ,
2023-05-15 20:00:52 +00:00
err : nil ,
out : fleet . DesktopNotifications {
2023-05-18 17:21:54 +00:00
NeedsMDMMigration : true ,
RenewEnrollmentProfile : false ,
2023-05-15 20:00:52 +00:00
} ,
} ,
}
for _ , c := range cases {
t . Run ( c . name , func ( t * testing . T ) {
2023-05-18 17:21:54 +00:00
ctx = test . HostContext ( ctx , c . host )
2024-06-28 15:09:22 +00:00
ds . GetHostMDMFunc = func ( ctx context . Context , hostID uint ) ( * fleet . HostMDM , error ) {
if c . hostMDM == nil {
return nil , sql . ErrNoRows
}
return c . hostMDM , nil
}
ds . IsHostConnectedToFleetMDMFunc = func ( ctx context . Context , host * fleet . Host ) ( bool , error ) {
return c . hostMDM != nil && c . hostMDM . Enrolled == true && c . hostMDM . Name == fleet . WellKnownMDMFleet , nil
}
2023-05-15 20:00:52 +00:00
sum , err := svc . GetFleetDesktopSummary ( ctx )
if c . err != nil {
require . ErrorIs ( t , err , c . err )
require . Empty ( t , sum )
} else {
require . NoError ( t , err )
require . Equal ( t , c . out , sum . Notifications )
require . EqualValues ( t , 1 , * sum . FailingPolicies )
}
} )
}
} )
}
2024-11-18 22:44:25 +00:00
func TestTriggerLinuxDiskEncryptionEscrow ( t * testing . T ) {
t . Run ( "unavailable in Fleet Free" , func ( t * testing . T ) {
ds := new ( mock . Store )
svc , ctx := newTestService ( t , ds , nil , nil , & TestServerOpts { SkipCreateTestUsers : true } )
err := svc . TriggerLinuxDiskEncryptionEscrow ( ctx , & fleet . Host { ID : 1 } )
require . ErrorIs ( t , err , fleet . ErrMissingLicense )
} )
t . Run ( "no-op on already pending" , func ( t * testing . T ) {
ds := new ( mock . Store )
svc , ctx := newTestService ( t , ds , nil , nil , & TestServerOpts { License : & fleet . LicenseInfo { Tier : fleet . TierPremium } , SkipCreateTestUsers : true } )
ds . IsHostPendingEscrowFunc = func ( ctx context . Context , hostID uint ) bool {
return true
}
err := svc . TriggerLinuxDiskEncryptionEscrow ( ctx , & fleet . Host { ID : 1 } )
require . NoError ( t , err )
require . True ( t , ds . IsHostPendingEscrowFuncInvoked )
} )
2025-07-09 16:47:17 +00:00
t . Run ( "encryption key is already escrowed" , func ( t * testing . T ) {
ds := new ( mock . Store )
svc , ctx := newTestService ( t , ds , nil , nil , & TestServerOpts { License : & fleet . LicenseInfo { Tier : fleet . TierPremium } , SkipCreateTestUsers : true } )
var reportedErrors [ ] string
host := & fleet . Host { ID : 1 , Platform : "rhel" , OSVersion : "Red Hat Enterprise Linux 9.0.0" }
ds . ReportEscrowErrorFunc = func ( ctx context . Context , hostID uint , err string ) error {
require . Equal ( t , hostID , host . ID )
reportedErrors = append ( reportedErrors , err )
return nil
}
orbitInfo := & fleet . HostOrbitInfo { Version : fleet . MinOrbitLUKSVersion }
ds . IsHostPendingEscrowFunc = func ( ctx context . Context , hostID uint ) bool {
return false
}
ds . GetHostOrbitInfoFunc = func ( ctx context . Context , id uint ) ( * fleet . HostOrbitInfo , error ) {
return orbitInfo , nil
}
ds . AssertHasNoEncryptionKeyStoredFunc = func ( ctx context . Context , hostID uint ) error {
return errors . New ( "encryption key is already escrowed" )
}
err := svc . TriggerLinuxDiskEncryptionEscrow ( ctx , host )
require . ErrorContains ( t , err , "encryption key is already escrowed" )
require . Len ( t , reportedErrors , 0 , "No error should be reported when key is already escrowed" )
} )
2024-11-18 22:44:25 +00:00
t . Run ( "validation failures" , func ( t * testing . T ) {
ds := new ( mock . Store )
svc , ctx := newTestService ( t , ds , nil , nil , & TestServerOpts { License : & fleet . LicenseInfo { Tier : fleet . TierPremium } , SkipCreateTestUsers : true } )
ds . IsHostPendingEscrowFunc = func ( ctx context . Context , hostID uint ) bool {
return false
}
var reportedErrors [ ] string
host := & fleet . Host { ID : 1 , Platform : "rhel" , OSVersion : "Red Hat Enterprise Linux 9.0.0" }
2025-07-09 16:47:17 +00:00
ds . AssertHasNoEncryptionKeyStoredFunc = func ( ctx context . Context , hostID uint ) error { return nil }
2024-11-18 22:44:25 +00:00
ds . ReportEscrowErrorFunc = func ( ctx context . Context , hostID uint , err string ) error {
require . Equal ( t , hostID , host . ID )
reportedErrors = append ( reportedErrors , err )
return nil
}
// invalid platform
err := svc . TriggerLinuxDiskEncryptionEscrow ( ctx , host )
2024-11-21 19:33:37 +00:00
require . ErrorContains ( t , err , "Fleet does not yet support creating LUKS disk encryption keys on this platform." )
2024-11-18 22:44:25 +00:00
require . True ( t , ds . IsHostPendingEscrowFuncInvoked )
// valid platform, no-team, encryption not enabled
host . OSVersion = "Fedora 32.0.0"
appConfig := & fleet . AppConfig { MDM : fleet . MDM { EnableDiskEncryption : optjson . SetBool ( false ) } }
ds . AppConfigFunc = func ( ctx context . Context ) ( * fleet . AppConfig , error ) {
return appConfig , nil
}
err = svc . TriggerLinuxDiskEncryptionEscrow ( ctx , host )
2024-11-21 19:33:37 +00:00
require . ErrorContains ( t , err , "Disk encryption is not enabled for hosts not assigned to a team." )
2024-11-18 22:44:25 +00:00
// valid platform, team, encryption not enabled
host . TeamID = ptr . Uint ( 1 )
teamConfig := & fleet . TeamMDM { }
ds . TeamMDMConfigFunc = func ( ctx context . Context , teamID uint ) ( * fleet . TeamMDM , error ) {
require . Equal ( t , uint ( 1 ) , teamID )
return teamConfig , nil
}
err = svc . TriggerLinuxDiskEncryptionEscrow ( ctx , host )
2024-11-21 19:33:37 +00:00
require . ErrorContains ( t , err , "Disk encryption is not enabled for this host's team." )
2024-11-18 22:44:25 +00:00
// valid platform, team, host disk is not encrypted or unknown encryption state
teamConfig = & fleet . TeamMDM { EnableDiskEncryption : true }
err = svc . TriggerLinuxDiskEncryptionEscrow ( ctx , host )
2024-11-21 19:33:37 +00:00
require . ErrorContains ( t , err , "Host's disk is not encrypted. Please encrypt your disk first." )
2024-11-18 22:44:25 +00:00
host . DiskEncryptionEnabled = ptr . Bool ( false )
err = svc . TriggerLinuxDiskEncryptionEscrow ( ctx , host )
2024-11-21 19:33:37 +00:00
require . ErrorContains ( t , err , "Host's disk is not encrypted. Please encrypt your disk first." )
2024-11-18 22:44:25 +00:00
2024-11-21 18:41:38 +00:00
// No Fleet Desktop
2024-11-18 22:44:25 +00:00
host . DiskEncryptionEnabled = ptr . Bool ( true )
2024-11-21 18:41:38 +00:00
orbitInfo := & fleet . HostOrbitInfo { Version : "1.35.1" }
ds . GetHostOrbitInfoFunc = func ( ctx context . Context , id uint ) ( * fleet . HostOrbitInfo , error ) {
return orbitInfo , nil
}
2024-11-18 22:44:25 +00:00
err = svc . TriggerLinuxDiskEncryptionEscrow ( ctx , host )
2024-11-21 19:33:37 +00:00
require . ErrorContains ( t , err , "Your version of fleetd does not support creating disk encryption keys on Linux. Please upgrade fleetd, then click Refetch, then try again." )
2024-11-18 22:44:25 +00:00
2025-07-09 16:47:17 +00:00
require . Len ( t , reportedErrors , 6 )
2024-11-18 22:44:25 +00:00
} )
t . Run ( "validation success" , func ( t * testing . T ) {
ds := new ( mock . Store )
svc , ctx := newTestService ( t , ds , nil , nil , & TestServerOpts { License : & fleet . LicenseInfo { Tier : fleet . TierPremium } , SkipCreateTestUsers : true } )
ds . IsHostPendingEscrowFunc = func ( ctx context . Context , hostID uint ) bool {
return false
}
ds . AppConfigFunc = func ( ctx context . Context ) ( * fleet . AppConfig , error ) {
return & fleet . AppConfig { MDM : fleet . MDM { EnableDiskEncryption : optjson . SetBool ( true ) } } , nil
}
2024-11-21 18:41:38 +00:00
ds . GetHostOrbitInfoFunc = func ( ctx context . Context , id uint ) ( * fleet . HostOrbitInfo , error ) {
return & fleet . HostOrbitInfo { Version : "1.36.0" , DesktopVersion : ptr . String ( "42" ) } , nil
}
2024-11-18 22:44:25 +00:00
ds . AssertHasNoEncryptionKeyStoredFunc = func ( ctx context . Context , hostID uint ) error {
return nil
}
host := & fleet . Host { ID : 1 , Platform : "ubuntu" , DiskEncryptionEnabled : ptr . Bool ( true ) , OrbitVersion : ptr . String ( fleet . MinOrbitLUKSVersion ) }
ds . QueueEscrowFunc = func ( ctx context . Context , hostID uint ) error {
require . Equal ( t , uint ( 1 ) , hostID )
return nil
}
err := svc . TriggerLinuxDiskEncryptionEscrow ( ctx , host )
require . NoError ( t , err )
require . True ( t , ds . QueueEscrowFuncInvoked )
} )
}