From a6f7cce45ad2cb27383d1c060ba81c9822dde6dd Mon Sep 17 00:00:00 2001 From: Arne Luenser Date: Fri, 2 Feb 2024 13:38:19 +0100 Subject: [PATCH] fix: revert fetcher API for backwards-compatibility (#760) --- fetcher/fetcher.go | 14 ++++++++++++-- fetcher/fetcher_test.go | 6 +++--- jwksx/fetcher_v2.go | 3 +-- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/fetcher/fetcher.go b/fetcher/fetcher.go index 11ae6c9d..aeda9e1a 100644 --- a/fetcher/fetcher.go +++ b/fetcher/fetcher.go @@ -83,13 +83,23 @@ func NewFetcher(opts ...Modifier) *Fetcher { } // Fetch fetches the file contents from the source. -func (f *Fetcher) Fetch(source string) ([]byte, error) { +func (f *Fetcher) Fetch(source string) (*bytes.Buffer, error) { return f.FetchContext(context.Background(), source) } // FetchContext fetches the file contents from the source and allows to pass a // context that is used for HTTP requests. -func (f *Fetcher) FetchContext(ctx context.Context, source string) ([]byte, error) { +func (f *Fetcher) FetchContext(ctx context.Context, source string) (*bytes.Buffer, error) { + b, err := f.FetchBytes(ctx, source) + if err != nil { + return nil, err + } + return bytes.NewBuffer(b), nil +} + +// FetchBytes fetches the file contents from the source and allows to pass a +// context that is used for HTTP requests. +func (f *Fetcher) FetchBytes(ctx context.Context, source string) ([]byte, error) { switch s := stringsx.SwitchPrefix(source); { case s.HasPrefix("http://"), s.HasPrefix("https://"): return f.fetchRemote(ctx, source) diff --git a/fetcher/fetcher_test.go b/fetcher/fetcher_test.go index 56c3cf7b..62241bee 100644 --- a/fetcher/fetcher_test.go +++ b/fetcher/fetcher_test.go @@ -63,7 +63,7 @@ func TestFetcher(t *testing.T) { t.Run(fmt.Sprintf("config=%d/case=%d", fc, k), func(t *testing.T) { actual, err := fetcher.Fetch(tc.source) require.NoError(t, err) - assert.JSONEq(t, tc.expect, string(actual)) + assert.JSONEq(t, tc.expect, actual.String()) }) } } @@ -115,7 +115,7 @@ func TestFetcher(t *testing.T) { res, err := f.Fetch(srv.URL) require.NoError(t, err) - require.Equal(t, "toodaloo", string(res)) + require.Equal(t, "toodaloo", res.String()) require.EqualValues(t, 1, atomic.LoadInt32(&hits)) @@ -124,7 +124,7 @@ func TestFetcher(t *testing.T) { for i := 0; i < 100; i++ { res2, err := f.Fetch(srv.URL) require.NoError(t, err) - require.Equal(t, "toodaloo", string(res2)) + require.Equal(t, "toodaloo", res2.String()) if &res == &res2 { t.Fatalf("cache should not return the same pointer") } diff --git a/jwksx/fetcher_v2.go b/jwksx/fetcher_v2.go index cba71506..a0ea7590 100644 --- a/jwksx/fetcher_v2.go +++ b/jwksx/fetcher_v2.go @@ -4,7 +4,6 @@ package jwksx import ( - "bytes" "context" "crypto/sha256" "time" @@ -157,7 +156,7 @@ func (f *FetcherNext) fetch(ctx context.Context, location string, opts *fetcherN return nil, err } - set, err := jwk.ParseReader(bytes.NewBuffer(result)) + set, err := jwk.ParseReader(result) if err != nil { return nil, errors.WithStack(herodot.ErrBadRequest.WithReason("failed to parse JWK set").WithWrap(err)) }