Skip to content

Commit 55e9220

Browse files
authored
[Bugfix] Fix Integration Service (#1614)
1 parent dc43263 commit 55e9220

File tree

5 files changed

+74
-8
lines changed

5 files changed

+74
-8
lines changed

integrations/authentication/v1/cache.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type cache struct {
4343
validationTokens [][]byte
4444
}
4545

46-
func (i *implementation) newCache() (*cache, error) {
46+
func (i *implementation) newCache(cfg Configuration) (*cache, error) {
4747
files, err := os.ReadDir(i.cfg.Path)
4848
if err != nil {
4949
return nil, err
@@ -66,7 +66,7 @@ func (i *implementation) newCache() (*cache, error) {
6666
continue
6767
}
6868

69-
buff := make([]byte, 32)
69+
buff := make([]byte, cfg.Create.MaxSize)
7070

7171
for id := range buff {
7272
buff[id] = 0
@@ -133,7 +133,7 @@ func (i *implementation) refreshCache() (*cache, error) {
133133

134134
// Get was not successful, retry
135135

136-
if c, err := i.newCache(); err != nil {
136+
if c, err := i.newCache(i.cfg); err != nil {
137137
return nil, err
138138
} else if c == nil {
139139
return nil, errors.Errorf("cache returned is nil")

integrations/authentication/v1/configuration.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ const (
3737
DefaultTokenMinTTL = time.Minute
3838
DefaultTokenMaxTTL = time.Hour
3939
DefaultTokenDefaultTTL = time.Hour
40+
41+
DefaultMaxTokenSize = 64
4042
)
4143

4244
type Mod func(c Configuration) Configuration
@@ -52,6 +54,7 @@ func NewConfiguration() Configuration {
5254
MinTTL: DefaultTokenMinTTL,
5355
MaxTTL: DefaultTokenMaxTTL,
5456
DefaultTTL: DefaultTokenDefaultTTL,
57+
MaxSize: DefaultMaxTokenSize,
5558
},
5659
}
5760
}
@@ -82,7 +85,7 @@ func (c Configuration) Validate() error {
8285
}
8386

8487
if c.TTL < 0 {
85-
return errors.Errorf("TTLS should be not negative")
88+
return errors.Errorf("TLS should be not negative")
8689
}
8790

8891
if err := c.Create.Validate(); err != nil {
@@ -98,6 +101,8 @@ type Token struct {
98101
AllowedUsers []string
99102

100103
MinTTL, MaxTTL, DefaultTTL time.Duration
104+
105+
MaxSize uint16
101106
}
102107

103108
func (t Token) Validate() error {
@@ -117,6 +122,10 @@ func (t Token) Validate() error {
117122
return errors.Errorf("DefautTTL Cannot be higher than MaxTTL")
118123
}
119124

125+
if t.MaxSize <= 0 {
126+
return errors.Errorf("MaxSize cannot be less or equal 0")
127+
}
128+
120129
if len(t.AllowedUsers) > 0 {
121130
// We are enforcing allowed users
122131

integrations/authentication/v1/implementation.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,13 @@ func (i *implementation) CreateToken(ctx context.Context, request *pbAuthenticat
165165
return nil, err
166166
}
167167

168-
user, exp, err := i.extractTokenDetails(cache, signedToken)
168+
user, _, err = i.extractTokenDetails(cache, signedToken)
169169
if err != nil {
170170
return nil, err
171171
}
172172

173173
return &pbAuthenticationV1.CreateTokenResponse{
174-
Lifetime: durationpb.New(exp),
174+
Lifetime: durationpb.New(duration),
175175
User: user,
176176
Token: signedToken,
177177
}, nil
@@ -195,8 +195,11 @@ func (i *implementation) extractTokenDetails(cache *cache, t string) (string, ti
195195
duration := DefaultTokenMaxTTL
196196

197197
if v, ok := p[token.ClaimEXP]; ok {
198-
if s, ok := v.(int64); ok {
199-
duration = time.Until(time.Unix(s, 0))
198+
switch o := v.(type) {
199+
case int64:
200+
duration = time.Until(time.Unix(o, 0))
201+
case float64:
202+
duration = time.Until(time.Unix(int64(o), 0))
200203
}
201204
}
202205

integrations/authentication/v1/service_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ package v1
2323
import (
2424
"context"
2525
"testing"
26+
"time"
2627

2728
"github.com/stretchr/testify/require"
29+
"google.golang.org/protobuf/types/known/durationpb"
2830

2931
pbAuthenticationV1 "github.com/arangodb/kube-arangodb/integrations/authentication/v1/definition"
3032
"github.com/arangodb/kube-arangodb/pkg/util"
@@ -189,3 +191,54 @@ func Test_Service_AskForDefaultIfBlocked(t *testing.T) {
189191
})
190192
require.EqualError(t, err, "rpc error: code = Unknown desc = User blocked is not allowed")
191193
}
194+
195+
func Test_Service_WithTTL(t *testing.T) {
196+
ctx, c := context.WithCancel(context.Background())
197+
defer c()
198+
199+
client, directory := Client(t, ctx)
200+
201+
reSaveJWTTokens(t, directory, generateJWTToken())
202+
203+
extract := func(t *testing.T, duration time.Duration) (time.Duration, time.Duration) {
204+
token, err := client.CreateToken(ctx, &pbAuthenticationV1.CreateTokenRequest{
205+
Lifetime: durationpb.New(duration),
206+
})
207+
require.NoError(t, err)
208+
209+
valid, err := client.Validate(ctx, &pbAuthenticationV1.ValidateRequest{
210+
Token: token.Token,
211+
})
212+
require.NoError(t, err)
213+
214+
require.NotNil(t, token.Lifetime)
215+
require.True(t, valid.IsValid)
216+
require.NotNil(t, valid.Details)
217+
218+
return token.Lifetime.AsDuration(), valid.Details.Lifetime.AsDuration()
219+
}
220+
221+
t.Run("10h", func(t *testing.T) {
222+
base, current := extract(t, 10*time.Hour)
223+
require.EqualValues(t, time.Hour, base)
224+
require.True(t, base-time.Second < current)
225+
})
226+
227+
t.Run("1h", func(t *testing.T) {
228+
base, current := extract(t, time.Hour)
229+
require.EqualValues(t, time.Hour, base)
230+
require.True(t, base-time.Second < current)
231+
})
232+
233+
t.Run("1min", func(t *testing.T) {
234+
base, current := extract(t, time.Minute)
235+
require.EqualValues(t, time.Minute, base)
236+
require.True(t, base-time.Second < current)
237+
})
238+
239+
t.Run("1sec", func(t *testing.T) {
240+
base, current := extract(t, time.Second)
241+
require.EqualValues(t, time.Minute, base)
242+
require.True(t, base-time.Second < current)
243+
})
244+
}

pkg/integrations/authentication_v1.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func (a *authenticationV1) Register(cmd *cobra.Command, arg ArgGen) error {
5050
f.DurationVar(&a.config.Create.DefaultTTL, arg("token.ttl.default"), pbImplAuthenticationV1.DefaultTokenDefaultTTL, "Default Token TTL")
5151
f.DurationVar(&a.config.Create.MinTTL, arg("token.ttl.min"), pbImplAuthenticationV1.DefaultTokenMinTTL, "Min Token TTL")
5252
f.DurationVar(&a.config.Create.MaxTTL, arg("token.ttl.max"), pbImplAuthenticationV1.DefaultTokenMaxTTL, "Max Token TTL")
53+
f.Uint16Var(&a.config.Create.MaxSize, arg("token.max-size"), pbImplAuthenticationV1.DefaultMaxTokenSize, "Max Token max size in bytes")
5354
f.StringSliceVar(&a.config.Create.AllowedUsers, arg("token.allowed"), []string{}, "Allowed users for the Token")
5455

5556
return nil

0 commit comments

Comments
 (0)