Skip to content

feat: validate observe-agent config instead of simply parsing the yaml #115

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

Merged
merged 3 commits into from
Oct 28, 2024
Merged
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
6 changes: 3 additions & 3 deletions internal/commands/diagnose/authcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ func makeTestRequest(URL string, headers map[string]string) NetworkTestResult {
}
}

func makeAuthTestRequest() (any, error) {
collector_url := viper.GetString("observe_url")
authToken := fmt.Sprintf("Bearer %s", viper.GetString("token"))
func makeAuthTestRequest(v *viper.Viper) (any, error) {
collector_url := v.GetString("observe_url")
authToken := fmt.Sprintf("Bearer %s", v.GetString("token"))
authTestResponse := makeTestRequest(collector_url, map[string]string{"Authorization": authToken})
return authTestResponse, nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/diagnose/authcheck.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Running auth check against {{ .URL }}
Request to test URL responded with response code {{ .ResponseCode }}
{{- else }}
{{- if eq .ResponseCode 401 }}
Request to test URL failed with error {{ .Error }}.
⚠️ Request to test URL failed with error {{ .Error }}.

Remediation
Please check that the token is present in the `observe-agent.yaml` config file and that the token is valid.
Expand Down
40 changes: 24 additions & 16 deletions internal/commands/diagnose/configcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,48 @@ import (
"fmt"
"os"

"github.com/observeinc/observe-agent/internal/config"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)

type ConfigTestResult struct {
ConfigFile string
Passed bool
Error string
ConfigFile string
ParseSucceeded bool
IsValid bool
Error string
}

func validateYaml(yamlContent []byte) error {
m := make(map[string]any)
return yaml.Unmarshal(yamlContent, &m)
}

func checkConfig() (any, error) {
configFile := viper.ConfigFileUsed()
func checkConfig(v *viper.Viper) (any, error) {
configFile := v.ConfigFileUsed()
if configFile == "" {
return nil, fmt.Errorf("no config file defined")
}
contents, err := os.ReadFile(configFile)
if err != nil {
return nil, err
}
if err = validateYaml(contents); err != nil {
var conf config.AgentConfig
if err = yaml.Unmarshal(contents, &conf); err != nil {
return ConfigTestResult{
ConfigFile: configFile,
ParseSucceeded: false,
IsValid: false,
Error: err.Error(),
}, nil
}
if err = conf.Validate(); err != nil {
return ConfigTestResult{
configFile,
false,
err.Error(),
ConfigFile: configFile,
ParseSucceeded: true,
IsValid: false,
Error: err.Error(),
}, nil
}
return ConfigTestResult{
ConfigFile: configFile,
Passed: true,
ConfigFile: configFile,
ParseSucceeded: true,
IsValid: true,
}, nil
}

Expand Down
11 changes: 7 additions & 4 deletions internal/commands/diagnose/configcheck.tmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Running check on config file {{ .ConfigFile }}
{{- if .Passed }}
Config file is valid YAML.
Running check on observe-agent config file {{ .ConfigFile }}
{{- if .IsValid }}
Config file is valid.
{{- else if .ParseSucceeded}}
⚠️ Config file validation failed with error {{ .Error }}
{{- else }}
⚠️ Config file parse failed with error {{ .Error }}
⚠️ Config file could not be parsed as YAML
{{ .Error }}
{{- end }}
63 changes: 43 additions & 20 deletions internal/commands/diagnose/configcheck_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package diagnose

import (
"os"
"testing"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)

const testConfig = `
# Observe data token
token: "some token"
token: "some:token"

# Target Observe collection url
observe_url: "localhost"
observe_url: "https://collect.observeinc.com"

# Debug mode - Sets agent log level to debug
debug: false
Expand All @@ -35,25 +37,46 @@ host_monitoring:
enabled: false
`

var (
validCases = []string{
testConfig,
"key:\n spaceIndented: \"value\"",
}
invalidCases = []string{
"key:\n\ttabIndented: \"value\"",
"key:\n twoSpaces: true\n threeSpaces: true",
"\tstartsWithTab: true",
}
)
var testCases = []struct {
confStr string
shouldParse bool
isValid bool
}{
// Invalid YAML
{"key:\n\ttabIndented: \"value\"", false, false},
{"key:\n twoSpaces: true\n threeSpaces: true", false, false},
{"\tstartsWithTab: true", false, false},
// Invalid Configs
{"", true, false},
{"token: some:token\nmissing: URL", true, false},
{"missing: token\nobserve_url: https://collect.observeinc.com", true, false},
{"token: bad token\nobserve_url: https://collect.observeinc.com", true, false},
{"token: some:token\nobserve_url: bad url", true, false},
// Valid configs
{testConfig, true, true},
{"key:\n twoSpaces: true\ntoken: some:token\nobserve_url: https://collect.observeinc.com", true, true},
}

func Test_validateYaml(t *testing.T) {
for _, tc := range validCases {
err := validateYaml([]byte(tc))
func Test_checkConfig(t *testing.T) {
for _, tc := range testCases {
f, err := os.CreateTemp("", "test-config-*.yaml")
assert.NoError(t, err)
}
for _, tc := range invalidCases {
err := validateYaml([]byte(tc))
assert.Error(t, err)
defer os.Remove(f.Name())
f.Write([]byte(tc.confStr))

v := viper.New()
v.SetConfigFile(f.Name())
resultAny, err := checkConfig(v)
assert.NoError(t, err)
result, ok := resultAny.(ConfigTestResult)
assert.True(t, ok)
if tc.isValid {
assert.Empty(t, result.Error)
} else {
assert.NotEmpty(t, result.Error)
}
assert.Equal(t, tc.shouldParse, result.ParseSucceeded)
assert.Equal(t, tc.isValid, result.IsValid)
assert.Equal(t, f.Name(), result.ConfigFile)
}
}
8 changes: 5 additions & 3 deletions internal/commands/diagnose/diagnose.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ import (

"github.com/observeinc/observe-agent/internal/root"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

type Diagnostic struct {
check func() (any, error)
check func(*viper.Viper) (any, error)
checkName string
templateName string
templateFS embed.FS
}

var diagnostics = []Diagnostic{
authDiagnostic(),
configDiagnostic(),
authDiagnostic(),
}

// diagnoseCmd represents the diagnose command
Expand All @@ -32,10 +33,11 @@ var diagnoseCmd = &cobra.Command{
Long: `This command runs diagnostic checks for various settings and configurations
to attempt to identify issues that could cause the agent to function improperly.`,
Run: func(cmd *cobra.Command, args []string) {
v := viper.GetViper()
fmt.Print("Running diagnosis checks...\n")
for _, diagnostic := range diagnostics {
fmt.Printf("\n%s\n================\n\n", diagnostic.checkName)
data, err := diagnostic.check()
data, err := diagnostic.check(v)
if err != nil {
fmt.Printf("⚠️ Failed to run check: %s\n", err.Error())
continue
Expand Down
35 changes: 0 additions & 35 deletions internal/commands/initconfig/configschema.go

This file was deleted.

31 changes: 16 additions & 15 deletions internal/commands/initconfig/initconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"testing"

"github.com/observeinc/observe-agent/internal/config"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
)
Expand All @@ -14,27 +15,27 @@ func Test_InitConfigCommand(t *testing.T) {
})
testcases := []struct {
args []string
expectedConfig AgentConfig
expectedConfig config.AgentConfig
expectErr string
}{
{
args: []string{"--config_path=./test-config.yaml", "--token=test-token", "--observe_url=test-url"},
expectedConfig: AgentConfig{
expectedConfig: config.AgentConfig{
Token: "test-token",
ObserveURL: "test-url",
SelfMonitoring: SelfMonitoringConfig{
SelfMonitoring: config.SelfMonitoringConfig{
Enabled: true,
},
HostMonitoring: HostMonitoringConfig{
HostMonitoring: config.HostMonitoringConfig{
Enabled: true,
Logs: HostMonitoringLogsConfig{
Logs: config.HostMonitoringLogsConfig{
Enabled: true,
},
Metrics: HostMonitoringMetricsConfig{
Host: HostMonitoringHostMetricsConfig{
Metrics: config.HostMonitoringMetricsConfig{
Host: config.HostMonitoringHostMetricsConfig{
Enabled: true,
},
Process: HostMonitoringProcessMetricsConfig{
Process: config.HostMonitoringProcessMetricsConfig{
Enabled: false,
},
},
Expand All @@ -44,19 +45,19 @@ func Test_InitConfigCommand(t *testing.T) {
},
{
args: []string{"--config_path=./test-config.yaml", "--token=test-token", "--observe_url=test-url", "--self_monitoring::enabled=false", "--host_monitoring::enabled=false", "--host_monitoring::logs::enabled=false", "--host_monitoring::metrics::host::enabled=false", "--host_monitoring::metrics::process::enabled=false"},
expectedConfig: AgentConfig{
expectedConfig: config.AgentConfig{
Token: "test-token",
ObserveURL: "test-url",
HostMonitoring: HostMonitoringConfig{
HostMonitoring: config.HostMonitoringConfig{
Enabled: false,
Logs: HostMonitoringLogsConfig{
Logs: config.HostMonitoringLogsConfig{
Enabled: false,
},
Metrics: HostMonitoringMetricsConfig{
Host: HostMonitoringHostMetricsConfig{
Metrics: config.HostMonitoringMetricsConfig{
Host: config.HostMonitoringHostMetricsConfig{
Enabled: false,
},
Process: HostMonitoringProcessMetricsConfig{
Process: config.HostMonitoringProcessMetricsConfig{
Enabled: false,
},
},
Expand All @@ -75,7 +76,7 @@ func Test_InitConfigCommand(t *testing.T) {
t.Errorf("Expected no error, got %v", err)
}
}
var config AgentConfig
var config config.AgentConfig
configFile, err := os.ReadFile("./test-config.yaml")
if err != nil {
t.Errorf("Expected no error, got %v", err)
Expand Down
Loading
Loading