Debian Codegraph @

ipfs/resolver.go

raw
1 /*
2 Copyright The containerd Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package ipfs
18
19 import (
20 "context"
21 "encoding/json"
22 "fmt"
23 "io"
24 "path"
25 "os"
26
27 "github.com/containerd/containerd/remotes"
28 ipfsclient "github.com/containerd/stargz-snapshotter/ipfs/client"
29 ocispec "github.com/opencontainers/image-spec/specs-go/v1"
30 )
31
32 type resolver struct {
33 scheme string
34 client *ipfsclient.Client
35 }
36
37 type ResolverOptions struct {
38 // Scheme is the scheme to fetch the specified IPFS content. "ipfs" or "ipns".
39 Scheme string
40
41 // IPFSPath is the path to the IPFS repository directory.
42 IPFSPath string
43 }
44
45 func NewResolver(options ResolverOptions) (remotes.Resolver, error) {
46 s := options.Scheme
47 if s != "ipfs" && s != "ipns" {
48 return nil, fmt.Errorf("unsupported scheme %q", s)
49 }
50 var ipath string
51 if idir := os.Getenv("IPFS_PATH"); idir != "" {
52 ipath = idir
53 }
54 if options.IPFSPath != "" {
55 ipath = options.IPFSPath
56 }
57 // HTTP is only supported as of now. We can add https support here if needed (e.g. for connecting to it via proxy, etc)
58 iurl, err := ipfsclient.GetIPFSAPIAddress(ipath, "http")
59 if err != nil {
60 return nil, fmt.Errorf("failed to get IPFS URL from ipfs path")
61 }
62 return &resolver{
63 scheme: s,
64 client: ipfsclient.New(iurl),
65 }, nil
66 }
67
68 // Resolve resolves the provided ref for IPFS. ref must be a CID.
69 // TODO: Allow specifying IPFS path or URL. This requires to modify `reference` pkg because
70 //
71 // it's incompatbile to the current reference specification.
72 func (r *resolver) Resolve(ctx context.Context, ref string) (name string, desc ocispec.Descriptor, err error) {
73 rc, err := r.client.Get(path.Join("/", r.scheme, ref), nil, nil)
74 if err != nil {
75 return "", ocispec.Descriptor{}, err
76 }
77 defer rc.Close()
78 if err := json.NewDecoder(rc).Decode(&desc); err != nil {
79 return "", ocispec.Descriptor{}, err
80 }
81 if _, err := GetCID(desc); err != nil {
82 return "", ocispec.Descriptor{}, err
83 }
84 return ref, desc, nil
85 }
86
87 func (r *resolver) Fetcher(ctx context.Context, ref string) (remotes.Fetcher, error) {
88 return &fetcher{r}, nil
89 }
90
91 func (r *resolver) Pusher(ctx context.Context, ref string) (remotes.Pusher, error) {
92 return nil, fmt.Errorf("immutable remote")
93 }
94
95 type fetcher struct {
96 r *resolver
97 }
98
99 func (f *fetcher) Fetch(ctx context.Context, desc ocispec.Descriptor) (io.ReadCloser, error) {
100 cid, err := GetCID(desc)
101 if err != nil {
102 return nil, err
103 }
104 return f.r.client.Get(path.Join("/", f.r.scheme, cid), nil, nil)
105 }