cloudapi: add helper method to extract tenant channel from echo.Context

Extract the determination of tenant channel into a helper function.
This will simplify handler and middleware  methods, which won't have
to implement the same logic by themselves.

Fix the multi-tenancy unit test to pass the appropriate context when
querying compose statuses, because the server that is being use has JWT
enabled and expects the tenant to be set in it.
This commit is contained in:
Tomas Hozza 2022-06-03 17:30:32 +02:00 committed by Tom Gundersen
parent 947a083aae
commit 6fa2aa7b4a
3 changed files with 25 additions and 10 deletions

View file

@ -14,7 +14,6 @@ import (
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/osbuild/osbuild-composer/internal/auth"
"github.com/osbuild/osbuild-composer/internal/blueprint"
"github.com/osbuild/osbuild-composer/internal/common"
"github.com/osbuild/osbuild-composer/internal/distro"
@ -123,15 +122,9 @@ func (h *apiHandlers) PostCompose(ctx echo.Context) error {
}
// channel is empty if JWT is not enabled
var channel string
if h.server.config.JWTEnabled {
tenant, err := auth.GetFromClaims(ctx.Request().Context(), h.server.config.TenantProviderFields)
if err != nil {
return HTTPErrorWithInternal(ErrorTenantNotFound, err)
}
// prefix the tenant to prevent collisions if support for specifying channels in a request is ever added
channel = "org-" + tenant
channel, err := h.server.getTenantChannel(ctx)
if err != nil {
return HTTPErrorWithInternal(ErrorTenantNotFound, err)
}
distribution := h.server.distros.GetDistro(request.Distribution)

View file

@ -0,0 +1,21 @@
package v2
import (
"github.com/labstack/echo/v4"
"github.com/osbuild/osbuild-composer/internal/auth"
)
// getTenantChannel returns the tenant channel for the provided request context
func (s *Server) getTenantChannel(ctx echo.Context) (string, error) {
// channel is empty if JWT is not enabled
var channel string
if s.config.JWTEnabled {
tenant, err := auth.GetFromClaims(ctx.Request().Context(), s.config.TenantProviderFields)
if err != nil {
return "", err
}
// prefix the tenant to prevent collisions if support for specifying channels in a request is ever added
channel = "org-" + tenant
}
return channel, nil
}

View file

@ -267,6 +267,7 @@ func TestMultitenancy(t *testing.T) {
resp := test.APICall{
Handler: handler,
Method: http.MethodGet,
Context: reqContext(c.orgID),
Path: "/api/image-builder-composer/v2/composes/" + c.id.String(),
ExpectedStatus: http.StatusOK,
}.Do(t)