Skip to content

Commit 9330dde

Browse files
committed
Use the path component as key for looking up cached entries
spiffeIDFromProto() takes about 20% of the time to lookup authorized entries, so it seems worthwhile removing it. The path component is sufficient to identify the entries since all entries are going to have the same trust domain. Signed-off-by: Sorin Dumitru <[email protected]>
1 parent a7e4ec7 commit 9330dde

File tree

1 file changed

+14
-35
lines changed

1 file changed

+14
-35
lines changed

pkg/server/cache/entrycache/fullcache.go

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -89,23 +89,16 @@ type Agent struct {
8989
}
9090

9191
type FullEntryCache struct {
92-
aliases map[spiffeID][]aliasEntry
93-
entries map[spiffeID][]*types.Entry
92+
aliases map[string][]aliasEntry
93+
entries map[string][]*types.Entry
9494
}
9595

9696
type selectorSet map[Selector]struct{}
97-
type seenSet map[spiffeID]struct{}
97+
type seenSet map[string]struct{}
9898
type stringSet map[string]struct{}
9999

100-
type spiffeID struct {
101-
// TrustDomain is the trust domain of the SPIFFE ID.
102-
TrustDomain string
103-
// Path is the path of the SPIFFE ID.
104-
Path string
105-
}
106-
107100
type aliasEntry struct {
108-
id spiffeID
101+
id string
109102
entry *types.Entry
110103
}
111104

@@ -118,14 +111,14 @@ func Build(ctx context.Context, entryIter EntryIterator, agentIter AgentIterator
118111
}
119112
bysel := make(map[Selector][]aliasInfo)
120113

121-
entries := make(map[spiffeID][]*types.Entry)
114+
entries := make(map[string][]*types.Entry)
122115
for entryIter.Next(ctx) {
123116
entry := entryIter.Entry()
124-
parentID := spiffeIDFromProto(entry.ParentId)
125-
if parentID.Path == "/spire/server" {
117+
parentID := entry.ParentId.Path
118+
if entry.ParentId.Path == "/spire/server" {
126119
alias := aliasInfo{
127120
aliasEntry: aliasEntry{
128-
id: spiffeIDFromProto(entry.SpiffeId),
121+
id: entry.SpiffeId.Path,
129122
entry: entry,
130123
},
131124
selectors: selectorSetFromProto(entry.Selectors),
@@ -144,10 +137,10 @@ func Build(ctx context.Context, entryIter EntryIterator, agentIter AgentIterator
144137
aliasSeen := allocStringSet()
145138
defer freeStringSet(aliasSeen)
146139

147-
aliases := make(map[spiffeID][]aliasEntry)
140+
aliases := make(map[string][]aliasEntry)
148141
for agentIter.Next(ctx) {
149142
agent := agentIter.Agent()
150-
agentID := spiffeIDFromID(agent.ID)
143+
agentID := agent.ID.Path()
151144
agentSelectors := selectorSetFromProto(agent.Selectors)
152145
// track which aliases we've evaluated so far to make sure we don't
153146
// add one twice.
@@ -179,7 +172,7 @@ func (c *FullEntryCache) LookupAuthorizedEntries(agentID spiffeid.ID, requestedE
179172
defer freeSeenSet(seen)
180173

181174
foundEntries := make(map[string]api.ReadOnlyEntry)
182-
c.crawl(spiffeIDFromID(agentID), seen, func(entry *types.Entry) {
175+
c.crawl(agentID.Path(), seen, func(entry *types.Entry) {
183176
if _, ok := requestedEntries[entry.Id]; ok {
184177
foundEntries[entry.Id] = api.NewReadOnlyEntry(entry)
185178
}
@@ -194,43 +187,29 @@ func (c *FullEntryCache) GetAuthorizedEntries(agentID spiffeid.ID) []api.ReadOnl
194187
defer freeSeenSet(seen)
195188

196189
foundEntries := []api.ReadOnlyEntry{}
197-
c.crawl(spiffeIDFromID(agentID), seen, func(entry *types.Entry) {
190+
c.crawl(agentID.Path(), seen, func(entry *types.Entry) {
198191
foundEntries = append(foundEntries, api.NewReadOnlyEntry(entry))
199192
})
200193

201194
return foundEntries
202195
}
203196

204-
func (c *FullEntryCache) crawl(parentID spiffeID, seen map[spiffeID]struct{}, visit func(*types.Entry)) {
197+
func (c *FullEntryCache) crawl(parentID string, seen map[string]struct{}, visit func(*types.Entry)) {
205198
if _, ok := seen[parentID]; ok {
206199
return
207200
}
208201
seen[parentID] = struct{}{}
209202

210203
for _, entry := range c.entries[parentID] {
211204
visit(entry)
212-
c.crawl(spiffeIDFromProto(entry.SpiffeId), seen, visit)
205+
c.crawl(entry.SpiffeId.Path, seen, visit)
213206
}
214207

215208
for _, alias := range c.aliases[parentID] {
216209
c.crawl(alias.id, seen, visit)
217210
}
218211
}
219212

220-
func spiffeIDFromID(id spiffeid.ID) spiffeID {
221-
return spiffeID{
222-
TrustDomain: id.TrustDomain().Name(),
223-
Path: id.Path(),
224-
}
225-
}
226-
227-
func spiffeIDFromProto(id *types.SPIFFEID) spiffeID {
228-
return spiffeID{
229-
TrustDomain: id.TrustDomain,
230-
Path: id.Path,
231-
}
232-
}
233-
234213
func selectorSetFromProto(selectors []*types.Selector) selectorSet {
235214
set := make(selectorSet, len(selectors))
236215
for _, selector := range selectors {

0 commit comments

Comments
 (0)