Skip to content

Commit

Permalink
feat: add environment variable support in config
Browse files Browse the repository at this point in the history
  • Loading branch information
whiskeyjimbo committed Jan 8, 2025
1 parent 9169602 commit e5c62b5
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
10 changes: 5 additions & 5 deletions examples/config.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
monitorSite: "sol"
monitorSite: "${MONITOR_SITE:-sol}" # Uses "sol" as default if MONITOR_SITE is not set

sites:
- name: "mars-lab"
tags: ["region-mars", "dev"]
- name: "${SITE_NAME:-mars-lab}"
tags: ["region-${REGION:-mars}", "env-${ENV:-dev}"]
groups:
- name: "api-service"
tags: ["service-api", "critical"]
ruleMode: "any" # Override default "all" mode for this group
hosts:
- host: "api-1.mars.lab"
- host: "${API_HOST_1:-api-1.mars.lab}"
tags: ["primary"]
- host: "api-2.mars.lab"
- host: "${API_HOST_2:-api-2.mars.lab}"
tags: ["primary"]
- host: "api-3.mars.lab"
tags: ["backup"]
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/whiskeyjimbo/CheckMate
go 1.23.4

require (
github.com/drone/envsubst v1.0.3
github.com/expr-lang/expr v1.16.9
go.uber.org/automaxprocs v1.6.0
go.uber.org/zap v1.27.0
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UF
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/drone/envsubst v1.0.3 h1:PCIBwNDYjs50AsLZPYdfhSATKaRg/FJmDc2D6+C2x8g=
github.com/drone/envsubst v1.0.3/go.mod h1:N2jZmlMufstn1KEqvbHjw40h1KyTmnVzHcSc9bFiJ2g=
github.com/expr-lang/expr v1.16.9 h1:WUAzmR0JNI9JCiF0/ewwHB1gmcGw5wW7nWt8gc6PpCI=
github.com/expr-lang/expr v1.16.9/go.mod h1:8/vRC7+7HBzESEqt5kKpYXxrxkr31SaO8r40VO/1IT4=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
Expand Down
13 changes: 8 additions & 5 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"strings"

"github.com/drone/envsubst"
"github.com/whiskeyjimbo/CheckMate/internal/rules"
"gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -84,17 +85,19 @@ func loadConfig(filename string) (*Config, error) {
return nil, fmt.Errorf("absolute paths are not allowed: %s", filename)
}

if strings.Contains(cleanPath, "..") {
return nil, fmt.Errorf("path traversal detected: %s", filename)
}

data, err := os.ReadFile(cleanPath)
if err != nil {
return nil, err
}

// Use envsubst to handle environment variable substitution
expandedData, err := envsubst.EvalEnv(string(data))
if err != nil {
return nil, fmt.Errorf("failed to substitute environment variables: %w", err)
}

var config Config
if err := yaml.Unmarshal(data, &config); err != nil {
if err := yaml.Unmarshal([]byte(expandedData), &config); err != nil {
return nil, err
}

Expand Down

0 comments on commit e5c62b5

Please sign in to comment.