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

Fix event being dropped on error #5030

Merged
merged 4 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion pkg/server/endpoints/authorized_entryfetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ func (a *AuthorizedEntryFetcherWithEventsBasedCache) updateAttestedNodesCache(ct
if err != nil {
return err
}
a.lastAttestedNodeEventID = event.EventID

if node == nil {
a.cache.RemoveAgent(event.SpiffeID)
a.lastAttestedNodeEventID = event.EventID
continue
}

Expand All @@ -176,10 +176,12 @@ func (a *AuthorizedEntryFetcherWithEventsBasedCache) updateAttestedNodesCache(ct
agentExpiresAt := time.Unix(node.CertNotAfter, 0)
if agentExpiresAt.Before(a.clk.Now()) {
a.cache.RemoveAgent(event.SpiffeID)
a.lastAttestedNodeEventID = event.EventID
continue
}

a.cache.UpdateAgent(node.SpiffeId, agentExpiresAt, api.ProtoFromSelectors(node.Selectors))
a.lastAttestedNodeEventID = event.EventID
}

return nil
Expand Down
62 changes: 62 additions & 0 deletions pkg/server/endpoints/authorized_entryfetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,65 @@ func TestBuildRegistrationEntriesCache(t *testing.T) {
})
}
}

func TestUpdateAttestedNodesCache(t *testing.T) {
ctx := context.Background()
log, _ := test.NewNullLogger()
clk := clock.NewMock(t)
ds := fakedatastore.New(t)

ef, err := NewAuthorizedEntryFetcherWithEventsBasedCache(ctx, log, clk, ds, defaultCacheReloadInterval, defaultPruneEventsOlderThan)
require.NoError(t, err)
require.NotNil(t, ef)

agentID, err := spiffeid.FromString("spiffe://example.org/myagent")
require.NoError(t, err)

_, err = ds.CreateAttestedNode(ctx, &common.AttestedNode{
SpiffeId: agentID.String(),
CertNotAfter: time.Now().Add(5 * time.Hour).Unix(),
})
require.NoError(t, err)

for _, tt := range []struct {
name string
errs []error
expectedLastAttestedNodeEventID uint
}{
{
name: "Error Listing Attested Node Events",
errs: []error{errors.New("listing attested node events")},
expectedLastAttestedNodeEventID: uint(0),
},
{
name: "Error Fetching Attested Node",
errs: []error{nil, errors.New("fetching attested node")},
expectedLastAttestedNodeEventID: uint(0),
},
{
name: "Error Getting Node Selectors",
errs: []error{nil, nil, errors.New("getting node selectors")},
expectedLastAttestedNodeEventID: uint(0),
},
{
name: "No Errors",
expectedLastAttestedNodeEventID: uint(1),
},
} {
tt := tt
t.Run(tt.name, func(t *testing.T) {
for _, err = range tt.errs {
ds.AppendNextError(err)
}

err = ef.updateAttestedNodesCache(ctx)
if len(tt.errs) > 0 {
assert.EqualError(t, err, tt.errs[len(tt.errs)-1].Error())
} else {
assert.NoError(t, err)
}

assert.Equal(t, tt.expectedLastAttestedNodeEventID, ef.lastAttestedNodeEventID)
})
}
}
Loading