2022-01-11 10:09:34 +00:00
|
|
|
package resolver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpc/client"
|
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/resolver"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
NNSResolver = "nns"
|
|
|
|
DNSResolver = "dns"
|
|
|
|
)
|
|
|
|
|
2022-03-01 19:02:24 +00:00
|
|
|
// NeoFS represents virtual connection to the NeoFS network.
|
|
|
|
type NeoFS interface {
|
|
|
|
// SystemDNS reads system DNS network parameters of the NeoFS.
|
|
|
|
//
|
|
|
|
// Returns exactly on non-zero value. Returns any error encountered
|
|
|
|
// which prevented the parameter to be read.
|
|
|
|
SystemDNS(context.Context) (string, error)
|
|
|
|
}
|
|
|
|
|
2022-01-11 10:09:34 +00:00
|
|
|
type Config struct {
|
2022-03-01 19:02:24 +00:00
|
|
|
NeoFS NeoFS
|
|
|
|
RPC *client.Client
|
2022-01-11 10:09:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type BucketResolver struct {
|
|
|
|
Name string
|
|
|
|
resolve func(context.Context, string) (*cid.ID, error)
|
|
|
|
|
|
|
|
next *BucketResolver
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *BucketResolver) Resolve(ctx context.Context, name string) (*cid.ID, error) {
|
|
|
|
cnrID, err := r.resolve(ctx, name)
|
|
|
|
if err != nil {
|
|
|
|
if r.next != nil {
|
|
|
|
return r.next.Resolve(ctx, name)
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return cnrID, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewResolver(order []string, cfg *Config) (*BucketResolver, error) {
|
|
|
|
if len(order) == 0 {
|
|
|
|
return nil, fmt.Errorf("resolving order must not be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
bucketResolver, err := newResolver(order[len(order)-1], cfg, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := len(order) - 2; i >= 0; i-- {
|
|
|
|
resolverName := order[i]
|
|
|
|
next := bucketResolver
|
|
|
|
|
|
|
|
bucketResolver, err = newResolver(resolverName, cfg, next)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bucketResolver, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newResolver(name string, cfg *Config, next *BucketResolver) (*BucketResolver, error) {
|
|
|
|
switch name {
|
|
|
|
case DNSResolver:
|
2022-03-01 19:02:24 +00:00
|
|
|
return NewDNSResolver(cfg.NeoFS, next)
|
2022-01-11 10:09:34 +00:00
|
|
|
case NNSResolver:
|
|
|
|
return NewNNSResolver(cfg.RPC, next)
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown resolver: %s", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-01 19:02:24 +00:00
|
|
|
func NewDNSResolver(neoFS NeoFS, next *BucketResolver) (*BucketResolver, error) {
|
|
|
|
if neoFS == nil {
|
2022-01-11 10:09:34 +00:00
|
|
|
return nil, fmt.Errorf("pool must not be nil for DNS resolver")
|
|
|
|
}
|
|
|
|
|
|
|
|
resolveFunc := func(ctx context.Context, name string) (*cid.ID, error) {
|
2022-03-01 19:02:24 +00:00
|
|
|
domain, err := neoFS.SystemDNS(ctx)
|
2022-01-11 10:09:34 +00:00
|
|
|
if err != nil {
|
2022-03-01 19:02:24 +00:00
|
|
|
return nil, fmt.Errorf("read system DNS parameter of the NeoFS: %w", err)
|
2022-01-11 10:09:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
domain = name + "." + domain
|
|
|
|
cnrID, err := resolver.ResolveContainerDomainName(domain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't resolve container '%s' as '%s': %w", name, domain, err)
|
|
|
|
}
|
|
|
|
return cnrID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &BucketResolver{
|
|
|
|
Name: DNSResolver,
|
|
|
|
|
|
|
|
resolve: resolveFunc,
|
|
|
|
next: next,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewNNSResolver(rpc *client.Client, next *BucketResolver) (*BucketResolver, error) {
|
|
|
|
if rpc == nil {
|
|
|
|
return nil, fmt.Errorf("rpc client must not be nil for NNS resolver")
|
|
|
|
}
|
|
|
|
|
|
|
|
nnsRPCResolver, err := resolver.NewNNSResolver(rpc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resolveFunc := func(_ context.Context, name string) (*cid.ID, error) {
|
|
|
|
cnrID, err := nnsRPCResolver.ResolveContainerName(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't resolve container '%s': %w", name, err)
|
|
|
|
}
|
|
|
|
return cnrID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &BucketResolver{
|
|
|
|
Name: NNSResolver,
|
|
|
|
|
|
|
|
resolve: resolveFunc,
|
|
|
|
next: next,
|
|
|
|
}, nil
|
|
|
|
}
|