Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Composite configurable mutator cache key per rule. #885

Draft
wants to merge 24 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .schema/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,11 @@
"enabled": {
"$ref": "#/definitions/handlerSwitch"
},
"key": {
"type": "string",
"title": "Cache key",
"description": "Define custom cache key, i.e '{{ print .Subject }}'. All properties must be part of AuthenticationSession."
anderslauri marked this conversation as resolved.
Show resolved Hide resolved
},
"ttl": {
"type": "string",
"pattern": "^[0-9]+(ns|us|ms|s|m|h)$",
Expand Down
29 changes: 20 additions & 9 deletions pipeline/mutate/mutator_hydrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ type externalAPIConfig struct {
type cacheConfig struct {
Enabled bool `json:"enabled"`
TTL string `json:"ttl"`

ttl time.Duration
Key string `json:"key"`
ttl time.Duration
}

type MutatorHydratorConfig struct {
Expand Down Expand Up @@ -114,16 +114,16 @@ func (a *MutatorHydrator) GetID() string {
return "hydrator"
}

func (a *MutatorHydrator) cacheKey(config *MutatorHydratorConfig, session string) string {
return fmt.Sprintf("%s|%x", config.Api.URL, md5.Sum([]byte(session)))
func (a *MutatorHydrator) cacheKey(config *MutatorHydratorConfig, key string) string {
return fmt.Sprintf("%s|%x", config.Api.URL, md5.Sum([]byte(key)))
anderslauri marked this conversation as resolved.
Show resolved Hide resolved
}

func (a *MutatorHydrator) hydrateFromCache(config *MutatorHydratorConfig, session string) (*authn.AuthenticationSession, bool) {
func (a *MutatorHydrator) hydrateFromCache(config *MutatorHydratorConfig, key string) (*authn.AuthenticationSession, bool) {
if !config.Cache.Enabled {
return nil, false
}

item, found := a.hydrateCache.Get(a.cacheKey(config, session))
item, found := a.hydrateCache.Get(a.cacheKey(config, key))
if !found {
return nil, false
}
Expand All @@ -141,12 +141,20 @@ func (a *MutatorHydrator) hydrateToCache(config *MutatorHydratorConfig, key stri
}
}

func (a *MutatorHydrator) Mutate(r *http.Request, session *authn.AuthenticationSession, config json.RawMessage, _ pipeline.Rule) error {
func (a *MutatorHydrator) Mutate(r *http.Request, session *authn.AuthenticationSession, config json.RawMessage, p pipeline.Rule) error {
cfg, err := a.Config(config)
if err != nil {
return err
}

if len(cfg.Cache.Key) > 0 {
if cacheSession, ok := a.hydrateFromCache(cfg, cfg.Cache.Key+p.GetID()); ok {
*session = *cacheSession
return nil
}
a.d.Logger().Debugf("Cache key %s in rule %s was not found. Falling back on default.",
cfg.Cache.Key, p.GetID())
}
var b bytes.Buffer
if err := json.NewEncoder(&b).Encode(session); err != nil {
return errors.WithStack(err)
Expand Down Expand Up @@ -234,8 +242,11 @@ func (a *MutatorHydrator) Mutate(r *http.Request, session *authn.AuthenticationS
}
*session = sessionFromUpstream

a.hydrateToCache(cfg, encodedSession, session)

if len(cfg.Cache.Key) > 0 {
a.hydrateToCache(cfg, cfg.Cache.Key+p.GetID(), session)
} else {
a.hydrateToCache(cfg, encodedSession, session)
}
return nil
}

Expand Down
16 changes: 16 additions & 0 deletions pipeline/mutate/mutator_hydrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ func configWithRetriesForMutator(giveUpAfter, retryDelay string) func(*httptest.
}
}

func configWithSpecialCacheKey(key string) func(*httptest.Server) json.RawMessage {
return func(s *httptest.Server) json.RawMessage {
return []byte(fmt.Sprintf(`{"api": {"url": "%s"}, "cache": {"key": "%s"}}`, s.URL, key))
}
}

func TestMutatorHydrator(t *testing.T) {
conf := internal.NewConfigurationWithDefaults()
reg := internal.NewRegistry(conf)
Expand All @@ -148,6 +154,7 @@ func TestMutatorHydrator(t *testing.T) {
sampleUserId := "user"
sampleValidPassword := "passwd1"
sampleNotValidPassword := "passwd7"

var testMap = map[string]struct {
Setup func(*testing.T) http.Handler
Session *authn.AuthenticationSession
Expand Down Expand Up @@ -346,6 +353,15 @@ func TestMutatorHydrator(t *testing.T) {
Match: newAuthenticationSession(),
Err: nil,
},
"Custom Cache Key": {
Setup: defaultRouterSetup(),
Session: newAuthenticationSession(setSubject(sampleSubject)),
Rule: &rule.Rule{ID: "test-rule"},
Config: configWithSpecialCacheKey(sampleSubject),
anderslauri marked this conversation as resolved.
Show resolved Hide resolved
Request: &http.Request{},
Match: newAuthenticationSession(setSubject(sampleSubject)),
Err: nil,
},
}

for testName, specs := range testMap {
Expand Down