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

[kibana]: Fix CreateFleetServerHosts and CreateFleetProxy for 8.x #250

Merged
merged 1 commit into from
Nov 7, 2024
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
43 changes: 13 additions & 30 deletions kibana/fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,13 +467,12 @@ type FleetServerHost struct {
}

type ListFleetServerHostsRequest struct {
HostURLs []string `json:"host_urls"`
ID string `json:"id"`
IsDefault bool `json:"is_default"`
IsInternal bool `json:"is_internal"`
IsPreconfigured bool `json:"is_preconfigured"`
Name string `json:"name"`
ProxyID string `json:"proxy_id"`
HostURLs []string `json:"host_urls"`
ID string `json:"id"`
IsDefault bool `json:"is_default"`
IsInternal bool `json:"is_internal"`
Name string `json:"name"`
ProxyID string `json:"proxy_id"`
}

type ListFleetServerHostsResponse struct {
Expand Down Expand Up @@ -519,22 +518,10 @@ func (client *Client) CreateFleetServerHosts(ctx context.Context, req ListFleetS
return FleetServerHostsResponse{}, fmt.Errorf("error calling new fleet server hosts API: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return FleetServerHostsResponse{}, fmt.Errorf("error creating fleet-server host: unexpected status code: %s", resp.Status)
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return FleetServerHostsResponse{}, fmt.Errorf("error reading fleet response: %w", err)
}

var fleetResp FleetServerHostsResponse
err = json.Unmarshal(body, &fleetResp)
if err != nil {
return FleetServerHostsResponse{}, fmt.Errorf("error parsing fleet response: %w", err)
}

return fleetResp, nil
err = readJSONResponse(resp, &fleetResp)
return fleetResp, err
}

type GetFleetServerHostRequest struct {
Expand Down Expand Up @@ -664,7 +651,6 @@ type ProxiesRequest struct {
CertificateAuthorities string `json:"certificate_authorities"`
CertificateKey string `json:"certificate_key"`
ID string `json:"id"`
IsPreconfigured bool `json:"is_preconfigured"`
Name string `json:"name"`
ProxyHeaders map[string]string `json:"proxy_headers"`
URL string `json:"url"`
Expand All @@ -685,6 +671,10 @@ type ProxiesResponse struct {

// CreateFleetProxy creates a new proxy
func (client *Client) CreateFleetProxy(ctx context.Context, req ProxiesRequest) (ProxiesResponse, error) {
// if `proxy_headers` is `null` 8.x kibana/Fleet will return a 400 - Bad request
if req.ProxyHeaders == nil {
req.ProxyHeaders = map[string]string{}
}
bs, err := json.Marshal(req)
if err != nil {
return ProxiesResponse{}, fmt.Errorf("could not marshal ListFleetServerHostsRequest: %w", err)
Expand All @@ -698,17 +688,10 @@ func (client *Client) CreateFleetProxy(ctx context.Context, req ProxiesRequest)
return ProxiesResponse{}, err
}
defer r.Body.Close()
if r.StatusCode != http.StatusOK {
return ProxiesResponse{}, fmt.Errorf("error creating proxy: unexpected status code: %s", r.Status)
}

resp := ProxiesResponse{}
err = readJSONResponse(r, &resp)
if err != nil {
return ProxiesResponse{}, fmt.Errorf("failes parsing response: %w", err)
}

return resp, nil
return resp, err
}

type UninstallTokenResponse struct {
Expand Down
56 changes: 40 additions & 16 deletions kibana/fleet_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,25 +447,49 @@ func TestCreateFleetProxy(t *testing.T) {
client, err := NewClientWithConfig(&cfg, "", "", "", "")
require.NoError(t, err)

id := uuid.Must(uuid.NewV4()).String()
req := ProxiesRequest{
ID: "CreateFleetServerHosts" + id,
Name: "CreateFleetServerHosts" + id,
URL: "https://proxy.elastic.co",
CertificateAuthorities: "some CA",
Certificate: "some certificate",
CertificateKey: "some certificate key",
IsPreconfigured: true,
ProxyHeaders: map[string]string{
"h1": "v1",
"h2": "v2",
tcs := []struct {
Name string
Req ProxiesRequest
}{
{
Name: "nil ProxyHeaders",
Req: ProxiesRequest{
ID: "CreateFleetServerHosts",
Name: "CreateFleetServerHosts",
URL: "https://proxy.elastic.co",
CertificateAuthorities: "some CA",
Certificate: "some certificate",
CertificateKey: "some certificate key",
},
},
{
Name: "with ProxyHeaders",
Req: ProxiesRequest{
ID: "CreateFleetServerHosts",
Name: "CreateFleetServerHosts",
URL: "https://proxy.elastic.co",
CertificateAuthorities: "some CA",
Certificate: "some certificate",
CertificateKey: "some certificate key",
ProxyHeaders: map[string]string{
"h1": "v1",
"h2": "v2",
},
},
},
}
got, err := client.CreateFleetProxy(ctx, req)
require.NoError(t, err, "error creating new fleet host")

require.Equal(t, req.ID, got.Item.ID)
require.Equal(t, req, got.Item)
for _, tc := range tcs {
id := uuid.Must(uuid.NewV4()).String()
tc.Req.ID += id
tc.Req.Name += id

got, err := client.CreateFleetProxy(ctx, tc.Req)
require.NoError(t, err, "error creating new fleet host")

require.Equal(t, tc.Req.ID, got.Item.ID)
require.Equal(t, tc.Req, got.Item)
}
}

func TestGetFleetServerHost(t *testing.T) {
Expand Down
Loading