Skip to content

Commit b1a8bdf

Browse files
Revert "Use a free port in u2m authentication flows rather than 8020. (#1239)"
1 parent b6d5a08 commit b1a8bdf

2 files changed

Lines changed: 10 additions & 69 deletions

File tree

credentials/u2m/persistent_auth.go

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ const (
2626
appClientID = "databricks-cli"
2727

2828
// appRedirectAddr is the default address for the OAuth2 callback server.
29-
// Using ":0" tells the system to pick a random available port.
30-
appRedirectAddr = "localhost:0"
29+
appRedirectAddr = "localhost:8020"
3130

3231
// listenerTimeout is the maximum amount of time to acquire listener on
3332
// appRedirectAddr.
@@ -58,11 +57,6 @@ type PersistentAuth struct {
5857
// ctx is the context to use for underlying operations. This is needed in
5958
// order to implement the oauth2.TokenSource interface.
6059
ctx context.Context
61-
// redirectAddr is the redirect address for OAuth2 callbacks. The value is
62-
// set to localhost:PORT by startListener which will dynamically assign a
63-
// random port. If a value is already provided, it will be used instead
64-
// (e.g. for testing).
65-
redirectAddr string
6660
}
6761

6862
type PersistentAuthOption func(*PersistentAuth)
@@ -134,6 +128,9 @@ func NewPersistentAuth(ctx context.Context, opts ...PersistentAuthOption) (*Pers
134128
return nil, fmt.Errorf("cache: %w", err)
135129
}
136130
}
131+
if p.oAuthArgument == nil {
132+
return nil, errors.New("missing OAuthArgument")
133+
}
137134
if err := p.validateArg(); err != nil {
138135
return nil, err
139136
}
@@ -165,9 +162,7 @@ func (a *PersistentAuth) Token() (t *oauth2.Token, err error) {
165162
return nil, fmt.Errorf("token refresh: %w", err)
166163
}
167164
}
168-
169-
// Do not include the refresh token for security reasons. Refresh tokens are
170-
// long-lived credentials that we do not want to expose unnecessarily.
165+
// do not print refresh token to end-user
171166
t.RefreshToken = ""
172167
return t, nil
173168
}
@@ -274,19 +269,12 @@ func (a *PersistentAuth) Challenge() error {
274269
// startListener starts a listener on appRedirectAddr, retrying if the address
275270
// is already in use.
276271
func (a *PersistentAuth) startListener(ctx context.Context) error {
277-
// Use the value of redirectURL if it is already set. This is only expected
278-
// in tests to set a fixed redirect URL.
279-
addr := a.redirectAddr
280-
if addr == "" {
281-
addr = appRedirectAddr
282-
}
283-
284272
listener, err := retries.Poll(ctx, listenerTimeout,
285273
func() (*net.Listener, *retries.Err) {
286274
var lc net.ListenConfig
287-
l, err := lc.Listen(ctx, "tcp", addr)
275+
l, err := lc.Listen(ctx, "tcp", appRedirectAddr)
288276
if err != nil {
289-
logger.Debugf(ctx, "failed to listen on %s: %v, retrying", addr, err)
277+
logger.Debugf(ctx, "failed to listen on %s: %v, retrying", appRedirectAddr, err)
290278
return nil, retries.Continue(err)
291279
}
292280
return &l, nil
@@ -295,10 +283,6 @@ func (a *PersistentAuth) startListener(ctx context.Context) error {
295283
return fmt.Errorf("listener: %w", err)
296284
}
297285
a.ln = *listener
298-
299-
// Get the actual address that was assigned (including the port).
300-
a.redirectAddr = a.ln.Addr().String()
301-
logger.Debugf(ctx, "OAuth callback server listening on %s", a.redirectAddr)
302286
return nil
303287
}
304288

@@ -312,9 +296,6 @@ func (a *PersistentAuth) Close() error {
312296
// validateArg ensures that the OAuthArgument is either a WorkspaceOAuthArgument
313297
// or an AccountOAuthArgument.
314298
func (a *PersistentAuth) validateArg() error {
315-
if a.oAuthArgument == nil {
316-
return errors.New("missing OAuthArgument")
317-
}
318299
_, isWorkspaceArg := a.oAuthArgument.(WorkspaceOAuthArgument)
319300
_, isAccountArg := a.oAuthArgument.(AccountOAuthArgument)
320301
if !isWorkspaceArg && !isAccountArg {
@@ -350,7 +331,7 @@ func (a *PersistentAuth) oauth2Config() (*oauth2.Config, error) {
350331
TokenURL: endpoints.TokenEndpoint,
351332
AuthStyle: oauth2.AuthStyleInParams,
352333
},
353-
RedirectURL: fmt.Sprintf("http://%s", a.redirectAddr),
334+
RedirectURL: fmt.Sprintf("http://%s", appRedirectAddr),
354335
Scopes: scopes,
355336
}, nil
356337
}

credentials/u2m/persistent_auth_test.go

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"net/http"
88
"net/url"
9-
"regexp"
109
"strings"
1110
"testing"
1211
"time"
@@ -298,9 +297,6 @@ func TestChallenge(t *testing.T) {
298297
}
299298
defer p.Close()
300299

301-
// Set a fixed redirect URL for the test.
302-
p.redirectAddr = "localhost:1337"
303-
304300
errc := make(chan error)
305301
go func() {
306302
err := p.Challenge()
@@ -309,7 +305,7 @@ func TestChallenge(t *testing.T) {
309305
}()
310306

311307
state := <-browserOpened
312-
resp, err := http.Get(fmt.Sprintf("http://localhost:1337?code=__THIS__&state=%s", state))
308+
resp, err := http.Get(fmt.Sprintf("http://localhost:8020?code=__THIS__&state=%s", state))
313309
if err != nil {
314310
t.Fatalf("http.Get(): want no error, got %v", err)
315311
}
@@ -351,8 +347,6 @@ func TestChallenge_ReturnsErrorOnFailure(t *testing.T) {
351347
}
352348
defer p.Close()
353349

354-
p.redirectAddr = "localhost:1337" // set a fixed redirect URL for the test
355-
356350
errc := make(chan error)
357351
go func() {
358352
err := p.Challenge()
@@ -361,7 +355,7 @@ func TestChallenge_ReturnsErrorOnFailure(t *testing.T) {
361355
}()
362356

363357
<-browserOpened
364-
resp, err := http.Get("http://localhost:1337?error=access_denied&error_description=Policy%20evaluation%20failed%20for%20this%20request")
358+
resp, err := http.Get("http://localhost:8020?error=access_denied&error_description=Policy%20evaluation%20failed%20for%20this%20request")
365359
if err != nil {
366360
t.Fatalf("http.Get(): want no error, got %v", err)
367361
}
@@ -379,37 +373,3 @@ func TestChallenge_ReturnsErrorOnFailure(t *testing.T) {
379373
t.Fatalf("p.Challenge(): want error containing 'authorize: access_denied: Policy evaluation failed for this request', got %v", err)
380374
}
381375
}
382-
383-
// Verifies that startListener assigns a random port to the redirectAddr.
384-
func TestPersistentAuth_startListener_useDifferentPorts(t *testing.T) {
385-
ctx := context.Background()
386-
arg, err := NewBasicAccountOAuthArgument("https://accounts.cloud.databricks.com", "xyz")
387-
if err != nil {
388-
t.Fatalf("NewBasicAccountOAuthArgument(): want no error, got %v", err)
389-
}
390-
391-
p1, err := NewPersistentAuth(ctx, WithOAuthArgument(arg))
392-
if err != nil {
393-
t.Fatalf("NewPersistentAuth(): want no error, got %v", err)
394-
}
395-
defer p1.Close()
396-
397-
p2, err := NewPersistentAuth(ctx, WithOAuthArgument(arg))
398-
if err != nil {
399-
t.Fatalf("NewPersistentAuth(): want no error, got %v", err)
400-
}
401-
defer p2.Close()
402-
403-
p1.startListener(ctx)
404-
p2.startListener(ctx)
405-
406-
if !regexp.MustCompile(`^127\.0\.0\.1:\d+$`).MatchString(p1.redirectAddr) {
407-
t.Errorf("p1.redirectAddr should be random localhost port, got %s", p1.redirectAddr)
408-
}
409-
if !regexp.MustCompile(`^127\.0\.0\.1:\d+$`).MatchString(p2.redirectAddr) {
410-
t.Errorf("p2.redirectAddr should be random localhost port, got %s", p2.redirectAddr)
411-
}
412-
if p1.redirectAddr == p2.redirectAddr {
413-
t.Errorf("p1.redirectURL and p2.redirectURL should be different, got %s", p1.redirectAddr)
414-
}
415-
}

0 commit comments

Comments
 (0)