Skip to content

Commit e329cc6

Browse files
jharvey10dehaansa
andauthored
fix: Ensure the squid exporter wrapper properly brackets ipv6 addresses [backport] (#5205)
Ensure that the squid exporter component properly brackets IPv6 addresses when passing the hostname to the exporter module. Co-authored-by: Sam DeHaan <sam.dehaan@grafana.com>
1 parent 17124b7 commit e329cc6

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

internal/static/integrations/squid_exporter/squid_exporter.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"net"
7+
"net/netip"
78
"strconv"
89

910
se "github.com/boynux/squid-exporter/collector"
@@ -44,7 +45,13 @@ func (c *Config) validate() error {
4445
if host == "" {
4546
return ErrNoHostname
4647
}
47-
c.Host = host
48+
49+
if ip, err := netip.ParseAddr(host); err == nil && ip.IsValid() && ip.Is6() {
50+
// Restore the explicit brackets for IPv6 addresses
51+
c.Host = fmt.Sprintf("[%s]", host)
52+
} else {
53+
c.Host = host
54+
}
4855

4956
if port == "" {
5057
return ErrNoPort
@@ -95,7 +102,7 @@ func New(c *Config) (integrations.Integration, error) {
95102
}
96103

97104
seExporter := se.New(&se.CollectorConfig{
98-
Hostname: c.Host,
105+
Hostname: fmt.Sprintf("[%s]", c.Host),
99106
Port: c.Port,
100107
Login: c.Username,
101108
Password: string(c.Password),

internal/static/integrations/squid_exporter/squid_exporter_test.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ import (
1212

1313
func TestConfigValidate(t *testing.T) {
1414
cases := []struct {
15-
name string
16-
getConfig func() Config
17-
expectedErr error
15+
name string
16+
getConfig func() Config
17+
expectedErr error
18+
expectedHost string
1819
}{
1920
{
2021
name: "valid",
@@ -64,17 +65,30 @@ func TestConfigValidate(t *testing.T) {
6465
},
6566
expectedErr: errors.New("address a@#$%:asdf::12312: too many colons in address"),
6667
},
68+
{
69+
name: "valid ipv6",
70+
getConfig: func() Config {
71+
cfg := Config{}
72+
cfg.Address = "[::1]:51001"
73+
return cfg
74+
},
75+
expectedHost: "[::1]",
76+
},
6777
}
6878

6979
for _, tc := range cases {
7080
t.Run(tc.name, func(t *testing.T) {
7181
cfg := tc.getConfig()
7282
err := cfg.validate()
73-
if tc.expectedErr == nil {
74-
require.NoError(t, err)
83+
if tc.expectedErr != nil {
84+
require.ErrorContains(t, err, tc.expectedErr.Error())
7585
return
7686
}
77-
require.ErrorContains(t, err, tc.expectedErr.Error())
87+
88+
require.NoError(t, err)
89+
if tc.expectedHost != "" {
90+
require.Equal(t, tc.expectedHost, cfg.Host)
91+
}
7892
})
7993
}
8094
}

0 commit comments

Comments
 (0)