Skip to content

Commit

Permalink
Merge pull request #111 from kaleido-io/wsclient/handle-embedded-basi…
Browse files Browse the repository at this point in the history
…c-auth

[wsclient] Allow for Embedded Basic Auth in URLs
  • Loading branch information
peterbroadhurst authored Nov 21, 2023
2 parents b87b635 + e3aa13f commit 43e860e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
13 changes: 7 additions & 6 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,10 +453,16 @@ func TestConfigWatchE2E(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
tmpDir := t.TempDir()

// Create the file
os.WriteFile(fmt.Sprintf("%s/test.yaml", tmpDir), []byte(`{"ut_conf": "one"}`), 0664)

// Read initial config
viper.SetConfigType("yaml")
viper.SetConfigFile(fmt.Sprintf("%s/test.yaml", tmpDir))
viper.ReadInConfig()
assert.Equal(t, "one", viper.Get("ut_conf"))

// Start listener on empty dir
// Start listener on config file
fsListenerDone := make(chan struct{})
fsListenerFired := make(chan bool)
ctx, cancelCtx := context.WithCancel(context.Background())
Expand All @@ -469,11 +475,6 @@ func TestConfigWatchE2E(t *testing.T) {
})
assert.NoError(t, err)

// Create the file
os.WriteFile(fmt.Sprintf("%s/test.yaml", tmpDir), []byte(`{"ut_conf": "one"}`), 0664)
<-fsListenerFired
assert.Equal(t, "one", viper.Get("ut_conf"))

// Delete and rename in another file
os.Remove(fmt.Sprintf("%s/test.yaml", tmpDir))
os.WriteFile(fmt.Sprintf("%s/another.yaml", tmpDir), []byte(`{"ut_conf": "two"}`), 0664)
Expand Down
13 changes: 7 additions & 6 deletions pkg/fswatcher/fswatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,16 @@ func TestFileListenerE2E(t *testing.T) {

filePath := fmt.Sprintf(fmt.Sprintf("%s/test.yaml", tmpDir))

// Create the file
os.WriteFile(fmt.Sprintf("%s/test.yaml", tmpDir), []byte(`{"ut_conf": "one"}`), 0664)

// Read initial config
viper.SetConfigType("yaml")
viper.SetConfigFile(filePath)
viper.ReadInConfig()
assert.Equal(t, "one", viper.Get("ut_conf"))

// Start listener on empty dir
// Start listener on config file
fsListenerDone := make(chan struct{})
fsListenerFired := make(chan bool)
ctx, cancelCtx := context.WithCancel(context.Background())
Expand All @@ -51,11 +57,6 @@ func TestFileListenerE2E(t *testing.T) {
})
assert.NoError(t, err)

// Create the file
os.WriteFile(fmt.Sprintf("%s/test.yaml", tmpDir), []byte(`{"ut_conf": "one"}`), 0664)
<-fsListenerFired
assert.Equal(t, "one", viper.Get("ut_conf"))

// Delete and rename in another file
os.Remove(fmt.Sprintf("%s/test.yaml", tmpDir))
os.WriteFile(fmt.Sprintf("%s/another.yaml", tmpDir), []byte(`{"ut_conf": "two"}`), 0664)
Expand Down
5 changes: 5 additions & 0 deletions pkg/wsclient/wsclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,11 @@ func buildWSUrl(ctx context.Context, config *WSConfig) (string, error) {
if u.Scheme == "https" {
u.Scheme = "wss"
}
if config.AuthUsername == "" && config.AuthPassword == "" && u.User != nil {
config.AuthUsername = u.User.Username()
config.AuthPassword, _ = u.User.Password()
}
u.User = nil
return u.String(), nil
}

Expand Down
12 changes: 12 additions & 0 deletions pkg/wsclient/wsclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,18 @@ func TestWSClientBadHTTPURL(t *testing.T) {
assert.Regexp(t, "FF00149", err)
}

func TestBasicAuthRemap(t *testing.T) {
wsConfig := generateConfig()
wsConfig.HTTPURL = "https://user:pass@test:12345"
wsConfig.WSKeyPath = "/websocket"

url, err := buildWSUrl(context.Background(), wsConfig)
assert.NoError(t, err)
assert.Equal(t, "wss://test:12345/websocket", url)
assert.Equal(t, "user", wsConfig.AuthUsername)
assert.Equal(t, "pass", wsConfig.AuthPassword)
}

func TestHTTPToWSURLRemap(t *testing.T) {
wsConfig := generateConfig()
wsConfig.HTTPURL = "http://test:12345"
Expand Down

0 comments on commit 43e860e

Please sign in to comment.