Skip to content

Commit

Permalink
fix: added DiscardCacheError in fetcher
Browse files Browse the repository at this point in the history
Signed-off-by: Junjie Gao <junjiegao@microsoft.com>
  • Loading branch information
JeyJeyGao committed Sep 20, 2024
1 parent 7ed8899 commit 00e8bce
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
13 changes: 10 additions & 3 deletions revocation/crl/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ type HTTPFetcher struct {
// If Cache is nil, no cache is used.
Cache Cache

// DiscardCacheError specifies whether to discard the cache on error.
DiscardCacheError bool

httpClient *http.Client
}

Expand Down Expand Up @@ -84,7 +87,9 @@ func (f *HTTPFetcher) Fetch(ctx context.Context, url string) (*Bundle, error) {
return bundle, nil
}
}
// ignore the cache error as it is not critical
if err != nil && !f.DiscardCacheError {
return nil, fmt.Errorf("failed to retrieve CRL from cache: %w", err)
}
}

bundle, err := f.fetch(ctx, url)
Expand All @@ -93,8 +98,10 @@ func (f *HTTPFetcher) Fetch(ctx context.Context, url string) (*Bundle, error) {
}

if f.Cache != nil {
// ignore the cache error as it is not critical
_ = f.Cache.Set(ctx, url, bundle)
err = f.Cache.Set(ctx, url, bundle)
if err != nil && !f.DiscardCacheError {
return nil, fmt.Errorf("failed to store CRL to cache: %w", err)

Check warning on line 103 in revocation/crl/fetcher.go

View check run for this annotation

Codecov / codecov/patch

revocation/crl/fetcher.go#L103

Added line #L103 was not covered by tests
}
}

return bundle, nil
Expand Down
4 changes: 4 additions & 0 deletions revocation/crl/fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func TestFetch(t *testing.T) {
t.Errorf("NewHTTPFetcher() error = %v, want nil", err)
}
f.Cache = c
f.DiscardCacheError = true
bundle, err := f.Fetch(context.Background(), uncachedURL)
if err != nil {
t.Errorf("Fetcher.Fetch() error = %v, want nil", err)
Expand Down Expand Up @@ -182,6 +183,7 @@ func TestFetch(t *testing.T) {
t.Errorf("NewHTTPFetcher() error = %v, want nil", err)
}
f.Cache = c
f.DiscardCacheError = true
bundle, err = f.Fetch(context.Background(), expiredCRLURL)
if err != nil {
t.Errorf("Fetcher.Fetch() error = %v, want nil", err)
Expand Down Expand Up @@ -218,6 +220,7 @@ func TestFetch(t *testing.T) {
t.Errorf("NewHTTPFetcher() error = %v, want nil", err)
}
f.Cache = c
f.DiscardCacheError = true
_, err = f.Fetch(context.Background(), uncachedURL)
if !strings.Contains(err.Error(), "delta CRL is not supported") {
t.Errorf("Fetcher.Fetch() error = %v, want delta CRL is not supported", err)
Expand All @@ -234,6 +237,7 @@ func TestFetch(t *testing.T) {
t.Errorf("NewHTTPFetcher() error = %v, want nil", err)
}
f.Cache = c
f.DiscardCacheError = true
bundle, err = f.Fetch(context.Background(), exampleURL)
if err != nil {
t.Errorf("Fetcher.Fetch() error = %v, want nil", err)
Expand Down
7 changes: 7 additions & 0 deletions revocation/internal/crl/crl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func TestCertCheckStatus(t *testing.T) {
t.Fatal(err)
}
fetcher.Cache = memoryCache
fetcher.DiscardCacheError = true
r := CertCheckStatus(context.Background(), chain[0].Cert, issuerCert, CertCheckStatusOptions{
Fetcher: fetcher,
})
Expand Down Expand Up @@ -166,6 +167,7 @@ func TestCertCheckStatus(t *testing.T) {
}

fetcher.Cache = memoryCache
fetcher.DiscardCacheError = true
r := CertCheckStatus(context.Background(), chain[0].Cert, issuerCert, CertCheckStatusOptions{
Fetcher: fetcher,
})
Expand All @@ -192,6 +194,7 @@ func TestCertCheckStatus(t *testing.T) {
t.Fatal(err)
}
fetcher.Cache = memoryCache
fetcher.DiscardCacheError = true
r := CertCheckStatus(context.Background(), chain[0].Cert, issuerCert, CertCheckStatusOptions{
Fetcher: fetcher,
})
Expand Down Expand Up @@ -223,6 +226,7 @@ func TestCertCheckStatus(t *testing.T) {
t.Fatal(err)
}
fetcher.Cache = memoryCache
fetcher.DiscardCacheError = true
r := CertCheckStatus(context.Background(), chain[0].Cert, issuerCert, CertCheckStatusOptions{
Fetcher: fetcher,
})
Expand Down Expand Up @@ -264,6 +268,7 @@ func TestCertCheckStatus(t *testing.T) {
t.Fatal(err)
}
fetcher.Cache = memoryCache
fetcher.DiscardCacheError = true
r := CertCheckStatus(context.Background(), chain[0].Cert, issuerCert, CertCheckStatusOptions{
Fetcher: fetcher,
})
Expand All @@ -285,6 +290,7 @@ func TestCertCheckStatus(t *testing.T) {
t.Fatal(err)
}
fetcher.Cache = memoryCache
fetcher.DiscardCacheError = true
r := CertCheckStatus(context.Background(), chain[0].Cert, issuerCert, CertCheckStatusOptions{
Fetcher: fetcher,
})
Expand Down Expand Up @@ -314,6 +320,7 @@ func TestCertCheckStatus(t *testing.T) {
t.Fatal(err)
}
fetcher.Cache = memoryCache
fetcher.DiscardCacheError = true
r := CertCheckStatus(context.Background(), chain[0].Cert, issuerCert, CertCheckStatusOptions{
Fetcher: fetcher,
})
Expand Down

0 comments on commit 00e8bce

Please sign in to comment.