Skip to content

Commit

Permalink
fix ennvar expansion in configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Mar 11, 2024
1 parent ee9cca4 commit 01c55f0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
13 changes: 8 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"bytes"
"context"
"flag"
"fmt"
Expand All @@ -20,8 +19,10 @@ import (

csbouncer "github.com/crowdsecurity/go-cs-bouncer"
"github.com/crowdsecurity/go-cs-lib/csdaemon"
"github.com/crowdsecurity/go-cs-lib/csstring"
"github.com/crowdsecurity/go-cs-lib/version"


"github.com/crowdsecurity/crowdsec/pkg/models"

"github.com/crowdsecurity/cs-firewall-bouncer/pkg/backend"
Expand Down Expand Up @@ -152,17 +153,19 @@ func Execute() error {
return fmt.Errorf("configuration file is required")
}

configBytes, err := cfg.MergedConfig(*configPath)
configMerged, err := cfg.MergedConfig(*configPath)
if err != nil {
return fmt.Errorf("unable to read config file: %w", err)
}

if *showConfig {
fmt.Println(string(configBytes))
fmt.Println(string(configMerged))
return nil
}

config, err := cfg.NewConfig(bytes.NewReader(configBytes))
configExpanded := csstring.StrictExpand(string(configMerged), os.LookupEnv)

config, err := cfg.NewConfig(strings.NewReader(configExpanded))
if err != nil {
return fmt.Errorf("unable to load configuration: %w", err)
}
Expand All @@ -186,7 +189,7 @@ func Execute() error {

bouncer := &csbouncer.StreamBouncer{}

err = bouncer.ConfigReader(bytes.NewReader(configBytes))
err = bouncer.ConfigReader(strings.NewReader(configExpanded))
if err != nil {
return err
}
Expand Down
11 changes: 4 additions & 7 deletions pkg/cfg/config.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package cfg

import (
"errors"
"fmt"
"io"
"os"

log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"

"github.com/crowdsecurity/go-cs-lib/csstring"
"github.com/crowdsecurity/go-cs-lib/ptr"
"github.com/crowdsecurity/go-cs-lib/yamlpatch"
)
Expand Down Expand Up @@ -86,9 +85,7 @@ func NewConfig(reader io.Reader) (*BouncerConfig, error) {
return nil, err
}

configBuff := csstring.StrictExpand(string(fcontent), os.LookupEnv)

err = yaml.Unmarshal([]byte(configBuff), &config)
err = yaml.Unmarshal(fcontent, &config)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
Expand All @@ -98,7 +95,7 @@ func NewConfig(reader io.Reader) (*BouncerConfig, error) {
}

if config.Mode == "" {
return nil, fmt.Errorf("config does not contain 'mode'")
return nil, errors.New("config does not contain 'mode'")
}

if len(config.SupportedDecisionsTypes) == 0 {
Expand Down Expand Up @@ -191,7 +188,7 @@ func nftablesConfig(config *BouncerConfig) error {
}

if !*config.Nftables.Ipv4.Enabled && !*config.Nftables.Ipv6.Enabled {
return fmt.Errorf("both IPv4 and IPv6 disabled, doing nothing")
return errors.New("both IPv4 and IPv6 disabled, doing nothing")
}

if config.NftablesHooks == nil || len(config.NftablesHooks) == 0 {
Expand Down

0 comments on commit 01c55f0

Please sign in to comment.