Skip to content

Commit

Permalink
refactor: upgrade to identity v2
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Zapletal <[email protected]>
  • Loading branch information
lzap committed Jan 24, 2024
1 parent d5523c4 commit 7d3f09b
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 164 deletions.
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ require (
github.com/oapi-codegen/runtime v1.1.1
github.com/prometheus/client_golang v1.18.0
github.com/redhatinsights/app-common-go v1.6.7
github.com/redhatinsights/platform-go-middlewares v1.0.0
github.com/redhatinsights/platform-go-middlewares/v2 v2.0.0-beta.1
github.com/redis/go-redis/v9 v9.4.0
github.com/riandyrn/otelchi v0.5.1
github.com/rs/zerolog v1.31.0
Expand Down Expand Up @@ -85,7 +85,6 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
Expand Down Expand Up @@ -129,7 +128,6 @@ require (
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/oleiade/lane/v2 v2.0.0 // indirect
github.com/onsi/gomega v1.27.6 // indirect
github.com/perimeterx/marshmallow v1.1.5 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
Expand Down
51 changes: 5 additions & 46 deletions go.sum

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion internal/headers/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/RHEnVision/provisioning-backend/internal/config"
"github.com/RHEnVision/provisioning-backend/internal/logging"
"github.com/redhatinsights/platform-go-middlewares/identity"
"github.com/redhatinsights/platform-go-middlewares/v2/identity"
"github.com/rs/zerolog"
)

Expand All @@ -21,6 +21,8 @@ func addIdentityHeader(ctx context.Context, req *http.Request, username, passwor
zerolog.Ctx(ctx).Warn().Msgf("Username/password authentication: %s", username)
req.Header.Add("Authorization", "Basic "+basicAuth(username, password))
} else {
logger := zerolog.Ctx(ctx)
logger.Trace().Str("identity", identity.GetIdentityHeader(ctx)).Msg("HTTP client identity set")
req.Header.Set("X-RH-Identity", identity.GetIdentityHeader(ctx))
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions internal/identity/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ const (
accountIdCtxKey cxtKeyId = iota
)

var MissingAccountInContextError = errors.New("operation requires account_id in context")
var ErrMissingAccountInContext = errors.New("operation requires account_id in context")

// AccountId returns current account model or panics when not set
func AccountId(ctx context.Context) int64 {
value := ctx.Value(accountIdCtxKey)
if value == nil {
panic(MissingAccountInContextError)
panic(ErrMissingAccountInContext)
}
return value.(int64)
}
Expand Down
12 changes: 4 additions & 8 deletions internal/identity/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@ import (
"encoding/json"
"fmt"

"github.com/redhatinsights/platform-go-middlewares/identity"
"github.com/redhatinsights/platform-go-middlewares/v2/identity"
)

type Principal = identity.XRHID

// Identity returns identity header struct or nil when not set.
func Identity(ctx context.Context) Principal {
val := ctx.Value(identity.Key)
if val == nil {
return Principal{}
}
return val.(Principal)
return identity.GetIdentity(ctx)
}

// IdentityHeader returns identity header (base64-encoded JSON)
Expand All @@ -27,7 +23,7 @@ func IdentityHeader(ctx context.Context) string {

// WithIdentity returns context copy with identity.
func WithIdentity(ctx context.Context, id Principal) context.Context {
return context.WithValue(ctx, identity.Key, id)
return identity.WithIdentity(ctx, id)
}

// WithIdentityFrom64 returns context copy with identity parsed from base64-encoded JSON string.
Expand All @@ -43,5 +39,5 @@ func WithIdentityFrom64(ctx context.Context, id string) (context.Context, error)
return nil, fmt.Errorf("could not unmarshal json %w", err)
}

return context.WithValue(ctx, identity.Key, jsonData), nil
return WithIdentity(ctx, jsonData), nil
}
100 changes: 0 additions & 100 deletions internal/middleware/enforce_identity.go

This file was deleted.

10 changes: 9 additions & 1 deletion internal/routes/all_routes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package routes

import (
"context"
"fmt"
"net/http"

Expand All @@ -11,6 +12,8 @@ import (
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
redoc "github.com/go-openapi/runtime/middleware"
"github.com/redhatinsights/platform-go-middlewares/v2/identity"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

Expand Down Expand Up @@ -42,6 +45,11 @@ func MountRoot(r *chi.Mux) {
})
}

func IdentityErrorLogFunc(ctx context.Context, rawId, msg string) {
logger := zerolog.Ctx(ctx)
logger.Error().Str("identity", rawId).Msgf("identity enforcement error: %s", msg)
}

func MountAPI(r *chi.Mux) {
r.Route("/openapi.json", func(r chi.Router) {
r.Use(middleware.ETagMiddleware(api.ETagValue))
Expand All @@ -56,7 +64,7 @@ func MountAPI(r *chi.Mux) {
r.Group(func(r chi.Router) {
r.Use(render.SetContentType(render.ContentTypeJSON))

r.Use(middleware.EnforceIdentity)
r.Use(identity.EnforceIdentityWithLogger(IdentityErrorLogFunc))
r.Use(middleware.AccountMiddleware)

// OpenAPI documented and supported routes
Expand Down
6 changes: 3 additions & 3 deletions internal/testing/identity/dummy_identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/RHEnVision/provisioning-backend/internal/dao"
"github.com/RHEnVision/provisioning-backend/internal/identity"
"github.com/RHEnVision/provisioning-backend/internal/ptr"
rhidentity "github.com/redhatinsights/platform-go-middlewares/identity"
rhidentity "github.com/redhatinsights/platform-go-middlewares/v2/identity"
)

const (
Expand All @@ -28,11 +28,11 @@ func AddIdentityHeader(t *testing.T, req *http.Request) *http.Request {
}

func WithIdentity(t *testing.T, ctx context.Context) context.Context {
return context.WithValue(ctx, rhidentity.Key, xRhId)
return rhidentity.WithIdentity(ctx, xRhId)
}

func WithCustomIdentity(t *testing.T, ctx context.Context, orgId string, accountNumber *string) context.Context {
return context.WithValue(ctx, rhidentity.Key, newIdentity(orgId, accountNumber))
return rhidentity.WithIdentity(ctx, newIdentity(orgId, accountNumber))
}

func WithTenant(t *testing.T, ctx context.Context) context.Context {
Expand Down

0 comments on commit 7d3f09b

Please sign in to comment.