Skip to content

Commit

Permalink
Revert "setting security agent config file instead of merging because…
Browse files Browse the repository at this point in the history
… Viper only supports 1 config file per viper instance"

This reverts commit 8e6736d.
  • Loading branch information
modernplumbing committed Dec 29, 2022
1 parent 0e9731a commit a2bb920
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
31 changes: 31 additions & 0 deletions cmd/security-agent/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.

package config

import (
"fmt"
"os"

aconfig "github.com/DataDog/datadog-agent/pkg/config"
"github.com/DataDog/datadog-agent/pkg/util/log"
)

// Merge will merge the security-agent configuration into the existing datadog configuration
func Merge(configPaths []string) error {
for _, configPath := range configPaths {
if f, err := os.Open(configPath); err == nil {
err = aconfig.Datadog.MergeConfig(f)
_ = f.Close()
if err != nil {
return fmt.Errorf("error merging %s config file: %w", configPath, err)
}
} else {
log.Infof("no config exists at %s, ignoring...", configPath)
}
}

return nil
}
34 changes: 18 additions & 16 deletions comp/core/config/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"runtime"
"strings"

secconfig "github.com/DataDog/datadog-agent/cmd/security-agent/config"
sysconfig "github.com/DataDog/datadog-agent/cmd/system-probe/config"
"github.com/DataDog/datadog-agent/pkg/config"
"github.com/DataDog/viper"
Expand All @@ -30,18 +31,8 @@ func setupConfig(deps dependencies) (*config.Warnings, error) {
config.Datadog.SetConfigName(configName)
}

if deps.Params.configLoadSecurityAgent {
// TODO: One instance of Viper only supports 1 configuration file, so we should refactor securityAgentConfigFilePaths into just a string instead of a slice of strings
if len(deps.Params.securityAgentConfigFilePaths) < 1 {
return nil, fmt.Errorf("Security Agent is missing configuration file")
}
config.Datadog.SetConfigFile(deps.Params.securityAgentConfigFilePaths[0])
} else if deps.Params.configLoadSysProbe {
_, err := sysconfig.Merge(deps.Params.sysProbeConfFilePath)
if err != nil {
return nil, err
}
} else if confFilePath != "" {
// set the paths where a config file is expected
if len(confFilePath) != 0 {
// if the configuration file path was supplied on the command line,
// add that first so it's first in line
config.Datadog.AddConfigPath(confFilePath)
Expand All @@ -50,7 +41,6 @@ func setupConfig(deps dependencies) (*config.Warnings, error) {
config.Datadog.SetConfigFile(confFilePath)
}
}

if defaultConfPath != "" {
config.Datadog.AddConfigPath(defaultConfPath)
}
Expand All @@ -59,6 +49,19 @@ func setupConfig(deps dependencies) (*config.Warnings, error) {
var err error
var warnings *config.Warnings

if deps.Params.configLoadSysProbe {
_, err := sysconfig.Merge(deps.Params.sysProbeConfFilePath)
if err != nil {
return nil, err
}
}

if deps.Params.configLoadSecurityAgent {
if err := secconfig.Merge(deps.Params.securityAgentConfigFilePaths); err != nil {
return nil, err
}
}

if withoutSecrets {
warnings, err = config.LoadWithoutSecret()
} else {
Expand All @@ -82,8 +85,8 @@ func setupConfig(deps dependencies) (*config.Warnings, error) {
return warnings, nil
}

// MergeConfigurationFiles reads an array of configuration filenames and attempts to merge them. The userDefined value is used to specify that configurationFilesArray contains filenames defined on the command line.
// TODO: This is ONLY for SecAgent use! Deleting this once all SecAgent commands have been converted to fx
// MergeConfigurationFiles reads an array of configuration filenames and attempts to merge them. The userDefined value is used to specify that configurationFilesArray contains filenames defined on the command line
// TODO(paulcacheux): change this a component method once all security-agent commands have been converted to fx
func MergeConfigurationFiles(configName string, configurationFilesArray []string, userDefined bool) (*config.Warnings, error) {
// we'll search for a config file named `datadog.yaml`
config.Datadog.SetConfigName(configName)
Expand All @@ -103,7 +106,6 @@ func MergeConfigurationFiles(configName string, configurationFilesArray []string
continue
}
if !loadedConfiguration {
deps.Params.configLoadSecurityAgent = true
deps.Params.confFilePath = configurationFilename
deps.Params.configName = ""
deps.Params.configLoadSecrets = true
Expand Down

0 comments on commit a2bb920

Please sign in to comment.