Skip to content

Commit

Permalink
Merge pull request #3632 from renuka-fernando/fix-send-empty-path
Browse files Browse the repository at this point in the history
Fix sending empty path for API resource /* when upstream has no base path
  • Loading branch information
renuka-fernando authored Jan 7, 2025
2 parents 64dc7bd + 9b2fdd1 commit 129d843
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 18 deletions.
2 changes: 1 addition & 1 deletion adapter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ require (
github.com/sirupsen/logrus v1.7.0
github.com/streadway/amqp v1.0.0
github.com/stretchr/testify v1.9.0
golang.org/x/net v0.32.0
golang.org/x/net v0.33.0
golang.org/x/text v0.21.0
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98
google.golang.org/grpc v1.58.3
Expand Down
14 changes: 2 additions & 12 deletions adapter/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,6 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/crypto v0.30.0 h1:RwoQn3GkWiMkzlX562cLB7OxWvjH1L8xutO2WoJcRoY=
golang.org/x/crypto v0.30.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down Expand Up @@ -389,10 +385,8 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI=
golang.org/x/net v0.32.0/go.mod h1:CwU0IoeOlnQQWJ6ioyFrfRuomB8GKF6KbYXZVyeXNfs=
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down Expand Up @@ -426,8 +420,6 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand All @@ -444,8 +436,6 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down
32 changes: 32 additions & 0 deletions adapter/internal/oasparser/envoyconf/envoyconf_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,38 @@ func TestCreateRouteClusterSpecifier(t *testing.T) {
assert.NotNil(t, routeWithProdSandEp.GetRoute().GetClusterHeader(), "Route Cluster Header should not be null.")
assert.Empty(t, routeWithProdSandEp.GetRoute().GetCluster(), "Route Cluster Name should be empty.")
assert.Equal(t, clusterHeaderName, routeWithProdSandEp.GetRoute().GetClusterHeader(), "Route Cluster Name mismatch.")

// Test the same scenario with endpointBasePath as empty string and API Resource with root slash
// If endpointBasepath is empty enforce the path to be "/" to avoid setting empty path to upstream.
// https://dev-us-east-azure/dlif/request-info/v1.0 -> https://bachendhost/
// https://dev-us-east-azure/dlif/request-info/v1.0/ -> https://bachendhost/
routeWithRootSlash := createRoute(generateRouteCreateParamsForUnitTests(title, apiType, vHost, xWso2BasePath, version, "",
"/", resourceWithGet.GetMethodList(), prodClusterName, nil, false))
assert.NotNil(t, routeWithRootSlash, "Route should not be null")
assert.Equal(t, "^/xWso2BasePath/?", routeWithRootSlash.GetRoute().GetRegexRewrite().Pattern.Regex)
assert.Equal(t, "/", routeWithRootSlash.GetRoute().GetRegexRewrite().Substitution)

routeWithRootSlashWithWildcard := createRoute(generateRouteCreateParamsForUnitTests(title, apiType, vHost, xWso2BasePath, version, "",
"/*", resourceWithGet.GetMethodList(), prodClusterName, nil, false))
assert.NotNil(t, routeWithRootSlashWithWildcard, "Route should not be null")
assert.Equal(t, "^/xWso2BasePath/?", routeWithRootSlashWithWildcard.GetRoute().GetRegexRewrite().Pattern.Regex)
assert.Equal(t, "/", routeWithRootSlashWithWildcard.GetRoute().GetRegexRewrite().Substitution)

// if endpointBasepath is not empty, enforce the path to be the endpointBasepath and allow the downnstream to handle trailing slash.
// if downstream sends the trailing slash, send the trailing slash to the upstream otherwise send the path without the trailing slash.
// https://dev-us-east-azure/dlif/request-info/v1.0 -> https://bachendhost/context
// https://dev-us-east-azure/dlif/request-info/v1.0/ -> https://bachendhost/context/
routeWithRootSlashNonEmptyEp := createRoute(generateRouteCreateParamsForUnitTests(title, apiType, vHost, xWso2BasePath, version, "/foo",
"/", resourceWithGet.GetMethodList(), prodClusterName, nil, false))
assert.NotNil(t, routeWithRootSlashNonEmptyEp, "Route should not be null")
assert.Equal(t, "^/xWso2BasePath", routeWithRootSlashNonEmptyEp.GetRoute().GetRegexRewrite().Pattern.Regex)
assert.Equal(t, "/foo", routeWithRootSlashNonEmptyEp.GetRoute().GetRegexRewrite().Substitution)

routeWithRootSlashWithWildcardNonEmptyEp := createRoute(generateRouteCreateParamsForUnitTests(title, apiType, vHost, xWso2BasePath, version, "/foo",
"/*", resourceWithGet.GetMethodList(), prodClusterName, nil, false))
assert.NotNil(t, routeWithRootSlashWithWildcardNonEmptyEp, "Route should not be null")
assert.Equal(t, "^/xWso2BasePath", routeWithRootSlashWithWildcardNonEmptyEp.GetRoute().GetRegexRewrite().Pattern.Regex)
assert.Equal(t, "/foo", routeWithRootSlashWithWildcardNonEmptyEp.GetRoute().GetRegexRewrite().Substitution)
}

func TestCreateRouteExtAuthzContext(t *testing.T) {
Expand Down
31 changes: 27 additions & 4 deletions adapter/internal/oasparser/envoyconf/routes_with_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -944,10 +944,33 @@ func createRoute(params *routeCreateParams) *routev3.Route {
if strings.Contains(resourcePath, "?") {
resourcePath = strings.Split(resourcePath, "?")[0]
}
resourceRegex := generatePathRegexSegment(resourcePath, false)
substitutionString := generateSubstitutionString(resourcePath, endpointBasepath)
if strings.HasSuffix(resourcePath, "/*") {
resourceRegex = strings.TrimSuffix(resourceRegex, "((/(.*))*)")

var resourceRegex string
var substitutionString string
if resourcePath == "/*" || resourcePath == "/" {
// This logic is only applicable for substitution and rewrite regex (not applicable for route match regex).
// Hence doing the special logic here.
if endpointBasepath == "" {
// If endpointBasepath is empty enforce the path to be "/" to avoid setting empty path to upstream.
// https://dev-us-east-azure/dlif/request-info/v1.0 -> https://bachendhost/
// https://dev-us-east-azure/dlif/request-info/v1.0/ -> https://bachendhost/
resourceRegex = "/?"
substitutionString = "/"
} else {
// if endpointBasepath is not empty, enforce the path to be the endpointBasepath and allow the downnstream to handle trailing slash.
// if downstream sends the trailing slash, send the trailing slash to the upstream otherwise send the path without the trailing slash.
// https://dev-us-east-azure/dlif/request-info/v1.0 -> https://bachendhost/context
// https://dev-us-east-azure/dlif/request-info/v1.0/ -> https://bachendhost/context/
resourceRegex = ""
substitutionString = endpointBasepath
}

} else {
resourceRegex = generatePathRegexSegment(resourcePath, false)
substitutionString = generateSubstitutionString(resourcePath, endpointBasepath)
if strings.HasSuffix(resourcePath, "/*") {
resourceRegex = strings.TrimSuffix(resourceRegex, "((/(.*))*)")
}
}
pathRegex := "^" + regexp.QuoteMeta(basePath) + resourceRegex

Expand Down
2 changes: 1 addition & 1 deletion adapter/src/main/resources/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# limitations under the License.
# -----------------------------------------------------------------------

FROM alpine:3.19.1
FROM alpine:3.21.0
LABEL maintainer="WSO2 Docker Maintainers <wso2.com>"

RUN apk update && apk upgrade --no-cache
Expand Down

0 comments on commit 129d843

Please sign in to comment.