2022-01-11 10:09:34 +00:00
|
|
|
package resolver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
2022-04-25 09:57:58 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/ns"
|
2022-01-11 10:09:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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.
|
|
|
|
//
|
2022-04-13 16:56:58 +00:00
|
|
|
// It returns exactly on non-zero value. It returns any error encountered
|
|
|
|
// which prevented the parameter from being read.
|
2022-03-01 19:02:24 +00:00
|
|
|
SystemDNS(context.Context) (string, error)
|
|
|
|
}
|
|
|
|
|
2022-01-11 10:09:34 +00:00
|
|
|
type Config struct {
|
2022-04-25 09:57:58 +00:00
|
|
|
NeoFS NeoFS
|
|
|
|
RPCAddress string
|
2022-01-11 10:09:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type BucketResolver struct {
|
|
|
|
Name string
|
|
|
|
resolve func(context.Context, string) (*cid.ID, error)
|
|
|
|
|
|
|
|
next *BucketResolver
|
|
|
|
}
|
|
|
|
|
2022-03-01 15:07:15 +00:00
|
|
|
func (r *BucketResolver) SetResolveFunc(fn func(context.Context, string) (*cid.ID, error)) {
|
|
|
|
r.resolve = fn
|
|
|
|
}
|
|
|
|
|
2022-01-11 10:09:34 +00:00
|
|
|
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:
|
2022-04-25 09:57:58 +00:00
|
|
|
return NewNNSResolver(cfg.RPCAddress, next)
|
2022-01-11 10:09:34 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2022-04-25 09:57:58 +00:00
|
|
|
var dns ns.DNS
|
|
|
|
|
2022-01-11 10:09:34 +00:00
|
|
|
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
|
2022-04-25 09:57:58 +00:00
|
|
|
cnrID, err := dns.ResolveContainerName(domain)
|
2022-01-11 10:09:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't resolve container '%s' as '%s': %w", name, domain, err)
|
|
|
|
}
|
2022-04-25 09:57:58 +00:00
|
|
|
return &cnrID, nil
|
2022-01-11 10:09:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &BucketResolver{
|
|
|
|
Name: DNSResolver,
|
|
|
|
|
|
|
|
resolve: resolveFunc,
|
|
|
|
next: next,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-04-25 09:57:58 +00:00
|
|
|
func NewNNSResolver(address string, next *BucketResolver) (*BucketResolver, error) {
|
|
|
|
if address == "" {
|
|
|
|
return nil, fmt.Errorf("rpc address must not be empty for NNS resolver")
|
2022-01-11 10:09:34 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 09:57:58 +00:00
|
|
|
var nns ns.NNS
|
|
|
|
|
|
|
|
if err := nns.Dial(address); err != nil {
|
|
|
|
return nil, fmt.Errorf("dial %s: %w", address, err)
|
2022-01-11 10:09:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resolveFunc := func(_ context.Context, name string) (*cid.ID, error) {
|
2022-04-25 09:57:58 +00:00
|
|
|
cnrID, err := nns.ResolveContainerName(name)
|
2022-01-11 10:09:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't resolve container '%s': %w", name, err)
|
|
|
|
}
|
2022-04-25 09:57:58 +00:00
|
|
|
return &cnrID, nil
|
2022-01-11 10:09:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &BucketResolver{
|
|
|
|
Name: NNSResolver,
|
|
|
|
|
|
|
|
resolve: resolveFunc,
|
|
|
|
next: next,
|
|
|
|
}, nil
|
|
|
|
}
|