fix encrypt-on-receive with placeholders
fixes https://github.com/zrepl/zrepl/issues/504 Problem: plain send + recv with root_fs encrypted + placeholders causes plain recvs whereas user would expect encrypt-on-recv Reason: We create placeholder filesytems with -o encryption=off. Thus, children received below those placeholders won't inherit encryption of root_fs. Fix: We'll have three values for `recv.placeholders.encryption: unspecified (default) | off | inherit`. When we create a placeholder, we will fail the operation if `recv.placeholders.encryption = unspecified`. The exception is if the placeholder filesystem is to encode the client identity ($root_fs/$client_identity) in a pull job. Those are created in `inherit` mode if the config field is `unspecified` so that users who don't need placeholders are not bothered by these details. Future Work: Automatically warn existing users of encrypt-on-recv about the problem if they are affected. The problem that I hit during implementation of this is that the `encryption` prop's `source` doesn't quite behave like other props: `source` is `default` for `encryption=off` and `-` when `encryption=on`. Hence, we can't use `source` to distinguish the following 2x2 cases: (1) placeholder created with explicit -o encryption=off (2) placeholder created without specifying -o encryption with (A) an encrypted parent at creation time (B) an unencrypted parent at creation time
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
// Code generated by "enumer -type=FilesystemPlaceholderCreateEncryptionValue -trimprefix=FilesystemPlaceholderCreateEncryption"; DO NOT EDIT.
|
||||
|
||||
//
|
||||
package zfs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const _FilesystemPlaceholderCreateEncryptionValueName = "InheritOff"
|
||||
|
||||
var _FilesystemPlaceholderCreateEncryptionValueIndex = [...]uint8{0, 7, 10}
|
||||
|
||||
func (i FilesystemPlaceholderCreateEncryptionValue) String() string {
|
||||
i -= 1
|
||||
if i < 0 || i >= FilesystemPlaceholderCreateEncryptionValue(len(_FilesystemPlaceholderCreateEncryptionValueIndex)-1) {
|
||||
return fmt.Sprintf("FilesystemPlaceholderCreateEncryptionValue(%d)", i+1)
|
||||
}
|
||||
return _FilesystemPlaceholderCreateEncryptionValueName[_FilesystemPlaceholderCreateEncryptionValueIndex[i]:_FilesystemPlaceholderCreateEncryptionValueIndex[i+1]]
|
||||
}
|
||||
|
||||
var _FilesystemPlaceholderCreateEncryptionValueValues = []FilesystemPlaceholderCreateEncryptionValue{1, 2}
|
||||
|
||||
var _FilesystemPlaceholderCreateEncryptionValueNameToValueMap = map[string]FilesystemPlaceholderCreateEncryptionValue{
|
||||
_FilesystemPlaceholderCreateEncryptionValueName[0:7]: 1,
|
||||
_FilesystemPlaceholderCreateEncryptionValueName[7:10]: 2,
|
||||
}
|
||||
|
||||
// FilesystemPlaceholderCreateEncryptionValueString retrieves an enum value from the enum constants string name.
|
||||
// Throws an error if the param is not part of the enum.
|
||||
func FilesystemPlaceholderCreateEncryptionValueString(s string) (FilesystemPlaceholderCreateEncryptionValue, error) {
|
||||
if val, ok := _FilesystemPlaceholderCreateEncryptionValueNameToValueMap[s]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%s does not belong to FilesystemPlaceholderCreateEncryptionValue values", s)
|
||||
}
|
||||
|
||||
// FilesystemPlaceholderCreateEncryptionValueValues returns all values of the enum
|
||||
func FilesystemPlaceholderCreateEncryptionValueValues() []FilesystemPlaceholderCreateEncryptionValue {
|
||||
return _FilesystemPlaceholderCreateEncryptionValueValues
|
||||
}
|
||||
|
||||
// IsAFilesystemPlaceholderCreateEncryptionValue returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||
func (i FilesystemPlaceholderCreateEncryptionValue) IsAFilesystemPlaceholderCreateEncryptionValue() bool {
|
||||
for _, v := range _FilesystemPlaceholderCreateEncryptionValueValues {
|
||||
if i == v {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
+52
-5
@@ -80,7 +80,15 @@ func ZFSGetFilesystemPlaceholderState(ctx context.Context, p *DatasetPath) (stat
|
||||
return state, nil
|
||||
}
|
||||
|
||||
func ZFSCreatePlaceholderFilesystem(ctx context.Context, fs *DatasetPath, parent *DatasetPath) (err error) {
|
||||
//go:generate enumer -type=FilesystemPlaceholderCreateEncryptionValue -trimprefix=FilesystemPlaceholderCreateEncryption
|
||||
type FilesystemPlaceholderCreateEncryptionValue int
|
||||
|
||||
const (
|
||||
FilesystemPlaceholderCreateEncryptionInherit FilesystemPlaceholderCreateEncryptionValue = 1 << iota
|
||||
FilesystemPlaceholderCreateEncryptionOff
|
||||
)
|
||||
|
||||
func ZFSCreatePlaceholderFilesystem(ctx context.Context, fs *DatasetPath, parent *DatasetPath, encryption FilesystemPlaceholderCreateEncryptionValue) (err error) {
|
||||
if fs.Length() == 1 {
|
||||
return fmt.Errorf("cannot create %q: pools cannot be created with zfs create", fs.ToString())
|
||||
}
|
||||
@@ -90,11 +98,19 @@ func ZFSCreatePlaceholderFilesystem(ctx context.Context, fs *DatasetPath, parent
|
||||
"-o", fmt.Sprintf("%s=%s", PlaceholderPropertyName, placeholderPropertyOn),
|
||||
"-o", "mountpoint=none",
|
||||
}
|
||||
if parentEncrypted, err := ZFSGetEncryptionEnabled(ctx, parent.ToString()); err != nil {
|
||||
return errors.Wrap(err, "cannot determine encryption support")
|
||||
} else if parentEncrypted {
|
||||
cmdline = append(cmdline, "-o", "encryption=off")
|
||||
|
||||
if !encryption.IsAFilesystemPlaceholderCreateEncryptionValue() {
|
||||
panic(encryption)
|
||||
}
|
||||
switch encryption {
|
||||
case FilesystemPlaceholderCreateEncryptionInherit:
|
||||
// no-op
|
||||
case FilesystemPlaceholderCreateEncryptionOff:
|
||||
cmdline = append(cmdline, "-o", "encryption=off")
|
||||
default:
|
||||
panic(encryption)
|
||||
}
|
||||
|
||||
cmdline = append(cmdline, fs.ToString())
|
||||
cmd := zfscmd.CommandContext(ctx, ZFS_BINARY, cmdline...)
|
||||
|
||||
@@ -148,3 +164,34 @@ func ZFSMigrateHashBasedPlaceholderToCurrent(ctx context.Context, fs *DatasetPat
|
||||
}
|
||||
return &report, nil
|
||||
}
|
||||
|
||||
func ZFSListPlaceholderFilesystemsWithAdditionalProps(ctx context.Context, root string, additionalProps []string) (map[string]*ZFSProperties, error) {
|
||||
|
||||
props := []string{PlaceholderPropertyName}
|
||||
if len(additionalProps) > 0 {
|
||||
props = append(props, additionalProps...)
|
||||
}
|
||||
|
||||
propsByFS, err := zfsGetRecursive(ctx, root, -1, []string{"filesystem", "volume"}, props, SourceAny)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "cannot get placeholder filesystems under %q", root)
|
||||
}
|
||||
|
||||
filtered := make(map[string]*ZFSProperties)
|
||||
for fs, props := range propsByFS {
|
||||
details := props.GetDetails(PlaceholderPropertyName)
|
||||
if details.Source != SourceLocal {
|
||||
continue
|
||||
}
|
||||
fsp, err := NewDatasetPath(fs)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "zfs get returned invalid dataset path %q", fs)
|
||||
}
|
||||
if !isLocalPlaceholderPropertyValuePlaceholder(fsp, details.Value) {
|
||||
continue
|
||||
}
|
||||
filtered[fs] = props
|
||||
}
|
||||
|
||||
return filtered, nil
|
||||
}
|
||||
|
||||
+57
-16
@@ -1567,8 +1567,18 @@ func (s PropertySource) zfsGetSourceFieldPrefixes() []string {
|
||||
return prefixes
|
||||
}
|
||||
|
||||
func zfsGet(ctx context.Context, path string, props []string, allowedSources PropertySource) (*ZFSProperties, error) {
|
||||
args := []string{"get", "-Hp", "-o", "property,value,source", strings.Join(props, ","), path}
|
||||
func zfsGetRecursive(ctx context.Context, path string, depth int, dstypes []string, props []string, allowedSources PropertySource) (map[string]*ZFSProperties, error) {
|
||||
args := []string{"get", "-Hp", "-o", "name,property,value,source"}
|
||||
if depth != 0 {
|
||||
args = append(args, "-r")
|
||||
if depth != -1 {
|
||||
args = append(args, "-d", fmt.Sprintf("%d", depth))
|
||||
}
|
||||
}
|
||||
if len(dstypes) > 0 {
|
||||
args = append(args, "-t", strings.Join(dstypes, ","))
|
||||
}
|
||||
args = append(args, strings.Join(props, ","), path)
|
||||
cmd := zfscmd.CommandContext(ctx, ZFS_BINARY, args...)
|
||||
stdout, err := cmd.Output()
|
||||
if err != nil {
|
||||
@@ -1588,36 +1598,67 @@ func zfsGet(ctx context.Context, path string, props []string, allowedSources Pro
|
||||
}
|
||||
o := string(stdout)
|
||||
lines := strings.Split(o, "\n")
|
||||
if len(lines) < 1 || // account for newlines
|
||||
len(lines)-1 != len(props) {
|
||||
return nil, fmt.Errorf("zfs get did not return the number of expected property values")
|
||||
}
|
||||
res := &ZFSProperties{
|
||||
make(map[string]PropertyValue, len(lines)),
|
||||
}
|
||||
propsByFS := make(map[string]*ZFSProperties)
|
||||
allowedPrefixes := allowedSources.zfsGetSourceFieldPrefixes()
|
||||
for _, line := range lines[:len(lines)-1] {
|
||||
for _, line := range lines[:len(lines)-1] { // last line is an empty line due to how strings.Split works
|
||||
fields := strings.FieldsFunc(line, func(r rune) bool {
|
||||
return r == '\t'
|
||||
})
|
||||
if len(fields) != 3 {
|
||||
return nil, fmt.Errorf("zfs get did not return property,value,source tuples")
|
||||
if len(fields) != 4 {
|
||||
return nil, fmt.Errorf("zfs get did not return name,property,value,source tuples")
|
||||
}
|
||||
for _, p := range allowedPrefixes {
|
||||
// prefix-match so that SourceAny (= "") works
|
||||
if strings.HasPrefix(fields[2], p) {
|
||||
source, err := parsePropertySource(fields[2])
|
||||
if strings.HasPrefix(fields[3], p) {
|
||||
source, err := parsePropertySource(fields[3])
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "parse property source")
|
||||
}
|
||||
res.m[fields[0]] = PropertyValue{
|
||||
Value: fields[1],
|
||||
fsProps, ok := propsByFS[fields[0]]
|
||||
if !ok {
|
||||
fsProps = &ZFSProperties{
|
||||
make(map[string]PropertyValue),
|
||||
}
|
||||
}
|
||||
if _, ok := fsProps.m[fields[1]]; ok {
|
||||
return nil, errors.Errorf("duplicate property %q for dataset %q", fields[1], fields[0])
|
||||
}
|
||||
fsProps.m[fields[1]] = PropertyValue{
|
||||
Value: fields[2],
|
||||
Source: source,
|
||||
}
|
||||
propsByFS[fields[0]] = fsProps
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
// validate we got expected output
|
||||
for fs, fsProps := range propsByFS {
|
||||
if len(fsProps.m) != len(props) {
|
||||
return nil, errors.Errorf("zfs get did not return all requested values for dataset %q\noutput was:\n%s", fs, o)
|
||||
}
|
||||
}
|
||||
return propsByFS, nil
|
||||
}
|
||||
|
||||
func zfsGet(ctx context.Context, path string, props []string, allowedSources PropertySource) (*ZFSProperties, error) {
|
||||
propMap, err := zfsGetRecursive(ctx, path, 0, nil, props, allowedSources)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(propMap) == 0 {
|
||||
// XXX callers expect to always get a result here
|
||||
// They will observe props.Get("propname") == ""
|
||||
// We should change .Get to return a tuple, or an error, or whatever.
|
||||
return &ZFSProperties{make(map[string]PropertyValue)}, nil
|
||||
}
|
||||
if len(propMap) != 1 {
|
||||
return nil, errors.Errorf("zfs get unexpectedly returned properties for multiple datasets")
|
||||
}
|
||||
res, ok := propMap[path]
|
||||
if !ok {
|
||||
return nil, errors.Errorf("zfs get returned properties for a different dataset that requested")
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user