-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[client] Don't ask for an SSO login when the login never reached management #6983
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
lixmal
wants to merge
2
commits into
main
Choose a base branch
from
worktree-daemon-login-transport-error
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.
+122
−4
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,89 @@ | ||
| package server | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| "google.golang.org/grpc/codes" | ||
| gstatus "google.golang.org/grpc/status" | ||
|
|
||
| "github.com/netbirdio/netbird/client/internal" | ||
| "github.com/netbirdio/netbird/client/proto" | ||
| ) | ||
|
|
||
| // A login that never reached Management is not a decision about the peer's | ||
| // credentials, so it must come back as a retryable error rather than an SSO | ||
| // prompt: the user cannot finish a browser login while Management is down, and | ||
| // the CLI's own backoff resolves the outage on its own once the daemon reports | ||
| // the failure. Reproduces `netbird down; netbird up` printing a device-code URL | ||
| // because Management happened to be restarting when the daemon dialed it. | ||
| func TestLogin_ManagementUnreachableIsReturnedInsteadOfDemandingSSO(t *testing.T) { | ||
| s, _, _, username, _ := setupServerWithProfile(t) | ||
| s.rootCtx = internal.CtxInitState(context.Background()) | ||
|
|
||
| unreachable := errors.New("create connection: dial context: context deadline exceeded") | ||
| attempts := 0 | ||
| s.loginAttemptFn = func(context.Context, string, string) (internal.StatusType, error) { | ||
| attempts++ | ||
| return internal.StatusLoginFailed, unreachable | ||
| } | ||
|
|
||
| resp, err := s.Login(userCtx(), &proto.LoginRequest{Username: &username}) | ||
| require.Error(t, err) | ||
| require.ErrorIs(t, err, unreachable, "the transport failure was replaced by something else") | ||
| require.Nil(t, resp, "a failed login must not answer with a login response") | ||
| require.Equal(t, 1, attempts) | ||
| require.Nil(t, s.oauthAuthFlow.flow, "the daemon started an SSO flow for a peer whose login was never decided") | ||
|
|
||
| status, err := internal.CtxGetState(s.rootCtx).Status() | ||
| require.NoError(t, err) | ||
| require.Equal(t, internal.StatusLoginFailed, status, | ||
| "a peer that could not reach Management is not waiting on a login") | ||
| } | ||
|
|
||
| // The counterpart: Management refusing the peer's credentials is a decision, and | ||
| // the SSO flow still has to start for it. The profile carries an unusable | ||
| // private key so the flow setup fails immediately instead of dialing, which is | ||
| // enough to show the branch was entered — the refusal itself is never what comes | ||
| // back out. | ||
| func TestLogin_AuthRefusalStartsSSOFlow(t *testing.T) { | ||
| s, _, _, username, cfgPath := setupServerWithProfile(t) | ||
| s.rootCtx = internal.CtxInitState(context.Background()) | ||
| breakProfilePrivateKey(t, cfgPath) | ||
|
|
||
| refused := gstatus.Error(codes.PermissionDenied, "peer is not registered") | ||
| s.loginAttemptFn = func(context.Context, string, string) (internal.StatusType, error) { | ||
| return internal.StatusNeedsLogin, refused | ||
| } | ||
|
|
||
| _, err := s.Login(userCtx(), &proto.LoginRequest{Username: &username}) | ||
| require.Error(t, err) | ||
| require.NotErrorIs(t, err, refused, | ||
| "the refusal was handed back to the caller instead of starting the SSO flow") | ||
|
|
||
| status, stateErr := internal.CtxGetState(s.rootCtx).Status() | ||
| require.NoError(t, stateErr) | ||
| require.Equal(t, internal.StatusLoginFailed, status, | ||
| "the SSO flow setup was never reached with the broken key") | ||
| } | ||
|
|
||
| // breakProfilePrivateKey replaces the profile's private key with an unparseable | ||
| // one, which makes any attempt to build a Management client fail on the spot. | ||
| func breakProfilePrivateKey(t *testing.T, cfgPath string) { | ||
| t.Helper() | ||
|
|
||
| raw, err := os.ReadFile(cfgPath) | ||
| require.NoError(t, err) | ||
|
|
||
| var cfg map[string]any | ||
| require.NoError(t, json.Unmarshal(raw, &cfg)) | ||
| cfg["PrivateKey"] = "not-a-key" | ||
|
|
||
| patched, err := json.Marshal(cfg) | ||
| require.NoError(t, err) | ||
| require.NoError(t, os.WriteFile(cfgPath, patched, 0o600)) | ||
| } | ||
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.