From c08d85066898369f87d61327730a21cc803a8a70 Mon Sep 17 00:00:00 2001 From: matt-gp Date: Sat, 25 Apr 2026 14:07:56 +0100 Subject: [PATCH] AWS SD: Set Directory --- discovery/aws/aws.go | 30 +++++++++++++ discovery/aws/aws_test.go | 86 ++++++++++++++++++++++++++++++++++++ discovery/aws/ecs.go | 5 +++ discovery/aws/elasticache.go | 5 +++ discovery/aws/lightsail.go | 5 +++ discovery/aws/msk.go | 5 +++ discovery/aws/rds.go | 5 +++ 7 files changed, 141 insertions(+) diff --git a/discovery/aws/aws.go b/discovery/aws/aws.go index 81afe5ddda..3d0c246199 100644 --- a/discovery/aws/aws.go +++ b/discovery/aws/aws.go @@ -342,6 +342,36 @@ func (c *SDConfig) NewDiscoverer(opts discovery.DiscovererOptions) (discovery.Di } } +// SetDirectory joins any relative file paths with dir. +func (c *SDConfig) SetDirectory(dir string) { + switch c.Role { + case RoleEC2: + if c.EC2SDConfig != nil { + c.EC2SDConfig.SetDirectory(dir) + } + case RoleECS: + if c.ECSSDConfig != nil { + c.ECSSDConfig.SetDirectory(dir) + } + case RoleElasticache: + if c.ElasticacheSDConfig != nil { + c.ElasticacheSDConfig.SetDirectory(dir) + } + case RoleLightsail: + if c.LightsailSDConfig != nil { + c.LightsailSDConfig.SetDirectory(dir) + } + case RoleMSK: + if c.MSKSDConfig != nil { + c.MSKSDConfig.SetDirectory(dir) + } + case RoleRDS: + if c.RDSSDConfig != nil { + c.RDSSDConfig.SetDirectory(dir) + } + } +} + // loadRegion finds the region in order: AWS config/env vars ->IMDS. func loadRegion(ctx context.Context, specifiedRegion string) (string, error) { if specifiedRegion != "" { diff --git a/discovery/aws/aws_test.go b/discovery/aws/aws_test.go index d1ec7b2282..3c2b61273d 100644 --- a/discovery/aws/aws_test.go +++ b/discovery/aws/aws_test.go @@ -487,3 +487,89 @@ region = ` + randomRegion + ` require.Contains(t, err.Error(), "failed to get region from IMDS") }) } + +func TestSDConfigSetDirectory(t *testing.T) { + tmpDir := t.TempDir() + tests := []struct { + name string + yaml string + role Role + }{ + { + name: "EC2", + yaml: ` +role: ec2 +region: us-east-1 +`, + role: RoleEC2, + }, + { + name: "ECS", + yaml: ` +role: ecs +region: us-west-2 +clusters: [test-cluster] +`, + role: RoleECS, + }, + { + name: "Elasticache", + yaml: ` +role: elasticache +region: eu-west-1 +`, + role: RoleElasticache, + }, + { + name: "Lightsail", + yaml: ` +role: lightsail +region: ap-south-1 +`, + role: RoleLightsail, + }, + { + name: "MSK", + yaml: ` +role: msk +region: us-east-2 +`, + role: RoleMSK, + }, + { + name: "RDS", + yaml: ` +role: rds +region: us-west-1 +`, + role: RoleRDS, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var cfg SDConfig + err := yaml.Unmarshal([]byte(tt.yaml), &cfg) + require.NoError(t, err) + require.Equal(t, tt.role, cfg.Role) + + // Call SetDirectory - should not panic + require.NotPanics(t, func() { + cfg.SetDirectory(tmpDir) + }) + }) + } + + t.Run("SetDirectoryWithNilConfigs", func(t *testing.T) { + // Test that SetDirectory doesn't panic when called on an SDConfig + // where the role-specific config might be nil (this was the original bug) + cfg := SDConfig{ + Role: RoleEC2, + // EC2SDConfig is nil - this would have caused a panic before the fix + } + // This should not panic + require.NotPanics(t, func() { + cfg.SetDirectory(tmpDir) + }) + }) +} diff --git a/discovery/aws/ecs.go b/discovery/aws/ecs.go index 18d2746cb6..0c2fd443fa 100644 --- a/discovery/aws/ecs.go +++ b/discovery/aws/ecs.go @@ -129,6 +129,11 @@ func (c *ECSSDConfig) NewDiscoverer(opts discovery.DiscovererOptions) (discovery return NewECSDiscovery(c, opts) } +// SetDirectory joins any relative file paths with dir. +func (c *ECSSDConfig) SetDirectory(dir string) { + c.HTTPClientConfig.SetDirectory(dir) +} + // UnmarshalYAML implements the yaml.Unmarshaler interface for the ECS Config. func (c *ECSSDConfig) UnmarshalYAML(unmarshal func(any) error) error { *c = DefaultECSSDConfig diff --git a/discovery/aws/elasticache.go b/discovery/aws/elasticache.go index 8f722e377e..206374fce3 100644 --- a/discovery/aws/elasticache.go +++ b/discovery/aws/elasticache.go @@ -217,6 +217,11 @@ func (c *ElasticacheSDConfig) NewDiscoverer(opts discovery.DiscovererOptions) (d return NewElasticacheDiscovery(c, opts) } +// SetDirectory joins any relative file paths with dir. +func (c *ElasticacheSDConfig) SetDirectory(dir string) { + c.HTTPClientConfig.SetDirectory(dir) +} + // UnmarshalYAML implements the yaml.Unmarshaler interface for the Elasticache Config. func (c *ElasticacheSDConfig) UnmarshalYAML(unmarshal func(any) error) error { *c = DefaultElasticacheSDConfig diff --git a/discovery/aws/lightsail.go b/discovery/aws/lightsail.go index 9f200b69e2..77087b0821 100644 --- a/discovery/aws/lightsail.go +++ b/discovery/aws/lightsail.go @@ -97,6 +97,11 @@ func (c *LightsailSDConfig) NewDiscoverer(opts discovery.DiscovererOptions) (dis return NewLightsailDiscovery(c, opts) } +// SetDirectory joins any relative file paths with dir. +func (c *LightsailSDConfig) SetDirectory(dir string) { + c.HTTPClientConfig.SetDirectory(dir) +} + // UnmarshalYAML implements the yaml.Unmarshaler interface for the Lightsail Config. func (c *LightsailSDConfig) UnmarshalYAML(unmarshal func(any) error) error { *c = DefaultLightsailSDConfig diff --git a/discovery/aws/msk.go b/discovery/aws/msk.go index 3ecc1e6235..782574c0a9 100644 --- a/discovery/aws/msk.go +++ b/discovery/aws/msk.go @@ -129,6 +129,11 @@ func (c *MSKSDConfig) NewDiscoverer(opts discovery.DiscovererOptions) (discovery return NewMSKDiscovery(c, opts) } +// SetDirectory joins any relative file paths with dir. +func (c *MSKSDConfig) SetDirectory(dir string) { + c.HTTPClientConfig.SetDirectory(dir) +} + // UnmarshalYAML implements the yaml.Unmarshaler interface for the MSK Config. func (c *MSKSDConfig) UnmarshalYAML(unmarshal func(any) error) error { *c = DefaultMSKSDConfig diff --git a/discovery/aws/rds.go b/discovery/aws/rds.go index ba700f8763..603817831b 100644 --- a/discovery/aws/rds.go +++ b/discovery/aws/rds.go @@ -248,6 +248,11 @@ func (c *RDSSDConfig) NewDiscoverer(opts discovery.DiscovererOptions) (discovery return NewRDSDiscovery(c, opts) } +// SetDirectory joins any relative file paths with dir. +func (c *RDSSDConfig) SetDirectory(dir string) { + c.HTTPClientConfig.SetDirectory(dir) +} + // UnmarshalYAML implements the yaml.Unmarshaler interface for the RDS Config. func (c *RDSSDConfig) UnmarshalYAML(unmarshal func(any) error) error { *c = DefaultRDSSDConfig