Skip to content

Commit

Permalink
fix(Handler): Update tenant name resolution to match new api (#389)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan P committed Feb 28, 2023
1 parent 104125b commit b806f91
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
12 changes: 5 additions & 7 deletions e2core/auth/access.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package auth

import (
"context"
"encoding/json"
"fmt"
"net/http"
Expand All @@ -18,9 +17,9 @@ import (

type TenantInfo struct {
AuthorizedParty string `json:"authorized_party"`
Organization string `json:"organization"`
Environment string `json:"environment"`
Tenant string `json:"tenant"`
ID string `json:"id"`
Name string `json:"name"`
}

func AuthorizationMiddleware(opts *options.Options) echo.MiddlewareFunc {
Expand All @@ -37,8 +36,7 @@ func AuthorizationMiddleware(opts *options.Options) echo.MiddlewareFunc {
return echo.NewHTTPError(http.StatusUnauthorized).SetInternal(err)
}

c.Set("ident", tntInfo.Tenant)
c.SetRequest(c.Request().WithContext(context.WithValue(c.Request().Context(), "ident", tntInfo.Tenant)))
c.Set("ident", tntInfo.ID)

return next(c)
}
Expand All @@ -51,7 +49,7 @@ func NewApiAuthClient(opts *options.Options) *AuthzClient {
Timeout: 20 * time.Second,
Transport: http.DefaultTransport,
},
location: opts.ControlPlane + "/api/v1/tenant/%s",
location: opts.ControlPlane + "/environment/v1/tenant/",
cache: NewAuthorizationCache(opts.AuthCacheTTL),
}
}
Expand All @@ -74,7 +72,7 @@ func (client *AuthzClient) Authorize(token system.Credential, identifier, namesp

func (client *AuthzClient) loadAuth(token system.Credential, identifier string) func() (*TenantInfo, error) {
return func() (*TenantInfo, error) {
authzReq, err := http.NewRequest(http.MethodGet, fmt.Sprintf(client.location, identifier), nil)
authzReq, err := http.NewRequest(http.MethodGet, client.location+identifier, nil)
if err != nil {
return nil, common.Error(err, "post authorization request")
}
Expand Down
18 changes: 7 additions & 11 deletions e2core/auth/authorizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ func TestAuthorizerCache_ConcurrentRequests(t *testing.T) {
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(&TenantInfo{
AuthorizedParty: "tester",
Organization: "acct",
Environment: "env",
Tenant: "123",
ID: "tnt",
})
},
assertOpts: func(t *testing.T, actual uint64) bool {
Expand Down Expand Up @@ -96,7 +95,7 @@ func TestAuthorizerCache_ConcurrentRequests(t *testing.T) {

authorizer := &AuthzClient{
httpClient: svr.Client(),
location: svr.URL + "/api/v2/tenant/%s",
location: svr.URL + "/environment/v1/tenant/",
cache: newAuthorizationCache(common.StableTime(time.Now()), 10*time.Minute),
}

Expand Down Expand Up @@ -175,9 +174,8 @@ func TestAuthorizerCache(t *testing.T) {
env, tenant, _ := strings.Cut(ident, ".")
_ = json.NewEncoder(w).Encode(&TenantInfo{
AuthorizedParty: "tester",
Organization: "acct",
Environment: env,
Tenant: tenant,
ID: tenant,
})
},
assertOpts: func(t *testing.T, actual uint64) bool {
Expand Down Expand Up @@ -247,9 +245,8 @@ func TestAuthorizerCache(t *testing.T) {
env, tenant, _ := strings.Cut(ident, ".")
_ = json.NewEncoder(w).Encode(&TenantInfo{
AuthorizedParty: "tester",
Organization: "acct",
Environment: env,
Tenant: tenant,
ID: tenant,
})
}
},
Expand All @@ -269,7 +266,7 @@ func TestAuthorizerCache(t *testing.T) {

authorizer := &AuthzClient{
httpClient: svr.Client(),
location: svr.URL + "/api/v2/tenant/%s",
location: svr.URL + "/api/v2/tenant/",
cache: newAuthorizationCache(common.StableTime(time.Now()), 10*time.Minute),
}

Expand Down Expand Up @@ -306,9 +303,8 @@ func TestAuthorizerCache_ExpiringEntry(t *testing.T) {
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(&TenantInfo{
AuthorizedParty: "tester",
Organization: "acct",
Environment: "env",
Tenant: "123",
ID: "123",
})
},
assertOpts: func(t *testing.T, actual uint64) bool {
Expand All @@ -332,7 +328,7 @@ func TestAuthorizerCache_ExpiringEntry(t *testing.T) {

authorizer := &AuthzClient{
httpClient: svr.Client(),
location: svr.URL + "/api/v2/tenant/%s",
location: svr.URL + "/api/v2/tenant/",
cache: authzCache,
}

Expand Down
15 changes: 12 additions & 3 deletions e2core/server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (

func (s *Server) executePluginByNameHandler() echo.HandlerFunc {
return func(c echo.Context) error {
ident := c.Param("ident")
namespace := c.Param("namespace")
name := c.Param("name")
ident := ReadParam(c, "ident")
namespace := ReadParam(c, "namespace")
name := ReadParam(c, "name")

mod := s.syncer.GetModuleByName(ident, namespace, name)
if mod == nil {
Expand Down Expand Up @@ -203,3 +203,12 @@ func (s *Server) healthHandler() echo.HandlerFunc {
return c.JSON(http.StatusOK, map[string]bool{"healthy": true})
}
}

func ReadParam(ctx echo.Context, name string) string {
v := ctx.Get(name)
if v != nil {
return v.(string)
}

return ctx.Param(name)
}

0 comments on commit b806f91

Please sign in to comment.