Repository navigation
Feat: Add token-exchange-aiac, an outbound token exchange with no routes #1496
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a22cdd3
feat: Add discovery-exchange, an outbound token exchange with no routes
anatolykoyfman 42be316
Merge remote-tracking branch 'origin/main' into feat/discovery-exchange
anatolykoyfman eaaa2dc
Merge remote-tracking branch 'origin/main' into feat/discovery-exchange
anatolykoyfman 315e431
feat: Align discovery-exchange's bearer handling with token-exchange
anatolykoyfman 9ea8df3
Merge remote-tracking branch 'origin/main' into feat/discovery-exchange
anatolykoyfman 934a575
refactor: Rename discovery-exchange to token-exchange-aiac
anatolykoyfman 3f36255
fix: Deny a disabled target as forbidden, not as a failed exchange
anatolykoyfman f9a9602
fix: Pass non-targets and opaque tunnels through without a bearer
anatolykoyfman 3e204e0
fix: Decode JWT claims once and keep the catalog in order
anatolykoyfman 34bcb92
test: Bound the live Keycloak test's HTTP calls with a timeout
anatolykoyfman 1049bd0
feat: Read the namespace for short names from a file
anatolykoyfman 60f396c
fix: Judge a rejected bearer and a foreign namespace by the destination
anatolykoyfman 7122cd6
fix: Bound the stale own-token retry and document the remaining outcomes
anatolykoyfman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| //go:build include_plugin_tokenexchangeaiac | ||
|
|
||
| package main | ||
|
|
||
| import _ "github.com/rossoctl/cortex/core/plugins/tokenexchangeaiac" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| //go:build include_plugin_tokenexchangeaiac | ||
|
|
||
| package main | ||
|
|
||
| import _ "github.com/rossoctl/cortex/core/plugins/tokenexchangeaiac" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| package tokenexchangeaiac | ||
|
|
||
| import ( | ||
| "crypto/sha256" | ||
| "encoding/hex" | ||
| "sync" | ||
| "time" | ||
| ) | ||
|
|
||
| const ( | ||
| // expirySkew is taken off every token's lifetime, so a token is never | ||
| // served from cache in the seconds before the IdP would reject it. | ||
| expirySkew = 30 * time.Second | ||
|
|
||
| // maxCacheEntries bounds each cache. A subject token is per user session, | ||
| // so a long-running agent sees an unbounded stream of them. | ||
| maxCacheEntries = 10000 | ||
| ) | ||
|
|
||
| type tokenEntry struct { | ||
| token string | ||
| expires time.Time | ||
| } | ||
|
|
||
| // tokenCache holds issued tokens by (subject, audience). Keys are a SHA-256 of | ||
| // the subject, so the cache never holds a usable copy of a caller's token. | ||
| type tokenCache struct { | ||
| mu sync.Mutex | ||
| now func() time.Time | ||
| entries map[string]tokenEntry | ||
| } | ||
|
|
||
| func newTokenCache(now func() time.Time) *tokenCache { | ||
| return &tokenCache{now: now, entries: make(map[string]tokenEntry)} | ||
| } | ||
|
|
||
| func tokenKey(subject, audience string) string { | ||
| sum := sha256.Sum256([]byte(subject)) | ||
| return hex.EncodeToString(sum[:]) + "\x00" + audience | ||
| } | ||
|
|
||
| func (c *tokenCache) get(key string) (string, bool) { | ||
| c.mu.Lock() | ||
| defer c.mu.Unlock() | ||
| e, ok := c.entries[key] | ||
| if !ok { | ||
| return "", false | ||
| } | ||
| if !c.now().Before(e.expires) { | ||
| delete(c.entries, key) | ||
| return "", false | ||
| } | ||
| return e.token, true | ||
| } | ||
|
|
||
| // drop forgets one entry. | ||
| func (c *tokenCache) drop(key string) { | ||
| c.mu.Lock() | ||
| defer c.mu.Unlock() | ||
| delete(c.entries, key) | ||
| } | ||
|
|
||
| // put caches a token for its lifetime less expirySkew. A token too short-lived | ||
| // to outlast the skew is not cached at all. | ||
| func (c *tokenCache) put(key string, tok issuedToken) { | ||
| if tok.expiresIn <= expirySkew { | ||
| return | ||
| } | ||
| c.mu.Lock() | ||
| defer c.mu.Unlock() | ||
| now := c.now() | ||
| if len(c.entries) >= maxCacheEntries { | ||
| for k, e := range c.entries { | ||
| if !now.Before(e.expires) { | ||
| delete(c.entries, k) | ||
| } | ||
| } | ||
| // Still full of live entries: start over rather than evict | ||
| // selectively. A miss costs one IdP round trip. | ||
| if len(c.entries) >= maxCacheEntries { | ||
| c.entries = make(map[string]tokenEntry) | ||
| } | ||
| } | ||
| c.entries[key] = tokenEntry{token: tok.accessToken, expires: now.Add(tok.expiresIn - expirySkew)} | ||
| } | ||
|
|
||
| // unknownCache remembers audiences the IdP said it has no client for, so that | ||
| // traffic to a non-target — an inference endpoint, a telemetry collector — costs | ||
| // one IdP round trip per TTL rather than one per request. The TTL is also how | ||
| // long a newly registered target keeps being passed through. | ||
| type unknownCache struct { | ||
| mu sync.Mutex | ||
| now func() time.Time | ||
| ttl time.Duration | ||
| entries map[string]time.Time | ||
| } | ||
|
|
||
| func newUnknownCache(now func() time.Time, ttl time.Duration) *unknownCache { | ||
| return &unknownCache{now: now, ttl: ttl, entries: make(map[string]time.Time)} | ||
| } | ||
|
|
||
| func (c *unknownCache) has(audience string) bool { | ||
| if c.ttl <= 0 { | ||
| return false | ||
| } | ||
| c.mu.Lock() | ||
| defer c.mu.Unlock() | ||
| exp, ok := c.entries[audience] | ||
| if !ok { | ||
| return false | ||
| } | ||
| if !c.now().Before(exp) { | ||
| delete(c.entries, audience) | ||
| return false | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| func (c *unknownCache) add(audience string) { | ||
| if c.ttl <= 0 { | ||
| return | ||
| } | ||
| c.mu.Lock() | ||
| defer c.mu.Unlock() | ||
| if len(c.entries) >= maxCacheEntries { | ||
| c.entries = make(map[string]time.Time) | ||
| } | ||
| c.entries[audience] = c.now().Add(c.ttl) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.