Skip to content

Modify cache purging limit #70

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
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ func (da *DigestAuth) purgeLocked(count int) {
}
cache := digestCache(entries)
sort.Sort(cache)
for _, client := range cache[:count] {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use min of count/len if you really want to remove count entries only

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andig updated!


purgeCount := count
if count > len(cache) {
purgeCount = len(cache)
}
for _, client := range cache[:purgeCount] {
delete(da.clients, client.nonce)
}
}
Expand Down
26 changes: 26 additions & 0 deletions digest_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package auth

import (
"fmt"
"context"
"net/http"
"net/url"
Expand Down Expand Up @@ -79,6 +80,31 @@ func TestDigestAuthParams(t *testing.T) {
}
}

// TestDigestPurge tests that when we purge clients from the authenticator we do not purge
// more cache entries than the number of clients we have received.
// This is to avoid regressing and hitting a "slice bounds out of range" panic.
func TestDigestPurge(t *testing.T) {
t.Parallel()
// Creating dummy clients for the digest authenticator.
nClients := 10
clients := make(map[string]*digestClient, nClients)
for i := 0; i < nClients; i++ {
clients[fmt.Sprintf("%d", i)] = &digestClient{}
}

secrets := HtdigestFileProvider("test.htdigest")
da := &DigestAuth{
Opaque: "U7H+ier3Ae8Skd/g",
Realm: "example.com",
Secrets: secrets,
clients: clients,
}

// Purging more than the number of clients we have stored in the
// digest authenticator.
da.Purge(nClients * 2)
}

func TestNewContextNoDeadlock(t *testing.T) {
t.Parallel()
const (
Expand Down