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-04-10 23:27:04 +01:00
|
|
|
"github.com/vivint/infectious"
|
|
|
|
|
2019-04-03 09:46:21 +01:00
|
|
|
"storj.io/storj/internal/memory"
|
|
|
|
"storj.io/storj/pkg/eestream"
|
|
|
|
"storj.io/storj/pkg/identity"
|
|
|
|
"storj.io/storj/pkg/metainfo/kvmetainfo"
|
|
|
|
"storj.io/storj/pkg/peertls/tlsopts"
|
|
|
|
"storj.io/storj/pkg/storage/buckets"
|
|
|
|
"storj.io/storj/pkg/storage/segments"
|
|
|
|
"storj.io/storj/pkg/storage/streams"
|
|
|
|
"storj.io/storj/pkg/storj"
|
|
|
|
"storj.io/storj/pkg/transport"
|
|
|
|
"storj.io/storj/uplink/metainfo"
|
2019-03-20 15:43:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-04-03 09:46:21 +01:00
|
|
|
maxBucketMetaSize = 10 * memory.MiB
|
2019-03-20 15:43:53 +00:00
|
|
|
)
|
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 {
|
|
|
|
// 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-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-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-05-10 12:17:58 +01:00
|
|
|
ident *identity.FullIdentity
|
|
|
|
tc transport.Client
|
|
|
|
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-04-03 09:46:21 +01:00
|
|
|
func NewUplink(ctx context.Context, cfg *Config) (*Uplink, error) {
|
2019-05-10 12:17:58 +01:00
|
|
|
ident, err := identity.NewFullIdentity(ctx, identity.NewCAOptions{
|
|
|
|
Difficulty: 0,
|
|
|
|
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-05-10 12:17:58 +01:00
|
|
|
tlsOpts, err := tlsopts.NewOptions(ident, tlsConfig)
|
2019-04-03 09:46:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tc := transport.NewClient(tlsOpts)
|
|
|
|
|
|
|
|
return &Uplink{
|
2019-05-10 12:17:58 +01:00
|
|
|
ident: ident,
|
|
|
|
tc: tc,
|
|
|
|
cfg: cfg,
|
2019-04-03 09:46:21 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-04-16 16:29:33 +01:00
|
|
|
// ProjectOptions allows configuration of various project options during opening
|
|
|
|
type ProjectOptions struct {
|
|
|
|
Volatile struct {
|
|
|
|
EncryptionKey *storj.Key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-03 09:46:21 +01:00
|
|
|
// OpenProject returns a Project handle with the given APIKey
|
2019-04-16 16:29:33 +01:00
|
|
|
func (u *Uplink) OpenProject(ctx context.Context, satelliteAddr string, apiKey APIKey, opts *ProjectOptions) (p *Project, err error) {
|
2019-04-03 09:46:21 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-05-14 19:15:12 +01:00
|
|
|
metainfo, err := metainfo.NewClient(ctx, u.tc, satelliteAddr, apiKey.Serialize())
|
2019-04-03 09:46:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-04-10 23:27:04 +01:00
|
|
|
// TODO: we shouldn't really need encoding parameters to manage buckets.
|
|
|
|
whoCares := 1
|
|
|
|
fc, err := infectious.NewFEC(whoCares, whoCares)
|
2019-04-03 09:46:21 +01:00
|
|
|
if err != nil {
|
2019-04-10 23:27:04 +01:00
|
|
|
return nil, Error.New("failed to create erasure coding client: %v", err)
|
|
|
|
}
|
|
|
|
rs, err := eestream.NewRedundancyStrategy(eestream.NewRSScheme(fc, whoCares), whoCares, whoCares)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.New("failed to create redundancy strategy: %v", err)
|
|
|
|
}
|
|
|
|
segments := segments.NewSegmentStore(metainfo, nil, rs, maxBucketMetaSize.Int(), maxBucketMetaSize.Int64())
|
2019-04-16 16:29:33 +01:00
|
|
|
var encryptionKey *storj.Key
|
|
|
|
if opts != nil {
|
|
|
|
encryptionKey = opts.Volatile.EncryptionKey
|
|
|
|
}
|
|
|
|
if encryptionKey == nil {
|
|
|
|
// volatile warning: we're setting an encryption key of all zeros when one isn't provided.
|
|
|
|
// TODO: fix before the final alpha network wipe
|
|
|
|
encryptionKey = new(storj.Key)
|
|
|
|
}
|
2019-04-10 23:27:04 +01:00
|
|
|
streams, err := streams.NewStreamStore(segments, maxBucketMetaSize.Int64(),
|
|
|
|
encryptionKey, memory.KiB.Int(), storj.AESGCM)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.New("failed to create stream store: %v", err)
|
2019-04-03 09:46:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Project{
|
2019-04-10 23:27:04 +01:00
|
|
|
uplinkCfg: u.cfg,
|
2019-04-03 09:46:21 +01:00
|
|
|
tc: u.tc,
|
|
|
|
metainfo: metainfo,
|
2019-04-10 23:27:04 +01:00
|
|
|
project: kvmetainfo.NewProject(buckets.NewStore(streams), memory.KiB.Int32(), rs, 64*memory.MiB.Int64()),
|
2019-04-03 09:46:21 +01:00
|
|
|
maxInlineSize: u.cfg.Volatile.MaxInlineSize,
|
2019-04-10 23:27:04 +01:00
|
|
|
encryptionKey: encryptionKey,
|
2019-04-03 09:46:21 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the Uplink. This may not do anything at present, but should
|
2019-04-10 23:27:04 +01:00
|
|
|
// still be called to allow forward compatibility. No Project or Bucket
|
|
|
|
// objects using this Uplink should be used after calling Close.
|
2019-04-03 09:46:21 +01:00
|
|
|
func (u *Uplink) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|