diff --git a/go.mod b/go.mod index a24f5bd..69d834f 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ // along with this program. If not, see . module codeberg.org/scip/esctl -go 1.26.4 +go 1.26 require ( github.com/MichaelMure/go-term-markdown v0.1.4 diff --git a/pkg/cfg/cluster.go b/pkg/cfg/cluster.go index 0a3d3fd..65b86f9 100644 --- a/pkg/cfg/cluster.go +++ b/pkg/cfg/cluster.go @@ -189,6 +189,7 @@ func (cluster *Cluster) getDefaultOptions() []elasticsearch.Option { cluster.getTransport(), elastictransport.WithHeader(headers), ), + elasticsearch.WithCompatibilityMode(), } } @@ -203,7 +204,9 @@ func (cluster *Cluster) getTransport() elastictransport.Option { ) } - return elastictransport.WithTransport(transport) + return elastictransport.WithTransport( + &CompatibilityTransport{Transport: transport}, + ) } func (conf *Config) SetupES() error { diff --git a/pkg/cfg/transport.go b/pkg/cfg/transport.go index 070ef29..42e19d8 100644 --- a/pkg/cfg/transport.go +++ b/pkg/cfg/transport.go @@ -53,6 +53,11 @@ func (t *DebugTransport) RoundTrip(req *http.Request) (*http.Response, error) { contentline = buf.String() } + if req.Header.Get("Accept") != "" { + req.Header.Del("Accept") + req.Header.Add("Accept", "application/json") + } + slog.Info("req", "host", req.URL.Host, "uri", req.URL.Path, "body", content, "bodyline", contentline, "headers", req.Header, @@ -60,3 +65,21 @@ func (t *DebugTransport) RoundTrip(req *http.Request) (*http.Response, error) { return t.Transport.RoundTrip(req) } + +// Fixes https://codeberg.org/scip/esctl/issues/79: +// the API client has this Accept header hardcoded everywhere: +// req.Header.Set("Accept", "application/vnd.elasticsearch+json;compatible-with=9") +// While this works pretty well with ES9, it doesn't with ES8. So, we replace +// this header with a new one without the compatibility part. +type CompatibilityTransport struct { + Transport http.RoundTripper +} + +func (t *CompatibilityTransport) RoundTrip(req *http.Request) (*http.Response, error) { + if req.Header.Get("Accept") != "" { + req.Header.Del("Accept") + req.Header.Add("Accept", "application/json") + } + + return t.Transport.RoundTrip(req) +}