Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of unmapped configuration options #185

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions config/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import (
var (
optionsLock sync.RWMutex
options = make(map[string]*Option)

// unmappedValues holds a list of configuration values that have been
// read from the persistence layer but no option has been defined yet.
// This is mainly to support the plugin system of the Portmaster.
unmappedValuesLock sync.Mutex
unmappedValues map[string]interface{}
)

// ForEachOption calls fn for each defined option. If fn returns
Expand Down Expand Up @@ -98,9 +104,47 @@ func Register(option *Option) error {
return fmt.Errorf("config: invalid default value: %w", vErr)
}

hasUnmappedValue, vErr := loadUnmappedValue(option)
if vErr != nil && !vErr.SoftError {
return fmt.Errorf("config: invalid value: %w", vErr)
}

optionsLock.Lock()
defer optionsLock.Unlock()
options[option.Key] = option

if hasUnmappedValue {
signalChanges()
}

// return the validation-error from loadUnmappedValue here
if vErr != nil {
return vErr
}

return nil
}

func loadUnmappedValue(option *Option) (bool, *ValidationError) {
unmappedValuesLock.Lock()
defer unmappedValuesLock.Unlock()

if value, ok := unmappedValues[option.Key]; ok {
delete(unmappedValues, option.Key)

var vErr *ValidationError
option.activeValue, vErr = validateValue(option, value)
if vErr != nil {
// we consider this error as a "soft" error so lazily registered
// options don't fail the hard way.
option.activeValue = option.activeFallbackValue
vErr.SoftError = true

return true, vErr
}

return true, nil
}

return false, nil
}
14 changes: 13 additions & 1 deletion config/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,21 @@ func signalChanges() {
func replaceConfig(newValues map[string]interface{}) []*ValidationError {
var validationErrors []*ValidationError

valuesCopy := make(map[string]interface{}, len(newValues))
for key, value := range newValues {
valuesCopy[key] = value
}

// RLock the options because we are not adding or removing
// options from the registration but rather only update the
// options value which is guarded by the option's lock itself
optionsLock.RLock()
defer optionsLock.RUnlock()

for key, option := range options {
newValue, ok := newValues[key]
newValue, ok := valuesCopy[key]

delete(valuesCopy, key)

option.Lock()
option.activeValue = nil
Expand All @@ -66,6 +73,11 @@ func replaceConfig(newValues map[string]interface{}) []*ValidationError {
option.Unlock()
}

unmappedValuesLock.Lock()
defer unmappedValuesLock.Unlock()

unmappedValues = valuesCopy

signalChanges()

return validationErrors
Expand Down
5 changes: 3 additions & 2 deletions config/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,9 @@ func validateValue(option *Option, value interface{}) (*valueCache, *ValidationE

// ValidationError error holds details about a config option value validation error.
type ValidationError struct {
Option *Option
Err error
Option *Option
Err error
SoftError bool
}

// Error returns the formatted error.
Expand Down