2019-03-20 15:43:53 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package uplink
|
|
|
|
|
|
|
|
import (
|
2019-04-03 09:46:21 +01:00
|
|
|
"context"
|
2019-07-18 16:13:59 +01:00
|
|
|
"time"
|
2019-04-03 09:46:21 +01:00
|
|
|
|
2019-11-22 21:00:04 +00:00
|
|
|
"github.com/zeebo/errs"
|
2019-07-02 11:08:02 +01:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/identity"
|
|
|
|
"storj.io/common/memory"
|
|
|
|
"storj.io/common/peertls/tlsopts"
|
|
|
|
"storj.io/common/rpc"
|
2020-02-21 14:07:29 +00:00
|
|
|
"storj.io/uplink/private/metainfo"
|
|
|
|
"storj.io/uplink/private/metainfo/kvmetainfo"
|
2019-03-20 15:43:53 +00:00
|
|
|
)
|
|
|
|
|
2019-07-18 16:13:59 +01:00
|
|
|
const defaultUplinkDialTimeout = 20 * time.Second
|
|
|
|
|
2019-04-03 09:46:21 +01:00
|
|
|
// Config represents configuration options for an Uplink
|
|
|
|
type Config struct {
|
|
|
|
// Volatile groups config values that are likely to change semantics
|
|
|
|
// or go away entirely between releases. Be careful when using them!
|
|
|
|
Volatile struct {
|
2019-07-02 11:08:02 +01:00
|
|
|
// Log is the logger to use for uplink components
|
|
|
|
Log *zap.Logger
|
|
|
|
|
2019-04-03 09:46:21 +01:00
|
|
|
// TLS defines options that affect TLS negotiation for outbound
|
|
|
|
// connections initiated by this uplink.
|
|
|
|
TLS struct {
|
|
|
|
// SkipPeerCAWhitelist determines whether to require all
|
|
|
|
// remote hosts to have identity certificates signed by
|
|
|
|
// Certificate Authorities in the default whitelist. If
|
|
|
|
// set to true, the whitelist will be ignored.
|
|
|
|
SkipPeerCAWhitelist bool
|
|
|
|
|
|
|
|
// PeerCAWhitelistPath gives the path to a CA cert
|
|
|
|
// whitelist file. It is ignored if SkipPeerCAWhitelist
|
|
|
|
// is set. If empty, the internal default peer whitelist
|
|
|
|
// is used.
|
|
|
|
PeerCAWhitelistPath string
|
|
|
|
}
|
|
|
|
|
2019-04-09 18:01:45 +01:00
|
|
|
// PeerIDVersion is the identity versions remote peers to this node
|
|
|
|
// will be supported by this node.
|
|
|
|
PeerIDVersion string
|
|
|
|
|
2019-04-03 09:46:21 +01:00
|
|
|
// MaxInlineSize determines whether the uplink will attempt to
|
2019-04-25 09:46:32 +01:00
|
|
|
// store a new object in the satellite's metainfo. Objects at
|
2019-04-03 09:46:21 +01:00
|
|
|
// or below this size will be marked for inline storage, and
|
|
|
|
// objects above this size will not. (The satellite may reject
|
|
|
|
// the inline storage and require remote storage, still.)
|
|
|
|
MaxInlineSize memory.Size
|
2019-04-10 23:27:04 +01:00
|
|
|
|
|
|
|
// MaxMemory is the default maximum amount of memory to be
|
|
|
|
// allocated for read buffers while performing decodes of
|
|
|
|
// objects. (This option is overrideable per Bucket if the user
|
|
|
|
// so desires.) If set to zero, the library default (4 MiB) will
|
|
|
|
// be used. If set to a negative value, the system will use the
|
|
|
|
// smallest amount of memory it can.
|
|
|
|
MaxMemory memory.Size
|
2019-06-26 17:22:01 +01:00
|
|
|
|
|
|
|
// PartnerID is the identity given to the partner for value
|
2019-11-26 11:12:37 +00:00
|
|
|
// attribution.
|
|
|
|
//
|
|
|
|
// Deprecated: prefer UserAgent
|
2019-06-26 17:22:01 +01:00
|
|
|
PartnerID string
|
2019-07-18 16:13:59 +01:00
|
|
|
|
2019-11-26 11:12:37 +00:00
|
|
|
// UserAgent for the product using the library.
|
|
|
|
UserAgent string
|
|
|
|
|
2019-07-18 16:13:59 +01:00
|
|
|
// DialTimeout is the maximum time to wait connecting to another node.
|
|
|
|
// If not set, the library default (20 seconds) will be used.
|
|
|
|
DialTimeout time.Duration
|
2019-11-22 21:00:04 +00:00
|
|
|
|
|
|
|
// PBKDFConcurrency is the passphrase-based key derivation function
|
|
|
|
// concurrency to use.
|
|
|
|
// WARNING: changing this value fundamentally changes how keys are
|
|
|
|
// derived. Keys generated with one value will not be the same keys
|
|
|
|
// as generated with other values! Leaving this at the default is
|
|
|
|
// highly recommended.
|
|
|
|
//
|
2019-11-25 15:55:54 +00:00
|
|
|
// Unfortunately, up to version v0.26.2, we automatically set this to the
|
2019-11-22 21:00:04 +00:00
|
|
|
// number of CPU cores your processor had. If you are having trouble
|
2019-11-25 15:55:54 +00:00
|
|
|
// decrypting data uploaded with v0.26.2 or older, you may need to set
|
2019-11-22 21:00:04 +00:00
|
|
|
// this value to the number of cores your computer had at the time
|
|
|
|
// you entered a passphrase.
|
|
|
|
//
|
|
|
|
// Otherwise, this value should be left at the default value of 0
|
|
|
|
// (which means to use the internal default).
|
|
|
|
PBKDFConcurrency int
|
2019-04-03 09:46:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-26 18:15:41 +01:00
|
|
|
func (cfg *Config) clone() *Config {
|
|
|
|
clone := *cfg
|
|
|
|
return &clone
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *Config) setDefaults(ctx context.Context) error {
|
|
|
|
if cfg.Volatile.MaxInlineSize == 0 {
|
|
|
|
cfg.Volatile.MaxInlineSize = 4 * memory.KiB
|
2019-04-03 09:46:21 +01:00
|
|
|
}
|
2019-04-26 18:15:41 +01:00
|
|
|
if cfg.Volatile.MaxMemory.Int() == 0 {
|
|
|
|
cfg.Volatile.MaxMemory = 4 * memory.MiB
|
|
|
|
} else if cfg.Volatile.MaxMemory.Int() < 0 {
|
|
|
|
cfg.Volatile.MaxMemory = 0
|
2019-04-10 23:27:04 +01:00
|
|
|
}
|
2019-07-02 11:08:02 +01:00
|
|
|
if cfg.Volatile.Log == nil {
|
2019-08-01 12:14:09 +01:00
|
|
|
cfg.Volatile.Log = zap.NewNop()
|
2019-07-02 11:08:02 +01:00
|
|
|
}
|
2019-07-18 16:13:59 +01:00
|
|
|
if cfg.Volatile.DialTimeout.Seconds() == 0 {
|
|
|
|
cfg.Volatile.DialTimeout = defaultUplinkDialTimeout
|
|
|
|
}
|
2019-11-22 21:00:04 +00:00
|
|
|
if cfg.Volatile.PBKDFConcurrency == 0 {
|
|
|
|
// WARNING: if this default value changes, the root keys of every user will change.
|
|
|
|
// So, don't change this without sufficiently good reason.
|
|
|
|
// some other argon2 wrapper libraries have chosen 8 as the default, so
|
|
|
|
// we do here.
|
|
|
|
cfg.Volatile.PBKDFConcurrency = 8
|
|
|
|
}
|
|
|
|
if cfg.Volatile.PBKDFConcurrency < 0 || cfg.Volatile.PBKDFConcurrency >= 256 {
|
|
|
|
return errs.New("Invalid value for PBKDFConcurrency (must fit in a uint8)")
|
|
|
|
}
|
2019-04-03 09:46:21 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uplink represents the main entrypoint to Storj V3. An Uplink connects to
|
|
|
|
// a specific Satellite and caches connections and resources, allowing one to
|
|
|
|
// create sessions delineated by specific access controls.
|
|
|
|
type Uplink struct {
|
2019-09-19 05:46:39 +01:00
|
|
|
ident *identity.FullIdentity
|
|
|
|
dialer rpc.Dialer
|
|
|
|
cfg *Config
|
2019-04-03 09:46:21 +01:00
|
|
|
}
|
|
|
|
|
2019-05-07 16:44:01 +01:00
|
|
|
// NewUplink creates a new Uplink. This is the first step to create an uplink
|
|
|
|
// session with a user specified config or with default config, if nil config
|
2019-06-05 16:03:11 +01:00
|
|
|
func NewUplink(ctx context.Context, cfg *Config) (_ *Uplink, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-05-10 12:17:58 +01:00
|
|
|
ident, err := identity.NewFullIdentity(ctx, identity.NewCAOptions{
|
2019-06-10 13:00:54 +01:00
|
|
|
Difficulty: 9,
|
2019-05-10 12:17:58 +01:00
|
|
|
Concurrency: 1,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-04-03 09:46:21 +01:00
|
|
|
if cfg == nil {
|
|
|
|
cfg = &Config{}
|
|
|
|
}
|
2019-04-26 18:15:41 +01:00
|
|
|
cfg = cfg.clone()
|
2019-04-03 09:46:21 +01:00
|
|
|
if err := cfg.setDefaults(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tlsConfig := tlsopts.Config{
|
|
|
|
UsePeerCAWhitelist: !cfg.Volatile.TLS.SkipPeerCAWhitelist,
|
|
|
|
PeerCAWhitelistPath: cfg.Volatile.TLS.PeerCAWhitelistPath,
|
2019-04-10 23:27:04 +01:00
|
|
|
PeerIDVersions: "0",
|
2019-04-03 09:46:21 +01:00
|
|
|
}
|
2019-09-19 05:46:39 +01:00
|
|
|
|
|
|
|
tlsOptions, err := tlsopts.NewOptions(ident, tlsConfig, nil)
|
2019-04-03 09:46:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-18 16:13:59 +01:00
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
dialer := rpc.NewDefaultDialer(tlsOptions)
|
|
|
|
dialer.DialTimeout = cfg.Volatile.DialTimeout
|
2019-04-03 09:46:21 +01:00
|
|
|
|
|
|
|
return &Uplink{
|
2019-09-19 05:46:39 +01:00
|
|
|
ident: ident,
|
|
|
|
dialer: dialer,
|
|
|
|
cfg: cfg,
|
2019-04-03 09:46:21 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-06-25 16:36:23 +01:00
|
|
|
// TODO: move the project related OpenProject and Close to project.go
|
|
|
|
|
2019-04-03 09:46:21 +01:00
|
|
|
// OpenProject returns a Project handle with the given APIKey
|
2019-06-24 03:06:14 +01:00
|
|
|
func (u *Uplink) OpenProject(ctx context.Context, satelliteAddr string, apiKey APIKey) (p *Project, err error) {
|
2019-04-03 09:46:21 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-11-26 11:12:37 +00:00
|
|
|
m, err := metainfo.Dial(ctx, u.dialer, satelliteAddr, apiKey.key, u.cfg.Volatile.UserAgent)
|
2019-04-03 09:46:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-24 03:06:14 +01:00
|
|
|
project, err := kvmetainfo.SetupProject(m)
|
2019-04-03 09:46:21 +01:00
|
|
|
if err != nil {
|
2019-06-24 03:06:14 +01:00
|
|
|
return nil, err
|
2019-04-03 09:46:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Project{
|
2019-11-22 21:00:04 +00:00
|
|
|
uplinkCfg: u.cfg,
|
|
|
|
dialer: u.dialer,
|
|
|
|
metainfo: m,
|
|
|
|
project: project,
|
2019-04-03 09:46:21 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-06-25 16:36:23 +01:00
|
|
|
// Close closes the Project. Opened buckets or objects must not be used after calling Close.
|
|
|
|
func (p *Project) Close() error {
|
|
|
|
return p.metainfo.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the Uplink. Opened projects, buckets or objects must not be used after calling Close.
|
2019-04-03 09:46:21 +01:00
|
|
|
func (u *Uplink) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|