Skip to content

Commit 9654108

Browse files
committed
feat: service authorized blob index retrieval
1 parent 446b6e7 commit 9654108

File tree

7 files changed

+216
-94
lines changed

7 files changed

+216
-94
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ require (
3030
github.com/multiformats/go-multihash v0.2.3
3131
github.com/redis/go-redis/extra/redisotel/v9 v9.10.0
3232
github.com/redis/go-redis/v9 v9.10.0
33-
github.com/storacha/go-libstoracha v0.3.1
33+
github.com/storacha/go-libstoracha v0.3.2
3434
github.com/storacha/go-ucanto v0.6.5
3535
github.com/stretchr/testify v1.10.0
3636
github.com/testcontainers/testcontainers-go v0.39.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -676,8 +676,8 @@ github.com/spf13/cobra v1.2.1/go.mod h1:ExllRjgxM/piMAM+3tAZvg8fsklGAf3tPfi+i8t6
676676
github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo=
677677
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
678678
github.com/spf13/viper v1.8.1/go.mod h1:o0Pch8wJ9BVSWGQMbra6iw0oQ5oktSIBaujf1rJH9Ns=
679-
github.com/storacha/go-libstoracha v0.3.1 h1:cjXEVfEJHdBLBcs2cbk94Gf5xoL/eMxWtY6o6RxnY/0=
680-
github.com/storacha/go-libstoracha v0.3.1/go.mod h1:UF4t2uPwq7vhqqoRWVPnsTsvnWghd8uTJ5WW6QekjVA=
679+
github.com/storacha/go-libstoracha v0.3.2 h1:GH/6Q+lJ/HmjI3/T+3ECqOdblLTYLPtZWqLB5AG6Dus=
680+
github.com/storacha/go-libstoracha v0.3.2/go.mod h1:UF4t2uPwq7vhqqoRWVPnsTsvnWghd8uTJ5WW6QekjVA=
681681
github.com/storacha/go-ucanto v0.6.5 h1:mxy1UkJDqszAGe6SkoT0N2SG9YJ62YX7fzU1Pg9lxnA=
682682
github.com/storacha/go-ucanto v0.6.5/go.mod h1:O35Ze4x18EWtz3ftRXXd/mTZ+b8OQVjYYrnadJ/xNjg=
683683
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

pkg/service/blobindexlookup/cachinglookup_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ type mockBlobIndexLookup struct {
180180
err error
181181
}
182182

183-
func (m *mockBlobIndexLookup) Find(ctx context.Context, contextID types.EncodedContextID, provider model.ProviderResult, spec types.RetrievalRequest) (blobindex.ShardedDagIndexView, error) {
183+
func (m *mockBlobIndexLookup) Find(ctx context.Context, contextID types.EncodedContextID, provider model.ProviderResult, req types.RetrievalRequest) (blobindex.ShardedDagIndexView, error) {
184184
return m.index, m.err
185185
}
186186

pkg/service/blobindexlookup/mock_BlobIndexLookup.go

Lines changed: 15 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/service/blobindexlookup/simplelookup.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/storacha/go-libstoracha/blobindex"
1313
rclient "github.com/storacha/go-ucanto/client/retrieval"
1414
"github.com/storacha/go-ucanto/core/dag/blockstore"
15+
"github.com/storacha/go-ucanto/core/delegation"
1516
"github.com/storacha/go-ucanto/core/invocation"
1617
"github.com/storacha/go-ucanto/core/receipt"
1718
"github.com/storacha/go-ucanto/core/result"
@@ -30,11 +31,11 @@ func NewBlobIndexLookup(httpClient *http.Client) BlobIndexLookup {
3031
}
3132

3233
// Find fetches the blob index from the given fetchURL
33-
func (s *simpleLookup) Find(ctx context.Context, _ types.EncodedContextID, result model.ProviderResult, spec types.RetrievalRequest) (blobindex.ShardedDagIndexView, error) {
34+
func (s *simpleLookup) Find(ctx context.Context, _ types.EncodedContextID, result model.ProviderResult, request types.RetrievalRequest) (blobindex.ShardedDagIndexView, error) {
3435
// If retrieval authroization details were provided, make a UCAN authorized
3536
// retrieval request.
36-
if spec.Auth != nil {
37-
body, err := doAuthorizedRetrieval(ctx, spec, s.httpClient)
37+
if request.Auth != nil {
38+
body, err := doAuthorizedRetrieval(ctx, s.httpClient, request)
3839
if err != nil {
3940
return nil, fmt.Errorf("executing authorized retrieval: %w", err)
4041
}
@@ -43,52 +44,52 @@ func (s *simpleLookup) Find(ctx context.Context, _ types.EncodedContextID, resul
4344
}
4445

4546
// attempt to fetch the index from provided url
46-
req, err := http.NewRequestWithContext(ctx, http.MethodGet, spec.URL.String(), nil)
47+
httpReq, err := http.NewRequestWithContext(ctx, http.MethodGet, request.URL.String(), nil)
4748
if err != nil {
4849
return nil, fmt.Errorf("constructing request: %w", err)
4950
}
50-
rng := spec.Range
51+
rng := request.Range
5152
if rng != nil {
5253
rangeHeader := fmt.Sprintf("bytes=%d-", rng.Offset)
5354
if rng.Length != nil {
5455
rangeHeader += strconv.FormatUint(rng.Offset+*rng.Length-1, 10)
5556
}
56-
req.Header.Set("Range", rangeHeader)
57+
httpReq.Header.Set("Range", rangeHeader)
5758
}
58-
resp, err := s.httpClient.Do(req)
59+
resp, err := s.httpClient.Do(httpReq)
5960
if err != nil {
6061
return nil, fmt.Errorf("failed to fetch index: %w", err)
6162
}
6263
if resp.StatusCode < 200 || resp.StatusCode > 299 {
6364
body, _ := io.ReadAll(resp.Body)
64-
return nil, fmt.Errorf("failure response fetching index. status: %s, message: %s, url: %s", resp.Status, string(body), spec.URL.String())
65+
return nil, fmt.Errorf("failure response fetching index. status: %s, message: %s, url: %s", resp.Status, string(body), request.URL.String())
6566
}
6667
defer resp.Body.Close()
6768
return blobindex.Extract(resp.Body)
6869
}
6970

70-
func doAuthorizedRetrieval(ctx context.Context, spec types.RetrievalRequest, httpClient *http.Client) (io.ReadCloser, error) {
71+
func doAuthorizedRetrieval(ctx context.Context, httpClient *http.Client, request types.RetrievalRequest) (io.ReadCloser, error) {
7172
headers := http.Header{}
72-
if spec.Range != nil {
73-
if spec.Range.Length != nil {
74-
headers.Set("Range", fmt.Sprintf("bytes=%d-%d", spec.Range.Offset, spec.Range.Offset+*spec.Range.Length-1))
73+
if request.Range != nil {
74+
if request.Range.Length != nil {
75+
headers.Set("Range", fmt.Sprintf("bytes=%d-%d", request.Range.Offset, request.Range.Offset+*request.Range.Length-1))
7576
} else {
76-
headers.Set("Range", fmt.Sprintf("bytes=%d-", spec.Range.Offset))
77+
headers.Set("Range", fmt.Sprintf("bytes=%d-", request.Range.Offset))
7778
}
7879
}
7980

8081
conn, err := rclient.NewConnection(
81-
spec.Auth.Audience,
82-
spec.URL,
82+
request.Auth.Audience,
83+
request.URL,
8384
rclient.WithClient(httpClient),
8485
rclient.WithHeaders(headers),
8586
)
8687
if err != nil {
8788
return nil, err
8889
}
8990

90-
iss, aud, cap := spec.Auth.Issuer, spec.Auth.Audience, spec.Auth.Capability
91-
inv, err := invocation.Invoke(iss, aud, cap)
91+
iss, aud, cap := request.Auth.Issuer, request.Auth.Audience, request.Auth.Capability
92+
inv, err := invocation.Invoke(iss, aud, cap, delegation.WithProof(request.Auth.Proofs...))
9293
if err != nil {
9394
return nil, err
9495
}

0 commit comments

Comments
 (0)