Skip to content

Commit

Permalink
Merge pull request #2027 from openziti/fix.2026.root.version.handlers
Browse files Browse the repository at this point in the history
fixes #2026 root version handlers 404
  • Loading branch information
andrewpmartinez authored May 8, 2024
2 parents 7202172 + f5df9c7 commit 159d3c8
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 5 deletions.
6 changes: 5 additions & 1 deletion controller/server/client-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (clientApi ClientApiHandler) RootPath() string {
}

func (clientApi ClientApiHandler) IsHandler(r *http.Request) bool {
return strings.HasPrefix(r.URL.Path, clientApi.RootPath()) || r.URL.Path == WellKnownEstCaCerts
return strings.HasPrefix(r.URL.Path, clientApi.RootPath()) || r.URL.Path == WellKnownEstCaCerts || r.URL.Path == VersionPath || r.URL.Path == RootPath
}

func (clientApi ClientApiHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
Expand Down Expand Up @@ -166,6 +166,10 @@ func (clientApi ClientApiHandler) newHandler(ae *env.AppEnv) http.Handler {
r.URL.Path = controller.ClientRestApiBaseUrlLatest + WellKnownEstCaCerts
}

if r.URL.Path == VersionPath || r.URL.Path == RootPath {
r.URL.Path = controller.ClientRestApiBaseUrlLatest + VersionPath
}

if r.URL.Path == controller.ClientRestApiSpecUrl {

//work around for: https://github.com/go-openapi/runtime/issues/226
Expand Down
16 changes: 12 additions & 4 deletions controller/server/management-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@ import (
"fmt"
"github.com/openziti/edge-api/rest_management_api_client"
"github.com/openziti/edge-api/rest_management_api_server"
"github.com/openziti/xweb/v2"
"github.com/openziti/ziti/controller"
"github.com/openziti/ziti/controller/api"
"github.com/openziti/ziti/controller/apierror"
"github.com/openziti/ziti/controller/env"
"github.com/openziti/ziti/controller/response"
"github.com/openziti/ziti/controller/api"
"github.com/openziti/xweb/v2"
"net/http"
"strings"
"time"
)

const WellKnownEstCaCerts = "/.well-known/est/cacerts"
const (
WellKnownEstCaCerts = "/.well-known/est/cacerts"
VersionPath = "/version"
RootPath = "/"
)

var _ xweb.ApiHandlerFactory = &ManagementApiFactory{}

Expand Down Expand Up @@ -89,7 +93,7 @@ func (managementApi ManagementApiHandler) RootPath() string {
}

func (managementApi ManagementApiHandler) IsHandler(r *http.Request) bool {
return strings.HasPrefix(r.URL.Path, managementApi.RootPath()) || r.URL.Path == WellKnownEstCaCerts
return strings.HasPrefix(r.URL.Path, managementApi.RootPath()) || r.URL.Path == WellKnownEstCaCerts || r.URL.Path == VersionPath || r.URL.Path == RootPath
}

func (managementApi ManagementApiHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
Expand Down Expand Up @@ -125,6 +129,10 @@ func (managementApi ManagementApiHandler) newHandler(ae *env.AppEnv) http.Handle
r.URL.Path = controller.ManagementRestApiBaseUrlLatest + WellKnownEstCaCerts
}

if r.URL.Path == VersionPath || r.URL.Path == RootPath {
r.URL.Path = controller.ManagementRestApiBaseUrlLatest + VersionPath
}

rc := ae.CreateRequestContext(rw, r)

api.AddRequestContextToHttpContext(r, rc)
Expand Down
115 changes: 115 additions & 0 deletions tests/root_endpoint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package tests

import (
"testing"
)

func Test_Root_Endpoints(t *testing.T) {
ctx := NewTestContext(t)
defer ctx.Teardown()
ctx.StartServer()

t.Run("version can be retrieved", func(t *testing.T) {

t.Run("version can be retrieved from root path plus version", func(t *testing.T) {
ctx.testContextChanged(t)
versionRootUrl := "https://" + ctx.ApiHost + "/version"

resp, err := ctx.newAnonymousClientApiRequest().Get(versionRootUrl)
ctx.Req.NoError(err)
ctx.Req.NotNil(resp)
ctx.Req.Equal(200, resp.StatusCode())
ctx.Req.NotEmpty(resp.Body())
})

t.Run("version can be retrieved from root path", func(t *testing.T) {
ctx.testContextChanged(t)
versionRootUrl := "https://" + ctx.ApiHost + "/"

resp, err := ctx.newAnonymousClientApiRequest().Get(versionRootUrl)
ctx.Req.NoError(err)
ctx.Req.NotNil(resp)
ctx.Req.Equal(200, resp.StatusCode())
ctx.Req.NotEmpty(resp.Body())
})

t.Run("version can be retrieved from the base management path plus version", func(t *testing.T) {
ctx.testContextChanged(t)
versionRootUrl := "https://" + ctx.ApiHost + "/edge/management/v1/version"

resp, err := ctx.newAnonymousClientApiRequest().Get(versionRootUrl)
ctx.Req.NoError(err)
ctx.Req.NotNil(resp)
ctx.Req.Equal(200, resp.StatusCode())
ctx.Req.NotEmpty(resp.Body())
})

t.Run("version can be retrieved from the base management path", func(t *testing.T) {
ctx.testContextChanged(t)
versionRootUrl := "https://" + ctx.ApiHost + "/edge/management/v1"

resp, err := ctx.newAnonymousClientApiRequest().Get(versionRootUrl)
ctx.Req.NoError(err)
ctx.Req.NotNil(resp)
ctx.Req.Equal(200, resp.StatusCode())
ctx.Req.NotEmpty(resp.Body())
})

t.Run("version can be retrieved from the base client path plus version", func(t *testing.T) {
ctx.testContextChanged(t)
versionRootUrl := "https://" + ctx.ApiHost + "/edge/client/v1/version"

resp, err := ctx.newAnonymousClientApiRequest().Get(versionRootUrl)
ctx.Req.NoError(err)
ctx.Req.NotNil(resp)
ctx.Req.Equal(200, resp.StatusCode())
ctx.Req.NotEmpty(resp.Body())
})

t.Run("version can be retrieved from the base client path", func(t *testing.T) {
ctx.testContextChanged(t)
versionRootUrl := "https://" + ctx.ApiHost + "/edge/client/v1"

resp, err := ctx.newAnonymousClientApiRequest().Get(versionRootUrl)
ctx.Req.NoError(err)
ctx.Req.NotNil(resp)
ctx.Req.Equal(200, resp.StatusCode())
ctx.Req.NotEmpty(resp.Body())
})
})

t.Run(".well-known endpoint can be retrieved", func(t *testing.T) {
t.Run("well-known/est/ca/certs can be retrieved from root path", func(t *testing.T) {
ctx.testContextChanged(t)
caCertsRootUrl := "https://" + ctx.ApiHost + "/.well-known/est/cacerts"

resp, err := ctx.newAnonymousClientApiRequest().Get(caCertsRootUrl)
ctx.Req.NoError(err)
ctx.Req.NotNil(resp)
ctx.Req.Equal(200, resp.StatusCode())
ctx.Req.NotEmpty(resp.Body())
})

t.Run("well-known/est/ca/certs can be retrieved from management path", func(t *testing.T) {
ctx.testContextChanged(t)
caCertsRootUrl := "https://" + ctx.ApiHost + "/edge/management/v1//.well-known/est/cacerts"

resp, err := ctx.newAnonymousClientApiRequest().Get(caCertsRootUrl)
ctx.Req.NoError(err)
ctx.Req.NotNil(resp)
ctx.Req.Equal(200, resp.StatusCode())
ctx.Req.NotEmpty(resp.Body())
})

t.Run("well-known/est/ca/certs can be retrieved from client path", func(t *testing.T) {
ctx.testContextChanged(t)
caCertsRootUrl := "https://" + ctx.ApiHost + "/edge/client/v1/.well-known/est/cacerts"

resp, err := ctx.newAnonymousClientApiRequest().Get(caCertsRootUrl)
ctx.Req.NoError(err)
ctx.Req.NotNil(resp)
ctx.Req.Equal(200, resp.StatusCode())
ctx.Req.NotEmpty(resp.Body())
})
})
}

0 comments on commit 159d3c8

Please sign in to comment.