|
| 1 | +// Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package grpcauth |
| 16 | + |
| 17 | +import ( |
| 18 | + "crypto/sha256" |
| 19 | + "fmt" |
| 20 | + "time" |
| 21 | + |
| 22 | + lru "github.com/hashicorp/golang-lru/v2" |
| 23 | +) |
| 24 | + |
| 25 | +const ( |
| 26 | + // cacheMaxEntries bounds the cache. Each entry is one live publisher's |
| 27 | + // token, so this is far above the number of pods that can address a single |
| 28 | + // node's platform-connector; the bound exists so a caller presenting many |
| 29 | + // distinct tokens cannot grow the map without limit. |
| 30 | + cacheMaxEntries = 4096 |
| 31 | + |
| 32 | + // cacheTTL is how long a positive verdict is remembered, for every caller. |
| 33 | + // |
| 34 | + // It is a fixed value rather than the token's own expiry. TokenReview is the |
| 35 | + // only check that notices the bound pod being deleted, so the TTL is the |
| 36 | + // window in which a deleted pod's token still works. Honouring the token's |
| 37 | + // exp would make that window the token lifetime — an hour at the chart's |
| 38 | + // default — for every ordinary publisher. A flat two minutes bounds it for |
| 39 | + // everyone, costs about one TokenReview per token per two minutes, and |
| 40 | + // needs no assumption about what kind of credential TokenReview accepted. |
| 41 | + // It matches the API server's own webhook-authenticator cache default. |
| 42 | + cacheTTL = 2 * time.Minute |
| 43 | +) |
| 44 | + |
| 45 | +// verdictCache remembers successful authentication verdicts keyed by the token |
| 46 | +// that produced them, so a steady stream of requests from the same caller |
| 47 | +// costs one TokenReview per cacheTTL instead of one per request. |
| 48 | +// |
| 49 | +// Only positive verdicts are cached. A rejected token is re-reviewed every |
| 50 | +// time: rejections are rare in a healthy system, and remembering them would |
| 51 | +// add a second expiry policy for no measurable saving. |
| 52 | +type verdictCache struct { |
| 53 | + entries *lru.Cache[[sha256.Size]byte, cacheEntry] |
| 54 | +} |
| 55 | + |
| 56 | +type cacheEntry struct { |
| 57 | + identity Identity |
| 58 | + expires time.Time |
| 59 | +} |
| 60 | + |
| 61 | +func newVerdictCache() (*verdictCache, error) { |
| 62 | + entries, err := lru.New[[sha256.Size]byte, cacheEntry](cacheMaxEntries) |
| 63 | + if err != nil { |
| 64 | + return nil, fmt.Errorf("failed to build verdict cache: %w", err) |
| 65 | + } |
| 66 | + |
| 67 | + return &verdictCache{entries: entries}, nil |
| 68 | +} |
| 69 | + |
| 70 | +func (c *verdictCache) get(token string, now time.Time) (*Identity, bool) { |
| 71 | + key := sha256.Sum256([]byte(token)) |
| 72 | + |
| 73 | + entry, ok := c.entries.Get(key) |
| 74 | + if !ok { |
| 75 | + return nil, false |
| 76 | + } |
| 77 | + |
| 78 | + if now.After(entry.expires) { |
| 79 | + c.entries.Remove(key) |
| 80 | + |
| 81 | + return nil, false |
| 82 | + } |
| 83 | + |
| 84 | + // Identity is all value fields, so this copy fully isolates the caller from |
| 85 | + // the stored entry. |
| 86 | + identity := entry.identity |
| 87 | + |
| 88 | + return &identity, true |
| 89 | +} |
| 90 | + |
| 91 | +func (c *verdictCache) put(token string, identity *Identity, now time.Time) { |
| 92 | + c.entries.Add(sha256.Sum256([]byte(token)), cacheEntry{ |
| 93 | + identity: *identity, |
| 94 | + expires: now.Add(cacheTTL), |
| 95 | + }) |
| 96 | +} |
0 commit comments