Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
fix: add resiliency when an integration cannot be added to a policy (#…
Browse files Browse the repository at this point in the history
…1914)

* chore: do not exit if the integration cannot be added to the policy

* chore: add resiliency when adding an integration to a policy

We have detected 500 codes in the staging environment of package-registry

[2021-12-16T05:41:55.069Z] FATA[2021-12-16T05:41:53Z] Unable to add integration to policy           err="could not add package to policy; API status code = 500; response body = {\"statusCode\":500,\"error\":\"Internal Server Error\",\"message\":\"'502 Bad Gateway' error response from package registry at https://epr-staging.elastic.co/package/endpoint/1.4.0\"}" packageDS="{ endpoint-50fff084-9495-4633-b1a7-0a817b61d6bc Endpoint Security default cdd3d370-5e32-11ec-9ece-534be1f506d0 true  [] { endpoint Endpoint Security 1.4.0}}"

* fix: add missing return

* fix: format source
  • Loading branch information
mdelapenya authored Dec 16, 2021
1 parent 1b04d82 commit 66d399a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 13 deletions.
3 changes: 2 additions & 1 deletion e2e/_suites/fleet/fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,8 @@ func theIntegrationIsOperatedInThePolicy(ctx context.Context, client *kibana.Cli
log.WithFields(log.Fields{
"err": err,
"packageDS": packageDataStream,
}).Fatal("Unable to add integration to policy")
}).Error("Unable to add integration to policy")
return err
}
} else if strings.ToLower(action) == actionREMOVED {
packageDataStream, err := client.GetIntegrationFromAgentPolicy(ctx, integration.Name, policy)
Expand Down
70 changes: 58 additions & 12 deletions internal/kibana/integrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (
"encoding/json"
"fmt"
"strings"
"time"

"github.com/Jeffail/gabs/v2"
"github.com/cenkalti/backoff/v4"
"github.com/elastic/e2e-testing/internal/utils"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"go.elastic.co/apm"
Expand All @@ -22,24 +25,67 @@ type IntegrationPackage struct {

// AddIntegrationToPolicy adds an integration to policy
func (c *Client) AddIntegrationToPolicy(ctx context.Context, packageDS PackageDataStream) error {
span, _ := apm.StartSpanOptions(ctx, "Adding integration to policy", "fleet.package.add-to-policy", apm.SpanOptions{
Parent: apm.SpanFromContext(ctx).TraceContext(),
})
defer span.End()
maxTimeout := time.Duration(utils.TimeoutFactor) * time.Minute
retryCount := 1

reqBody, err := json.Marshal(packageDS)
if err != nil {
return errors.Wrap(err, "could not convert policy-package (request) to JSON")
exp := utils.GetExponentialBackOff(maxTimeout)

addIntegrationFn := func() error {
span, _ := apm.StartSpanOptions(ctx, "Adding integration to policy", "fleet.package.add-to-policy", apm.SpanOptions{
Parent: apm.SpanFromContext(ctx).TraceContext(),
})
defer span.End()

reqBody, err := json.Marshal(packageDS)
if err != nil {
log.WithFields(log.Fields{
"elapsedTime": exp.GetElapsedTime(),
"err": err,
"package": packageDS,
"retry": retryCount,
}).Warn("Could not convert policy-package (request) to JSON. Retrying")

retryCount++

return err
}

statusCode, respBody, err := c.post(ctx, fmt.Sprintf("%s/package_policies", FleetAPI), reqBody)
if err != nil {
log.WithFields(log.Fields{
"elapsedTime": exp.GetElapsedTime(),
"err": err,
"package": packageDS,
"retry": retryCount,
}).Warn("Could not add package to policy. Retrying")

retryCount++

return err
}

if statusCode != 200 {
log.WithFields(log.Fields{
"elapsedTime": exp.GetElapsedTime(),
"err": err,
"statusCode": statusCode,
"response": respBody,
"package": packageDS,
"retry": retryCount,
}).Warn("could not add package to policy because of HTTP code is not 200")

retryCount++
return fmt.Errorf("could not add package to policy; API status code = %d; response body = %s", statusCode, respBody)
}

return nil
}

statusCode, respBody, err := c.post(ctx, fmt.Sprintf("%s/package_policies", FleetAPI), reqBody)
err := backoff.Retry(addIntegrationFn, exp)
if err != nil {
return errors.Wrap(err, "could not add package to policy")
return err
}

if statusCode != 200 {
return fmt.Errorf("could not add package to policy; API status code = %d; response body = %s", statusCode, respBody)
}
return nil
}

Expand Down

0 comments on commit 66d399a

Please sign in to comment.