Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add checks for serverless support #150

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions kibana/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,19 @@ func (client *Client) readVersion() error {
// IgnoreVersion was set when creating the client.
func (client *Client) GetVersion() version.V { return client.Version }

// KibanaIsServerless returns true if we're talking to a serverless instance.
// 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.
// see https://github.com/elastic/kibana/pull/164850
func (client *Client) KibanaIsServerless() (bool, error) {
ret, _, err := client.Connection.Request("GET", "/api/saved_objects/_find", nil, nil, nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If Kibana changes this, which is entirely possible because saved objects are one of the major APIs, then this might give us the wrong result at some point. Hopefully we can catch that in the snapshot builds before release.

if ret > 300 && strings.Contains(err.Error(), "not available with the current configuration") {
return true, nil
} else if err != nil {
return false, fmt.Errorf("error checking serverless status: %w", err)
}
return false, nil
}

func (client *Client) ImportMultiPartFormFile(url string, params url.Values, filename string, contents string) error {
buf := &bytes.Buffer{}
w := multipart.NewWriter(buf)
Expand All @@ -364,9 +377,13 @@ func (client *Client) ImportMultiPartFormFile(url string, params url.Values, fil
}
w.Close()

headers := http.Header{}
headers.Add("Content-Type", w.FormDataContentType())
statusCode, response, err := client.Connection.Request("POST", url, params, headers, buf)
// On serverless, special header is required to talk to this endpoint
sendHeaders := http.Header{}
sendHeaders.Add("Content-Type", w.FormDataContentType())
if serverless, _ := client.KibanaIsServerless(); serverless {
sendHeaders.Add("x-elastic-internal-origin", "elastic-agent-libs")
}
statusCode, response, err := client.Connection.Request("POST", url, params, sendHeaders, buf)
if err != nil {
return fmt.Errorf("returned %d to import file: %w. Response: %s", statusCode, err, response)
}
Expand Down
Loading