Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions admin/internal/handler/zone/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,18 @@ var _ = Describe("Zone Handler", func() {
Expect(route.Spec.Hostnames).To(Equal([]string{"test-stargate.de"}))
})

It("keeps the percent-encoding of the managed route upstream path", func() {
zone.Spec.ManagedRoutes = &adminv1.ManagedRoutesConfig{Routes: []adminv1.ManagedRouteConfig{{Name: "proxy", Path: "/proxy", Url: "https://backend.example.com/api/url%20encoded%20path", Type: adminv1.ManagedRouteTypeProxy}}}
handler := newTestHandler()
Expect(handler.CreateOrUpdate(newTestContext(zone), zone)).To(Succeed())
markSubResourcesReady(zone)
Expect(handler.CreateOrUpdate(newTestContext(zone), zone)).To(Succeed())
route := &gatewayapi.Route{}
Expect(k8sClient.Get(ctx, client.ObjectKey{Namespace: zone.Status.Namespace, Name: naming.ForGateway(zone, "standard") + "--proxy"}, route)).To(Succeed())
Expect(route.Spec.Backend.Upstreams).To(HaveLen(1))
Expect(route.Spec.Backend.Upstreams[0].Path).To(Equal("/api/url%20encoded%20path"))
})

It("deduplicates managed route paths and hostnames shared by preset URLs", func() {
zone.Spec.Presets[0].Urls = []adminv1.UrlConfig{
{Hostname: "irix.example.com", BasePath: "/"},
Expand Down
9 changes: 1 addition & 8 deletions admin/internal/handler/zone/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package zone

import (
"context"
"net/url"
"slices"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -135,16 +134,10 @@ func createManagedRoute(ctx context.Context, hc *HandlingContext, routeConfig ad
route.Labels[cconfig.DomainLabelKey] = domainName
route.Labels[cconfig.OwnerUidLabelKey] = string(hc.Zone.GetUID())

upstreamUrl, err := url.Parse(routeConfig.Url)
upstream, err := gatewayapi.ParseUpstream(routeConfig.Url)
if err != nil {
return ctrlerrors.BlockedErrorf("cannot parse upstream url of internal route %s: %s", routeConfig.Url, err)
}
upstream := gatewayapi.Upstream{
Scheme: upstreamUrl.Scheme,
Hostname: upstreamUrl.Hostname(),
Port: gatewayapi.GetPortOrDefaultFromScheme(upstreamUrl),
Path: upstreamUrl.Path,
}

hostnames, paths := preset.ResolveHostnamesAndPaths(routeConfig.Path)

Expand Down
21 changes: 3 additions & 18 deletions agentic/internal/handler/util/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func CreateAgenticProxyRoute(
if err != nil {
return nil, errors.Wrap(err, "failed to build upstream URL for proxy route")
}
upstream, err := AsUpstream(upstreamUrl, 0)
upstream, err := gatewayapi.ParseUpstream(upstreamUrl)
if err != nil {
return nil, errors.Wrap(err, "failed to create upstream for proxy route")
}
Expand Down Expand Up @@ -270,29 +270,14 @@ func DeleteRouteIfExists(ctx context.Context, ref *ctypes.ObjectRef) error {
return nil
}

// AsUpstream converts a raw URL to a gateway Upstream struct.
func AsUpstream(rawUrl string, weight int32) (gatewayapi.Upstream, error) {
u, err := url.Parse(rawUrl)
if err != nil {
return gatewayapi.Upstream{}, errors.Wrapf(err, "failed to parse URL %s", rawUrl)
}

return gatewayapi.Upstream{
Scheme: u.Scheme,
Hostname: u.Hostname(),
Port: gatewayapi.GetPortOrDefaultFromScheme(u),
Path: u.Path,
Weight: weight,
}, nil
}

func MapUpstreamsToGateway(upstreams []agenticv1.Upstream) ([]gatewayapi.Upstream, error) {
mapped := make([]gatewayapi.Upstream, 0, len(upstreams))
for _, upstream := range upstreams {
gatewayUpstream, err := AsUpstream(upstream.Url, int32(upstream.Weight)) //nolint:gosec // weight is a small positive integer
gatewayUpstream, err := gatewayapi.ParseUpstream(upstream.Url)
if err != nil {
return nil, errors.Wrapf(err, "failed to map upstream %s", upstream.Url)
}
gatewayUpstream.Weight = int32(upstream.Weight) //nolint:gosec // weight is a small positive integer
mapped = append(mapped, gatewayUpstream)
}
return mapped, nil
Expand Down
17 changes: 17 additions & 0 deletions agentic/internal/handler/util/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,20 @@ var _ = Describe("MapSubscriberSecurityToGateway", func() {
Entry("client_secret_post", agenticv1.TokenRequestClientSecretPost, gatewayapi.TokenRequestClientSecretPost),
)
})

var _ = Describe("MapUpstreamsToGateway", func() {
It("keeps the percent-encoding of the path and the weight", func() {
mapped, err := MapUpstreamsToGateway([]agenticv1.Upstream{
{Url: "https://provider.example.com/api/url%20encoded%20path", Weight: 40},
})

Expect(err).ToNot(HaveOccurred())
Expect(mapped).To(Equal([]gatewayapi.Upstream{{
Scheme: "https",
Hostname: "provider.example.com",
Port: 443,
Path: "/api/url%20encoded%20path",
Weight: 40,
}}))
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ var _ = Describe("RemoteApiSubscription Controller - Consumer Scenario", Ordered
ApprovalState: approvalapi.ApprovalStateGranted.String(),
Message: "Test-Message",
},
GatewayUrl: "http://ger.gateway.pol:8080/ger",
GatewayUrl: "http://ger.gateway.pol:8080/ger/url%20encoded%20path",
}
remoteApiSubscription.SetCondition(condition.NewReadyCondition("RemoteApiSubscriptionReady", "Trust me, I am ready"))

Expand All @@ -359,6 +359,8 @@ var _ = Describe("RemoteApiSubscription Controller - Consumer Scenario", Ordered
g.Expect(k8sClient.Get(ctx, remoteApiSubscription.Status.Route.K8s(), route)).To(Succeed())
g.Expect(route.Spec.AdditionalTags).To(Equal([]string{"variant--default"}))
g.Expect(route.Spec.Security.RealmName).To(Equal("remoteapisub-test-cons-realm"))
g.Expect(route.Spec.Backend.Upstreams).To(HaveLen(1))
g.Expect(route.Spec.Backend.Upstreams[0].Path).To(Equal("/ger/url%20encoded%20path"))
}, timeout, interval).Should(Succeed())
})
})
Expand Down
18 changes: 4 additions & 14 deletions api/internal/handler/remoteapisubscription/consumer_scenario.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package remoteapisubscription

import (
"context"
"net/url"

"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -84,13 +83,11 @@ func (h *RemoteApiSubscriptionHandler) handleConsumerScenario(ctx context.Contex
config.BuildLabelKey("type"): "real",
}

u, parseErr := url.Parse(obj.Status.GatewayUrl)
if parseErr != nil {
return errors.Wrapf(parseErr, "failed to parse gateway url")
upstream, upstreamErr := gatewayapi.ParseUpstream(obj.Status.GatewayUrl)
if upstreamErr != nil {
return errors.Wrapf(upstreamErr, "failed to parse gateway url")
}

port := gatewayapi.GetPortOrDefaultFromScheme(u)

hostnames, paths := preset.ResolveHostnamesAndPaths(obj.Spec.ApiBasePath)

route.Spec = gatewayapi.RouteSpec{
Expand All @@ -99,14 +96,7 @@ func (h *RemoteApiSubscriptionHandler) handleConsumerScenario(ctx context.Contex
Type: gatewayapi.RouteTypePrimary,
Security: gatewayapi.Security{RealmName: zone.Status.RealmName},
Backend: gatewayapi.Backend{
Upstreams: []gatewayapi.Upstream{
{
Scheme: u.Scheme,
Hostname: u.Hostname(),
Port: port,
Path: u.Path,
},
},
Upstreams: []gatewayapi.Upstream{upstream},
},
Hostnames: hostnames,
Paths: paths,
Expand Down
31 changes: 0 additions & 31 deletions api/internal/handler/util/gateway.go

This file was deleted.

10 changes: 6 additions & 4 deletions api/internal/handler/util/route_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func CreateProxyRoute(ctx context.Context, downstreamZoneRef, upstreamZoneRef ty
if joinErr != nil {
return errors.Wrap(joinErr, "failed to build upstream URL for proxy route")
}
upstream, upstreamErr := AsUpstream(upstreamUrl, 0)
upstream, upstreamErr := gatewayapi.ParseUpstream(upstreamUrl)
if upstreamErr != nil {
return errors.Wrap(upstreamErr, "failed to create upstream")
}
Expand Down Expand Up @@ -380,10 +380,11 @@ func configureAsFailoverTarget(_ context.Context, proxyRoute *gatewayapi.Route,

failoverTargets := make([]gatewayapi.FailoverTarget, 0, len(options.FailoverUpstreams))
for _, rawUpstream := range options.FailoverUpstreams {
failoverUpstream, upstreamErr := AsUpstream(rawUpstream.Url, int32(rawUpstream.Weight)) //nolint:gosec // weight is a small positive integer
failoverUpstream, upstreamErr := gatewayapi.ParseUpstream(rawUpstream.Url)
if upstreamErr != nil {
return errors.Wrapf(upstreamErr, "failed to create failover upstream %s", rawUpstream.Url)
}
failoverUpstream.Weight = int32(rawUpstream.Weight) //nolint:gosec // weight is a small positive integer
failoverTargets = append(failoverTargets, gatewayapi.FailoverTarget{Upstream: failoverUpstream})
}

Expand Down Expand Up @@ -426,7 +427,7 @@ func addFailoverFallback(ctx context.Context, proxyRoute *gatewayapi.Route, opti
return errors.Wrapf(err, "failed to build failover URL for zone %s", failoverZone.String())
}

failoverUpstream, err := AsUpstream(failoverUrl, 0)
failoverUpstream, err := gatewayapi.ParseUpstream(failoverUrl)
if err != nil {
return errors.Wrapf(err, "failed to create failover upstream for zone %s", failoverZone.String())
}
Expand Down Expand Up @@ -571,10 +572,11 @@ func CreateRealRoute(ctx context.Context, downstreamZoneRef types.ObjectRef, api

gatewayUpstreams := make([]gatewayapi.Upstream, 0, len(apiExposure.Spec.Upstreams))
for _, upstream := range apiExposure.Spec.Upstreams {
gatewayUpstream, upstreamErr := AsUpstream(upstream.Url, int32(upstream.Weight)) //nolint:gosec // weight is a small positive integer
gatewayUpstream, upstreamErr := gatewayapi.ParseUpstream(upstream.Url)
if upstreamErr != nil {
return errors.Wrapf(upstreamErr, "failed to create upstream for URL %s", upstream.Url)
}
gatewayUpstream.Weight = int32(upstream.Weight) //nolint:gosec // weight is a small positive integer
gatewayUpstreams = append(gatewayUpstreams, gatewayUpstream)
}

Expand Down
64 changes: 64 additions & 0 deletions api/internal/handler/util_real_route_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2026 Deutsche Telekom IT GmbH
//
// SPDX-License-Identifier: Apache-2.0

package handler

import (
"context"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"

adminapi "github.com/telekom/controlplane/admin/api/v1"
apiapi "github.com/telekom/controlplane/api/api/v1"
"github.com/telekom/controlplane/api/internal/handler/util"
"github.com/telekom/controlplane/common/pkg/client"
"github.com/telekom/controlplane/common/pkg/types"
"github.com/telekom/controlplane/common/pkg/util/contextutil"
gatewayapi "github.com/telekom/controlplane/gateway/api/v1"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("Creation of Real-Routes", Ordered, func() {
var (
routeCtx context.Context
providerZone *adminapi.Zone
)

BeforeAll(func() {
routeCtx = client.WithClient(context.Background(), client.NewJanitorClient(client.NewScopedClient(k8sClient, testEnvironment)))
routeCtx = contextutil.WithEnv(routeCtx, testEnvironment)

providerZone = CreateZone("encoded-upstream")
})

It("should persist the percent-encoded upstream path", func() {
apiExposure := &apiapi.ApiExposure{
ObjectMeta: metav1.ObjectMeta{
Name: "encoded-upstream",
Namespace: testEnvironment,
},
Spec: apiapi.ApiExposureSpec{
ApiBasePath: "/encoded/upstream/v1",
Upstreams: []apiapi.Upstream{
{Url: "https://provider.example.com:8443/api/encoded%20url%20path"},
},
},
}

route, err := util.CreateRealRoute(routeCtx, *types.ObjectRefFromObject(providerZone), apiExposure)
Expect(err).ToNot(HaveOccurred())

persistedRoute := &gatewayapi.Route{}
Expect(k8sClient.Get(routeCtx, k8sclient.ObjectKeyFromObject(route), persistedRoute)).To(Succeed())
Expect(persistedRoute.Spec.Backend.Upstreams).To(Equal([]gatewayapi.Upstream{{
Scheme: "https",
Hostname: "provider.example.com",
Port: 8443,
Path: "/api/encoded%20url%20path",
}}))
})
})
2 changes: 1 addition & 1 deletion docs/docs/architecture/event.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ sequenceDiagram

Some subscribers prefer to be *called back*: instead of fetching events, they give the platform an address of their own and let each event be pushed there. The callback route makes this happen. The event backend sends every event to the callback route, which reads the subscriber's real address (carried along with the request) and forwards the event to it.

Callback delivery starts in the **backend zone**, where Horizon runs. In this example, Horizon is configured with `https://dataplane1-gateway/horizon-dataplane3/callback/v1?url=U`, where `U` represents the subscriber's URL. The hostname is illustrative; the actual address comes from the gateway preset. **Backend-local callback ingress means that gateway's advertised URL, not loopback or `localhost`.**
Callback delivery starts in the **backend zone**, where Horizon runs. In this example, Horizon is configured with `https://dataplane1-gateway/horizon-dataplane3/callback/v1?url=U`, where `U` represents the subscriber's query-encoded URL. The hostname is illustrative; the actual address comes from the gateway preset. **Backend-local callback ingress means that gateway's advertised URL, not loopback or `localhost`.**

```mermaid
sequenceDiagram
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/user-journey/exposing-apis.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ roverctl apply -f rover.yaml
| Field | Description |
| ----- | ----------- |
| `basePath` | The public API path. Must match an existing Api resource and be unique across the platform. This is how consumers will reach your API. |
| `upstream` | The internal URL where your service is running. The gateway proxies incoming requests to this address. |
| `upstream` | The internal URL where your service is running. The gateway proxies incoming requests to this address. Encoded characters in the path, such as `%20`, are forwarded unchanged. |
| `approval` | The approval strategy for subscription requests: `auto`, `simple`, or `foureyes`. See [Approvals](./approvals.mdx). |

### What Happens
Expand Down
4 changes: 2 additions & 2 deletions event/internal/controller/callback_lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ var _ = Describe("callback lifecycle with persisted resources", func() {
reconcileSub()
Eventually(func(g Gomega) {
g.Expect(k8sClient.Get(ctx, childKey, child)).To(Succeed())
g.Expect(child.Spec.Delivery.Callback).To(Equal(backend.Status.ProxyCallbackURLs["subscriber"] + "?url=https://consumer.example.com/events"))
g.Expect(child.Spec.Delivery.Callback).To(Equal(backend.Status.ProxyCallbackURLs["subscriber"] + "?url=https%3A%2F%2Fconsumer.example.com%2Fevents"))
}, 5*time.Second, 100*time.Millisecond).Should(Succeed())

oldScalar := proxy.Status.CallbackURL
Expand All @@ -511,7 +511,7 @@ var _ = Describe("callback lifecycle with persisted resources", func() {
Eventually(func() []reconcile.Request { return r.MapEventConfigToEventSubscription(ctx, proxy) }, 5*time.Second, 100*time.Millisecond).Should(ContainElement(reconcile.Request{NamespacedName: client.ObjectKeyFromObject(sub)}))
reconcileSub()
Expect(k8sClient.Get(ctx, childKey, child)).To(Succeed())
Expect(child.Spec.Delivery.Callback).To(Equal(backend.Status.ProxyCallbackURLs["subscriber"] + "?url=https://consumer.example.com/events"))
Expect(child.Spec.Delivery.Callback).To(Equal(backend.Status.ProxyCallbackURLs["subscriber"] + "?url=https%3A%2F%2Fconsumer.example.com%2Fevents"))

proxy.Spec.Mesh.ZoneNames = []string{"backend"}
Expect(k8sClient.Update(ctx, proxy)).To(Succeed())
Expand Down
9 changes: 8 additions & 1 deletion event/internal/handler/eventsubscription/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,14 @@ func updateCallbackURL(ctx context.Context, sub *eventv1.EventSubscription, expo
}

// Use rawCallbackUrl as new callback URL and add actual callback URL as query parameter so that provider can use it for callbacks.
sub.Spec.Delivery.Callback = rawCallbackUrl + "?" + util.CallbackURLQueryParam + "=" + sub.Spec.Delivery.Callback
gatewayCallbackUrl, err := url.Parse(rawCallbackUrl)
if err != nil {
return ctrlerrors.BlockedErrorf("cannot parse gateway callback URL %q: %s", rawCallbackUrl, err)
}
query := gatewayCallbackUrl.Query()
query.Set(util.CallbackURLQueryParam, sub.Spec.Delivery.Callback)
gatewayCallbackUrl.RawQuery = query.Encode()
sub.Spec.Delivery.Callback = gatewayCallbackUrl.String()
logger.V(1).Info("Updated callback URL for proxy scenario", "callback", sub.Spec.Delivery.Callback)

return nil
Expand Down
Loading
Loading