-
Notifications
You must be signed in to change notification settings - Fork 121
Add a new validation to use or not upload API to install packages #2511
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
Changes from all commits
c562c0b
c84f0fe
999bba0
b7fc84d
9994fc2
b7f500c
62704d8
4da2bd9
538149c
b2fbf7d
05e082b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,13 +7,16 @@ package kibana | |
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/elastic/elastic-package/internal/packages" | ||
) | ||
|
||
var ErrNotSupported error = errors.New("not supported") | ||
|
||
// InstallPackage installs the given package in Fleet. | ||
func (c *Client) InstallPackage(ctx context.Context, name, version string) ([]packages.Asset, error) { | ||
path := c.epmPackageUrl(name, version) | ||
|
@@ -27,6 +30,47 @@ func (c *Client) InstallPackage(ctx context.Context, name, version string) ([]pa | |
return processResults("install", statusCode, respBody) | ||
} | ||
|
||
// EnsureZipPackageCanBeInstalled checks whether or not it can be installed a package using the upload API. | ||
// This is intened to be used between 8.7.0 and 8.8.2 stack versions, and it is only safe to be run in those | ||
// stack versions. | ||
func (c *Client) EnsureZipPackageCanBeInstalled(ctx context.Context) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bit hacky and would be prone to fail if this API would change, maybe add a comment mentioning that is only intended to be used between 8.7.0 and 8.8.2, and is only safe to be used on these versions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added comment here 4da2bd9 |
||
path := fmt.Sprintf("%s/epm/packages", FleetAPI) | ||
|
||
req, err := c.newRequest(ctx, http.MethodPost, path, nil) | ||
if err != nil { | ||
return err | ||
} | ||
req.Header.Set("Content-Type", "application/zip") | ||
req.Header.Add("elastic-api-version", "2023-10-31") | ||
|
||
statusCode, respBody, err := c.doRequest(req) | ||
if err != nil { | ||
return fmt.Errorf("could not install zip package: %w", err) | ||
} | ||
switch statusCode { | ||
case http.StatusBadRequest: | ||
// If the stack allows to use the upload API, the response is like this one: | ||
// { | ||
// "statusCode":400, | ||
// "error":"Bad Request", | ||
// "message":"Error during extraction of package: Error: end of central directory record signature not found. Assumed content type was application/zip, check if this matches the archive type." | ||
// } | ||
return nil | ||
case http.StatusForbidden: | ||
var resp struct { | ||
Message string `json:"message"` | ||
} | ||
if err := json.Unmarshal(respBody, &resp); err != nil { | ||
return fmt.Errorf("could not unmarhsall response to JSON: %w", err) | ||
} | ||
if resp.Message == "Requires Enterprise license" { | ||
return ErrNotSupported | ||
} | ||
} | ||
|
||
return fmt.Errorf("unexpected response (status code %d): %s", statusCode, string(respBody)) | ||
} | ||
|
||
// InstallZipPackage installs the local zip package in Fleet. | ||
func (c *Client) InstallZipPackage(ctx context.Context, zipFile string) ([]packages.Asset, error) { | ||
path := fmt.Sprintf("%s/epm/packages", FleetAPI) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noticed while trying to get and use the Elastic stack subscription.