Skip to content
This repository has been archived by the owner on Aug 6, 2024. It is now read-only.

Commit

Permalink
core: fix path resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
h3adex committed Dec 6, 2023
1 parent 40676d2 commit e8f16f8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func (r *RoutingTable) GetBackend(host, uri, ip string) (*url.URL, map[string]st
path.Backend.Service.Name,
path.Backend.Service.Port.Number,
),
Path: path.Path,
Scheme: "http",
}, ingress.Annotations, RoutingError{}
}
Expand Down Expand Up @@ -98,6 +99,7 @@ func (r *RoutingTable) GetBackend(host, uri, ip string) (*url.URL, map[string]st
path.Backend.Service.Name,
path.Backend.Service.Port.Number,
),
Path: path.Path,
Scheme: "http",
}, ingress.Annotations, RoutingError{}
}
Expand Down
26 changes: 26 additions & 0 deletions pkg/router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package router

import (
"crypto/tls"
"fmt"
"github.com/h3adex/guardgress/pkg/mocks"
"github.com/stretchr/testify/assert"
"github.com/ulule/limiter/v3"
Expand Down Expand Up @@ -264,3 +265,28 @@ func TestGetBackendWithMultipleIngresses(t *testing.T) {
assert.NoError(t, err.Error)
assert.Equal(t, url.Host, "127.0.0.1:20100")
}

func TestCertManagerImplementation(t *testing.T) {
mock := mocks.IngressPathTypeImplementationSpecificTypeMock()
mock.Spec.Rules[0].HTTP.Paths[0].Backend.Service.Port.Number = 8089
mock.Spec.Rules[0].HTTP.Paths[0].Backend.Service.Name = "cm-acme-http-solver-mqvwg"
mock.Spec.Rules[0].HTTP.Paths[0].Path = "/.well-known/acme-challenge/5XSJIlrUE9OZl_Og7-Y--vIM2eeGhnvSXJLSejioqcM"

routingTable := RoutingTable{
Ingresses: &v1.IngressList{
TypeMeta: v12.TypeMeta{},
ListMeta: v12.ListMeta{},
Items: []v1.Ingress{
mock,
},
},
TlsCertificates: mocks.TlsCertificatesMock(),
IngressLimiters: []*limiter.Limiter{nil},
}

// should work with path type implementation specific
url, _, err := routingTable.GetBackend("www.guardgress.com", "/.well-known/acme-challenge/5XSJIlrUE9OZl_Og7-Y--vIM2eeGhnvSXJLSejioqcM", "127.0.0.1")
assert.NoError(t, err.Error)
assert.Equal(t, mock.Spec.Rules[0].HTTP.Paths[0].Path, url.Path)
assert.Equal(t, fmt.Sprintf("%s:%d", mock.Spec.Rules[0].HTTP.Paths[0].Backend.Service.Name, mock.Spec.Rules[0].HTTP.Paths[0].Backend.Service.Port.Number), url.Host)
}
15 changes: 15 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"github.com/caarlos0/env"
"github.com/gin-gonic/gin"
"github.com/gospider007/net/http2"
"github.com/h3adex/fp"
"github.com/h3adex/guardgress/pkg/algorithms"
"github.com/h3adex/guardgress/pkg/annotations"
Expand Down Expand Up @@ -141,6 +142,13 @@ func (s Server) ServeHttps(ctx *gin.Context) {

log.Debug("proxying https request to: ", svcUrl)
proxy := httputil.NewSingleHostReverseProxy(svcUrl)

if svcUrl.Scheme == "https" {
proxy.Transport = &http2.Transport{
AllowHTTP: true,
}
}

proxy.Director = func(req *http.Request) {
req.Header = ctx.Request.Header
req.Host = svcUrl.Host
Expand Down Expand Up @@ -173,6 +181,13 @@ func (s Server) ServeHTTP(ctx *gin.Context) {

log.Debug("proxying http request to: ", svcUrl)
proxy := httputil.NewSingleHostReverseProxy(svcUrl)

if svcUrl.Scheme == "https" {
proxy.Transport = &http2.Transport{
AllowHTTP: true,
}
}

proxy.Director = func(req *http.Request) {
req.Header = ctx.Request.Header
req.Host = svcUrl.Host
Expand Down

0 comments on commit e8f16f8

Please sign in to comment.