Skip to content

Commit

Permalink
feat: tolerate serverURL scheme lack (#547)
Browse files Browse the repository at this point in the history
* feat: tolerate serverURL scheme lack

Signed-off-by: Mattia Lavacca <[email protected]>

* move regexp to global var

Signed-off-by: Mattia Lavacca <[email protected]>

---------

Signed-off-by: Mattia Lavacca <[email protected]>
  • Loading branch information
mlavacca authored Sep 4, 2024
1 parent 603d1e5 commit 53eb79d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
19 changes: 18 additions & 1 deletion controller/konnect/reconciler_konnectapiauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package konnect
import (
"context"
"fmt"
"regexp"
"strings"
"time"

sdkkonnectgoops "github.com/Kong/sdk-konnect-go/models/operations"
Expand Down Expand Up @@ -129,8 +131,12 @@ func (r *KonnectAPIAuthConfigurationReconciler) Reconcile(
return ctrl.Result{}, err
}

serverURL, err := getKonnectServerURL(apiAuth.Spec.ServerURL)
if err != nil {
return ctrl.Result{}, err
}
sdk := r.SDKFactory.NewKonnectSDK(
"https://"+apiAuth.Spec.ServerURL,
serverURL,
SDKToken(token),
)

Expand Down Expand Up @@ -244,3 +250,14 @@ func getTokenFromKonnectAPIAuthConfiguration(

return "", fmt.Errorf("unknown KonnectAPIAuthType: %s", apiAuth.Spec.Type)
}

var serverURLRegexp = regexp.MustCompile(".*://")

func getKonnectServerURL(serverURL string) (string, error) {
if !serverURLRegexp.MatchString(serverURL) {
serverURL = "https://" + serverURL
} else if !strings.HasPrefix(serverURL, "https://") {
return "", fmt.Errorf("in case scheme is specified in the ServerURL, it must be https://: %s", serverURL)
}
return serverURL, nil
}
37 changes: 37 additions & 0 deletions controller/konnect/reconciler_konnectapiauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package konnect

import (
"context"
"errors"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -159,3 +160,39 @@ func TestGetTokenFromKonnectAPIAuthConfiguration(t *testing.T) {
})
}
}

func TestGetKonnectServerURL(t *testing.T) {
var testCases = []struct {
name string
serverURL string
expectedServerURL string
expectedError error
}{
{
name: "valid Server URL, with scheme",
serverURL: "https://konghq.com",
expectedServerURL: "https://konghq.com",
},
{
name: "valid Server URL, without scheme",
serverURL: "konghq.com",
expectedServerURL: "https://konghq.com",
},
{
name: "invalid Server URL",
serverURL: "http://konghq.com",
expectedServerURL: "",
expectedError: errors.New("in case scheme is specified in the ServerURL, it must be https://: http://konghq.com"),
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
serverURL, err := getKonnectServerURL(tc.serverURL)

assert.Equal(t, tc.expectedError, err)
assert.Equal(t, tc.expectedServerURL, serverURL)
})
}
}

0 comments on commit 53eb79d

Please sign in to comment.