util: Add EncryptionTypeNone and unit tests

Add type none to distinguish disabled encryption (positive result)
from invalid configuration (negative result).

Signed-off-by: Marcel Lauhoff <marcel.lauhoff@suse.com>
This commit is contained in:
Marcel Lauhoff
2022-09-06 18:44:00 +02:00
committed by mergify[bot]
parent f89076b1d7
commit 2abfafdf3f
3 changed files with 62 additions and 11 deletions

View File

@ -83,17 +83,29 @@ func FetchEncryptionKMSID(encrypted, kmsID string) (string, error) {
type EncryptionType int
const (
// EncryptionTypeInvalid signals invalid or unsupported configuration.
EncryptionTypeInvalid EncryptionType = iota
EncryptionTypeBlock = iota
EncryptionTypeFile = iota
// EncryptionTypeNone disables encryption.
EncryptionTypeNone
// EncryptionTypeBlock enables block encryption.
EncryptionTypeBlock
// EncryptionTypeBlock enables file encryption (fscrypt).
EncryptionTypeFile
)
const (
encryptionTypeBlockString = "block"
encryptionTypeFileString = "file"
)
func ParseEncryptionType(typeStr string) EncryptionType {
switch typeStr {
case "block":
case encryptionTypeBlockString:
return EncryptionTypeBlock
case "file":
case encryptionTypeFileString:
return EncryptionTypeFile
case "":
return EncryptionTypeNone
default:
return EncryptionTypeInvalid
}
@ -102,13 +114,15 @@ func ParseEncryptionType(typeStr string) EncryptionType {
func EncryptionTypeString(encType EncryptionType) string {
switch encType {
case EncryptionTypeBlock:
return "block"
return encryptionTypeBlockString
case EncryptionTypeFile:
return "file"
return encryptionTypeFileString
case EncryptionTypeNone:
return ""
case EncryptionTypeInvalid:
return ""
return "INVALID"
default:
return ""
return "UNKNOWN"
}
}
@ -121,6 +135,10 @@ func FetchEncryptionType(volOptions map[string]string, fallback EncryptionType)
return fallback
}
if encType == "" {
return EncryptionTypeInvalid
}
return ParseEncryptionType(encType)
}