Use constants for default action values

This commit is contained in:
Daniel
2022-10-13 14:18:34 +02:00
parent 106793b56a
commit c99c4aeeff
4 changed files with 18 additions and 14 deletions

View File

@@ -44,11 +44,11 @@ func updateGlobalConfigProfile(ctx context.Context, task *modules.Task) error {
action := cfgOptionDefaultAction() action := cfgOptionDefaultAction()
switch action { switch action {
case "permit": case DefaultActionPermitValue:
cfgDefaultAction = DefaultActionPermit cfgDefaultAction = DefaultActionPermit
case "ask": case DefaultActionAskValue:
cfgDefaultAction = DefaultActionAsk cfgDefaultAction = DefaultActionAsk
case "block": case DefaultActionBlockValue:
cfgDefaultAction = DefaultActionBlock cfgDefaultAction = DefaultActionBlock
default: default:
// TODO: module error? // TODO: module error?

View File

@@ -23,6 +23,10 @@ var (
cfgOptionDefaultAction config.StringOption cfgOptionDefaultAction config.StringOption
cfgOptionDefaultActionOrder = 1 cfgOptionDefaultActionOrder = 1
DefaultActionPermitValue = "permit"
DefaultActionBlockValue = "block"
DefaultActionAskValue = "ask"
// Setting "Prompt Desktop Notifications" at order 2. // Setting "Prompt Desktop Notifications" at order 2.
// Setting "Prompt Timeout" at order 3. // Setting "Prompt Timeout" at order 3.
@@ -182,7 +186,7 @@ func registerConfiguration() error { //nolint:maintidx
Key: CfgOptionDefaultActionKey, Key: CfgOptionDefaultActionKey,
Description: `The default network action is applied when nothing else allows or blocks an outgoing connection. Incoming connections are always blocked by default.`, Description: `The default network action is applied when nothing else allows or blocks an outgoing connection. Incoming connections are always blocked by default.`,
OptType: config.OptTypeString, OptType: config.OptTypeString,
DefaultValue: "permit", DefaultValue: DefaultActionPermitValue,
Annotations: config.Annotations{ Annotations: config.Annotations{
config.DisplayHintAnnotation: config.DisplayHintOneOf, config.DisplayHintAnnotation: config.DisplayHintOneOf,
config.DisplayOrderAnnotation: cfgOptionDefaultActionOrder, config.DisplayOrderAnnotation: cfgOptionDefaultActionOrder,
@@ -191,17 +195,17 @@ func registerConfiguration() error { //nolint:maintidx
PossibleValues: []config.PossibleValue{ PossibleValues: []config.PossibleValue{
{ {
Name: "Allow", Name: "Allow",
Value: "permit", Value: DefaultActionPermitValue,
Description: "Allow all connections", Description: "Allow all connections",
}, },
{ {
Name: "Block", Name: "Block",
Value: "block", Value: DefaultActionBlockValue,
Description: "Block all connections", Description: "Block all connections",
}, },
{ {
Name: "Prompt", Name: "Prompt",
Value: "ask", Value: DefaultActionAskValue,
Description: "Prompt for decisions", Description: "Prompt for decisions",
}, },
}, },
@@ -209,7 +213,7 @@ func registerConfiguration() error { //nolint:maintidx
if err != nil { if err != nil {
return err return err
} }
cfgOptionDefaultAction = config.Concurrent.GetAsString(CfgOptionDefaultActionKey, "permit") cfgOptionDefaultAction = config.Concurrent.GetAsString(CfgOptionDefaultActionKey, DefaultActionPermitValue)
cfgStringOptions[CfgOptionDefaultActionKey] = cfgOptionDefaultAction cfgStringOptions[CfgOptionDefaultActionKey] = cfgOptionDefaultAction
// Disable Auto Permit // Disable Auto Permit

View File

@@ -175,11 +175,11 @@ func (profile *Profile) parseConfig() error {
profile.defaultAction = DefaultActionNotSet profile.defaultAction = DefaultActionNotSet
if ok { if ok {
switch action { switch action {
case "permit": case DefaultActionPermitValue:
profile.defaultAction = DefaultActionPermit profile.defaultAction = DefaultActionPermit
case "ask": case DefaultActionAskValue:
profile.defaultAction = DefaultActionAsk profile.defaultAction = DefaultActionAsk
case "block": case DefaultActionBlockValue:
profile.defaultAction = DefaultActionBlock profile.defaultAction = DefaultActionBlock
default: default:
lastErr = fmt.Errorf(`default action "%s" invalid`, action) lastErr = fmt.Errorf(`default action "%s" invalid`, action)

View File

@@ -174,7 +174,7 @@ func createSpecialProfile(profileID string, path string) *Profile {
// Resolved domain from the system resolver are checked again when // Resolved domain from the system resolver are checked again when
// attributed to a connection of a regular process. Otherwise, users // attributed to a connection of a regular process. Otherwise, users
// would see two connection prompts for the same domain. // would see two connection prompts for the same domain.
CfgOptionDefaultActionKey: "permit", CfgOptionDefaultActionKey: DefaultActionPermitValue,
// Explicitly allow incoming connections. // Explicitly allow incoming connections.
CfgOptionBlockInboundKey: status.SecurityLevelOff, CfgOptionBlockInboundKey: status.SecurityLevelOff,
// Explicitly allow localhost and answers to multicast protocols that // Explicitly allow localhost and answers to multicast protocols that
@@ -211,7 +211,7 @@ func createSpecialProfile(profileID string, path string) *Profile {
Source: SourceLocal, Source: SourceLocal,
PresentationPath: path, PresentationPath: path,
Config: map[string]interface{}{ Config: map[string]interface{}{
CfgOptionDefaultActionKey: "block", CfgOptionDefaultActionKey: DefaultActionBlockValue,
CfgOptionEndpointsKey: []string{ CfgOptionEndpointsKey: []string{
"+ Localhost", "+ Localhost",
"+ .safing.io", "+ .safing.io",
@@ -226,7 +226,7 @@ func createSpecialProfile(profileID string, path string) *Profile {
Source: SourceLocal, Source: SourceLocal,
PresentationPath: path, PresentationPath: path,
Config: map[string]interface{}{ Config: map[string]interface{}{
CfgOptionDefaultActionKey: "block", CfgOptionDefaultActionKey: DefaultActionBlockValue,
CfgOptionEndpointsKey: []string{ CfgOptionEndpointsKey: []string{
"+ Localhost", "+ Localhost",
}, },