-
Notifications
You must be signed in to change notification settings - Fork 0
Use only valkey go #202
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
Draft
majst01
wants to merge
12
commits into
main
Choose a base branch
from
use-only-valkey-go
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Use only valkey go #202
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
78dc391
token-store
majst01 161cbaf
only asynq left
majst01 5b44d8e
Test fixes
majst01 4b09e91
Test fixes
majst01 3aac779
Fix test
majst01 f055c12
Reuse test helper
majst01 a432369
reduce byte slice casting
majst01 3465503
Add token benchmark
majst01 0ef1712
Try to stabilize test
majst01 59e5bf5
Merge main
majst01 c27d6b0
Merge branch 'main' of https://github.com/metal-stack/metal-apiserver…
majst01 00e149c
Merge branch 'main' of https://github.com/metal-stack/metal-apiserver…
majst01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,20 +5,19 @@ import ( | |
| "fmt" | ||
| "log/slog" | ||
| "testing" | ||
| "testing/synctest" | ||
| "time" | ||
|
|
||
| "github.com/alicebob/miniredis/v2" | ||
| "github.com/lestrrat-go/jwx/v3/jwk" | ||
| "github.com/metal-stack/metal-apiserver/pkg/certs" | ||
| tokenservice "github.com/metal-stack/metal-apiserver/pkg/service/api/token" | ||
| "github.com/metal-stack/metal-apiserver/pkg/token" | ||
| "github.com/redis/go-redis/v9" | ||
| "github.com/stretchr/testify/require" | ||
| "github.com/valkey-io/valkey-go" | ||
| ) | ||
|
|
||
| func Test_jwt_cert_rotation(t *testing.T) { | ||
| t.Parallel() | ||
| // t.Parallel() | ||
| oldMaxExpiration := token.MaxExpiration | ||
| oldDefaultExpiration := token.DefaultExpiration | ||
|
|
||
|
|
@@ -33,11 +32,15 @@ func Test_jwt_cert_rotation(t *testing.T) { | |
| t.Logf("token lifetime: %s, certificate lifetime: %s, issue new signing certificate after: %s", token.DefaultExpiration, 2*token.MaxExpiration, 2*token.MaxExpiration-renewCertBeforeExpiration) | ||
|
|
||
| s := miniredis.RunT(t) | ||
| c := redis.NewClient(&redis.Options{Addr: s.Addr()}) | ||
| c, err := valkey.NewClient(valkey.ClientOption{ | ||
| InitAddress: []string{s.Addr()}, | ||
| DisableCache: true, | ||
| }) | ||
| require.NoError(t, err) | ||
| log := slog.Default() | ||
|
|
||
| certStore := certs.NewRedisStore(&certs.Config{ | ||
| RedisClient: c, | ||
| ValkeyClient: c, | ||
| RenewCertBeforeExpiration: &renewCertBeforeExpiration, | ||
| }) | ||
| tokenStore := token.NewRedisStore(c) | ||
|
|
@@ -66,111 +69,111 @@ func Test_jwt_cert_rotation(t *testing.T) { | |
| return s | ||
| }() | ||
|
|
||
| synctest.Test(t, func(t *testing.T) { | ||
|
|
||
| ctx := t.Context() | ||
|
|
||
| var ( | ||
| token1 = "" | ||
| token2 = "" | ||
| token3 = "" | ||
| previousAt *time.Duration | ||
| ) | ||
| steps := []struct { | ||
| name string | ||
| at time.Duration | ||
| task func(t *testing.T) | ||
| }{ | ||
| { | ||
| name: "token 1", | ||
| at: 0 * time.Second, | ||
| task: func(t *testing.T) { | ||
| token1 = createNewConsoleToken(t, ctx, service) | ||
| expectCertStore(t, ctx, certStore, 1) | ||
| expectTokenWorks(t, ctx, auth, token1) | ||
| }, | ||
| // synctest.Test(t, func(t *testing.T) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. valkey-go has its own background worker which breaks synctest.Test |
||
|
|
||
| ctx := t.Context() | ||
|
|
||
| var ( | ||
| token1 = "" | ||
| token2 = "" | ||
| token3 = "" | ||
| previousAt *time.Duration | ||
| ) | ||
| steps := []struct { | ||
| name string | ||
| at time.Duration | ||
| task func(t *testing.T) | ||
| }{ | ||
| { | ||
| name: "token 1", | ||
| at: 0 * time.Second, | ||
| task: func(t *testing.T) { | ||
| token1 = createNewConsoleToken(t, ctx, service) | ||
| expectCertStore(t, ctx, certStore, 1) | ||
| expectTokenWorks(t, ctx, auth, token1) | ||
| }, | ||
| { | ||
| name: "token2", | ||
| at: 2 * time.Second, | ||
| task: func(t *testing.T) { | ||
| token2 = createNewConsoleToken(t, ctx, service) | ||
| expectCertStore(t, ctx, certStore, 1) | ||
| expectTokenWorks(t, ctx, auth, token1) | ||
| expectTokenWorks(t, ctx, auth, token2) | ||
| }, | ||
| }, | ||
| { | ||
| name: "token2", | ||
| at: 2 * time.Second, | ||
| task: func(t *testing.T) { | ||
| token2 = createNewConsoleToken(t, ctx, service) | ||
| expectCertStore(t, ctx, certStore, 1) | ||
| expectTokenWorks(t, ctx, auth, token1) | ||
| expectTokenWorks(t, ctx, auth, token2) | ||
| }, | ||
| { | ||
| name: "token3, next signing cert gets created", | ||
| at: 4 * time.Second, | ||
| task: func(t *testing.T) { | ||
| token3 = createNewConsoleToken(t, ctx, service) | ||
| expectCertStore(t, ctx, certStore, 2) | ||
| expectTokenWorks(t, ctx, auth, token1) | ||
| expectTokenWorks(t, ctx, auth, token2) | ||
| expectTokenWorks(t, ctx, auth, token3) | ||
| }, | ||
| }, | ||
| { | ||
| name: "token3, next signing cert gets created", | ||
| at: 4 * time.Second, | ||
| task: func(t *testing.T) { | ||
| token3 = createNewConsoleToken(t, ctx, service) | ||
| expectCertStore(t, ctx, certStore, 2) | ||
| expectTokenWorks(t, ctx, auth, token1) | ||
| expectTokenWorks(t, ctx, auth, token2) | ||
| expectTokenWorks(t, ctx, auth, token3) | ||
| }, | ||
| { | ||
| name: "token1 expired, token 2 and 3 still work", | ||
| at: 6 * time.Second, | ||
| task: func(t *testing.T) { | ||
| token3 = createNewConsoleToken(t, ctx, service) | ||
| expectCertStore(t, ctx, certStore, 2) | ||
| expectTokenExpired(t, ctx, auth, token1) | ||
| expectTokenWorks(t, ctx, auth, token2) | ||
| expectTokenWorks(t, ctx, auth, token3) | ||
| }, | ||
| }, | ||
| { | ||
| name: "token1 expired, token 2 and 3 still work", | ||
| at: 6 * time.Second, | ||
| task: func(t *testing.T) { | ||
| token3 = createNewConsoleToken(t, ctx, service) | ||
| expectCertStore(t, ctx, certStore, 2) | ||
| expectTokenExpired(t, ctx, auth, token1) | ||
| expectTokenWorks(t, ctx, auth, token2) | ||
| expectTokenWorks(t, ctx, auth, token3) | ||
| }, | ||
| { | ||
| name: "token1 and token2 expired, token 3 still works", | ||
| at: 8 * time.Second, | ||
| task: func(t *testing.T) { | ||
| expectCertStore(t, ctx, certStore, 2) | ||
| expectTokenExpired(t, ctx, auth, token1) | ||
| expectTokenExpired(t, ctx, auth, token2) | ||
| expectTokenWorks(t, ctx, auth, token3) | ||
| }, | ||
| }, | ||
| { | ||
| name: "token1 and token2 expired, token 3 still works", | ||
| at: 8 * time.Second, | ||
| task: func(t *testing.T) { | ||
| expectCertStore(t, ctx, certStore, 2) | ||
| expectTokenExpired(t, ctx, auth, token1) | ||
| expectTokenExpired(t, ctx, auth, token2) | ||
| expectTokenWorks(t, ctx, auth, token3) | ||
| }, | ||
| { | ||
| name: "all tokens expired, first signing cert is gone", | ||
| at: 11 * time.Second, | ||
| task: func(t *testing.T) { | ||
| expectCertStore(t, ctx, certStore, 1) | ||
| expectTokenNoPublicKeyForSignatureFound(t, ctx, auth, token1) | ||
| expectTokenNoPublicKeyForSignatureFound(t, ctx, auth, token2) | ||
| expectTokenExpired(t, ctx, auth, token3) | ||
| }, | ||
| }, | ||
| { | ||
| name: "all tokens expired, first signing cert is gone", | ||
| at: 11 * time.Second, | ||
| task: func(t *testing.T) { | ||
| expectCertStore(t, ctx, certStore, 1) | ||
| expectTokenNoPublicKeyForSignatureFound(t, ctx, auth, token1) | ||
| expectTokenNoPublicKeyForSignatureFound(t, ctx, auth, token2) | ||
| expectTokenExpired(t, ctx, auth, token3) | ||
| }, | ||
| { | ||
| name: "all tokens expired, all signing certs gone", | ||
| at: 15 * time.Second, | ||
| task: func(t *testing.T) { | ||
| expectCertStore(t, ctx, certStore, 0) | ||
| expectTokenNoPublicKeyForSignatureFound(t, ctx, auth, token1) | ||
| expectTokenNoPublicKeyForSignatureFound(t, ctx, auth, token2) | ||
| expectTokenNoPublicKeyForSignatureFound(t, ctx, auth, token3) | ||
| }, | ||
| }, | ||
| { | ||
| name: "all tokens expired, all signing certs gone", | ||
| at: 15 * time.Second, | ||
| task: func(t *testing.T) { | ||
| expectCertStore(t, ctx, certStore, 0) | ||
| expectTokenNoPublicKeyForSignatureFound(t, ctx, auth, token1) | ||
| expectTokenNoPublicKeyForSignatureFound(t, ctx, auth, token2) | ||
| expectTokenNoPublicKeyForSignatureFound(t, ctx, auth, token3) | ||
| }, | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| time.Sleep(1 * time.Second) | ||
| time.Sleep(1 * time.Second) | ||
|
|
||
| for _, step := range steps { | ||
| forwardText := "" | ||
| if previousAt != nil { | ||
| forward := step.at - *previousAt | ||
| forwardText = fmt.Sprintf(" (forwarding by %s)", forward) | ||
| time.Sleep(forward) | ||
| s.FastForward(forward) | ||
| } | ||
| previousAt = &step.at | ||
| for _, step := range steps { | ||
| forwardText := "" | ||
| if previousAt != nil { | ||
| forward := step.at - *previousAt | ||
| forwardText = fmt.Sprintf(" (forwarding by %s)", forward) | ||
| time.Sleep(forward) | ||
| s.FastForward(forward) | ||
| } | ||
| previousAt = &step.at | ||
|
|
||
| t.Logf("%s: running step at %q%s: %q", time.Now(), step.at, forwardText, step.name) | ||
| t.Logf("%s: running step at %q%s: %q", time.Now(), step.at, forwardText, step.name) | ||
|
|
||
| step.task(t) | ||
| } | ||
| }) | ||
| step.task(t) | ||
| } | ||
| // }) | ||
| } | ||
|
|
||
| func createNewConsoleToken(t *testing.T, ctx context.Context, service tokenservice.TokenService) string { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either remove or comment in