Skip to content

Commit

Permalink
Merge pull request #2410 from openziti/fix.2405.fix.legacy.sdk.endpoints
Browse files Browse the repository at this point in the history
fix #2405 fix multiple default APIs, tests
  • Loading branch information
andrewpmartinez authored Sep 18, 2024
2 parents f1da24e + 22a800b commit 44e84d0
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 17 deletions.
6 changes: 5 additions & 1 deletion controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ func (c *Controller) initWeb() {
logrus.WithError(err).Fatalf("failed to create health checks api factory")
}

if err = c.xweb.GetRegistry().Add(webapis.NewFabricManagementApiFactory(c.config.Id, c.network, &c.xmgmts)); err != nil {
fabricManagementFactory := webapis.NewFabricManagementApiFactory(c.config.Id, c.network, &c.xmgmts)
if err = c.xweb.GetRegistry().Add(fabricManagementFactory); err != nil {
logrus.WithError(err).Fatalf("failed to create management api factory")
}

Expand Down Expand Up @@ -327,6 +328,9 @@ func (c *Controller) initWeb() {
}

webapis.OverrideRequestWrapper(webapis.NewFabricApiWrapper(c.env))
} else {
// if no edge we need 1 default API, make the fabric api the default
fabricManagementFactory.MakeDefault = true
}
c.xwebInitialized.MarkInitialized()
}
Expand Down
26 changes: 17 additions & 9 deletions controller/webapis/fabric-management-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ const (
var _ xweb.ApiHandlerFactory = &FabricManagementApiFactory{}

type FabricManagementApiFactory struct {
InitFunc func(managementApi *FabricManagementApiHandler) error
network *network.Network
nodeId identity.Identity
xmgmts *concurrenz.CopyOnWriteSlice[xmgmt.Xmgmt]
InitFunc func(managementApi *FabricManagementApiHandler) error
network *network.Network
nodeId identity.Identity
xmgmts *concurrenz.CopyOnWriteSlice[xmgmt.Xmgmt]
MakeDefault bool
}

func (factory *FabricManagementApiFactory) Validate(_ *xweb.InstanceConfig) error {
Expand All @@ -58,9 +59,10 @@ func (factory *FabricManagementApiFactory) Validate(_ *xweb.InstanceConfig) erro
func NewFabricManagementApiFactory(nodeId identity.Identity, network *network.Network, xmgmts *concurrenz.CopyOnWriteSlice[xmgmt.Xmgmt]) *FabricManagementApiFactory {
pfxlog.Logger().Infof("initializing management api factory with %d xmgmt instances", len(xmgmts.Value()))
return &FabricManagementApiFactory{
network: network,
nodeId: nodeId,
xmgmts: xmgmts,
network: network,
nodeId: nodeId,
xmgmts: xmgmts,
MakeDefault: false,
}
}

Expand Down Expand Up @@ -88,7 +90,7 @@ func (factory *FabricManagementApiFactory) New(_ *xweb.ServerConfig, options map
router.Register(fabricAPI, requestWrapper)
}

managementApiHandler, err := NewFabricManagementApiHandler(fabricAPI, options)
managementApiHandler, err := NewFabricManagementApiHandler(fabricAPI, factory.MakeDefault, options)

if err != nil {
return nil, err
Expand All @@ -105,10 +107,11 @@ func (factory *FabricManagementApiFactory) New(_ *xweb.ServerConfig, options map
return managementApiHandler, nil
}

func NewFabricManagementApiHandler(fabricApi *operations.ZitiFabricAPI, options map[interface{}]interface{}) (*FabricManagementApiHandler, error) {
func NewFabricManagementApiHandler(fabricApi *operations.ZitiFabricAPI, isDefault bool, options map[interface{}]interface{}) (*FabricManagementApiHandler, error) {
managementApi := &FabricManagementApiHandler{
fabricApi: fabricApi,
options: options,
isDefault: isDefault,
}

managementApi.handler = managementApi.newHandler()
Expand All @@ -125,6 +128,7 @@ type FabricManagementApiHandler struct {
wsUrl string
options map[interface{}]interface{}
bindHandler channel.BindHandler
isDefault bool
}

func (managementApi *FabricManagementApiHandler) Binding() string {
Expand Down Expand Up @@ -156,6 +160,10 @@ func (managementApi *FabricManagementApiHandler) newHandler() http.Handler {
return requestWrapper.WrapHttpHandler(innerManagementHandler)
}

func (managementApi *FabricManagementApiHandler) IsDefault() bool {
return managementApi.isDefault
}

func (managementApi *FabricManagementApiHandler) handleWebSocket(writer http.ResponseWriter, request *http.Request) {
log := pfxlog.Logger()
log.Debug("handling mgmt channel websocket upgrade")
Expand Down
2 changes: 1 addition & 1 deletion controller/webapis/oidc-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (h OidcApiHandler) ServeHTTP(writer http.ResponseWriter, request *http.Requ
}

func (h OidcApiHandler) IsDefault() bool {
return true
return false
}

func NewOidcApiHandler(serverConfig *xweb.ServerConfig, ae *env.AppEnv, options map[interface{}]interface{}) (*OidcApiHandler, error) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ require (
github.com/openziti/storage v0.3.1
github.com/openziti/transport/v2 v2.0.146
github.com/openziti/x509-claims v1.0.3
github.com/openziti/xweb/v2 v2.1.1
github.com/openziti/xweb/v2 v2.1.2
github.com/openziti/ziti-db-explorer v1.1.3
github.com/orcaman/concurrent-map/v2 v2.0.1
github.com/pkg/errors v0.9.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -598,8 +598,8 @@ github.com/openziti/transport/v2 v2.0.146 h1:Wdr4udri/fFpdj9GR9DR7/FKqt/2cMTgBdt
github.com/openziti/transport/v2 v2.0.146/go.mod h1:ULrJdwxs0sKmjAhen9Vk9E+Do4qpdDdx1YJeVVu3bZ4=
github.com/openziti/x509-claims v1.0.3 h1:HNdQ8Nf1agB3lBs1gahcO6zfkeS4S5xoQ2/PkY4HRX0=
github.com/openziti/x509-claims v1.0.3/go.mod h1:Z0WIpBm6c4ecrpRKrou6Gk2wrLWxJO/+tuUwKh8VewE=
github.com/openziti/xweb/v2 v2.1.1 h1:T6vbmG2189WWwq16wryM7RQEbT5wNARrVHNQs23jEPE=
github.com/openziti/xweb/v2 v2.1.1/go.mod h1:d9+vBsVCONyb3GCrJPHb2+GfTJ4MMIu0i6S71uE3WHc=
github.com/openziti/xweb/v2 v2.1.2 h1:435lpiXOkXwos71Dp4UCOjaFdnp32aQyvOjQ6uB+4X4=
github.com/openziti/xweb/v2 v2.1.2/go.mod h1:d9+vBsVCONyb3GCrJPHb2+GfTJ4MMIu0i6S71uE3WHc=
github.com/openziti/ziti-db-explorer v1.1.3 h1:9JER16MJzagtYPdGEhgDcw2p/BXNCVbf9IgA/sMB52w=
github.com/openziti/ziti-db-explorer v1.1.3/go.mod h1:pMIMNJoTRSTbkO2e7cZWiBokA3jMdeiGAILP3QhU+v8=
github.com/orcaman/concurrent-map/v2 v2.0.1 h1:jOJ5Pg2w1oeB6PeDurIYf6k9PQ+aTITr/6lP/L/zp6c=
Expand Down
67 changes: 67 additions & 0 deletions tests/endpoint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package tests

import (
"testing"
)

// Test_Endpoints does HTTP testing against public entry URLs to ensure they continue to function.
// Non-prefixed paths are deprecated, but some older clients do not use the edge/client/v1 path.
// The .well-known path has many handlers among different APIs and tests for those should exist
// perpetuity.
func Test_Endpoints(t *testing.T) {
ctx := NewTestContext(t)
defer ctx.Teardown()
ctx.StartServer()

t.Run("non-prefixed path defaults for enrollment", func(t *testing.T) {
ctx.testContextChanged(t)

rootPathClient, _, _ := ctx.NewClientComponents("/")

resp, err := rootPathClient.R().Post("https://" + ctx.ApiHost + "/enroll")

ctx.Req.NoError(err)
ctx.Req.Equal(400, resp.StatusCode())
ctx.Req.Equal("application/json", resp.Header().Get("Content-Type"))
ctx.Req.NotEmpty(resp.Body())
})

t.Run("non-prefixed path defaults for authentication", func(t *testing.T) {
ctx.testContextChanged(t)

rootPathClient, _, _ := ctx.NewClientComponents("/")

resp, err := rootPathClient.R().Post("https://" + ctx.ApiHost + "/authenticate")

ctx.Req.NoError(err)
ctx.Req.Equal(400, resp.StatusCode())
ctx.Req.Equal("application/json", resp.Header().Get("Content-Type"))
ctx.Req.NotEmpty(resp.Body())
})

t.Run("oidc-configuration works on .well-known", func(t *testing.T) {
ctx.testContextChanged(t)

rootPathClient, _, _ := ctx.NewClientComponents("/")

resp, err := rootPathClient.R().Get("https://" + ctx.ApiHost + "/.well-known/openid-configuration")

ctx.Req.NoError(err)
ctx.Req.Equal(200, resp.StatusCode())
ctx.Req.Equal("application/json", resp.Header().Get("Content-Type"))
ctx.Req.NotEmpty(resp.Body())
})

t.Run("est castore works on .well-known", func(t *testing.T) {
ctx.testContextChanged(t)

rootPathClient, _, _ := ctx.NewClientComponents("/")

resp, err := rootPathClient.R().Get("https://" + ctx.ApiHost + "/.well-known/est/cacerts")

ctx.Req.NoError(err)
ctx.Req.Equal(200, resp.StatusCode())
ctx.Req.Equal("application/pkcs7-mime", resp.Header().Get("Content-Type"))
ctx.Req.NotEmpty(resp.Body())
})
}
2 changes: 1 addition & 1 deletion zititest/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ require (
github.com/openziti/runzmd v1.0.51 // indirect
github.com/openziti/secretstream v0.1.24 // indirect
github.com/openziti/x509-claims v1.0.3 // indirect
github.com/openziti/xweb/v2 v2.1.1 // indirect
github.com/openziti/xweb/v2 v2.1.2 // indirect
github.com/openziti/ziti-db-explorer v1.1.3 // indirect
github.com/parallaxsecond/parsec-client-go v0.0.0-20221025095442-f0a77d263cf9 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
Expand Down
4 changes: 2 additions & 2 deletions zititest/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -622,8 +622,8 @@ github.com/openziti/transport/v2 v2.0.146 h1:Wdr4udri/fFpdj9GR9DR7/FKqt/2cMTgBdt
github.com/openziti/transport/v2 v2.0.146/go.mod h1:ULrJdwxs0sKmjAhen9Vk9E+Do4qpdDdx1YJeVVu3bZ4=
github.com/openziti/x509-claims v1.0.3 h1:HNdQ8Nf1agB3lBs1gahcO6zfkeS4S5xoQ2/PkY4HRX0=
github.com/openziti/x509-claims v1.0.3/go.mod h1:Z0WIpBm6c4ecrpRKrou6Gk2wrLWxJO/+tuUwKh8VewE=
github.com/openziti/xweb/v2 v2.1.1 h1:T6vbmG2189WWwq16wryM7RQEbT5wNARrVHNQs23jEPE=
github.com/openziti/xweb/v2 v2.1.1/go.mod h1:d9+vBsVCONyb3GCrJPHb2+GfTJ4MMIu0i6S71uE3WHc=
github.com/openziti/xweb/v2 v2.1.2 h1:435lpiXOkXwos71Dp4UCOjaFdnp32aQyvOjQ6uB+4X4=
github.com/openziti/xweb/v2 v2.1.2/go.mod h1:d9+vBsVCONyb3GCrJPHb2+GfTJ4MMIu0i6S71uE3WHc=
github.com/openziti/ziti-db-explorer v1.1.3 h1:9JER16MJzagtYPdGEhgDcw2p/BXNCVbf9IgA/sMB52w=
github.com/openziti/ziti-db-explorer v1.1.3/go.mod h1:pMIMNJoTRSTbkO2e7cZWiBokA3jMdeiGAILP3QhU+v8=
github.com/orcaman/concurrent-map/v2 v2.0.1 h1:jOJ5Pg2w1oeB6PeDurIYf6k9PQ+aTITr/6lP/L/zp6c=
Expand Down

0 comments on commit 44e84d0

Please sign in to comment.