Skip to content

Commit 5273d99

Browse files
update IsServerless() to use correct Kibana APIs (#213)
* update IsServerless * cleanup * remove old docs * fix older test * fixing unrelated tests
1 parent cb5caeb commit 5273d99

File tree

3 files changed

+59
-10
lines changed

3 files changed

+59
-10
lines changed

kibana/client.go

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"errors"
2626
"fmt"
2727
"io"
28-
"io/ioutil"
2928
"mime"
3029
"mime/multipart"
3130
"net/http"
@@ -226,7 +225,7 @@ func (conn *Connection) Request(method, extraPath string,
226225
}
227226
defer resp.Body.Close()
228227

229-
result, err := ioutil.ReadAll(resp.Body)
228+
result, err := io.ReadAll(resp.Body)
230229
if err != nil {
231230
return 0, nil, fmt.Errorf("fail to read response: %w", err)
232231
}
@@ -346,16 +345,43 @@ func (client *Client) readVersion() error {
346345
func (client *Client) GetVersion() version.V { return client.Version }
347346

348347
// KibanaIsServerless returns true if we're talking to a serverless instance.
349-
// Right now we don't have an API to tell us if we're running against serverless or not, so this actual implementation is something of a hack.
350-
// see https://github.com/elastic/kibana/pull/164850
351348
func (client *Client) KibanaIsServerless() (bool, error) {
352-
ret, _, err := client.Connection.Request("GET", "/api/saved_objects/_find", nil, nil, nil)
353-
if ret > 300 && strings.Contains(err.Error(), "not available with the current configuration") {
349+
350+
type apiStatus struct {
351+
Version struct {
352+
BuildFlavor string `json:"build_flavor"`
353+
} `json:"version"`
354+
}
355+
356+
// we can send a GET to `/api/status` without auth, but it won't actually return version info.
357+
params := http.Header{}
358+
if client.APIKey != "" {
359+
v := "ApiKey " + base64.StdEncoding.EncodeToString([]byte(client.APIKey))
360+
params.Add("Authorization", v)
361+
}
362+
363+
ret, resp, err := client.Connection.Request("GET", "/api/status", nil, params, nil)
364+
if err != nil {
365+
return false, fmt.Errorf("error in HTTP request: %w", err)
366+
}
367+
368+
respString := string(resp)
369+
if ret > http.StatusMultipleChoices {
370+
return false, fmt.Errorf("got invalid response code: %v (%s)", ret, respString)
371+
}
372+
373+
status := apiStatus{}
374+
err = json.Unmarshal(resp, &status)
375+
if err != nil {
376+
return false, fmt.Errorf("error unmarshalling JSON: %w", err)
377+
}
378+
379+
if status.Version.BuildFlavor == "serverless" {
354380
return true, nil
355-
} else if err != nil {
356-
return false, fmt.Errorf("error checking serverless status: %w", err)
381+
} else {
382+
return false, nil
357383
}
358-
return false, nil
384+
359385
}
360386

361387
func (client *Client) ImportMultiPartFormFile(url string, params url.Values, filename string, contents string) error {

kibana/client_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,28 @@ func assertConnection(t *testing.T, URL string, expectedStatusCode int) {
5959
assert.Error(t, err)
6060
}
6161

62+
func TestIsServerless(t *testing.T) {
63+
rawStatusCall := `{"name":"kb","uuid":"d2130570-f7d8-463b-bd67-8503150004d5","version":{"number":"8.15.0","build_hash":"13382875e99e8c97f4574d86eca07cac3be9edfc","build_number":75422,"build_snapshot":false,"build_flavor":"stateful","build_date":"2024-06-15T18:13:50.595Z"}}`
64+
65+
kibanaTS := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
66+
_, _ = w.Write([]byte(rawStatusCall))
67+
}))
68+
defer kibanaTS.Close()
69+
70+
conn := Connection{
71+
URL: kibanaTS.URL,
72+
HTTP: http.DefaultClient,
73+
}
74+
75+
testClient := Client{
76+
Connection: conn,
77+
}
78+
79+
got, err := testClient.KibanaIsServerless()
80+
require.NoError(t, err)
81+
require.False(t, got)
82+
}
83+
6284
func TestErrorBadJson(t *testing.T) {
6385
kibanaTS := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
6486
w.WriteHeader(http.StatusGone)

transport/httpcommon/diag_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,8 @@ func Test_diagError(t *testing.T) {
283283
}
284284
_, err = client.Do(req) //nolint:bodyclose // expected to return an error
285285
require.Error(t, err)
286-
require.Contains(t, diagError(err), "caused by expired mTLS client cert.")
286+
// different OSes seem to report different TLS errors, so just check for the "expired" string.
287+
require.Contains(t, diagError(err), "expired")
287288
})
288289
}
289290

0 commit comments

Comments
 (0)