2019-12-13 11:41:32 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 The Ceph-CSI Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// LuksFormat sets up volume as an encrypted LUKS partition.
|
2019-12-13 11:41:32 +00:00
|
|
|
func LuksFormat(devicePath, passphrase string) (stdout, stderr []byte, err error) {
|
2021-06-25 12:15:08 +00:00
|
|
|
return execCryptsetupCommand(
|
|
|
|
&passphrase,
|
|
|
|
"-q",
|
|
|
|
"luksFormat",
|
|
|
|
"--type",
|
|
|
|
"luks2",
|
|
|
|
"--hash",
|
|
|
|
"sha256",
|
|
|
|
devicePath,
|
|
|
|
"-d",
|
|
|
|
"/dev/stdin")
|
2019-12-13 11:41:32 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// LuksOpen opens LUKS encrypted partition and sets up a mapping.
|
2019-12-13 11:41:32 +00:00
|
|
|
func LuksOpen(devicePath, mapperFile, passphrase string) (stdout, stderr []byte, err error) {
|
|
|
|
return execCryptsetupCommand(&passphrase, "luksOpen", devicePath, mapperFile, "-d", "/dev/stdin")
|
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// LuksClose removes existing mapping.
|
2019-12-13 11:41:32 +00:00
|
|
|
func LuksClose(mapperFile string) (stdout, stderr []byte, err error) {
|
|
|
|
return execCryptsetupCommand(nil, "luksClose", mapperFile)
|
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// LuksStatus returns encryption status of a provided device.
|
2019-12-13 11:41:32 +00:00
|
|
|
func LuksStatus(mapperFile string) (stdout, stderr []byte, err error) {
|
|
|
|
return execCryptsetupCommand(nil, "status", mapperFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func execCryptsetupCommand(stdin *string, args ...string) (stdout, stderr []byte, err error) {
|
|
|
|
var (
|
|
|
|
program = "cryptsetup"
|
2020-07-20 12:49:05 +00:00
|
|
|
cmd = exec.Command(program, args...) // #nosec:G204, commands executing not vulnerable.
|
2019-12-13 11:41:32 +00:00
|
|
|
sanitizedArgs = StripSecretInArgs(args)
|
|
|
|
stdoutBuf bytes.Buffer
|
|
|
|
stderrBuf bytes.Buffer
|
|
|
|
)
|
|
|
|
|
|
|
|
cmd.Stdout = &stdoutBuf
|
|
|
|
cmd.Stderr = &stderrBuf
|
|
|
|
if stdin != nil {
|
|
|
|
cmd.Stdin = strings.NewReader(*stdin)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return stdoutBuf.Bytes(), stderrBuf.Bytes(), fmt.Errorf("an error (%v)"+
|
|
|
|
" occurred while running %s args: %v", err, program, sanitizedArgs)
|
|
|
|
}
|
|
|
|
|
|
|
|
return stdoutBuf.Bytes(), nil, nil
|
|
|
|
}
|