fix(fstab): Improve fstab parser

This commit is contained in:
Myzel394 2024-08-05 21:00:54 +02:00
parent afec5e896d
commit 3f102e283d
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
7 changed files with 169 additions and 131 deletions

View File

@ -0,0 +1,11 @@
package fstabdocumentation
import docvalues "config-lsp/doc-values"
var minValue = 0
var maxValue = 9
var FreqField = docvalues.NumberValue{
Min: &minValue,
Max: &maxValue,
}

View File

@ -0,0 +1,23 @@
package fstabdocumentation
import (
docvalues "config-lsp/doc-values"
"regexp"
)
var MountPointField = docvalues.OrValue{
Values: []docvalues.Value{
docvalues.EnumValue{
Values: []docvalues.EnumString{
{
InsertText: "none",
DescriptionText: "none",
Documentation: "Specify that the filesystem should be treated as swap space",
},
},
},
docvalues.RegexValue{
Regex: *regexp.MustCompile(`\S+`),
},
},
}

View File

@ -1,9 +1,8 @@
package fstab package fstabdocumentation
import ( import (
commondocumentation "config-lsp/common-documentation/filesystems" commondocumentation "config-lsp/common-documentation/filesystems/mountoptions"
docvalues "config-lsp/doc-values" docvalues "config-lsp/doc-values"
"config-lsp/utils"
"strings" "strings"
) )
@ -199,67 +198,40 @@ type assignOption struct {
Handler func(context docvalues.KeyValueAssignmentContext) docvalues.Value Handler func(context docvalues.KeyValueAssignmentContext) docvalues.Value
} }
var defaultAssignOptions = map[string]commondocumentation.AssignableOption{ var defaultAssignOptions = map[docvalues.EnumString]docvalues.Value{
"context": { docvalues.CreateEnumStringWithDoc(
Documentation: "The context= option is useful when mounting filesystems that do not support extended attributes, such as a floppy or hard disk formatted with VFAT, or systems that are not normally running under SELinux, such as an ext3 or ext4 formatted disk from a non-SELinux workstation. You can also use context= on filesystems you do not trust, such as a floppy. It also helps in compatibility with xattr-supporting filesystems on earlier 2.4.<x> kernel versions. Even where xattrs are supported, you can save time not having to label every file by assigning the entire disk one security context. A commonly used option for removable media is context=\"system_u:object_r:removable_t\".", "context",
Handler: func(_ docvalues.KeyValueAssignmentContext) docvalues.Value { "The context= option is useful when mounting filesystems that do not support extended attributes, such as a floppy or hard disk formatted with VFAT, or systems that are not normally running under SELinux, such as an ext3 or ext4 formatted disk from a non-SELinux workstation. You can also use context= on filesystems you do not trust, such as a floppy. It also helps in compatibility with xattr-supporting filesystems on earlier 2.4.<x> kernel versions. Even where xattrs are supported, you can save time not having to label every file by assigning the entire disk one security context. A commonly used option for removable media is context=\"system_u:object_r:removable_t\".",
return docvalues.StringValue{} ): docvalues.StringValue{},
}, docvalues.CreateEnumStringWithDoc(
}, "fscontext",
"fscontext": { "The fscontext= option works for all filesystems, regardless of their xattr support. The fscontext option sets the overarching filesystem label to a specific security context. This filesystem label is separate from the individual labels on the files. It represents the entire filesystem for certain kinds of permission checks, such as during mount or file creation. Individual file labels are still obtained from the xattrs on the files themselves. The context option actually sets the aggregate context that fscontext provides, in addition to supplying the same label for individual files.",
Documentation: "The fscontext= option works for all filesystems, regardless of their xattr support. The fscontext option sets the overarching filesystem label to a specific security context. This filesystem label is separate from the individual labels on the files. It represents the entire filesystem for certain kinds of permission checks, such as during mount or file creation. Individual file labels are still obtained from the xattrs on the files themselves. The context option actually sets the aggregate context that fscontext provides, in addition to supplying the same label for individual files.", ): docvalues.StringValue{},
Handler: func(_ docvalues.KeyValueAssignmentContext) docvalues.Value { docvalues.CreateEnumStringWithDoc(
return docvalues.StringValue{} "defcontext",
}, "You can set the default security context for unlabeled files using defcontext= option. This overrides the value set for unlabeled files in the policy and requires a filesystem that supports xattr labeling.",
}, ): docvalues.StringValue{},
"defcontext": { docvalues.CreateEnumStringWithDoc(
Documentation: "You can set the default security context for unlabeled files using defcontext= option. This overrides the value set for unlabeled files in the policy and requires a filesystem that supports xattr labeling.", "rootcontext",
Handler: func(_ docvalues.KeyValueAssignmentContext) docvalues.Value { "The rootcontext= option allows you to explicitly label the root inode of a FS being mounted before that FS or inode becomes visible to userspace. This was found to be useful for things like stateless Linux. The special value @target can be used to assign the current context of the target mountpoint location.",
return docvalues.StringValue{} ): docvalues.StringValue{},
},
},
"rootcontext": {
Documentation: "The rootcontext= option allows you to explicitly label the root inode of a FS being mounted before that FS or inode becomes visible to userspace. This was found to be useful for things like stateless Linux. The special value @target can be used to assign the current context of the target mountpoint location.",
Handler: func(_ docvalues.KeyValueAssignmentContext) docvalues.Value {
return docvalues.StringValue{}
},
},
} }
func createMountOptionField( func createMountOptionField(
options []docvalues.EnumString, options []docvalues.EnumString,
assignOption map[string]commondocumentation.AssignableOption, assignOption map[docvalues.EnumString]docvalues.Value,
) docvalues.Value { ) docvalues.Value {
dynamicOptions := utils.MergeMaps(defaultAssignOptions, assignOption) dynamicOptions := docvalues.MergeKeyEnumAssignmentMaps(defaultAssignOptions, assignOption)
return docvalues.ArrayValue{ return docvalues.ArrayValue{
Separator: ",", Separator: ",",
DuplicatesExtractor: &mountOptionsExtractor, DuplicatesExtractor: &mountOptionsExtractor,
SubValue: docvalues.OrValue{ SubValue: docvalues.OrValue{
Values: []docvalues.Value{ Values: []docvalues.Value{
docvalues.KeyValueAssignmentValue{ docvalues.KeyEnumAssignmentValue{
Separator: "=", Values: dynamicOptions,
ValueIsOptional: false, ValueIsOptional: false,
Key: docvalues.EnumValue{ Separator: "=",
EnforceValues: true,
Values: utils.Map(
utils.KeysOfMap(dynamicOptions),
func(key string) docvalues.EnumString {
return docvalues.CreateEnumStringWithDoc(
key,
dynamicOptions[key].Documentation,
)
},
),
},
Value: docvalues.CustomValue{
FetchValue: func(rawContext docvalues.CustomValueContext) docvalues.Value {
context := rawContext.(docvalues.KeyValueAssignmentContext)
option := dynamicOptions[context.SelectedKey]
return option.Handler(context)
},
},
}, },
docvalues.EnumValue{ docvalues.EnumValue{
EnforceValues: true, EnforceValues: true,
@ -270,30 +242,38 @@ func createMountOptionField(
} }
} }
var defaultMountOptionsField = createMountOptionField([]docvalues.EnumString{}, map[string]commondocumentation.AssignableOption{}) var DefaultMountOptionsField = createMountOptionField([]docvalues.EnumString{}, map[docvalues.EnumString]docvalues.Value{})
var mountOptionsMapField = map[string]docvalues.Value{ var MountOptionsMapField = map[string]docvalues.Value{
"adfs": createMountOptionField( // "adfs": createMountOptionField(
[]docvalues.EnumString{}, // []docvalues.EnumString{},
map[string]commondocumentation.AssignableOption{ // map[string]commondocumentation.AssignableOption{
"uid": { // "uid": {
Documentation: "Set the owner of the files in the filesystem", // Documentation: "Set the owner of the files in the filesystem",
Handler: func(context docvalues.KeyValueAssignmentContext) docvalues.Value { // Handler: func(context docvalues.KeyValueAssignmentContext) docvalues.Value {
min := 0 // min := 0
return docvalues.NumberValue{Min: &min} // return docvalues.NumberValue{Min: &min}
}, // },
}, // },
"gid": { // "gid": {
Documentation: "Set the group of the files in the filesystem", // Documentation: "Set the group of the files in the filesystem",
Handler: func(context docvalues.KeyValueAssignmentContext) docvalues.Value { // Handler: func(context docvalues.KeyValueAssignmentContext) docvalues.Value {
min := 0 // min := 0
return docvalues.NumberValue{Min: &min} // return docvalues.NumberValue{Min: &min}
}, // },
}, // },
}, // },
), // ),
"ext2": createMountOptionField( "ext2": createMountOptionField(
commondocumentation.Ext2DocumentationEnums, commondocumentation.Ext2DocumentationEnums,
commondocumentation.Ext2DocumentationAssignable, commondocumentation.Ext2DocumentationAssignable,
), ),
"ext3": createMountOptionField(
append(commondocumentation.Ext2DocumentationEnums, commondocumentation.Ext3DocumentationEnums...),
docvalues.MergeKeyEnumAssignmentMaps(commondocumentation.Ext2DocumentationAssignable, commondocumentation.Ext3DocumentationAssignable),
),
"ext4": createMountOptionField(
append(append(commondocumentation.Ext2DocumentationEnums, commondocumentation.Ext3DocumentationEnums...), commondocumentation.Ext4DocumentationEnums...),
docvalues.MergeKeyEnumAssignmentMaps(commondocumentation.Ext2DocumentationAssignable, docvalues.MergeKeyEnumAssignmentMaps(commondocumentation.Ext3DocumentationAssignable, commondocumentation.Ext4DocumentationAssignable)),
),
} }

View File

@ -0,0 +1,26 @@
package fstabdocumentation
import docvalues "config-lsp/doc-values"
var PassField = docvalues.OrValue{
Values: []docvalues.Value{
docvalues.EnumValue{
EnforceValues: false,
Values: []docvalues.EnumString{
docvalues.CreateEnumStringWithDoc(
"0",
"Defaults to zero (dont check the filesystem) if not present.",
),
docvalues.CreateEnumStringWithDoc(
"1",
"The root filesystem should be specified with a fs_passno of 1.",
),
docvalues.CreateEnumStringWithDoc(
"2",
"Other filesystems [than the root filesystem] should have a fs_passno of 2.",
),
},
},
docvalues.NumberValue{},
},
}

View File

@ -1,18 +1,18 @@
package fstab package fstabdocumentation
import ( import (
docvalues "config-lsp/doc-values" docvalues "config-lsp/doc-values"
"regexp" "regexp"
) )
var uuidField = docvalues.RegexValue{ var UuidField = docvalues.RegexValue{
Regex: *regexp.MustCompile(`[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}`), Regex: *regexp.MustCompile(`[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}`),
} }
var labelField = docvalues.RegexValue{ var LabelField = docvalues.RegexValue{
Regex: *regexp.MustCompile(`\S+`), Regex: *regexp.MustCompile(`\S+`),
} }
var specField = docvalues.OrValue{ var SpecField = docvalues.OrValue{
Values: []docvalues.Value{ Values: []docvalues.Value{
// docvalues.PathValue{ // docvalues.PathValue{
// RequiredType: docvalues.PathTypeFile & docvalues.PathTypeExistenceOptional, // RequiredType: docvalues.PathTypeFile & docvalues.PathTypeExistenceOptional,
@ -36,10 +36,10 @@ var specField = docvalues.OrValue{
switch context.SelectedKey { switch context.SelectedKey {
case "UUID": case "UUID":
case "PARTUUID": case "PARTUUID":
return uuidField return UuidField
case "LABEL": case "LABEL":
case "PARTLABEL": case "PARTLABEL":
return labelField return LabelField
} }
return docvalues.StringValue{} return docvalues.StringValue{}
@ -48,20 +48,3 @@ var specField = docvalues.OrValue{
}, },
}, },
} }
var mountPointField = docvalues.OrValue{
Values: []docvalues.Value{
docvalues.EnumValue{
Values: []docvalues.EnumString{
{
InsertText: "none",
DescriptionText: "none",
Documentation: "Specify that the filesystem should be treated as swap space",
},
},
},
docvalues.RegexValue{
Regex: *regexp.MustCompile(`\S+`),
},
},
}

View File

@ -1,8 +1,8 @@
package fstab package fstabdocumentation
import docvalues "config-lsp/doc-values" import docvalues "config-lsp/doc-values"
var fileSystemTypeField = docvalues.ArrayValue{ var FileSystemTypeField = docvalues.ArrayValue{
Separator: ",", Separator: ",",
DuplicatesExtractor: &docvalues.SimpleDuplicatesExtractor, DuplicatesExtractor: &docvalues.SimpleDuplicatesExtractor,
SubValue: docvalues.EnumValue{ SubValue: docvalues.EnumValue{

View File

@ -3,6 +3,7 @@ package fstab
import ( import (
"config-lsp/common" "config-lsp/common"
docvalues "config-lsp/doc-values" docvalues "config-lsp/doc-values"
fstabdocumentation "config-lsp/handlers/fstab/documentation"
"fmt" "fmt"
"regexp" "regexp"
"slices" "slices"
@ -62,45 +63,40 @@ type FstabEntry struct {
} }
func (e *FstabEntry) CheckIsValid() []protocol.Diagnostic { func (e *FstabEntry) CheckIsValid() []protocol.Diagnostic {
println(fmt.Sprintf("Checking entry at line %d; fields: %v", e.Line, e.Fields))
diagnostics := make([]protocol.Diagnostic, 0) diagnostics := make([]protocol.Diagnostic, 0)
severity := protocol.DiagnosticSeverityError
if e.Fields.Spec != nil { if e.Fields.Spec != nil {
err := specField.CheckIsValid(e.Fields.Spec.Value) errors := fstabdocumentation.SpecField.CheckIsValid(e.Fields.Spec.Value)
if err != nil { if len(errors) > 0 {
diagnostics = append(diagnostics, protocol.Diagnostic{ diagnostics = append(
Range: e.Fields.Spec.CreateRange(e.Line), diagnostics,
Message: err.Error(), docvalues.InvalidValuesToErrorDiagnostics(e.Line, e.Fields.Spec.Start, errors)...,
Severity: &severity, )
})
} }
} }
if e.Fields.MountPoint != nil { if e.Fields.MountPoint != nil {
err := mountPointField.CheckIsValid(e.Fields.MountPoint.Value) errors := fstabdocumentation.MountPointField.CheckIsValid(e.Fields.MountPoint.Value)
if err != nil { if len(errors) > 0 {
diagnostics = append(diagnostics, protocol.Diagnostic{ diagnostics = append(
Range: e.Fields.Spec.CreateRange(e.Line), diagnostics,
Message: err.Error(), docvalues.InvalidValuesToErrorDiagnostics(e.Line, e.Fields.MountPoint.Start, errors)...,
Severity: &severity, )
})
} }
} }
var fileSystemType string = "" var fileSystemType string = ""
if e.Fields.FilesystemType != nil { if e.Fields.FilesystemType != nil {
err := fileSystemTypeField.CheckIsValid(e.Fields.FilesystemType.Value) errors := fstabdocumentation.FileSystemTypeField.CheckIsValid(e.Fields.FilesystemType.Value)
if err != nil { if len(errors) > 0 {
diagnostics = append(diagnostics, protocol.Diagnostic{ diagnostics = append(
Range: e.Fields.FilesystemType.CreateRange(e.Line), diagnostics,
Message: err.Error(), docvalues.InvalidValuesToErrorDiagnostics(e.Line, e.Fields.FilesystemType.Start, errors)...,
Severity: &severity, )
})
} else { } else {
fileSystemType = e.Fields.FilesystemType.Value fileSystemType = e.Fields.FilesystemType.Value
} }
@ -109,22 +105,41 @@ func (e *FstabEntry) CheckIsValid() []protocol.Diagnostic {
if e.Fields.Options != nil && fileSystemType != "" { if e.Fields.Options != nil && fileSystemType != "" {
var optionsField docvalues.Value var optionsField docvalues.Value
if foundField, found := mountOptionsMapField[fileSystemType]; found { if foundField, found := fstabdocumentation.MountOptionsMapField[fileSystemType]; found {
optionsField = foundField optionsField = foundField
} else { } else {
optionsField = defaultMountOptionsField optionsField = fstabdocumentation.DefaultMountOptionsField
} }
println(fmt.Sprintf("Checking options for %s", fileSystemType)) errors := optionsField.CheckIsValid(e.Fields.Options.Value)
err := optionsField.CheckIsValid(e.Fields.Options.Value) if len(errors) > 0 {
diagnostics = append(
diagnostics,
docvalues.InvalidValuesToErrorDiagnostics(e.Line, e.Fields.Options.Start, errors)...,
)
}
}
if err != nil { if e.Fields.Freq != nil {
diagnostics = append(diagnostics, protocol.Diagnostic{ errors := fstabdocumentation.FreqField.CheckIsValid(e.Fields.Freq.Value)
Range: e.Fields.Options.CreateRange(e.Line),
Message: err.Error(), if len(errors) > 0 {
Severity: &severity, diagnostics = append(
}) diagnostics,
docvalues.InvalidValuesToErrorDiagnostics(e.Line, e.Fields.Freq.Start, errors)...,
)
}
}
if e.Fields.Pass != nil {
errors := fstabdocumentation.PassField.CheckIsValid(e.Fields.Pass.Value)
if len(errors) > 0 {
diagnostics = append(
diagnostics,
docvalues.InvalidValuesToErrorDiagnostics(e.Line, e.Fields.Pass.Start, errors)...,
)
} }
} }