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

[Ingest Manager] Unroll on unauthorised #19722

Merged
merged 16 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
errors udpdate
  • Loading branch information
michalpristas committed Jul 9, 2020
commit 2d18115f7768d3b99c6652518820f6a16a7ba8df
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ require (
gopkg.in/jcmturner/gokrb5.v7 v7.5.0
gopkg.in/mgo.v2 v2.0.0-20160818020120-3f83fa500528
gopkg.in/yaml.v2 v2.3.0
gotest.tools v2.2.0+incompatible
howett.net/plist v0.0.0-20181124034731-591f970eefbb
k8s.io/api v0.18.3
k8s.io/apimachinery v0.18.3
Expand Down
10 changes: 4 additions & 6 deletions x-pack/elastic-agent/pkg/agent/application/fleet_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package application

import (
"context"
"strings"
"fmt"
"sync"
"time"

Expand Down Expand Up @@ -216,6 +216,8 @@ func (f *fleetGateway) execute(ctx context.Context) (*fleetapi.CheckinResponse,
resp, err := cmd.Execute(ctx, req)
if isUnauth(err) {
f.unauthCounter++
fmt.Println("unauth", f.unauthCounter)

if f.shouldUnroll() {
f.log.Warnf("retrieved unauthorized for '%d' times. Unrolling.", f.unauthCounter)
return &fleetapi.CheckinResponse{
Expand All @@ -242,11 +244,7 @@ func (f *fleetGateway) shouldUnroll() bool {
}

func isUnauth(err error) bool {
if err == nil {
return false
}

return strings.Contains(err.Error(), fleetapi.ErrInvalidAPIKey.Error())
return errors.Is(err, fleetapi.ErrInvalidAPIKey)
}

func (f *fleetGateway) Start() {
Expand Down
49 changes: 48 additions & 1 deletion x-pack/elastic-agent/pkg/agent/errors/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,27 @@

package errors

import "github.com/pkg/errors"
import (
goerrors "errors"
"reflect"

"github.com/pkg/errors"
)

// As is just a helper so user dont have to use multiple imports for errors.
func As(err error, target interface{}) bool {
return goerrors.As(err, target)
}

// Is is just a helper so user dont have to use multiple imports for errors.
func Is(err, target error) bool {
return goerrors.Is(err, target)
}

// Unwrap is just a helper so user dont have to use multiple imports for errors.
func Unwrap(err error) error {
return goerrors.Unwrap(err)
}

// MetaRecord is a entry of metadata enhancing an error.
type MetaRecord struct {
Expand Down Expand Up @@ -101,6 +121,33 @@ func (e agentError) Meta() map[string]interface{} {
return resultingMeta
}

// Equal compares errors and evaluates if they are the same or not.
// Agent error is not comparable due to included map so we need to
// do the heavy lifting ourselves.
func (e agentError) Equal(target error) bool {
targetErr, ok := target.(agentError)
if !ok {
return false
}

return errors.Is(e.err, targetErr.err) &&
e.errType == targetErr.errType &&
e.msg == targetErr.msg &&
reflect.DeepEqual(e.meta, targetErr.meta)

}

// Is checks whether agent err is an err.
func (e agentError) Is(target error) bool {
if agentErr, ok := target.(agentError); ok && e.Equal(agentErr) {
return true
} else if ok {
return false
}

return goerrors.Is(e.err, target)
}

// Check it implements Error
var _ Error = agentError{}

Expand Down
48 changes: 48 additions & 0 deletions x-pack/elastic-agent/pkg/agent/errors/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,60 @@ package errors

import (
"fmt"
"io"
"strings"
"testing"

"github.com/pkg/errors"
"gotest.tools/assert"
)

func TestErrorsIs(t *testing.T) {
type testCase struct {
id string
actualErr error
expectedErr error
expectedMatch bool
}

simpleErr := io.ErrNoProgress
simpleWrap := errors.Wrap(simpleErr, "wrapping %w")
agentErr := New()
nestedSimple := New(simpleErr)
nestedWrap := New(simpleWrap)
agentInErr := errors.Wrap(nestedWrap, "wrapping %w")

tt := []testCase{
{"simple wrap", simpleWrap, simpleErr, true},
{"simple mismatch", simpleWrap, errors.New("sample"), false},

{"direct nested - root check", nestedSimple, simpleErr, true},
{"direct nested - mismatch", nestedSimple, errors.New("sample"), false},
{"direct nested - comparing agent errors", nestedSimple, agentErr, false},

{"deep nested - root check", New(nestedSimple), simpleErr, true},
{"deep nested - mismatch", New(nestedSimple), errors.New("sample"), false},
{"deep nested - comparing agent errors", New(nestedSimple), agentErr, false},

{"nested wrap - wrap check", New(nestedWrap), simpleWrap, true},
{"nested wrap - root", New(nestedWrap), simpleErr, true},

{"comparing agent errors", New(agentErr), agentErr, true},

{"agent in error", agentInErr, nestedWrap, true},
{"agent in error wrap", agentInErr, simpleWrap, true},
{"agent in error root", agentInErr, simpleErr, true},
{"agent in error nil check", agentInErr, nil, false},
}

for _, tc := range tt {
t.Run(tc.id, func(t *testing.T) {
match := Is(tc.actualErr, tc.expectedErr)
assert.Equal(t, tc.expectedMatch, match)
})
}
}

func TestErrorsWrap(t *testing.T) {
ce := New("custom error", TypePath, M("k", "v"))
ew := errors.Wrap(ce, "wrapper")
Expand Down