2022-02-07 09:42:07 +00:00
|
|
|
package supervisor
|
2019-12-04 17:22:08 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-02-10 16:42:09 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-12-04 17:22:08 +00:00
|
|
|
|
2021-03-26 04:04:56 +00:00
|
|
|
"github.com/cloudflare/cloudflared/retry"
|
2019-12-04 17:22:08 +00:00
|
|
|
tunnelpogs "github.com/cloudflare/cloudflared/tunnelrpc/pogs"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRefreshAuthBackoff(t *testing.T) {
|
2020-08-18 10:14:14 +00:00
|
|
|
rcm := newReconnectCredentialManager(t.Name(), t.Name(), 4)
|
2019-12-04 17:22:08 +00:00
|
|
|
|
|
|
|
var wait time.Duration
|
2021-03-26 04:04:56 +00:00
|
|
|
retry.Clock.After = func(d time.Duration) <-chan time.Time {
|
2019-12-04 17:22:08 +00:00
|
|
|
wait = d
|
|
|
|
return time.After(d)
|
|
|
|
}
|
2021-03-26 04:04:56 +00:00
|
|
|
backoff := &retry.BackoffHandler{MaxRetries: 3}
|
2019-12-04 17:22:08 +00:00
|
|
|
auth := func(ctx context.Context, n int) (tunnelpogs.AuthOutcome, error) {
|
|
|
|
return nil, fmt.Errorf("authentication failure")
|
|
|
|
}
|
|
|
|
|
|
|
|
// authentication failures should consume the backoff
|
|
|
|
for i := uint(0); i < backoff.MaxRetries; i++ {
|
2020-08-18 10:14:14 +00:00
|
|
|
retryChan, err := rcm.RefreshAuth(context.Background(), backoff, auth)
|
2021-02-10 16:42:09 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, retryChan)
|
|
|
|
require.Greater(t, wait.Seconds(), 0.0)
|
|
|
|
require.Less(t, wait.Seconds(), float64((1<<(i+1))*time.Second))
|
2019-12-04 17:22:08 +00:00
|
|
|
}
|
2020-08-18 10:14:14 +00:00
|
|
|
retryChan, err := rcm.RefreshAuth(context.Background(), backoff, auth)
|
2021-02-10 16:42:09 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, retryChan)
|
2019-12-04 17:22:08 +00:00
|
|
|
|
|
|
|
// now we actually make contact with the remote server
|
2020-08-18 10:14:14 +00:00
|
|
|
_, _ = rcm.RefreshAuth(context.Background(), backoff, func(ctx context.Context, n int) (tunnelpogs.AuthOutcome, error) {
|
2019-12-04 17:22:08 +00:00
|
|
|
return tunnelpogs.NewAuthUnknown(errors.New("auth unknown"), 19), nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// The backoff timer should have been reset. To confirm this, make timeNow
|
|
|
|
// return a value after the backoff timer's grace period
|
2021-03-26 04:04:56 +00:00
|
|
|
retry.Clock.Now = func() time.Time {
|
2019-12-04 17:22:08 +00:00
|
|
|
expectedGracePeriod := time.Duration(time.Second * 2 << backoff.MaxRetries)
|
|
|
|
return time.Now().Add(expectedGracePeriod * 2)
|
|
|
|
}
|
2021-02-10 16:42:09 +00:00
|
|
|
_, ok := backoff.GetMaxBackoffDuration(context.Background())
|
|
|
|
require.True(t, ok)
|
2019-12-04 17:22:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRefreshAuthSuccess(t *testing.T) {
|
2020-08-18 10:14:14 +00:00
|
|
|
rcm := newReconnectCredentialManager(t.Name(), t.Name(), 4)
|
2019-12-04 17:22:08 +00:00
|
|
|
|
|
|
|
var wait time.Duration
|
2021-03-26 04:04:56 +00:00
|
|
|
retry.Clock.After = func(d time.Duration) <-chan time.Time {
|
2019-12-04 17:22:08 +00:00
|
|
|
wait = d
|
|
|
|
return time.After(d)
|
|
|
|
}
|
|
|
|
|
2021-03-26 04:04:56 +00:00
|
|
|
backoff := &retry.BackoffHandler{MaxRetries: 3}
|
2019-12-04 17:22:08 +00:00
|
|
|
auth := func(ctx context.Context, n int) (tunnelpogs.AuthOutcome, error) {
|
|
|
|
return tunnelpogs.NewAuthSuccess([]byte("jwt"), 19), nil
|
|
|
|
}
|
|
|
|
|
2020-08-18 10:14:14 +00:00
|
|
|
retryChan, err := rcm.RefreshAuth(context.Background(), backoff, auth)
|
2019-12-04 17:22:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, retryChan)
|
|
|
|
assert.Equal(t, 19*time.Hour, wait)
|
|
|
|
|
2020-08-18 10:14:14 +00:00
|
|
|
token, err := rcm.ReconnectToken()
|
2019-12-04 17:22:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, []byte("jwt"), token)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRefreshAuthUnknown(t *testing.T) {
|
2020-08-18 10:14:14 +00:00
|
|
|
rcm := newReconnectCredentialManager(t.Name(), t.Name(), 4)
|
2019-12-04 17:22:08 +00:00
|
|
|
|
|
|
|
var wait time.Duration
|
2021-03-26 04:04:56 +00:00
|
|
|
retry.Clock.After = func(d time.Duration) <-chan time.Time {
|
2019-12-04 17:22:08 +00:00
|
|
|
wait = d
|
|
|
|
return time.After(d)
|
|
|
|
}
|
|
|
|
|
2021-03-26 04:04:56 +00:00
|
|
|
backoff := &retry.BackoffHandler{MaxRetries: 3}
|
2019-12-04 17:22:08 +00:00
|
|
|
auth := func(ctx context.Context, n int) (tunnelpogs.AuthOutcome, error) {
|
|
|
|
return tunnelpogs.NewAuthUnknown(errors.New("auth unknown"), 19), nil
|
|
|
|
}
|
|
|
|
|
2020-08-18 10:14:14 +00:00
|
|
|
retryChan, err := rcm.RefreshAuth(context.Background(), backoff, auth)
|
2019-12-04 17:22:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, retryChan)
|
|
|
|
assert.Equal(t, 19*time.Hour, wait)
|
|
|
|
|
2020-08-18 10:14:14 +00:00
|
|
|
token, err := rcm.ReconnectToken()
|
2019-12-04 17:22:08 +00:00
|
|
|
assert.Equal(t, errJWTUnset, err)
|
|
|
|
assert.Nil(t, token)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRefreshAuthFail(t *testing.T) {
|
2020-08-18 10:14:14 +00:00
|
|
|
rcm := newReconnectCredentialManager(t.Name(), t.Name(), 4)
|
2019-12-04 17:22:08 +00:00
|
|
|
|
2021-03-26 04:04:56 +00:00
|
|
|
backoff := &retry.BackoffHandler{MaxRetries: 3}
|
2019-12-04 17:22:08 +00:00
|
|
|
auth := func(ctx context.Context, n int) (tunnelpogs.AuthOutcome, error) {
|
|
|
|
return tunnelpogs.NewAuthFail(errors.New("auth fail")), nil
|
|
|
|
}
|
|
|
|
|
2020-08-18 10:14:14 +00:00
|
|
|
retryChan, err := rcm.RefreshAuth(context.Background(), backoff, auth)
|
2019-12-04 17:22:08 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Nil(t, retryChan)
|
|
|
|
|
2020-08-18 10:14:14 +00:00
|
|
|
token, err := rcm.ReconnectToken()
|
2019-12-04 17:22:08 +00:00
|
|
|
assert.Equal(t, errJWTUnset, err)
|
|
|
|
assert.Nil(t, token)
|
|
|
|
}
|