Skip to content

Commit 4886afe

Browse files
authored
[Fix] Fail fast when authenticating if host is not configured (#1033)
## Changes The SDK currently hangs when making API requests if the Host is set to a non-empty but invalid endpoint, such as `https://:443`. This PR adds a check during authentication to ensure that the hostname is defined, failing early if not. The underlying error is exported so other clients, such as the CLI or Terraform Provider, can provide specific advice when this happens. ## Tests Added a unit test for this case. - [ ] `make test` passing - [ ] `make fmt` applied - [ ] relevant integration tests applied
1 parent 796dae1 commit 4886afe

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

config/config.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package config
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"net/http"
78
"net/url"
@@ -336,7 +337,8 @@ func (c *Config) WithTesting() *Config {
336337
}
337338

338339
func (c *Config) CanonicalHostName() string {
339-
c.fixHostIfNeeded()
340+
// Missing host is tolerated here.
341+
_ = c.fixHostIfNeeded()
340342
return c.Host
341343
}
342344

@@ -361,7 +363,9 @@ func (c *Config) authenticateIfNeeded() error {
361363
if c.Credentials == nil {
362364
c.Credentials = &DefaultCredentials{}
363365
}
364-
c.fixHostIfNeeded()
366+
if err := c.fixHostIfNeeded(); err != nil && !errors.Is(err, ErrNoHostConfigured) {
367+
return err
368+
}
365369
ctx := c.refreshClient.InContextForOAuth2(c.refreshCtx)
366370
credentialsProvider, err := c.Credentials.Configure(ctx, c)
367371
if err != nil {
@@ -372,7 +376,9 @@ func (c *Config) authenticateIfNeeded() error {
372376
}
373377
c.credentialsProvider = credentialsProvider
374378
c.AuthType = c.Credentials.Name()
375-
c.fixHostIfNeeded()
379+
if err := c.fixHostIfNeeded(); err != nil {
380+
return err
381+
}
376382
// TODO: error customization
377383
return nil
378384
}
@@ -393,6 +399,9 @@ func (c *Config) fixHostIfNeeded() error {
393399
return err
394400
}
395401
}
402+
if parsedHost.Hostname() == "" {
403+
return ErrNoHostConfigured
404+
}
396405
// Create new instance to ensure other fields are initialized as empty.
397406
parsedHost = &url.URL{
398407
Scheme: parsedHost.Scheme,
@@ -403,6 +412,11 @@ func (c *Config) fixHostIfNeeded() error {
403412
return nil
404413
}
405414

415+
// ErrNoHostConfigured is the error returned when a user tries to authenticate
416+
// without a host configured. Applications can check for this error to provide
417+
// more user-friendly error messages.
418+
var ErrNoHostConfigured = fmt.Errorf("no host configured")
419+
406420
func (c *Config) refreshTokenErrorMapper(ctx context.Context, resp common.ResponseWrapper) error {
407421
defaultErr := httpclient.DefaultErrorMapper(ctx, resp)
408422
if defaultErr == nil {

config/config_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package config
22

33
import (
4+
"context"
5+
"net/http"
46
"testing"
57

68
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
710
)
811

912
func TestIsAccountClient_AwsAccount(t *testing.T) {
@@ -52,3 +55,14 @@ func TestNewWithWorkspaceHost(t *testing.T) {
5255
// The new config will not be automatically resolved.
5356
assert.False(t, c2.resolved)
5457
}
58+
59+
func TestAuthenticate_InvalidHostSet(t *testing.T) {
60+
c := &Config{
61+
Host: "https://:443",
62+
Token: "abcdefg",
63+
}
64+
req, err := http.NewRequestWithContext(context.Background(), "GET", c.Host, nil)
65+
require.NoError(t, err)
66+
err = c.Authenticate(req)
67+
assert.ErrorIs(t, err, ErrNoHostConfigured)
68+
}

0 commit comments

Comments
 (0)