Repository navigation
feat(authzen): report data freshness and a decision id in evaluation responses #224
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
Open
leifj
wants to merge
8
commits into
main
Choose a base branch
from
feat/221-trust-data
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e302715
feat(authzen): report data freshness and a decision id in evaluation …
leifj 2df8fe1
fix(authzen): address review of freshness reporting
leifj 7455395
fix(authzen): never overstate freshness; cut duplicated reporter code
leifj af1629e
fix(authzen): unknown freshness for failed pointers and expired caches
leifj 3eabba0
fix(authzen): freshness unknown for failed/circuit-skipped denials; T…
leifj c5bcda1
fix(registry): carry registry failures through all/best_match and com…
leifj 5a7cd8a
fix(registry): nil composite child is incomplete; trim duplicated code
leifj 8c2cc5f
refactor(registry): move strategy dispatch and trust-data attach out …
leifj 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
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,59 @@ | ||
| package etsi | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/sirosfoundation/g119612/pkg/etsi119612" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func tslWithNextUpdate(dt string) *etsi119612.TSL { | ||
| tsl := &etsi119612.TSL{} | ||
| tsl.StatusList.TslSchemeInformation = &etsi119612.TSLSchemeInformationType{} | ||
| if dt != "" { | ||
| tsl.StatusList.TslSchemeInformation.TslNextUpdate = &etsi119612.NextUpdateType{DateTime: dt} | ||
| } | ||
| return tsl | ||
| } | ||
|
|
||
| func TestFreshness(t *testing.T) { | ||
| loaded := time.Date(2026, 10, 19, 12, 0, 0, 0, time.UTC) | ||
|
|
||
| r := &TSLRegistry{loadedAt: loaded, tsls: []*etsi119612.TSL{ | ||
| tslWithNextUpdate("2026-11-01T00:00:00Z"), | ||
| tslWithNextUpdate("2026-10-26T00:00:00Z"), | ||
| tslWithNextUpdate(""), | ||
| nil, | ||
| }} | ||
| f := r.Freshness() | ||
| assert.True(t, f.AsOf.Equal(loaded)) | ||
| assert.True(t, f.NextUpdate.IsZero(), "one undated TSL makes the registry's NextUpdate unknown") | ||
|
|
||
| r = &TSLRegistry{loadedAt: loaded, tsls: []*etsi119612.TSL{ | ||
| tslWithNextUpdate("2026-11-01T00:00:00Z"), | ||
| tslWithNextUpdate("2026-10-26T00:00:00Z"), | ||
| nil, | ||
| }} | ||
| assert.True(t, r.Freshness().NextUpdate.Equal(time.Date(2026, 10, 26, 0, 0, 0, 0, time.UTC)), | ||
| "the registry is only as fresh as its soonest-expiring TSL") | ||
|
|
||
| r = &TSLRegistry{loadedAt: loaded, tsls: []*etsi119612.TSL{tslWithNextUpdate("not a date"), tslWithNextUpdate("2026-10-26T00:00:00Z")}} | ||
| assert.True(t, r.Freshness().NextUpdate.IsZero(), "an unparseable date is unknown") | ||
|
|
||
| assert.True(t, (&TSLRegistry{loadedAt: loaded}).Freshness().NextUpdate.IsZero(), "no TSLs: no bound") | ||
| } | ||
|
|
||
| func TestFreshness_UnknownWithPEMBundle(t *testing.T) { | ||
| loaded := time.Date(2026, 10, 19, 12, 0, 0, 0, time.UTC) | ||
| tsls := []*etsi119612.TSL{tslWithNextUpdate("2026-10-26T00:00:00Z")} | ||
|
|
||
| r := &TSLRegistry{loadedAt: loaded, tsls: tsls} | ||
| assert.False(t, r.Freshness().NextUpdate.IsZero()) | ||
|
|
||
| // A bundle certificate can authorise a decision without any TSL. | ||
| r = &TSLRegistry{loadedAt: loaded, tsls: tsls, config: TSLConfig{CertBundle: "/etc/ca.pem"}} | ||
| f := r.Freshness() | ||
| assert.True(t, f.NextUpdate.IsZero()) | ||
| assert.True(t, f.AsOf.Equal(loaded)) | ||
| } |
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,193 @@ | ||
| package registry | ||
|
|
||
| import ( | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/sirosfoundation/go-trust/pkg/authzen" | ||
| ) | ||
|
|
||
| // Freshness describes how current a registry's data is. A zero field means | ||
| // unknown or not applicable. | ||
| type Freshness struct { | ||
| // AsOf is when the data was last loaded. | ||
| AsOf time.Time | ||
| // NextUpdate is when the source says it will next be replaced. | ||
| NextUpdate time.Time | ||
| } | ||
|
|
||
| // FreshnessReporter is implemented by registries that know how fresh their data | ||
| // is. It is optional: a registry without it is reported with no as_of and no | ||
| // next_update, which a caller must read as "unknown". | ||
| type FreshnessReporter interface { | ||
| Freshness() Freshness | ||
| } | ||
|
|
||
| // ParseSourceTime parses a timestamp from a trust-data source (ETSI | ||
| // dateTime, VICAL/RICAL tdate). The value must carry an explicit UTC offset | ||
| // (RFC 3339). An empty, unparseable or offset-free value yields the zero time, | ||
| // i.e. "unknown": freshness must never be invented, and assuming a zone for an | ||
| // offset-free value could report a deadline later than the source meant. | ||
| func ParseSourceTime(s string) time.Time { | ||
| t, err := time.Parse(time.RFC3339Nano, s) // also accepts RFC 3339 without fractional seconds | ||
| if err != nil { | ||
| return time.Time{} | ||
| } | ||
| return t.UTC() | ||
| } | ||
|
|
||
| // CacheFreshness reports the freshness of a fetched-and-cached list. view | ||
| // returns when the cached list was fetched and the nextUpdate it declares | ||
| // (unknown unless it parses, see ParseSourceTime). Nothing is reported before | ||
| // the first fetch, nor once the cache has outlived ttl: the next evaluation must | ||
| // refetch, and if that fails the registry keeps the old list and denies, a | ||
| // denial that can flip as soon as fetching recovers. The expiry test is the | ||
| // registries' own (fresh only while time since fetch < ttl), so a zero or | ||
| // negative ttl is always expired. | ||
| func CacheFreshness[C any](mu *sync.RWMutex, cache **C, ttl time.Duration, view func(*C) (fetchedAt time.Time, nextUpdate string)) Freshness { | ||
| mu.RLock() | ||
| c := *cache | ||
| mu.RUnlock() | ||
| if c == nil { | ||
| return Freshness{} | ||
| } | ||
| fetchedAt, nextUpdate := view(c) | ||
| if time.Since(fetchedAt) >= ttl { | ||
| return Freshness{} | ||
| } | ||
| return Freshness{AsOf: fetchedAt, NextUpdate: ParseSourceTime(nextUpdate)} | ||
| } | ||
|
|
||
| // EarliestKnown returns the earliest of the times, or the zero time (unknown) | ||
| // when there are none or any of them is unknown. A registry that holds several | ||
| // lists can decide from any of them, so it is only as fresh as its | ||
| // soonest-expiring list, and unknown if any list is. | ||
| func EarliestKnown(times ...time.Time) time.Time { | ||
| var earliest time.Time | ||
| for _, t := range times { | ||
| if t.IsZero() { | ||
| return time.Time{} | ||
| } | ||
| if earliest.IsZero() || t.Before(earliest) { | ||
| earliest = t | ||
| } | ||
| } | ||
| return earliest | ||
| } | ||
|
|
||
| // trustDatum is one registry's freshness at a point in time. The times keep | ||
| // full precision so two snapshots are compared exactly, not as the rounded | ||
| // strings sent on the wire. | ||
| type trustDatum struct { | ||
| name string | ||
| // group is the name of the top-level registry this one was reached through: | ||
| // itself, or the composite containing it. | ||
| group string | ||
| asOf, next time.Time | ||
| } | ||
|
|
||
| func (d trustDatum) same(o trustDatum) bool { | ||
| return d.name == o.name && d.asOf.Equal(o.asOf) && d.next.Equal(o.next) | ||
| } | ||
|
|
||
| // collectTrustData appends one entry per leaf registry under reg. A composite | ||
| // contributes its children, not itself. | ||
| func collectTrustData(reg TrustRegistry, group string, out []trustDatum) []trustDatum { | ||
| if c, ok := reg.(*CompositeRegistry); ok { | ||
| for _, child := range c.registries { | ||
| out = collectTrustData(child, group, out) | ||
| } | ||
| return out | ||
| } | ||
| d := trustDatum{name: reg.Info().Name, group: group} | ||
| if fr, ok := reg.(FreshnessReporter); ok { | ||
| f := fr.Freshness() | ||
| d.asOf, d.next = f.AsOf, f.NextUpdate | ||
| } | ||
| return append(out, d) | ||
| } | ||
|
|
||
| // trustDataSnapshot returns the current freshness of every registry in regs. | ||
| func trustDataSnapshot(regs []TrustRegistry) []trustDatum { | ||
| var data []trustDatum | ||
| for _, r := range regs { | ||
| data = collectTrustData(r, r.Info().Name, data) | ||
| } | ||
| return data | ||
| } | ||
|
|
||
| // responseIncomplete reports whether resp says some registry behind it failed | ||
| // to answer (see CompositeRegistry). | ||
| func responseIncomplete(resp *authzen.EvaluationResponse) bool { | ||
| if resp == nil || resp.Context == nil { | ||
| return false | ||
| } | ||
| inc, _ := resp.Context.Reason["incomplete"].(bool) | ||
| return inc | ||
| } | ||
|
|
||
| // failedRegistries names the registries a denial did not really come from: the | ||
| // strategy recorded an error for them (including an open circuit breaker or a | ||
| // panic) or they gave no decision. Such a denial can become an allow without | ||
| // any list changing, so its freshness is unknown. Only a denial is examined: an | ||
| // allow stands on the registry that allowed it. | ||
| func failedRegistries(resp *authzen.EvaluationResponse) map[string]bool { | ||
| if resp == nil || resp.Decision || resp.Context == nil { | ||
| return nil | ||
| } | ||
| failed := map[string]bool{} | ||
| for _, key := range []string{"registry_results", "all_results"} { | ||
| entries, _ := resp.Context.Reason[key].([]map[string]interface{}) | ||
| for _, e := range entries { | ||
| name, _ := e["registry"].(string) | ||
| _, isErr := e["error"] | ||
| inner, _ := e["reason"].(map[string]interface{}) | ||
| nested, _ := inner["incomplete"].(bool) // a composite with a failed child | ||
| if decided, ok := e["decision"].(bool); name != "" && (isErr || nested || !ok || decided) { | ||
| failed[name] = true | ||
| } | ||
| } | ||
| } | ||
| return failed | ||
| } | ||
|
|
||
| // attachTrustData returns a copy of resp whose context carries the freshness of | ||
| // every registry that could have contributed to the decision. The copy matters: | ||
| // registries may hand back a response they also keep. | ||
| // | ||
| // before and after are snapshots taken around the evaluation. A registry may | ||
| // reload while it is being evaluated, and its freshness is read outside its own | ||
| // lock, so an entry is only reported when both snapshots agree; otherwise the | ||
| // data the decision used cannot be told apart from the data now loaded, and the | ||
| // entry carries no as_of or next_update (unknown). | ||
| // | ||
| // The set is the policy-filtered applicable registries, not only those that | ||
| // answered first. For a first-match allow that can name a registry that was not | ||
| // reached, which only makes the reported bound earlier, never later. | ||
| func attachTrustData(resp *authzen.EvaluationResponse, before, after []trustDatum) *authzen.EvaluationResponse { | ||
| if resp == nil || len(after) == 0 { | ||
| return resp | ||
| } | ||
| failed := failedRegistries(resp) | ||
| data := make([]authzen.TrustData, len(after)) | ||
| for i, a := range after { | ||
| data[i] = authzen.TrustData{Registry: a.name} | ||
| if len(before) != len(after) || !before[i].same(a) || failed[a.group] { | ||
| continue | ||
| } | ||
| if !a.asOf.IsZero() { | ||
| data[i].AsOf = a.asOf.UTC().Format(time.RFC3339) | ||
| } | ||
| if !a.next.IsZero() { | ||
| data[i].NextUpdate = a.next.UTC().Format(time.RFC3339) | ||
| } | ||
| } | ||
| out := *resp | ||
| var ctx authzen.EvaluationResponseContext | ||
| if resp.Context != nil { | ||
| ctx = *resp.Context | ||
| } | ||
| ctx.TrustData = data | ||
| out.Context = &ctx | ||
| return &out | ||
| } |
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.