-
Notifications
You must be signed in to change notification settings - Fork 571
fix(common/redis): re-AUTH new pool connections with fresh Entra token #4360
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
Open
paulstadler-mesh
wants to merge
15
commits into
dapr:main
Choose a base branch
from
paulstadler-mesh:fix/redis-entraid-onconnect
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.
+381
−30
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e5e90bf
fix(common/redis): re-AUTH new pool connections with fresh Entra token
paulstadler-mesh 5fea4e4
Merge branch 'main' into fix/redis-entraid-onconnect
paulstadler-mesh efb7158
Merge branch 'main' into fix/redis-entraid-onconnect
paulstadler-mesh d6a9638
Merge branch 'main' into fix/redis-entraid-onconnect
paulstadler-mesh 0b50883
Merge branch 'main' into fix/redis-entraid-onconnect
paulstadler-mesh 3ac139f
fix(common/redis): address Copilot review on Entra ID OnConnect PR
paulstadler-mesh fb3ca02
Merge branch 'main' into fix/redis-entraid-onconnect
paulstadler-mesh 2e37043
refactor(common/redis): address second Copilot review pass on Entra ID
paulstadler-mesh a9126a0
Merge branch 'main' into fix/redis-entraid-onconnect
paulstadler-mesh daa9460
fix(common/redis): fail fast on empty oid claim in Entra token
paulstadler-mesh b069b3f
Merge upstream/main into fix/redis-entraid-onconnect
paulstadler-mesh 5a73ff7
Merge branch 'main' into fix/redis-entraid-onconnect
paulstadler-mesh 8eeeae2
refactor(common/redis): address Copilot review feedback
paulstadler-mesh 7b710fa
Merge branch 'main' into fix/redis-entraid-onconnect
paulstadler-mesh abbf7f1
Merge branch 'main' into fix/redis-entraid-onconnect
mikeee 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,208 @@ | ||
| /* | ||
| Copyright 2021 The Dapr Authors | ||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package redis | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/Azure/azure-sdk-for-go/sdk/azcore" | ||
| "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| v8 "github.com/go-redis/redis/v8" | ||
| v9 "github.com/redis/go-redis/v9" | ||
| ) | ||
|
|
||
| // fakeTokenCredential is a stub azcore.TokenCredential for exercising the | ||
| // Entra ID auth path without contacting Azure. | ||
| type fakeTokenCredential struct { | ||
| token string | ||
| err error | ||
| } | ||
|
|
||
| func (f fakeTokenCredential) GetToken(context.Context, policy.TokenRequestOptions) (azcore.AccessToken, error) { | ||
| if f.err != nil { | ||
| return azcore.AccessToken{}, f.err | ||
| } | ||
| return azcore.AccessToken{Token: f.token, ExpiresOn: time.Now().Add(time.Hour)}, nil | ||
| } | ||
|
|
||
| // entraIDSettings returns Settings wired as InitEntraIDCredential would leave | ||
| // them after a successful init, but with a stubbed credential. | ||
| func entraIDSettings(redisType string, failover bool) *Settings { | ||
| return &Settings{ | ||
| Host: "localhost:6379", | ||
| RedisType: redisType, | ||
| Failover: failover, | ||
| UseEntraID: true, | ||
| entraIDUsername: "00000000-0000-0000-0000-000000000000", | ||
| entraIDTokenCredential: fakeTokenCredential{token: "fresh-token"}, | ||
| } | ||
| } | ||
|
|
||
| // v8OnConnectAndPassword extracts the OnConnect callback and Password configured | ||
| // on the underlying go-redis v8 client, across the node/cluster variants. | ||
| func v8OnConnectAndPassword(t *testing.T, c RedisClient) (onConnect any, password string) { | ||
| t.Helper() | ||
| switch client := c.(v8Client).client.(type) { | ||
| case *v8.Client: | ||
| return client.Options().OnConnect, client.Options().Password | ||
| case *v8.ClusterClient: | ||
| return client.Options().OnConnect, client.Options().Password | ||
| default: | ||
| t.Fatalf("unexpected v8 client type %T", client) | ||
| return nil, "" | ||
| } | ||
| } | ||
|
|
||
| // v9OnConnectAndPassword extracts the OnConnect callback and Password configured | ||
| // on the underlying go-redis v9 client, across the node/cluster variants. | ||
| func v9OnConnectAndPassword(t *testing.T, c RedisClient) (onConnect any, password string) { | ||
| t.Helper() | ||
| switch client := c.(v9Client).client.(type) { | ||
| case *v9.Client: | ||
| return client.Options().OnConnect, client.Options().Password | ||
| case *v9.ClusterClient: | ||
| return client.Options().OnConnect, client.Options().Password | ||
| default: | ||
| t.Fatalf("unexpected v9 client type %T", client) | ||
| return nil, "" | ||
| } | ||
| } | ||
|
paulstadler-mesh marked this conversation as resolved.
|
||
|
|
||
| func TestEntraIDOnConnectWiring(t *testing.T) { | ||
| cases := []struct { | ||
| name string | ||
| redisType string | ||
| failover bool | ||
| newV8 func(*Settings) (RedisClient, error) | ||
| newV9 func(*Settings) (RedisClient, error) | ||
| }{ | ||
| {name: "node", redisType: NodeType, newV8: newV8Client, newV9: newV9Client}, | ||
| {name: "cluster", redisType: ClusterType, newV8: newV8Client, newV9: newV9Client}, | ||
| {name: "failover", redisType: NodeType, failover: true, newV8: newV8FailoverClient, newV9: newV9FailoverClient}, | ||
| {name: "failover-cluster", redisType: ClusterType, failover: true, newV8: newV8FailoverClient, newV9: newV9FailoverClient}, | ||
| } | ||
|
|
||
| for _, tc := range cases { | ||
| t.Run("v8/"+tc.name+"/entraID-enabled", func(t *testing.T) { | ||
| c, err := tc.newV8(entraIDSettings(tc.redisType, tc.failover)) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, c) | ||
| defer c.Close() | ||
|
paulstadler-mesh marked this conversation as resolved.
|
||
|
|
||
| onConnect, password := v8OnConnectAndPassword(t, c) | ||
| assert.NotNil(t, onConnect, "OnConnect must be wired when UseEntraID is true") | ||
| assert.Empty(t, password, "static Password must not be set when UseEntraID is true") | ||
| }) | ||
|
|
||
| t.Run("v8/"+tc.name+"/entraID-disabled", func(t *testing.T) { | ||
| s := entraIDSettings(tc.redisType, tc.failover) | ||
| s.UseEntraID = false | ||
| c, err := tc.newV8(s) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, c) | ||
| defer c.Close() | ||
|
|
||
| onConnect, _ := v8OnConnectAndPassword(t, c) | ||
| assert.Nil(t, onConnect, "OnConnect must not be wired when UseEntraID is false") | ||
| }) | ||
|
|
||
| t.Run("v9/"+tc.name+"/entraID-enabled", func(t *testing.T) { | ||
| c, err := tc.newV9(entraIDSettings(tc.redisType, tc.failover)) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, c) | ||
| defer c.Close() | ||
|
|
||
| onConnect, password := v9OnConnectAndPassword(t, c) | ||
| assert.NotNil(t, onConnect, "OnConnect must be wired when UseEntraID is true") | ||
| assert.Empty(t, password, "static Password must not be set when UseEntraID is true") | ||
| }) | ||
|
|
||
| t.Run("v9/"+tc.name+"/entraID-disabled", func(t *testing.T) { | ||
| s := entraIDSettings(tc.redisType, tc.failover) | ||
| s.UseEntraID = false | ||
| c, err := tc.newV9(s) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, c) | ||
| defer c.Close() | ||
|
|
||
| onConnect, _ := v9OnConnectAndPassword(t, c) | ||
| assert.Nil(t, onConnect, "OnConnect must not be wired when UseEntraID is false") | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestEntraIDFetchAuthArgs(t *testing.T) { | ||
| t.Run("returns username and fresh token", func(t *testing.T) { | ||
| s := entraIDSettings(NodeType, false) | ||
| user, pass, err := s.entraIDFetchAuthArgs(context.Background()) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, "00000000-0000-0000-0000-000000000000", user) | ||
| assert.Equal(t, "fresh-token", pass) | ||
| }) | ||
|
|
||
| t.Run("errors when credential not initialized", func(t *testing.T) { | ||
| s := &Settings{UseEntraID: true, entraIDUsername: "oid"} | ||
| _, _, err := s.entraIDFetchAuthArgs(context.Background()) | ||
| require.Error(t, err) | ||
| }) | ||
|
|
||
| t.Run("errors when username (OID) not initialized", func(t *testing.T) { | ||
| s := &Settings{UseEntraID: true, entraIDTokenCredential: fakeTokenCredential{token: "fresh-token"}} | ||
| _, _, err := s.entraIDFetchAuthArgs(context.Background()) | ||
| require.Error(t, err) | ||
| }) | ||
|
|
||
| t.Run("propagates token acquisition error", func(t *testing.T) { | ||
| s := &Settings{UseEntraID: true, entraIDUsername: "oid", entraIDTokenCredential: fakeTokenCredential{err: errors.New("boom")}} | ||
| _, _, err := s.entraIDFetchAuthArgs(context.Background()) | ||
| require.Error(t, err) | ||
| }) | ||
| } | ||
|
|
||
| // TestEntraIDOnConnectCallback invokes the OnConnect callbacks themselves (not just | ||
| // asserting they are wired) to confirm they drive authentication through a fresh-token | ||
| // fetch and surface a credential failure as a dial error. A failing credential makes the | ||
| // callback return before it touches the (nil) connection, so this exercises the | ||
| // connect-time auth path end-to-end without a live Redis server. | ||
| func TestEntraIDOnConnectCallback(t *testing.T) { | ||
| t.Run("v8 surfaces token-fetch failure as a dial error", func(t *testing.T) { | ||
| s := &Settings{UseEntraID: true, entraIDUsername: "oid", entraIDTokenCredential: fakeTokenCredential{err: errors.New("boom")}} | ||
| err := entraIDOnConnectV8(s)(context.Background(), nil) | ||
| require.Error(t, err) | ||
| }) | ||
|
|
||
| t.Run("v9 surfaces token-fetch failure as a dial error", func(t *testing.T) { | ||
| s := &Settings{UseEntraID: true, entraIDUsername: "oid", entraIDTokenCredential: fakeTokenCredential{err: errors.New("boom")}} | ||
| err := entraIDOnConnectV9(s)(context.Background(), nil) | ||
| require.Error(t, err) | ||
| }) | ||
|
|
||
| t.Run("v8 surfaces missing-OID as a dial error", func(t *testing.T) { | ||
| s := &Settings{UseEntraID: true, entraIDTokenCredential: fakeTokenCredential{token: "fresh-token"}} | ||
| err := entraIDOnConnectV8(s)(context.Background(), nil) | ||
| require.Error(t, err) | ||
| }) | ||
|
|
||
| t.Run("v9 surfaces missing-OID as a dial error", func(t *testing.T) { | ||
| s := &Settings{UseEntraID: true, entraIDTokenCredential: fakeTokenCredential{token: "fresh-token"}} | ||
| err := entraIDOnConnectV9(s)(context.Background(), nil) | ||
| require.Error(t, err) | ||
| }) | ||
| } | ||
|
paulstadler-mesh marked this conversation as resolved.
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.