From da1316d146ad857f601dd32b1709935be1b11a8c Mon Sep 17 00:00:00 2001 From: Hayden Spitzley <105455169+hspitzley-czi@users.noreply.github.com> Date: Fri, 3 May 2024 09:55:10 -0700 Subject: [PATCH] fix: suppress decompression error and treat uncompressed token in cache as cache miss (#1058) --- oidc_cli/oidc_impl/cache/cache.go | 16 +++++++++------- oidc_cli/oidc_impl/cache/cache_test.go | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/oidc_cli/oidc_impl/cache/cache.go b/oidc_cli/oidc_impl/cache/cache.go index b973fe1d..8451bb5b 100644 --- a/oidc_cli/oidc_impl/cache/cache.go +++ b/oidc_cli/oidc_impl/cache/cache.go @@ -116,10 +116,11 @@ func (c *Cache) readFromStorage(ctx context.Context) (*client.Token, error) { // decode gzip data decompressedStr, err := decompressToken(*cached) if err != nil { - return nil, fmt.Errorf("failed to decompress token: %w", err) + // if we fail to decompress the token we should treat it as a cache miss instead of returning an error + logrus.Debug(fmt.Errorf("failed to decompress token: %w", err).Error()) } - cachedToken, err := client.TokenFromString(&decompressedStr) + cachedToken, err := client.TokenFromString(decompressedStr) if err != nil { logrus.WithError(err).Debug("error fetching stored token") err = c.storage.Delete(ctx) // can't read it, so attempt to purge it @@ -142,18 +143,19 @@ func compressToken(token string) (string, error) { return buf.String(), nil } -func decompressToken(token string) (string, error) { +func decompressToken(token string) (*string, error) { reader := bytes.NewReader([]byte(token)) gzreader, err := gzip.NewReader(reader) if err != nil { - return "", fmt.Errorf("failed to create gzip reader: %w", err) + return nil, fmt.Errorf("failed to create gzip reader: %w", err) } decompressed, err := io.ReadAll(gzreader) if err != nil { - return "", fmt.Errorf("failed to read gzip data: %w", err) + return nil, fmt.Errorf("failed to read gzip data: %w", err) } if err := gzreader.Close(); err != nil { - return "", fmt.Errorf("failed to close gzip: %w", err) + return nil, fmt.Errorf("failed to close gzip: %w", err) } - return string(decompressed), nil + decompressedStr := string(decompressed) + return &decompressedStr, nil } diff --git a/oidc_cli/oidc_impl/cache/cache_test.go b/oidc_cli/oidc_impl/cache/cache_test.go index 5016a500..98e11fdf 100644 --- a/oidc_cli/oidc_impl/cache/cache_test.go +++ b/oidc_cli/oidc_impl/cache/cache_test.go @@ -88,7 +88,7 @@ func TestCorruptedCache(t *testing.T) { decompressedToken, err := decompressToken(*cachedToken) r.NoError(err) - tok, err := client.TokenFromString(&decompressedToken) + tok, err := client.TokenFromString(decompressedToken) r.NoError(err) r.NotNil(t)