Skip to content

Commit

Permalink
Merge branch 'master' into PWX-36514
Browse files Browse the repository at this point in the history
  • Loading branch information
svijaykumar-px authored Jul 24, 2024
2 parents 51a888f + 5b34b43 commit 803b401
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions drivers/storage/portworx/components_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17662,6 +17662,8 @@ func validateTokenLifetime(t *testing.T, cluster *corev1.StorageCluster, jwtClai
iatTime := time.Unix(int64(iatFloat64), 0)
expTime := time.Unix(int64(expFloat64), 0)
tokenLifetime := expTime.Sub(iatTime)
// subtract 10 minutes from tokenLifetime to adjust for the iat time creation for ntp sync
tokenLifetime = tokenLifetime - 10*time.Minute
duration, err := pxutil.ParseExtendedDuration(*cluster.Spec.Security.Auth.SelfSigned.TokenLifetime)
require.NoError(t, err)
require.Equal(t, tokenLifetime, duration)
Expand Down
2 changes: 2 additions & 0 deletions drivers/storage/portworx/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,8 @@ func GenerateToken(
token, err := auth.Token(claims, signature, &auth.Options{
Expiration: time.Now().
Add(duration).Unix(),
// set IAT to 10 minutes in the past to avoid clock skew issues
IATSubtract: 10 * time.Minute,
})
if err != nil {
return "", err
Expand Down
54 changes: 54 additions & 0 deletions drivers/storage/portworx/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package util
import (
"encoding/json"
"testing"
"time"

"github.com/golang-jwt/jwt/v4"
version "github.com/hashicorp/go-version"
"github.com/libopenstorage/openstorage/pkg/auth"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -744,3 +747,54 @@ func TestIsVersionSupported(t *testing.T) {
supported = isVersionSupported("4.16.0", "4.15.0")
require.True(t, supported)
}

func TestGenerateToken(t *testing.T) {
// setup
cluster := &corev1.StorageCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "testcluster",
Namespace: "ns",
},
Spec: corev1.StorageClusterSpec{
Security: &corev1.SecuritySpec{
Enabled: true,
},
},
}

inputClaims := &auth.Claims{
Issuer: "testissuer",
Subject: "test-subject",
Name: "test-name",
Email: "test-email",
Roles: nil,
Groups: []string{"*"},
}

secretKey := "dummy-secret"
currentTime := time.Now()

tokenStr, err := GenerateToken(cluster, secretKey, inputClaims, 24*time.Hour)
require.NoError(t, err)

// define the Keyfunc
keyFunc := func(token *jwt.Token) (interface{}, error) {
return []byte(secretKey), nil
}

// parse the jwt token to get the issued time
token, err := jwt.Parse(tokenStr, keyFunc)
require.NoError(t, err)

claims, ok := token.Claims.(jwt.MapClaims)
require.True(t, ok)
// get issued time from token
iat, ok := claims["iat"].(float64)
require.True(t, ok)
iatTime := time.Unix(int64(iat), 0)

// check time difference between issued time and current time
// get time difference in minutes truncating milliseconds
timeDifferenceInMinutes := currentTime.Sub(iatTime).Truncate(time.Minute).Minutes()
require.Equal(t, float64(10), timeDifferenceInMinutes)
}

0 comments on commit 803b401

Please sign in to comment.