Skip to content

Commit

Permalink
Merge pull request #5065 from sensu/etcdCleanup
Browse files Browse the repository at this point in the history
Etcd cleanup
  • Loading branch information
ManishaKumari295 authored Oct 15, 2024
2 parents 1856562 + 9ab69a6 commit 613fc29
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
6 changes: 3 additions & 3 deletions backend/authentication/jwt/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const (
)

var (
defaultAccessTokenLifespan = 5 * time.Minute
DefaultAccessTokenLifespan = 5 * time.Minute
defaultRefreshTokenLifespan = 12 * time.Hour
secret []byte
privateKey *ecdsa.PrivateKey
Expand Down Expand Up @@ -58,7 +58,7 @@ func AccessToken(claims *corev2.Claims) (*jwt.Token, string, error) {
claims.Id = jti

// Add an expiration to the token
claims.ExpiresAt = time.Now().Add(defaultAccessTokenLifespan).Unix()
claims.ExpiresAt = time.Now().Add(DefaultAccessTokenLifespan).Unix()

token := jwt.NewWithClaims(signingMethod, claims)

Expand Down Expand Up @@ -91,7 +91,7 @@ func NewClaims(user *corev2.User) (*corev2.Claims, error) {
// library's documentation. We should replace its usage with
// RegisteredClaims.
StandardClaims: jwt.StandardClaims{
ExpiresAt: time.Now().Add(defaultAccessTokenLifespan).Unix(),
ExpiresAt: time.Now().Add(DefaultAccessTokenLifespan).Unix(),
Id: jti,
Subject: user.Username,
},
Expand Down
6 changes: 3 additions & 3 deletions backend/authentication/jwt/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestValidateTokenError(t *testing.T) {
assert.NoError(t, err)

// The token should expire after the expiration time
testTime.Set(time.Now().Add(defaultAccessTokenLifespan + time.Hour))
testTime.Set(time.Now().Add(DefaultAccessTokenLifespan + time.Hour))
_, err = ValidateToken(tokenString)
assert.Error(t, err)
}
Expand All @@ -134,7 +134,7 @@ func TestValidateExpiredToken(t *testing.T) {
_, tokenString, _ := AccessToken(claims)

// Wait for the token to expire
testTime.Set(time.Now().Add(defaultAccessTokenLifespan + time.Second))
testTime.Set(time.Now().Add(DefaultAccessTokenLifespan + time.Second))
_, err := ValidateExpiredToken(tokenString)
assert.NoError(t, err, "An expired token should not be considered as invalid")
}
Expand All @@ -158,7 +158,7 @@ func TestValidateExpiredTokenInvalid(t *testing.T) {
_, tokenString, _ := AccessToken(claims)

// The token will expire
testTime.Set(time.Now().Add(defaultAccessTokenLifespan + time.Second))
testTime.Set(time.Now().Add(DefaultAccessTokenLifespan + time.Second))

// Modify the secret so it's no longer valid
secret = []byte("qux")
Expand Down
15 changes: 10 additions & 5 deletions backend/store/etcd/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package etcd
import (
"context"
"fmt"

"github.com/sensu/sensu-go/backend/authentication/jwt"
"github.com/sensu/sensu-go/backend/store"
"go.etcd.io/etcd/client/v3"
)
Expand All @@ -30,12 +30,18 @@ func (s *Store) GetSession(ctx context.Context, username, sessionID string) (str
}

// UpdateSession applies the supplied state to the session uniquely identified
// by the given username and session ID.
// by the given username and session ID with attached lease and TTl for each key
func (s *Store) UpdateSession(ctx context.Context, username, sessionID, state string) error {
if _, err := s.client.Put(ctx, userSessionPath(username, sessionID), state); err != nil {

leaseDuration := jwt.DefaultAccessTokenLifespan
ttl := int64(leaseDuration.Minutes()+1) * 60
leaseResp, err := s.client.Grant(ctx, ttl)
if err != nil {
return fmt.Errorf("%s", err)
}
if _, err := s.client.Put(ctx, userSessionPath(username, sessionID), state, clientv3.WithLease(leaseResp.ID)); err != nil {
return err
}

return nil
}

Expand All @@ -45,6 +51,5 @@ func (s *Store) DeleteSession(ctx context.Context, username, sessionID string) e
if _, err := s.client.Delete(ctx, userSessionPath(username, sessionID)); err != nil {
return err
}

return nil
}

0 comments on commit 613fc29

Please sign in to comment.