@@ -30,17 +30,13 @@ import (
3030 "time"
3131)
3232
33- func New (kibanaURL , apiKey string , username , password string ) (* Client , error ) {
33+ func New (kibanaURL , username , password string ) (* Client , error ) {
3434 if kibanaURL == "" {
3535 return nil , fmt .Errorf ("kbclient.New kibanaURL must not be empty" )
3636 }
37- if apiKey == "" {
38- return nil , fmt .Errorf ("kbclient.New apiKey must not be empty" )
39- }
4037
4138 return & Client {
4239 url : kibanaURL ,
43- apiKey : apiKey ,
4440 superUsername : username ,
4541 superPassword : password ,
4642 SupportedAPIVersion : "2023-10-31" ,
@@ -52,9 +48,8 @@ type Client struct {
5248 http.Client
5349 // url is the Kibana URL where requests will be directed to.
5450 url string
55- // apiKey should be an Elasticsearch API key with appropriate privileges.
56- apiKey string
5751
52+ // Fleet API access require superuser role before 8.1.0.
5853 // superUsername should be an Elasticsearch superuser username.
5954 superUsername string
6055 // superPassword should be an Elasticsearch superuser password.
@@ -64,7 +59,7 @@ type Client struct {
6459}
6560
6661// prepareRequest creates a http.Request with required headers for interacting with Kibana.
67- func (c * Client ) prepareRequest (method , path string , body any , super bool ) (* http.Request , error ) {
62+ func (c * Client ) prepareRequest (method , path string , body any ) (* http.Request , error ) {
6863 b , err := json .Marshal (body )
6964 if err != nil {
7065 return nil , fmt .Errorf ("cannot marshal body: %w" , err )
@@ -79,13 +74,10 @@ func (c *Client) prepareRequest(method, path string, body any, super bool) (*htt
7974 req .Header .Add ("kbn-xsrf" , "true" )
8075 req .Header .Add ("Content-Type" , "application/json" )
8176 req .Header .Add ("Elastic-Api-Version" , c .SupportedAPIVersion )
82- if super {
83- userPass := fmt .Sprintf ("%s:%s" , c .superUsername , c .superPassword )
84- basicAuth := base64 .StdEncoding .EncodeToString ([]byte (userPass ))
85- req .Header .Add ("Authorization" , fmt .Sprintf ("Basic %s" , basicAuth ))
86- } else {
87- req .Header .Add ("Authorization" , fmt .Sprintf ("ApiKey %s" , c .apiKey ))
88- }
77+
78+ userPass := fmt .Sprintf ("%s:%s" , c .superUsername , c .superPassword )
79+ basicAuth := base64 .StdEncoding .EncodeToString ([]byte (userPass ))
80+ req .Header .Add ("Authorization" , fmt .Sprintf ("Basic %s" , basicAuth ))
8981
9082 return req , nil
9183}
@@ -98,11 +90,10 @@ func (c *Client) sendRequest(
9890 method string ,
9991 path string ,
10092 body any ,
101- super bool ,
10293 handleRespError func (statusCode int , body []byte ) error ,
10394) ([]byte , error ) {
10495 methodPath := fmt .Sprintf ("%s %s" , method , path )
105- req , err := c .prepareRequest (method , path , body , super )
96+ req , err := c .prepareRequest (method , path , body )
10697 if err != nil {
10798 return nil , fmt .Errorf ("cannot prepare request (%s): %w" , methodPath , err )
10899 }
@@ -144,18 +135,33 @@ func (e ElasticAgentPolicyNotFoundError) Error() string {
144135}
145136
146137type PackagePolicy struct {
147- Name string `json:"name"`
148- Description string `json:"description"`
149- Package PackagePolicyPkg `json:"package"`
150- PolicyID string `json:"policy_id"`
151- PolicyIDs []string `json:"policy_ids"`
138+ Name string `json:"name"`
139+ Description string `json:"description"`
140+ Package PackagePolicyPkg `json:"package"`
141+ Namespace string `json:"namespace"`
142+ Inputs []PackagePolicyInput `json:"inputs"`
143+ OutputID string `json:"output_id"`
144+ PolicyID string `json:"policy_id,omitempty"`
145+ PolicyIDs []string `json:"policy_ids,omitempty"`
146+ Enabled bool `json:"enabled"`
152147}
153148
154149type PackagePolicyPkg struct {
155150 Name string `json:"name"`
151+ Title string `json:"title"`
156152 Version string `json:"version"`
157153}
158154
155+ type PackagePolicyInput struct {
156+ Config any `json:"config"`
157+ Enabled bool `json:"enabled"`
158+ Streams []any `json:"streams"`
159+ Type string `json:"type"`
160+ Vars map [string ]any `json:"vars"`
161+ KeepEnabled bool `json:"keep_enabled"`
162+ PolicyTemplate string `json:"policy_template"`
163+ }
164+
159165type getPackagePolicyResponse struct {
160166 Item PackagePolicy `json:"item"`
161167}
@@ -174,7 +180,7 @@ func (c *Client) GetPackagePolicyByID(ctx context.Context, policyID string) (Pac
174180 return nil
175181 }
176182
177- b , err := c .sendRequest (ctx , http .MethodGet , path , nil , false , handleRespError )
183+ b , err := c .sendRequest (ctx , http .MethodGet , path , nil , handleRespError )
178184 if err != nil {
179185 return PackagePolicy {}, err
180186 }
@@ -187,19 +193,14 @@ func (c *Client) GetPackagePolicyByID(ctx context.Context, policyID string) (Pac
187193 return policyResp .Item , nil
188194}
189195
190- type UpdatePackagePolicyRequest struct {
191- PackagePolicy
192- Force bool `json:"force"`
193- }
194-
195196// UpdatePackagePolicyByID performs a Package Policy update in Fleet through the Fleet Kibana APIs.
196197// https://www.elastic.co/docs/api/doc/kibana/v8/operation/operation-update-package-policy
197- func (c * Client ) UpdatePackagePolicyByID (ctx context.Context , policyID string , request UpdatePackagePolicyRequest ) error {
198+ func (c * Client ) UpdatePackagePolicyByID (ctx context.Context , policyID string , policy PackagePolicy ) error {
198199 ctx , cancel := context .WithTimeout (ctx , 10 * time .Second )
199200 defer cancel ()
200201
201202 path := fmt .Sprintf ("/api/fleet/package_policies/%s" , policyID )
202- _ , err := c .sendRequest (ctx , http .MethodPut , path , request , false , nil )
203+ _ , err := c .sendRequest (ctx , http .MethodPut , path , policy , nil )
203204 return err
204205}
205206
@@ -218,7 +219,7 @@ func (c *Client) EnableIntegrationsServer(ctx context.Context) error {
218219 // This is an internal API that is not publicly documented.
219220 // https://github.com/elastic/kibana/blob/12aa3fc/x-pack/solutions/observability/plugins/apm/server/routes/fleet/route.ts#L146
220221 path := "/internal/apm/fleet/cloud_apm_package_policy"
221- b , err := c .sendRequest (ctx , http .MethodPost , path , nil , true , nil )
222+ b , err := c .sendRequest (ctx , http .MethodPost , path , nil , nil )
222223 if err != nil {
223224 return err
224225 }
0 commit comments