Skip to content

Commit

Permalink
fix: adds a function to properly sanitize the address
Browse files Browse the repository at this point in the history
  • Loading branch information
faiq committed Jan 3, 2024
1 parent d092c2d commit a01439d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
24 changes: 21 additions & 3 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"fmt"
"net/url"
"os"
"path/filepath"

Expand All @@ -43,6 +44,8 @@ const (
capxNamespaceKey = "POD_NAMESPACE"
)

var ErrPrismAddressNotSet = fmt.Errorf("cannot get credentials if Prism Address is not set")

type NutanixClientHelper struct {
secretInformer coreinformers.SecretInformer
configMapInformer coreinformers.ConfigMapInformer
Expand All @@ -63,9 +66,11 @@ func (n *NutanixClientHelper) GetClientFromEnvironment(ctx context.Context, nuta
// If PrismCentral is set, add the required env provider
prismCentralInfo := nutanixCluster.Spec.PrismCentral
if prismCentralInfo != nil {
if prismCentralInfo.Address == "" {
return nil, fmt.Errorf("cannot get credentials if Prism Address is not set")
address, err := validateAndSanitizePrismCentralInfoAddress(prismCentralInfo.Address)
if err != nil {
return nil, err

Check warning on line 71 in pkg/client/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/client/client.go#L69-L71

Added lines #L69 - L71 were not covered by tests
}
prismCentralInfo.Address = address

Check warning on line 73 in pkg/client/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/client/client.go#L73

Added line #L73 was not covered by tests
if prismCentralInfo.Port == 0 {
return nil, fmt.Errorf("cannot get credentials if Prism Port is not set")
}
Expand Down Expand Up @@ -116,7 +121,6 @@ func (n *NutanixClientHelper) GetClientFromEnvironment(ctx context.Context, nuta
*npe,
n.secretInformer,
n.configMapInformer))

// init env with providers
env := environment.NewEnvironment(
providers...,
Expand Down Expand Up @@ -209,3 +213,17 @@ func GetCredentialRefForCluster(nutanixCluster *infrav1.NutanixCluster) (*creden

return prismCentralInfo.CredentialRef, nil

Check warning on line 214 in pkg/client/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/client/client.go#L214

Added line #L214 was not covered by tests
}

func validateAndSanitizePrismCentralInfoAddress(address string) (string, error) {
if address == "" {
return "", ErrPrismAddressNotSet
}
u, err := url.Parse(address)
if err != nil {
return "", fmt.Errorf("failed to parse url from given address %w", err)

Check warning on line 223 in pkg/client/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/client/client.go#L223

Added line #L223 was not covered by tests
}
if u.Scheme != "" || u.Port() != "" {
return u.Hostname(), nil
}
return address, nil
}
52 changes: 52 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package client

import (
"errors"
"testing"
)

func Test_validateAndSanitizePrismCentralInfoAddress(t *testing.T) {
tests := []struct {
name string
providedAddress string
expectedAddress string
expectedErr error
}{
{
"with scheme",
"https://prism-bowser.ntnxsherlock.com",
"prism-bowser.ntnxsherlock.com",
nil,
},
{
"with scheme and port",
"https://prism-bowser.ntnxsherlock.com:9440",
"prism-bowser.ntnxsherlock.com",
nil,
},
{
"as expected from the user",
"prism-bowser.ntnxsherlock.com",
"prism-bowser.ntnxsherlock.com",
nil,
},
{
"not set",
"",
"",
ErrPrismAddressNotSet,
},
}

for _, test := range tests {
s, err := validateAndSanitizePrismCentralInfoAddress(test.providedAddress)
if err != nil {
if test.expectedErr == nil || !errors.Is(err, test.expectedErr) {
t.Errorf("validateAndSanitizePrismCentralInfoAddress() error = %v, wantErr = %v", err, test.expectedErr)
}
}
if s != test.expectedAddress {
t.Errorf("validateAndSanitizePrismCentralInfoAddress() got = %v, want = %v", s, test.expectedAddress)
}
}
}

0 comments on commit a01439d

Please sign in to comment.