diff --git a/.github/actions/find-changed-packages/action.yml b/.github/actions/find-changed-packages/action.yml index fce9c5b6..a4f11dab 100644 --- a/.github/actions/find-changed-packages/action.yml +++ b/.github/actions/find-changed-packages/action.yml @@ -24,7 +24,7 @@ runs: const path = require("path") const fs = require("fs") const changedFiles = ${{ steps.filter.outputs.changed_files }} - const changedDirs = changedFiles.map(f => path.dirname(f)) + const changedDirs = changedFiles.map(f => path.dirname(f).split('/')[0]) const changedGoPackages = changedDirs.filter(d => fs.existsSync(path.join(d, "go.mod"))) const uniqueChangedGoPackages = [...new Set(changedGoPackages)]; diff --git a/oidc_cli/oidc_impl/cache/cache.go b/oidc_cli/oidc_impl/cache/cache.go index 2cd97228..b973fe1d 100644 --- a/oidc_cli/oidc_impl/cache/cache.go +++ b/oidc_cli/oidc_impl/cache/cache.go @@ -1,7 +1,11 @@ package cache import ( + "bytes" + "compress/gzip" "context" + "fmt" + "io" "github.com/chanzuckerberg/go-misc/oidc_cli/oidc_impl/client" "github.com/chanzuckerberg/go-misc/oidc_cli/oidc_impl/storage" @@ -83,8 +87,14 @@ func (c *Cache) refresh(ctx context.Context) (*client.Token, error) { if err != nil { return nil, errors.Wrap(err, "unable to marshall token") } - // save token to storage - err = c.storage.Set(ctx, strToken) + + // 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, compressedToken) if err != nil { return nil, errors.Wrap(err, "Unable to cache the strToken") } @@ -99,7 +109,17 @@ func (c *Cache) readFromStorage(ctx context.Context) (*client.Token, error) { if err != nil { return nil, err } - cachedToken, err := client.TokenFromString(cached) + if cached == nil { + return nil, nil + } + + // decode gzip data + decompressedStr, err := decompressToken(*cached) + if err != nil { + return nil, fmt.Errorf("failed to decompress token: %w", err) + } + + 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 @@ -109,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 +} diff --git a/oidc_cli/oidc_impl/cache/cache_test.go b/oidc_cli/oidc_impl/cache/cache_test.go index 2a9f76db..5016a500 100644 --- a/oidc_cli/oidc_impl/cache/cache_test.go +++ b/oidc_cli/oidc_impl/cache/cache_test.go @@ -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() @@ -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) @@ -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) {