Skip to content

Commit

Permalink
fix: suppress decompression error and treat uncompressed token in cac…
Browse files Browse the repository at this point in the history
…he as cache miss (#1058)
  • Loading branch information
hspitzley-czi authored May 3, 2024
1 parent 35bea94 commit da1316d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
16 changes: 9 additions & 7 deletions oidc_cli/oidc_impl/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion oidc_cli/oidc_impl/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit da1316d

Please sign in to comment.