Skip to content

Commit

Permalink
fix(service-proxy): Correctly decompose host (#617)
Browse files Browse the repository at this point in the history
---------

Signed-off-by: Uwe Mayer <[email protected]>
Co-authored-by: Abhijith Ravindra <[email protected]>
  • Loading branch information
uwe-mayer and abhijith-darshan authored Oct 1, 2024
1 parent 94c3b88 commit bcb05d4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
4 changes: 3 additions & 1 deletion cmd/service-proxy/instrumentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"

"github.com/cloudoperators/greenhouse/pkg/common"
)

type ctxClusterKey struct {
Expand Down Expand Up @@ -67,7 +69,7 @@ func InstrumentHandler(next http.Handler, registry prometheus.Registerer) http.H

injector := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
if name, namespace, cluster, err := SplitHost(req.Host); err == nil {
if name, cluster, namespace, err := common.SplitHost(req.Host); err == nil {
ctx := req.Context()
ctx = context.WithValue(ctx, ctxClusterKey{}, cluster)
ctx = context.WithValue(ctx, ctxNamespaceKey{}, namespace)
Expand Down
19 changes: 3 additions & 16 deletions cmd/service-proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"net/http/httputil"
"net/url"
"regexp"
"strings"
"sync"

"github.com/go-logr/logr"
Expand All @@ -27,6 +26,7 @@ import (
greenhouseapis "github.com/cloudoperators/greenhouse/pkg/apis"
greenhousev1alpha1 "github.com/cloudoperators/greenhouse/pkg/apis/greenhouse/v1alpha1"
"github.com/cloudoperators/greenhouse/pkg/clientutil"
"github.com/cloudoperators/greenhouse/pkg/common"
)

func NewProxyManager() *ProxyManager {
Expand Down Expand Up @@ -171,8 +171,8 @@ func (pm *ProxyManager) rewrite(req *httputil.ProxyRequest) {
req.Out = req.Out.WithContext(log.IntoContext(req.Out.Context(), l))
}()

// hostname is expected to have the format $name--$namespace--$cluster.$organisation.$basedomain
name, namespace, cluster, err := SplitHost(req.In.Host)
// hostname is expected to have the format $name--$cluster--$namespace.$organisation.$basedomain
name, cluster, namespace, err := common.SplitHost(req.In.Host)
if err != nil {
return
}
Expand Down Expand Up @@ -243,16 +243,3 @@ func enqueuePluginForCluster(_ context.Context, o client.Object) []ctrl.Request
}
return []ctrl.Request{{NamespacedName: types.NamespacedName{Namespace: plugin.Namespace, Name: plugin.Spec.ClusterName}}}
}

// SplitHost splits a host into its name, namespace and cluster parts
func SplitHost(host string) (name, namespace, cluster string, err error) {
parts := strings.SplitN(host, ".", 2)
if len(parts) < 2 {
return "", "", "", fmt.Errorf("invalid host: %s", host)
}
parts = strings.SplitN(parts[0], "--", 3)
if len(parts) < 3 {
return "", "", "", fmt.Errorf("invalid host: %s", host)
}
return parts[0], parts[1], parts[2], nil
}
2 changes: 1 addition & 1 deletion cmd/service-proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestRewrite(t *testing.T) {
}{
{
name: "valid host",
url: "https://name--namespace--cluster.organisation.basedomain/abcd",
url: "https://name--cluster--namespace.organisation.basedomain/abcd",
expectedURL: "https://apiserver/proxy/url/abcd",
contextVal: "cluster",
},
Expand Down
17 changes: 17 additions & 0 deletions pkg/common/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package common
import (
"crypto/sha256"
"fmt"
"strings"

greenhousev1alpha1 "github.com/cloudoperators/greenhouse/pkg/apis/greenhouse/v1alpha1"
)
Expand All @@ -27,3 +28,19 @@ func URLForExposedServiceInPlugin(serviceName string, plugin *greenhousev1alpha1
subdomain, plugin.GetNamespace(), DNSDomain,
)
}

// SplitHost splits an exposed service host into its name, namespace and cluster parts
// The pattern shall be $service--$cluster--$namespace
// TODO, need to fix: We know that information on the namespace and even the cluster might be lost when the host was hashed due to it's length.
// Currently only cluster is critical, need to fix this.
func SplitHost(host string) (name, cluster, namespace string, err error) {
parts := strings.SplitN(host, ".", 2)
if len(parts) < 2 {
return "", "", "", fmt.Errorf("invalid host: %s", host)
}
parts = strings.SplitN(parts[0], "--", 3)
if len(parts) < 3 {
return "", "", "", fmt.Errorf("invalid host: %s", host)
}
return parts[0], parts[1], parts[2], nil
}

0 comments on commit bcb05d4

Please sign in to comment.