Skip to content

Commit ae7fa14

Browse files
committed
third party config support
1 parent 5d148d9 commit ae7fa14

File tree

9 files changed

+376
-47
lines changed

9 files changed

+376
-47
lines changed

internal/config/config.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ func ResolveConfig() (*Config, error) {
123123
Features: viperInstance.GetStringSlice(FeaturesKey),
124124
Labels: resolveLabels(),
125125
LibDir: viperInstance.GetString(LibDirPathKey),
126+
ExternalDataSource: resolveExternalDataSource(),
126127
}
127128

128129
defaultCollector(collector, config)
@@ -434,6 +435,7 @@ func registerFlags() {
434435
registerCollectorFlags(fs)
435436
registerClientFlags(fs)
436437
registerDataPlaneFlags(fs)
438+
registerExternalDataSourceFlags(fs)
437439

438440
fs.SetNormalizeFunc(normalizeFunc)
439441

@@ -448,6 +450,24 @@ func registerFlags() {
448450
})
449451
}
450452

453+
func registerExternalDataSourceFlags(fs *flag.FlagSet) {
454+
fs.String(
455+
ExternalDataSourceProxyUrlKey,
456+
DefExternalDataSourceProxyUrl,
457+
"Url to the proxy service to fetch the external file.",
458+
)
459+
fs.StringSlice(
460+
ExternalDataSourceAllowDomainsKey,
461+
[]string{},
462+
"List of allowed domains for external data sources.",
463+
)
464+
fs.Int64(
465+
ExternalDataSourceMaxBytesKey,
466+
DefExternalDataSourceMaxBytes,
467+
"Maximum size in bytes for external data sources.",
468+
)
469+
}
470+
451471
func registerDataPlaneFlags(fs *flag.FlagSet) {
452472
fs.Duration(
453473
NginxReloadMonitoringPeriodKey,
@@ -1506,3 +1526,26 @@ func areCommandServerProxyTLSSettingsSet() bool {
15061526
viperInstance.IsSet(CommandServerProxyTLSSkipVerifyKey) ||
15071527
viperInstance.IsSet(CommandServerProxyTLSServerNameKey)
15081528
}
1529+
1530+
func resolveExternalDataSource() *ExternalDataSource {
1531+
proxyURLStruct := ProxyURL{
1532+
URL: viperInstance.GetString(ExternalDataSourceProxyUrlKey),
1533+
}
1534+
externalDataSource := &ExternalDataSource{
1535+
ProxyURL: proxyURLStruct,
1536+
AllowedDomains: viperInstance.GetStringSlice(ExternalDataSourceAllowDomainsKey),
1537+
MaxBytes: viperInstance.GetInt64(ExternalDataSourceMaxBytesKey),
1538+
}
1539+
1540+
// Validate domains
1541+
if len(externalDataSource.AllowedDomains) > 0 {
1542+
for _, domain := range externalDataSource.AllowedDomains {
1543+
if strings.ContainsAny(domain, "/\\ ") || domain == "" {
1544+
slog.Error("domain is not specified in allowed_domains")
1545+
return nil
1546+
}
1547+
}
1548+
}
1549+
1550+
return externalDataSource
1551+
}

internal/config/config_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,13 @@ func createConfig() *Config {
13251325
config.FeatureCertificates, config.FeatureFileWatcher, config.FeatureMetrics,
13261326
config.FeatureAPIAction, config.FeatureLogsNap,
13271327
},
1328+
ExternalDataSource: &ExternalDataSource{
1329+
ProxyURL: ProxyURL{
1330+
URL: "",
1331+
},
1332+
AllowedDomains: nil,
1333+
MaxBytes: 0,
1334+
},
13281335
}
13291336
}
13301337

internal/config/defaults.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ const (
111111

112112
// File defaults
113113
DefLibDir = "/var/lib/nginx-agent"
114+
115+
DefExternalDataSourceProxyUrl = ""
116+
DefExternalDataSourceMaxBytes = 100 * 1024 * 1024
114117
)
115118

116119
func DefaultFeatures() []string {

internal/config/flags.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const (
2525
InstanceHealthWatcherMonitoringFrequencyKey = "watchers_instance_health_watcher_monitoring_frequency"
2626
FileWatcherKey = "watchers_file_watcher"
2727
LibDirPathKey = "lib_dir"
28+
ExternalDataSourceRootKey = "external_data_source"
2829
)
2930

3031
var (
@@ -138,6 +139,11 @@ var (
138139

139140
FileWatcherMonitoringFrequencyKey = pre(FileWatcherKey) + "monitoring_frequency"
140141
NginxExcludeFilesKey = pre(FileWatcherKey) + "exclude_files"
142+
143+
ExternalDataSourceProxyKey = pre(ExternalDataSourceRootKey) + "proxy"
144+
ExternalDataSourceProxyUrlKey = pre(ExternalDataSourceProxyKey) + "url"
145+
ExternalDataSourceMaxBytesKey = pre(ExternalDataSourceRootKey) + "max_bytes"
146+
ExternalDataSourceAllowDomainsKey = pre(ExternalDataSourceRootKey) + "allowed_domains"
141147
)
142148

143149
func pre(prefixes ...string) string {

internal/config/types.go

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,21 @@ func parseServerType(str string) (ServerType, bool) {
3636

3737
type (
3838
Config struct {
39-
Command *Command `yaml:"command" mapstructure:"command"`
40-
AuxiliaryCommand *Command `yaml:"auxiliary_command" mapstructure:"auxiliary_command"`
41-
Log *Log `yaml:"log" mapstructure:"log"`
42-
DataPlaneConfig *DataPlaneConfig `yaml:"data_plane_config" mapstructure:"data_plane_config"`
43-
Client *Client `yaml:"client" mapstructure:"client"`
44-
Collector *Collector `yaml:"collector" mapstructure:"collector"`
45-
Watchers *Watchers `yaml:"watchers" mapstructure:"watchers"`
46-
Labels map[string]any `yaml:"labels" mapstructure:"labels"`
47-
Version string `yaml:"-"`
48-
Path string `yaml:"-"`
49-
UUID string `yaml:"-"`
50-
LibDir string `yaml:"-"`
51-
AllowedDirectories []string `yaml:"allowed_directories" mapstructure:"allowed_directories"`
52-
Features []string `yaml:"features" mapstructure:"features"`
39+
Watchers *Watchers `yaml:"watchers" mapstructure:"watchers"`
40+
Labels map[string]any `yaml:"labels" mapstructure:"labels"`
41+
Log *Log `yaml:"log" mapstructure:"log"`
42+
DataPlaneConfig *DataPlaneConfig `yaml:"data_plane_config" mapstructure:"data_plane_config"`
43+
Client *Client `yaml:"client" mapstructure:"client"`
44+
Collector *Collector `yaml:"collector" mapstructure:"collector"`
45+
AuxiliaryCommand *Command `yaml:"auxiliary_command" mapstructure:"auxiliary_command"`
46+
ExternalDataSource *ExternalDataSource `yaml:"external_data_source" mapstructure:"external_data_source"`
47+
Command *Command `yaml:"command" mapstructure:"command"`
48+
Path string `yaml:"-"`
49+
Version string `yaml:"-"`
50+
LibDir string `yaml:"-"`
51+
UUID string `yaml:"-"`
52+
AllowedDirectories []string `yaml:"allowed_directories" mapstructure:"allowed_directories"`
53+
Features []string `yaml:"features" mapstructure:"features"`
5354
}
5455

5556
Log struct {
@@ -352,6 +353,16 @@ type (
352353
Token string `yaml:"token,omitempty" mapstructure:"token"`
353354
Timeout time.Duration `yaml:"timeout" mapstructure:"timeout"`
354355
}
356+
357+
ProxyURL struct {
358+
URL string `yaml:"url" mapstructure:"url"`
359+
}
360+
361+
ExternalDataSource struct {
362+
ProxyURL ProxyURL `yaml:"proxy" mapstructure:"proxy"`
363+
AllowedDomains []string `yaml:"allowed_domains" mapstructure:"allowed_domains"`
364+
MaxBytes int64 `yaml:"max_bytes" mapstructure:"max_bytes"`
365+
}
355366
)
356367

357368
func (col *Collector) Validate(allowedDirectories []string) error {

0 commit comments

Comments
 (0)