Skip to content

Commit

Permalink
tests updates
Browse files Browse the repository at this point in the history
  • Loading branch information
amold1 committed Sep 10, 2024
1 parent e76c094 commit c6f8078
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 86 deletions.
42 changes: 7 additions & 35 deletions internal/driver/luks.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ func getLuksContext(secrets map[string]string, context map[string]string, lifecy
}

func (e *Encryption) luksFormat(ctx LuksContext, source string) (string, error) {
args := []string{""}
s, err := e.Exec.Command("lsblk", args...).CombinedOutput()

Check failure on line 123 in internal/driver/luks.go

View workflow job for this annotation

GitHub Actions / ci

ineffectual assignment to err (ineffassign)
klog.V(2).Info("Command output ", s)
luks2 := cryptsetup.LUKS2{SectorSize: 512}
keySize, err := strconv.Atoi(ctx.EncryptionKeySize)
if err != nil {
Expand All @@ -138,6 +141,10 @@ func (e *Encryption) luksFormat(ctx LuksContext, source string) (string, error)
if err != nil {
return "", err
}
if device.Dump() == 0 {
klog.V(4).Info("The volume is already LUKS formatted ", ctx.VolumeName)
return "/dev/mapper/" + ctx.VolumeName, nil
}
err = device.KeyslotAddByVolumeKey(0, "", "")
if err != nil {
return "", err
Expand Down Expand Up @@ -210,38 +217,3 @@ func (e *Encryption) getCryptsetupCmd() (string, error) {
}
return cryptsetupCmd, nil
}

func (e *Encryption) blkidValid(source string) (bool, error) {
if source == "" {
return false, errors.New("invalid source")
}

blkidCmd := "blkid"
_, err := e.Exec.LookPath(blkidCmd)
if err != nil {
if err == exec.ErrNotFound {
return false, fmt.Errorf("%q executable invalid", blkidCmd)
}
return false, err
}

blkidArgs := []string{source}

exitCode := 0
cmd := e.Exec.Command(blkidCmd, blkidArgs...)
err = cmd.Run()
if err != nil {
exitError, ok := err.(utilexec.ExitError)
if !ok {
return false, fmt.Errorf("checking blkdid failed: %w cmd: %q, args: %q", err, blkidCmd, blkidArgs)
}
// ws := exitError.Sys().(syscall.WaitStatus)
exitCode = exitError.ExitStatus()
if exitCode == 2 {
return false, nil
}
return false, errors.New("checking blkdid failed")
}

return true, nil
}
26 changes: 9 additions & 17 deletions internal/driver/nodeserver_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,31 +324,23 @@ func (ns *NodeServer) mountVolume(ctx context.Context, devicePath string, req *c
// Finally, it prepares the LUKS volume for mounting.
func (ns *NodeServer) prepareLUKSVolume(ctx context.Context, devicePath string, luksContext LuksContext) (string, error) {
var luksSource string
var err error
log := logger.GetLogger(ctx)
log.V(4).Info("Entering prepareLUKSVolume", "devicePath", devicePath, "luksContext", luksContext)

// LUKS encryption enabled, check if the volume needs to be formatted.
log.V(4).Info("LUKS encryption enabled")

// Validate if the device is formatted with LUKS encryption or if it needs formatting.
formatted, err := ns.encrypt.blkidValid(devicePath)
if err != nil {
return "", errInternal("Failed to validate blkid (%q): %v", devicePath, err)
}

// If the device is not, format it.
if !formatted {
log.V(4).Info("luks volume now formatting: ", devicePath)
log.V(4).Info("luks volume now formatting: ", devicePath)

// Validate the LUKS context.
if err := luksContext.validate(); err != nil {
return "", errInternal("Failed to luks format validation (%q): %v", devicePath, err)
}
// Validate the LUKS context.
if err = luksContext.validate(); err != nil {
return "", errInternal("Failed to luks format validation (%q): %v", devicePath, err)
}

// Format the volume with LUKS encryption.
if luksSource, err = ns.encrypt.luksFormat(luksContext, devicePath); err != nil {
return "", errInternal("Failed to luks format (%q): %v", devicePath, err)
}
// Format the volume with LUKS encryption.
if luksSource, err = ns.encrypt.luksFormat(luksContext, devicePath); err != nil {
return "", errInternal("Failed to luks format (%q): %v", devicePath, err)
}

log.V(4).Info("Exiting prepareLUKSVolume", "luksSource", luksSource)
Expand Down
38 changes: 4 additions & 34 deletions internal/driver/nodeserver_helpers_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,34 +74,15 @@ func TestNodeServer_mountVolume_linux(t *testing.T) {
VolumeId: "test",
VolumeContext: map[string]string{
LuksEncryptedAttribute: "true",
LuksCipherAttribute: "test",
LuksKeySizeAttribute: "test",
LuksCipherAttribute: "aes-xts-plain64",
LuksKeySizeAttribute: "512",
PublishInfoVolumeName: "test",
},
Secrets: map[string]string{LuksKeyAttribute: "test"},
},
expectMounterCalls: func(m *mocks.MockMounter) {
m.EXPECT().MountSensitive("/dev/mapper/test", "", "ext4", []string{"defaults"}, emptyStringArray).Return(nil)
},
expectExecCalls: func(m *mocks.MockExecutor, c *mocks.MockCommand) {
m.EXPECT().LookPath("blkid").Return("/bin/blkid", nil)
m.EXPECT().Command("blkid", "/dev/test").Return(c)
c.EXPECT().Run().Return(nil)

// LuksOpen
m.EXPECT().LookPath("cryptsetup").Return("/bin/cryptsetup", nil)
m.EXPECT().Command("cryptsetup", "--batch-mode", "luksOpen", "--key-file", "-", "/dev/test", "test").Return(c)
c.EXPECT().SetStdin(gomock.Any())
c.EXPECT().CombinedOutput().Return([]byte("Command Successful"), nil)

// Mount_linux: Check disk format. Disk is not formatted.
m.EXPECT().Command("blkid", "-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/mapper/test").Return(c)
c.EXPECT().CombinedOutput().Return([]byte(""), exec.CodeExitError{Code: 2, Err: fmt.Errorf("not formatted")})

// Mount_linux: Format disk
m.EXPECT().Command("mkfs.ext4", "-F", "-m0", "/dev/mapper/test").Return(c)
c.EXPECT().CombinedOutput().Return([]byte("Formatted successfully"), nil)
},
expectFsCalls: func(m *mocks.MockFileSystem) {
m.EXPECT().IsNotExist(gomock.Any()).Return(true)
m.EXPECT().Stat(gomock.Any()).Return(nil, nil)
Expand All @@ -115,23 +96,12 @@ func TestNodeServer_mountVolume_linux(t *testing.T) {
VolumeId: "test",
VolumeContext: map[string]string{
LuksEncryptedAttribute: "true",
LuksCipherAttribute: "test",
LuksKeySizeAttribute: "test",
LuksCipherAttribute: "aes-xts-plain64",
LuksKeySizeAttribute: "512",
PublishInfoVolumeName: "test",
},
Secrets: map[string]string{LuksKeyAttribute: "test"},
},
expectExecCalls: func(m *mocks.MockExecutor, c *mocks.MockCommand) {
m.EXPECT().LookPath("blkid").Return("/bin/blkid", nil)
m.EXPECT().Command("blkid", "/dev/test").Return(c)
c.EXPECT().Run().Return(nil)

// LuksOpen
m.EXPECT().LookPath("cryptsetup").Return("/bin/cryptsetup", nil)
m.EXPECT().Command("cryptsetup", "--batch-mode", "luksOpen", "--key-file", "-", "/dev/test", "test").Return(c)
c.EXPECT().SetStdin(gomock.Any())
c.EXPECT().CombinedOutput().Return(nil, fmt.Errorf("Unable to LuksOpen"))
},
expectFsCalls: func(m *mocks.MockFileSystem) {
m.EXPECT().IsNotExist(gomock.Any()).Return(true)
m.EXPECT().Stat(gomock.Any()).Return(nil, nil)
Expand Down

0 comments on commit c6f8078

Please sign in to comment.