Skip to content

Commit

Permalink
refactor and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hspitzley-czi committed Apr 5, 2024
1 parent b56806b commit 78e89fe
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 22 deletions.
54 changes: 35 additions & 19 deletions oidc_cli/oidc_impl/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ func (c *Cache) refresh(ctx context.Context) (*client.Token, error) {
if err != nil {
return nil, err
}

// if we have a valid token, use it
if cachedToken.IsFresh() {
return cachedToken, nil
Expand All @@ -89,17 +88,13 @@ func (c *Cache) refresh(ctx context.Context) (*client.Token, error) {
return nil, errors.Wrap(err, "unable to marshall token")
}

// compress and save token to storage
var buf bytes.Buffer
gz := gzip.NewWriter(&buf)
if _, err := gz.Write([]byte(strToken)); err != nil {
return nil, fmt.Errorf("failed to write to gzip: %w", err)
}
if err := gz.Close(); err != nil {
return nil, fmt.Errorf("failed to close gzip: %w", err)
// gzip encode and save token to storage
compressedToken, err := compressToken(strToken)
if err != nil {
return nil, errors.Wrap(err, "unable to compress token")
}

err = c.storage.Set(ctx, buf.String())
err = c.storage.Set(ctx, compressedToken)
if err != nil {
return nil, errors.Wrap(err, "Unable to cache the strToken")
}
Expand All @@ -114,22 +109,15 @@ func (c *Cache) readFromStorage(ctx context.Context) (*client.Token, error) {
if err != nil {
return nil, err
}

if cached == nil {
return nil, nil
}

// decode gzip data
reader := bytes.NewReader([]byte(*cached))
gzreader, err := gzip.NewReader(reader)
if err != nil {
return nil, fmt.Errorf("failed to create gzip reader: %w", err)
}
decompressed, err := io.ReadAll(gzreader)
decompressedStr, err := decompressToken(*cached)
if err != nil {
return nil, fmt.Errorf("failed to read gzip data: %w", err)
return nil, fmt.Errorf("failed to decompress token: %w", err)
}
decompressedStr := string(decompressed)

cachedToken, err := client.TokenFromString(&decompressedStr)
if err != nil {
Expand All @@ -141,3 +129,31 @@ func (c *Cache) readFromStorage(ctx context.Context) (*client.Token, error) {
}
return cachedToken, nil
}

func compressToken(token string) (string, error) {
var buf bytes.Buffer
gz := gzip.NewWriter(&buf)
if _, err := gz.Write([]byte(token)); err != nil {
return "", fmt.Errorf("failed to write to gzip: %w", err)
}
if err := gz.Close(); err != nil {
return "", fmt.Errorf("failed to close gzip: %w", err)
}
return buf.String(), nil
}

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)
}
decompressed, err := io.ReadAll(gzreader)
if err != nil {
return "", 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 string(decompressed), nil
}
14 changes: 11 additions & 3 deletions oidc_cli/oidc_impl/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ func TestCorruptedCache(t *testing.T) {
r := require.New(t)
s := genStorage()
ctx := context.Background()
err := s.Set(ctx, "garbage token")
compressed, err := compressToken("garbage token")
r.NoError(err)
err = s.Set(ctx, compressed)
r.NoError(err)

u := uuid.New()
Expand All @@ -83,7 +85,10 @@ func TestCorruptedCache(t *testing.T) {
r.NoError(err)
r.NotNil(cachedToken)

tok, err := client.TokenFromString(cachedToken)
decompressedToken, err := decompressToken(*cachedToken)
r.NoError(err)

tok, err := client.TokenFromString(&decompressedToken)
r.NoError(err)
r.NotNil(t)

Expand All @@ -110,7 +115,10 @@ func TestCachedToken(t *testing.T) {
marshalled, err := freshToken.Marshal()
r.NoError(err)

err = s.Set(ctx, marshalled)
compressed, err := compressToken(marshalled)
r.NoError(err)

err = s.Set(ctx, compressed)
r.NoError(err)

refresh := func(ctx context.Context, c *client.Token) (*client.Token, error) {
Expand Down

0 comments on commit 78e89fe

Please sign in to comment.