Skip to content
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
21 changes: 16 additions & 5 deletions hugo/content/configuration-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ The following sections explain how to configure the NGINX Agent using configurat

### Configure with Config Files

The configuration files for the NGINX Agent are `/etc/nginx-agent/nginx-agent.conf` and `/var/lib/nginx-agent/agent-dynamic.conf`. The `agent-dynamic.conf` file location is different for FreeBSD which is located `/var/db/nginx-agent/agent-dynamic.conf`. These files have comments at the top indicating their purpose.
The default locations of configuration files for the NGINX Agent are `/etc/nginx-agent/nginx-agent.conf` and `/var/lib/nginx-agent/agent-dynamic.conf`. The `agent-dynamic.conf` file default location is different for FreeBSD which is located `/var/db/nginx-agent/agent-dynamic.conf`. These files have comments at the top indicating their purpose.

Examples of the configuration files are provided below:

Expand Down Expand Up @@ -111,11 +111,13 @@ nginx_app_protect:
<details open>
<summary>example dynamic-agent.conf</summary>

{{<note>}}
Default location in Linux environments: `/var/lib/nginx-agent/agent-dynamic.conf`

Default location in FreeBSD environments: `/var/db/nginx-agent/agent-dynamic.conf`
{{</note>}}

```yaml
#
# /var/lib/nginx-agent/agent-dynamic.conf
# On FreeBSD /var/db/nginx-agent/agent-dynamic.conf
#
# Dynamic configuration file for NGINX Agent.
#
# The purpose of this file is to track agent configuration
Expand Down Expand Up @@ -163,6 +165,7 @@ Flags:
--dataplane-report-interval duration The amount of time the agent will report on the dataplane. After this period of time it will send a snapshot of the dataplane information. (default 24h0m0s)
--dataplane-status-poll-interval duration The frequency the agent will check the dataplane for changes. Used as a "heartbeat" to keep the gRPC connections alive. (default 30s)
--display-name string The instance's 'name' value.
--dynamic-config-path string Defines the path of the Agent dynamic config file. (default "/var/lib/nginx-agent/agent-dynamic.conf")
--features strings A comma-separated list of features enabled for the agent. (default [registration,nginx-config-async,nginx-ssl-config,nginx-counting,metrics,dataplane-status,process-watcher,file-watcher,activity-events,agent-api])
-h, --help help for nginx-agent
--ignore-directives strings A comma-separated list of ignoring directives which contain sensitive info.
Expand Down Expand Up @@ -203,6 +206,14 @@ The NGINX Agent follows NGINX configuration directives to file paths outside the

- [`ssl_certificate`](https://nginx.org/en/docs/http/ngx_http_ssl_module.html#ssl_certificate)

#### NGINX Agent Dynamic Config Path Option

Use the `--dynamic-config-path` command-line option to set the location of the dynamic config file. This setting also requires you to move your dynamic config to the new path, or create a new dynamic config file at the specified location.

Default location in Linux environments: `/var/lib/nginx-agent/agent-dynamic.conf`

Default location in FreeBSD environments: `/var/db/nginx-agent/agent-dynamic.conf`

### NGINX Agent Environment Variables

This section displays the configurable options for the NGINX Agent that can be set with environment variables. A list of the configurable environment variables can be seen below:
Expand Down
8 changes: 7 additions & 1 deletion hugo/content/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,18 @@ sudo mkdir /etc/nginx-agent
sudo cp <project_root_directory>/nginx-agent.conf /etc/nginx-agent/
```

Create the `agent-dynamic.conf` file in the `/var/lib/nginx-agent/` directory, which is required for NGINX Agent to run.
Create the `agent-dynamic.conf` file, which is required for NGINX Agent to run.

In Linux environments:
```shell
sudo touch /var/lib/nginx-agent/agent-dynamic.conf
```

In FreeBSD environments:
```shell
sudo touch /var/db/nginx-agent/agent-dynamic.conf
```

### Enable the gRPC interface

Add the the following settings to `/etc/nginx-agent/nginx-agent.conf`:
Expand Down
9 changes: 4 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"strconv"

"github.com/nginx/agent/sdk/v2/agent/events"

sdkGRPC "github.com/nginx/agent/sdk/v2/grpc"
"github.com/nginx/agent/v2/src/core"
"github.com/nginx/agent/v2/src/core/config"
Expand All @@ -31,12 +30,12 @@ var (
env = &core.EnvironmentType{}
)

func init() {
config.InitConfiguration(version, commit)
}

func main() {
config.InitFlags(version, commit)

config.RegisterRunner(func(cmd *cobra.Command, _ []string) {
config.InitConfigurationFiles()

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand Down
44 changes: 19 additions & 25 deletions src/core/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"os"
"path/filepath"
"reflect"
"runtime"
"sort"
"strings"
"time"
Expand All @@ -32,10 +31,7 @@ import (
)

const (
dynamicConfigUsageComment = `#
# /etc/nginx-agent/dynamic-agent.conf
#
# Dynamic configuration file for NGINX Agent.
dynamicConfigUsageComment = `# Dynamic configuration file for NGINX Agent.
#
# The purpose of this file is to track agent configuration
# values that can be dynamically changed via the API and the agent install script.
Expand Down Expand Up @@ -63,15 +59,19 @@ func Execute() error {
return ROOT_COMMAND.Execute()
}

func InitConfiguration(version, commit string) {
func InitFlags(version, commit string) {
SetVersion(version, commit)
SetDefaults()
RegisterFlags()
dynamicConfigPath := DynamicConfigFileAbsPath
if runtime.GOOS == "freebsd" {
dynamicConfigPath = DynamicConfigFileAbsFreeBsdPath
}

func InitConfigurationFiles() {
dynamicConfFilePath := Viper.GetString(DynamicConfigPathKey)
if dynamicConfFilePath == "" {
dynamicConfFilePath = getDefaultDynamicConfPath()
}
configPath, err := RegisterConfigFile(dynamicConfigPath, ConfigFileName, ConfigFilePaths()...)

configPath, err := RegisterConfigFile(dynamicConfFilePath, ConfigFileName, ConfigFilePaths()...)
if err != nil {
log.Fatalf("Failed to load configuration file: %v", err)
}
Expand Down Expand Up @@ -104,13 +104,15 @@ func SetDefaults() {
Viper.SetDefault(NginxClientVersion, Defaults.Nginx.NginxClientVersion)
Viper.SetDefault(NginxConfigReloadMonitoringPeriod, Defaults.Nginx.ConfigReloadMonitoringPeriod)

// NGINX AGENT DEFAULTS
Viper.SetDefault(DynamicConfigPathKey, Defaults.DynamicConfigPath)
Viper.SetDefault(QueueSizeKey, Defaults.QueueSize)
}

func setFlagDeprecated(name string, usageMessage string) {
err := ROOT_COMMAND.Flags().MarkDeprecated(name, usageMessage)
if err != nil {
log.Warnf("error occurred deprecating flag %s: %v", name, err)
log.Warnf("Error occurred deprecating flag %s: %v", name, err)
}
}

Expand Down Expand Up @@ -149,7 +151,7 @@ func RegisterFlags() {
}
err := Viper.BindEnv(flag.Name)
if err != nil {
log.Warnf("error occurred binding env %s: %v", flag.Name, err)
log.Warnf("Error occurred binding env %s: %v", flag.Name, err)
}
})
}
Expand All @@ -160,7 +162,7 @@ func RegisterConfigFile(dynamicConfFilePath string, confFileName string, confPat
return cfg, err
}

SetDynamicConfigFileAbsPath(dynamicConfFilePath)
setDynamicConfigFileAbsPath(dynamicConfFilePath)
if err := LoadPropertiesFromFile(cfg); err != nil {
log.Fatalf("Unable to load properties from config files (%s, %s) - %v", cfg, dynamicConfFilePath, err)
}
Expand Down Expand Up @@ -270,11 +272,7 @@ func UpdateAgentConfig(systemId string, updateTags []string, updateFeatures []st
// already set.
dynamicCfgPath := Viper.GetString(DynamicConfigPathKey)
if dynamicCfgPath == "" {
if runtime.GOOS == "freebsd" {
dynamicCfgPath = DynamicConfigFileAbsFreeBsdPath
} else {
dynamicCfgPath = DynamicConfigFileAbsPath
}
dynamicCfgPath = getDefaultDynamicConfPath()
}

// Overwrite existing nginx-agent.conf with updated config
Expand Down Expand Up @@ -385,11 +383,7 @@ func LoadPropertiesFromFile(cfg string) error {
// already set.
dynamicCfgPath := Viper.GetString(DynamicConfigPathKey)
if dynamicCfgPath == "" {
if runtime.GOOS == "freebsd" {
dynamicCfgPath = DynamicConfigFileAbsFreeBsdPath
} else {
dynamicCfgPath = DynamicConfigFileAbsPath
}
dynamicCfgPath = getDefaultDynamicConfPath()
}

dynamicCfgDir, dynamicCfgFile := filepath.Split(dynamicCfgPath)
Expand Down Expand Up @@ -484,7 +478,7 @@ func removeFeatures(readFile io.Reader) (bool, []byte, error) {
return featuresSet, buf.Bytes(), nil
}

func SetDynamicConfigFileAbsPath(dynamicCfgPath string) {
func setDynamicConfigFileAbsPath(dynamicCfgPath string) {
Viper.Set(DynamicConfigPathKey, dynamicCfgPath)
log.Debugf("Set dynamic agent config file: %s", dynamicCfgPath)
}
Expand All @@ -493,7 +487,7 @@ func wordSepNormalizeFunc(f *flag.FlagSet, name string) flag.NormalizedName {
from := []string{"_", "."}
to := "-"
for _, sep := range from {
name = strings.Replace(name, sep, to, -1)
name = strings.ReplaceAll(name, sep, to)
}
return flag.NormalizedName(name)
}
Expand Down
Loading