-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb2f952
commit 52bdfc7
Showing
5 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/fleetdm/fleet/v4/server/fleet" | ||
) | ||
|
||
func (svc *Service) LinuxHostDiskEncryptionStatus(ctx context.Context, host fleet.Host) (fleet.HostMDMDiskEncryption, error) { | ||
if !host.IsLUKSSupported() { | ||
return fleet.HostMDMDiskEncryption{}, nil | ||
} | ||
|
||
actionRequired := fleet.DiskEncryptionActionRequired | ||
verified := fleet.DiskEncryptionVerified | ||
failed := fleet.DiskEncryptionFailed | ||
|
||
key, err := svc.ds.GetHostDiskEncryptionKey(ctx, host.ID) | ||
if err != nil { | ||
if fleet.IsNotFound(err) { | ||
return fleet.HostMDMDiskEncryption{ | ||
Status: &actionRequired, | ||
}, nil | ||
} | ||
return fleet.HostMDMDiskEncryption{}, err | ||
} | ||
|
||
if key.ClientError != "" { | ||
return fleet.HostMDMDiskEncryption{ | ||
Status: &failed, | ||
Detail: key.ClientError, | ||
}, nil | ||
} | ||
|
||
if key.Base64Encrypted == "" { | ||
return fleet.HostMDMDiskEncryption{ | ||
Status: &actionRequired, | ||
}, nil | ||
} | ||
|
||
return fleet.HostMDMDiskEncryption{ | ||
Status: &verified, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/fleetdm/fleet/v4/server/fleet" | ||
"github.com/fleetdm/fleet/v4/server/mock" | ||
"github.com/fleetdm/fleet/v4/server/ptr" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLinuxHostDiskEncryptionStatus(t *testing.T) { | ||
ds := new(mock.Store) | ||
svc, ctx := newTestService(t, ds, nil, nil) | ||
|
||
actionRequired := fleet.DiskEncryptionActionRequired | ||
verified := fleet.DiskEncryptionVerified | ||
failed := fleet.DiskEncryptionFailed | ||
|
||
testcases := []struct { | ||
name string | ||
host fleet.Host | ||
keyExists bool | ||
clientErrorExists bool | ||
status fleet.HostMDMDiskEncryption | ||
notFound bool | ||
}{ | ||
{ | ||
name: "no key", | ||
host: fleet.Host{ID: 1, Platform: "ubuntu"}, | ||
keyExists: false, | ||
clientErrorExists: false, | ||
status: fleet.HostMDMDiskEncryption{ | ||
Status: &actionRequired, | ||
}, | ||
}, | ||
{ | ||
name: "key exists", | ||
host: fleet.Host{ID: 1, Platform: "ubuntu"}, | ||
keyExists: true, | ||
clientErrorExists: false, | ||
status: fleet.HostMDMDiskEncryption{ | ||
Status: &verified, | ||
}, | ||
}, | ||
{ | ||
name: "key exists && client error", | ||
host: fleet.Host{ID: 1, Platform: "ubuntu"}, | ||
keyExists: true, | ||
clientErrorExists: true, | ||
status: fleet.HostMDMDiskEncryption{ | ||
Status: &failed, | ||
Detail: "client error", | ||
}, | ||
}, | ||
{ | ||
name: "key not found", | ||
host: fleet.Host{ID: 1, Platform: "ubuntu"}, | ||
keyExists: false, | ||
clientErrorExists: false, | ||
status: fleet.HostMDMDiskEncryption{ | ||
Status: &actionRequired, | ||
}, | ||
notFound: true, | ||
}, | ||
} | ||
|
||
for _, tt := range testcases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ds.GetHostDiskEncryptionKeyFunc = func(ctx context.Context, hostID uint) (*fleet.HostDiskEncryptionKey, error) { | ||
var encrypted string | ||
if tt.keyExists { | ||
encrypted = "encrypted" | ||
} | ||
|
||
var clientError string | ||
if tt.clientErrorExists { | ||
clientError = "client error" | ||
} | ||
|
||
var nfe notFoundError | ||
if tt.notFound { | ||
return nil, &nfe | ||
} | ||
|
||
return &fleet.HostDiskEncryptionKey{ | ||
HostID: hostID, | ||
Base64Encrypted: encrypted, | ||
Decryptable: ptr.Bool(true), | ||
UpdatedAt: time.Now(), | ||
ClientError: clientError, | ||
}, nil | ||
} | ||
|
||
status, err := svc.LinuxHostDiskEncryptionStatus(ctx, tt.host) | ||
assert.Nil(t, err) | ||
|
||
assert.Equal(t, tt.status, status) | ||
}) | ||
} | ||
} |