Skip to content
Merged
Show file tree
Hide file tree
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
46 changes: 46 additions & 0 deletions kibana/fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
fleetUninstallTokensAPI = "/api/fleet/uninstall_tokens" //nolint:gosec // NOT the "Potential hardcoded credentials"
fleetUpgradeAgentAPI = "/api/fleet/agents/%s/upgrade"
fleetAgentDownloadSourcesAPI = "/api/fleet/agent_download_sources"
fleetAgentDownloadSourceAPI = "/api/fleet/agent_download_sources/%s"
fleetProxiesAPI = "/api/fleet/proxies"
)

Expand Down Expand Up @@ -204,6 +205,51 @@ func (client *Client) CreateDownloadSource(ctx context.Context, source DownloadS
return body, nil
}

func (client *Client) UpdateDownloadSource(ctx context.Context, id string, source DownloadSource) (DownloadSourceResponse, error) {
reqBody, err := json.Marshal(source)
if err != nil {
return DownloadSourceResponse{},
fmt.Errorf("unable to marshal DownloadSource into JSON: %w", err)
}

resp, err := client.SendWithContext(
ctx,
http.MethodPut,
fmt.Sprintf(fleetAgentDownloadSourceAPI, id),
nil,
nil,
bytes.NewReader(reqBody))
if err != nil {
return DownloadSourceResponse{},
fmt.Errorf("error calling Agent Binary Download Source API: %w", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
var respBody string
if bs, err := io.ReadAll(resp.Body); err != nil {
respBody = "could not read response body"
} else {
respBody = string(bs)
}

client.log.Errorw(
"could not update download source, kibana returned "+resp.Status,
"http.response.body.content", respBody)
return DownloadSourceResponse{},
fmt.Errorf("could not update download source, kibana returned %s. response body: %s: %w",
resp.Status, respBody, err)
}

body := DownloadSourceResponse{}
if err = json.NewDecoder(resp.Body).Decode(&body); err != nil {
return DownloadSourceResponse{},
fmt.Errorf("failed parsing download source response: %w", err)
}

return body, nil
}

// GetPolicy returns the policy with 'policy_id' id.
func (client *Client) GetPolicy(ctx context.Context, id string) (r PolicyResponse, err error) {
apiURL := fmt.Sprintf(fleetAgentPolicyAPI, id)
Expand Down
45 changes: 45 additions & 0 deletions kibana/fleet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ var (

//go:embed testdata/fleet_get_fleet_server_host_response.json
fleetGetFleetServerHostResponse []byte

//go:embed testdata/fleet_create_download_source_response.json
fleetCreateDownloadSourceResponse []byte

//go:embed testdata/fleet_update_download_source_response.json
fleetUpdateDownloadSourceResponse []byte
)

func TestFleetCreatePolicy(t *testing.T) {
Expand Down Expand Up @@ -385,6 +391,45 @@ func TestFleetGetFleetServerHost(t *testing.T) {
require.True(t, resp.IsPreconfigured)
}

func TestFleetDownloadSource(t *testing.T) {
id := "test-id"
name := "test-name"
handler := func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPost:
_, _ = w.Write(fleetCreateDownloadSourceResponse)
case http.MethodPut:
_, _ = w.Write(fleetUpdateDownloadSourceResponse)
default:
http.Error(w, "not implemented", http.StatusNotImplemented)
}
}
client, err := createTestServerAndClient(handler)
require.NoError(t, err)
require.NotNil(t, client)

t.Run("create", func(t *testing.T) {
resp, err := client.CreateDownloadSource(t.Context(), DownloadSource{
Name: name,
Host: "http://test.local",
})
require.NoError(t, err)
require.Equal(t, id, resp.Item.ID)
require.Equal(t, name, resp.Item.Name)
require.NotEmpty(t, "http://test.local", resp.Item.Host)
})
t.Run("update", func(t *testing.T) {
resp, err := client.UpdateDownloadSource(t.Context(), id, DownloadSource{
Name: name,
Host: "http://newtest.local",
})
require.NoError(t, err)
require.Equal(t, id, resp.Item.ID)
require.Equal(t, name, resp.Item.Name)
require.NotEmpty(t, "http://newtest.local", resp.Item.Host)
})
}

func createTestServerAndClient(handler http.HandlerFunc) (*Client, error) {
kibanaTS := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
Expand Down
8 changes: 8 additions & 0 deletions kibana/testdata/fleet_create_download_source_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"item": {
"id": "test-id",
"name": "test-name",
"host": "http://test.local",
"is_default": false
}
}
9 changes: 9 additions & 0 deletions kibana/testdata/fleet_update_download_source_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"item": {
"id": "test-id",
"name": "test-name",
"host": "http://newtest.local",
"is_default": false
}
}

Loading