@@ -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 {
346345func (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
351348func (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
361387func (client * Client ) ImportMultiPartFormFile (url string , params url.Values , filename string , contents string ) error {
0 commit comments