feat(client): Allow configuration of client cookie storage (#1716)

* Allow configuration of cookie storage

* Update client/config.go

---------

Co-authored-by: TwiN <twin@linux.com>
This commit is contained in:
AaronandTwiN authored and GitHub committed 2026-07-25 17:12:10 -04:00
1 parent ae7ca199aa
commit b7100620c3
3 files changed
+29

No files matched your search

+2
View File
@@ -654,6 +654,7 @@ the client used to send the request.
| `client.tls.renegotiation` | Type of renegotiation support to provide. (`never`, `freely`, `once`). | `"never"` | | `client.tls.renegotiation` | Type of renegotiation support to provide. (`never`, `freely`, `once`). | `"never"` |
| `client.network` | The network to use for ICMP endpoint client (`ip`, `ip4` or `ip6`). | `"ip"` | | `client.network` | The network to use for ICMP endpoint client (`ip`, `ip4` or `ip6`). | `"ip"` |
| `client.tunnel` | Name of the SSH tunnel to use for this endpoint. See [Tunneling](#tunneling). | `""` | | `client.tunnel` | Name of the SSH tunnel to use for this endpoint. See [Tunneling](#tunneling). | `""` |
| `client.store-cookies` | Whether to store cookies between requests. | `false` |
> 📝 Some of these parameters are ignored based on the type of endpoint. For instance, there's no certificate involved > 📝 Some of these parameters are ignored based on the type of endpoint. For instance, there's no certificate involved
@@ -666,6 +667,7 @@ client:
insecure: false insecure: false
ignore-redirect: false ignore-redirect: false
timeout: 10s timeout: 10s
store-cookies: false
``` ```
Note that this configuration is only available under `endpoints[]`, `alerting.mattermost` and `alerting.custom`. Note that this configuration is only available under `endpoints[]`, `alerting.mattermost` and `alerting.custom`.
+12
View File
@@ -6,6 +6,7 @@ import (
"errors" "errors"
"net" "net"
"net/http" "net/http"
"net/http/cookiejar"
"net/url" "net/url"
"regexp" "regexp"
"strconv" "strconv"
@@ -34,6 +35,7 @@ var (
IgnoreRedirect: false, IgnoreRedirect: false,
Timeout: defaultTimeout, Timeout: defaultTimeout,
Network: "ip", Network: "ip",
StoreCookies: false,
} }
) )
@@ -82,6 +84,9 @@ type Config struct {
// ResolvedTunnel is the resolved SSH tunnel for this specific Config // ResolvedTunnel is the resolved SSH tunnel for this specific Config
ResolvedTunnel *sshtunnel.SSHTunnel `yaml:"-"` ResolvedTunnel *sshtunnel.SSHTunnel `yaml:"-"`
// StoreCookies determines whether cookies are stored and included across requests.
StoreCookies bool `yaml:"store-cookies,omitempty"`
httpClient *http.Client httpClient *http.Client
} }
@@ -280,6 +285,13 @@ func (c *Config) getHTTPClient() *http.Client {
} }
} }
} }
if c.StoreCookies {
jar, err := cookiejar.New(nil)
if err != nil {
logr.Fatalf("[client.getHTTPClient] Failed to initialize cookie jar: %v", err)
}
c.httpClient.Jar = jar
}
} }
return c.httpClient return c.httpClient
} }
+15
View File
@@ -17,6 +17,9 @@ func TestConfig_getHTTPClient(t *testing.T) {
if insecureClient.Timeout != defaultTimeout { if insecureClient.Timeout != defaultTimeout {
t.Error("expected Config.Timeout to default the HTTP client to a timeout of 10s") t.Error("expected Config.Timeout to default the HTTP client to a timeout of 10s")
} }
if insecureClient.Jar != nil {
t.Error("expected Config.StoreCookies to default the HTTP client to not store cookies")
}
request, _ := http.NewRequest("GET", "", nil) request, _ := http.NewRequest("GET", "", nil)
if err := insecureClient.CheckRedirect(request, nil); err != nil { if err := insecureClient.CheckRedirect(request, nil); err != nil {
t.Error("expected Config.IgnoreRedirect set to false to cause the HTTP client's CheckRedirect to return nil") t.Error("expected Config.IgnoreRedirect set to false to cause the HTTP client's CheckRedirect to return nil")
@@ -31,12 +34,24 @@ func TestConfig_getHTTPClient(t *testing.T) {
if secureClient.Timeout != 5*time.Second { if secureClient.Timeout != 5*time.Second {
t.Error("expected Config.Timeout to cause the HTTP client to have a timeout of 5s") t.Error("expected Config.Timeout to cause the HTTP client to have a timeout of 5s")
} }
if insecureClient.Jar != nil {
t.Error("expected Config.StoreCookies to default the HTTP client to not store cookies")
}
request, _ = http.NewRequest("GET", "", nil) request, _ = http.NewRequest("GET", "", nil)
if err := secureClient.CheckRedirect(request, nil); err != http.ErrUseLastResponse { if err := secureClient.CheckRedirect(request, nil); err != http.ErrUseLastResponse {
t.Error("expected Config.IgnoreRedirect set to true to cause the HTTP client's CheckRedirect to return http.ErrUseLastResponse") t.Error("expected Config.IgnoreRedirect set to true to cause the HTTP client's CheckRedirect to return http.ErrUseLastResponse")
} }
} }
func TestConfig_getHTTPClient_withCookieJar(t *testing.T) {
config := &Config{StoreCookies: true}
config.ValidateAndSetDefaults()
client := config.getHTTPClient()
if client.Jar == nil {
t.Error("expected Config.StoreCookies to cause the HTTP client to have a CookieJar")
}
}
func TestConfig_ValidateAndSetDefaults_withCustomDNSResolver(t *testing.T) { func TestConfig_ValidateAndSetDefaults_withCustomDNSResolver(t *testing.T) {
type args struct { type args struct {
dnsResolver string dnsResolver string