Skip to content

Commit

Permalink
chore: type for the Outputs removed and made it map[string]any to hav…
Browse files Browse the repository at this point in the history
…e better support in templates (#1594)
  • Loading branch information
dadrus authored Jul 3, 2024
1 parent 454e86e commit 8e4d74b
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 39 deletions.
4 changes: 2 additions & 2 deletions internal/handler/envoyextauth/grpcv3/request_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ func (r *RequestContext) SetPipelineError(err error) { r.err = err
func (r *RequestContext) AddHeaderForUpstream(name, value string) { r.upstreamHeaders.Add(name, value) }
func (r *RequestContext) AddCookieForUpstream(name, value string) { r.upstreamCookies[name] = value }

func (r *RequestContext) Outputs() heimdall.Outputs {
func (r *RequestContext) Outputs() map[string]any {
if r.outputs == nil {
r.outputs = make(heimdall.Outputs)
r.outputs = make(map[string]any)
}

return r.outputs
Expand Down
12 changes: 6 additions & 6 deletions internal/handler/requestcontext/mocks/context.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions internal/handler/requestcontext/request_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ func (r *RequestContext) UpstreamCookies() map[string]string { return r.ups
func (r *RequestContext) AppContext() context.Context { return r.req.Context() }
func (r *RequestContext) SetPipelineError(err error) { r.err = err }
func (r *RequestContext) PipelineError() error { return r.err }
func (r *RequestContext) Outputs() heimdall.Outputs {
func (r *RequestContext) Outputs() map[string]any {
if r.outputs == nil {
r.outputs = make(heimdall.Outputs)
r.outputs = make(map[string]any)
}

return r.outputs
Expand Down
16 changes: 1 addition & 15 deletions internal/heimdall/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,9 @@ package heimdall

import (
"context"
"crypto/sha256"
"net/url"

"github.com/goccy/go-json"
)

type Outputs map[string]any

func (o Outputs) Hash() []byte {
hash := sha256.New()
rawSub, _ := json.Marshal(o)

hash.Write(rawSub)

return hash.Sum(nil)
}

//go:generate mockery --name Context --structname ContextMock

type Context interface {
Expand All @@ -47,7 +33,7 @@ type Context interface {

SetPipelineError(err error)

Outputs() Outputs
Outputs() map[string]any
}

//go:generate mockery --name RequestFunctions --structname RequestFunctionsMock
Expand Down
12 changes: 6 additions & 6 deletions internal/heimdall/mocks/context.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ cookies:
ctx.EXPECT().AddCookieForUpstream("x_foo", "Bar")
ctx.EXPECT().AddCookieForUpstream("x_bar", "bar")
ctx.EXPECT().Request().Return(&heimdall.Request{RequestFunctions: reqf})
ctx.EXPECT().Outputs().Return(heimdall.Outputs{"foo": "bar"})
ctx.EXPECT().Outputs().Return(map[string]any{"foo": "bar"})
},
createSubject: func(t *testing.T) *subject.Subject {
t.Helper()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ headers:
ctx.EXPECT().AddHeaderForUpstream("X-Baz", "Bar")
ctx.EXPECT().AddHeaderForUpstream("X-Foo", "bar")
ctx.EXPECT().Request().Return(&heimdall.Request{RequestFunctions: reqf})
ctx.EXPECT().Outputs().Return(heimdall.Outputs{"foo": "bar"})
ctx.EXPECT().Outputs().Return(map[string]any{"foo": "bar"})
},
createSubject: func(t *testing.T) *subject.Subject {
t.Helper()
Expand Down
4 changes: 3 additions & 1 deletion internal/rules/mechanisms/finalizers/jwt_finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,9 @@ func (f *jwtFinalizer) calculateCacheKey(ctx heimdall.Context, sub *subject.Subj
func() []byte { return []byte{} }))
hash.Write(ttlBytes)
hash.Write(sub.Hash())
hash.Write(ctx.Outputs().Hash())

rawSub, _ := json.Marshal(ctx.Outputs())
hash.Write(rawSub)

return hex.EncodeToString(hash.Sum(nil))
}
Expand Down
10 changes: 5 additions & 5 deletions internal/rules/mechanisms/finalizers/jwt_finalizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ signer:
t.Helper()

ctx.EXPECT().AddHeaderForUpstream("Authorization", "Bearer TestToken")
ctx.EXPECT().Outputs().Return(heimdall.Outputs{"foo": "bar"})
ctx.EXPECT().Outputs().Return(map[string]any{"foo": "bar"})

cacheKey := fin.calculateCacheKey(ctx, sub)
cch.EXPECT().Get(mock.Anything, cacheKey).Return([]byte("TestToken"), nil)
Expand All @@ -778,7 +778,7 @@ ttl: 1m

ctx.EXPECT().AddHeaderForUpstream("Authorization",
mock.MatchedBy(func(val string) bool { return strings.HasPrefix(val, "Bearer ") }))
ctx.EXPECT().Outputs().Return(heimdall.Outputs{})
ctx.EXPECT().Outputs().Return(map[string]any{})

cch.EXPECT().Get(mock.Anything, mock.Anything).Return(nil, errors.New("no cache entry"))
cch.EXPECT().Set(mock.Anything, mock.Anything, mock.Anything, configuredTTL-defaultCacheLeeway).Return(nil)
Expand Down Expand Up @@ -812,7 +812,7 @@ claims: '{

ctx.EXPECT().AddHeaderForUpstream("X-Token",
mock.MatchedBy(func(val string) bool { return strings.HasPrefix(val, "Bar ") }))
ctx.EXPECT().Outputs().Return(heimdall.Outputs{"foo": "bar"})
ctx.EXPECT().Outputs().Return(map[string]any{"foo": "bar"})

cch.EXPECT().Get(mock.Anything, mock.Anything).Return(nil, errors.New("no cache entry"))
cch.EXPECT().Set(mock.Anything, mock.Anything, mock.Anything, defaultJWTTTL-defaultCacheLeeway).Return(nil)
Expand All @@ -838,7 +838,7 @@ claims: "foo: bar"
) {
t.Helper()

ctx.EXPECT().Outputs().Return(heimdall.Outputs{})
ctx.EXPECT().Outputs().Return(map[string]any{})

cch.EXPECT().Get(mock.Anything, mock.Anything).Return(nil, errors.New("no cache entry"))
},
Expand Down Expand Up @@ -869,7 +869,7 @@ claims: "{{ len .foobar }}"
) {
t.Helper()

ctx.EXPECT().Outputs().Return(heimdall.Outputs{})
ctx.EXPECT().Outputs().Return(map[string]any{})

cch.EXPECT().Get(mock.Anything, mock.Anything).Return(nil, errors.New("no cache entry"))
},
Expand Down

0 comments on commit 8e4d74b

Please sign in to comment.