2020-02-07 15:56:59 +00:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"net"
|
2022-05-06 22:04:07 +01:00
|
|
|
"runtime/pprof"
|
2020-02-07 15:56:59 +00:00
|
|
|
|
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
|
|
|
"storj.io/common/identity"
|
|
|
|
"storj.io/common/storj"
|
2020-03-23 19:18:20 +00:00
|
|
|
"storj.io/private/debug"
|
2020-03-23 19:30:31 +00:00
|
|
|
"storj.io/private/version"
|
2020-02-07 15:56:59 +00:00
|
|
|
"storj.io/storj/private/lifecycle"
|
|
|
|
"storj.io/storj/private/version/checker"
|
2020-02-07 16:36:28 +00:00
|
|
|
"storj.io/storj/satellite/admin"
|
2021-11-12 20:47:41 +00:00
|
|
|
"storj.io/storj/satellite/buckets"
|
2022-12-16 15:17:27 +00:00
|
|
|
"storj.io/storj/satellite/console"
|
2022-04-12 17:59:07 +01:00
|
|
|
"storj.io/storj/satellite/console/restkeys"
|
2021-11-12 20:47:41 +00:00
|
|
|
"storj.io/storj/satellite/metabase"
|
2020-07-06 22:31:40 +01:00
|
|
|
"storj.io/storj/satellite/payments"
|
|
|
|
"storj.io/storj/satellite/payments/stripecoinpayments"
|
2020-02-07 15:56:59 +00:00
|
|
|
)
|
|
|
|
|
2020-12-05 16:01:42 +00:00
|
|
|
// Admin is the satellite core process that runs chores.
|
2020-02-07 15:56:59 +00:00
|
|
|
//
|
|
|
|
// architecture: Peer
|
|
|
|
type Admin struct {
|
|
|
|
// core dependencies
|
2021-11-12 20:47:41 +00:00
|
|
|
Log *zap.Logger
|
|
|
|
Identity *identity.FullIdentity
|
|
|
|
DB DB
|
|
|
|
MetabaseDB *metabase.DB
|
2020-02-07 15:56:59 +00:00
|
|
|
|
|
|
|
Servers *lifecycle.Group
|
|
|
|
Services *lifecycle.Group
|
|
|
|
|
|
|
|
Debug struct {
|
|
|
|
Listener net.Listener
|
|
|
|
Server *debug.Server
|
|
|
|
}
|
|
|
|
|
2020-02-21 17:41:54 +00:00
|
|
|
Version struct {
|
|
|
|
Chore *checker.Chore
|
|
|
|
Service *checker.Service
|
|
|
|
}
|
2020-02-07 16:36:28 +00:00
|
|
|
|
2020-07-06 22:31:40 +01:00
|
|
|
Payments struct {
|
|
|
|
Accounts payments.Accounts
|
|
|
|
Service *stripecoinpayments.Service
|
|
|
|
Stripe stripecoinpayments.StripeClient
|
|
|
|
}
|
|
|
|
|
2020-02-07 16:36:28 +00:00
|
|
|
Admin struct {
|
|
|
|
Listener net.Listener
|
|
|
|
Server *admin.Server
|
|
|
|
}
|
2021-11-12 20:47:41 +00:00
|
|
|
|
|
|
|
Buckets struct {
|
|
|
|
Service *buckets.Service
|
|
|
|
}
|
2022-03-23 17:30:39 +00:00
|
|
|
|
2022-04-12 17:59:07 +01:00
|
|
|
REST struct {
|
|
|
|
Keys *restkeys.Service
|
2022-03-23 17:30:39 +00:00
|
|
|
}
|
2022-12-16 15:17:27 +00:00
|
|
|
|
|
|
|
FreezeAccounts struct {
|
|
|
|
Service *console.AccountFreezeService
|
|
|
|
}
|
2020-02-07 15:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAdmin creates a new satellite admin peer.
|
2021-11-12 20:47:41 +00:00
|
|
|
func NewAdmin(log *zap.Logger, full *identity.FullIdentity, db DB, metabaseDB *metabase.DB,
|
2020-05-12 20:10:32 +01:00
|
|
|
versionInfo version.Info, config *Config, atomicLogLevel *zap.AtomicLevel) (*Admin, error) {
|
2020-02-07 15:56:59 +00:00
|
|
|
peer := &Admin{
|
2021-11-12 20:47:41 +00:00
|
|
|
Log: log,
|
|
|
|
Identity: full,
|
|
|
|
DB: db,
|
|
|
|
MetabaseDB: metabaseDB,
|
2020-02-07 15:56:59 +00:00
|
|
|
|
|
|
|
Servers: lifecycle.NewGroup(log.Named("servers")),
|
|
|
|
Services: lifecycle.NewGroup(log.Named("services")),
|
|
|
|
}
|
|
|
|
|
2021-11-12 20:47:41 +00:00
|
|
|
{
|
|
|
|
peer.Buckets.Service = buckets.NewService(db.Buckets(), metabaseDB)
|
|
|
|
}
|
|
|
|
|
2022-04-12 17:59:07 +01:00
|
|
|
{ // setup rest keys
|
|
|
|
peer.REST.Keys = restkeys.NewService(db.OIDC().OAuthTokens(), config.RESTKeys)
|
2022-03-23 17:30:39 +00:00
|
|
|
}
|
|
|
|
|
2020-02-07 15:56:59 +00:00
|
|
|
{ // setup debug
|
|
|
|
var err error
|
|
|
|
if config.Debug.Address != "" {
|
|
|
|
peer.Debug.Listener, err = net.Listen("tcp", config.Debug.Address)
|
|
|
|
if err != nil {
|
|
|
|
withoutStack := errors.New(err.Error())
|
|
|
|
peer.Log.Debug("failed to start debug endpoints", zap.Error(withoutStack))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debugConfig := config.Debug
|
|
|
|
debugConfig.ControlTitle = "Admin"
|
2020-05-12 20:10:32 +01:00
|
|
|
peer.Debug.Server = debug.NewServerWithAtomicLevel(log.Named("debug"), peer.Debug.Listener, monkit.Default, debugConfig, atomicLogLevel)
|
2020-02-07 15:56:59 +00:00
|
|
|
peer.Servers.Add(lifecycle.Item{
|
|
|
|
Name: "debug",
|
|
|
|
Run: peer.Debug.Server.Run,
|
|
|
|
Close: peer.Debug.Server.Close,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
if !versionInfo.IsZero() {
|
2020-04-13 10:31:17 +01:00
|
|
|
peer.Log.Debug("Version info",
|
|
|
|
zap.Stringer("Version", versionInfo.Version.Version),
|
|
|
|
zap.String("Commit Hash", versionInfo.CommitHash),
|
|
|
|
zap.Stringer("Build Timestamp", versionInfo.Timestamp),
|
|
|
|
zap.Bool("Release Build", versionInfo.Release),
|
|
|
|
)
|
2020-02-07 15:56:59 +00:00
|
|
|
}
|
2020-02-21 17:41:54 +00:00
|
|
|
peer.Version.Service = checker.NewService(log.Named("version"), config.Version, versionInfo, "Satellite")
|
|
|
|
peer.Version.Chore = checker.NewChore(peer.Version.Service, config.Version.CheckInterval)
|
2020-02-07 15:56:59 +00:00
|
|
|
|
|
|
|
peer.Services.Add(lifecycle.Item{
|
|
|
|
Name: "version",
|
2020-02-21 17:41:54 +00:00
|
|
|
Run: peer.Version.Chore.Run,
|
2020-02-07 15:56:59 +00:00
|
|
|
})
|
|
|
|
}
|
2020-02-07 16:36:28 +00:00
|
|
|
|
2020-07-06 22:31:40 +01:00
|
|
|
{ // setup payments
|
|
|
|
pc := config.Payments
|
|
|
|
|
|
|
|
var stripeClient stripecoinpayments.StripeClient
|
|
|
|
var err error
|
|
|
|
switch pc.Provider {
|
|
|
|
default:
|
2020-10-08 18:14:09 +01:00
|
|
|
stripeClient = stripecoinpayments.NewStripeMock(
|
|
|
|
peer.ID(),
|
|
|
|
peer.DB.StripeCoinPayments().Customers(),
|
|
|
|
peer.DB.Console().Users(),
|
|
|
|
)
|
2020-07-06 22:31:40 +01:00
|
|
|
case "stripecoinpayments":
|
|
|
|
stripeClient = stripecoinpayments.NewStripeClient(log, pc.StripeCoinPayments)
|
|
|
|
}
|
|
|
|
|
2022-12-01 07:40:52 +00:00
|
|
|
prices, err := pc.UsagePrice.ToModel()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
priceOverrides, err := pc.UsagePriceOverrides.ToModels()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
|
|
|
|
2020-07-06 22:31:40 +01:00
|
|
|
peer.Payments.Service, err = stripecoinpayments.NewService(
|
|
|
|
peer.Log.Named("payments.stripe:service"),
|
|
|
|
stripeClient,
|
|
|
|
pc.StripeCoinPayments,
|
|
|
|
peer.DB.StripeCoinPayments(),
|
2022-05-10 20:19:53 +01:00
|
|
|
peer.DB.Wallets(),
|
|
|
|
peer.DB.Billing(),
|
2020-07-06 22:31:40 +01:00
|
|
|
peer.DB.Console().Projects(),
|
2023-01-30 22:11:12 +00:00
|
|
|
peer.DB.Console().Users(),
|
2020-07-06 22:31:40 +01:00
|
|
|
peer.DB.ProjectAccounting(),
|
2022-12-01 07:40:52 +00:00
|
|
|
prices,
|
|
|
|
priceOverrides,
|
2023-01-30 22:11:12 +00:00
|
|
|
pc.PackagePlans.Packages,
|
2021-08-27 01:51:26 +01:00
|
|
|
pc.BonusRate)
|
2020-07-06 22:31:40 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, peer.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
peer.Payments.Stripe = stripeClient
|
|
|
|
peer.Payments.Accounts = peer.Payments.Service.Accounts()
|
2022-12-16 15:17:27 +00:00
|
|
|
peer.FreezeAccounts.Service = console.NewAccountFreezeService(db.Console().AccountFreezeEvents(), db.Console().Users(), db.Console().Projects())
|
2020-07-06 22:31:40 +01:00
|
|
|
}
|
2022-05-27 21:13:01 +01:00
|
|
|
|
2020-10-13 13:47:55 +01:00
|
|
|
{ // setup admin endpoint
|
2020-02-07 16:36:28 +00:00
|
|
|
var err error
|
|
|
|
peer.Admin.Listener, err = net.Listen("tcp", config.Admin.Address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-07 17:24:58 +00:00
|
|
|
adminConfig := config.Admin
|
|
|
|
adminConfig.AuthorizationToken = config.Console.AuthToken
|
|
|
|
|
2022-12-16 15:17:27 +00:00
|
|
|
peer.Admin.Server = admin.NewServer(log.Named("admin"), peer.Admin.Listener, peer.DB, peer.Buckets.Service, peer.REST.Keys, peer.FreezeAccounts.Service, peer.Payments.Accounts, config.Console, adminConfig)
|
2020-02-07 16:36:28 +00:00
|
|
|
peer.Servers.Add(lifecycle.Item{
|
|
|
|
Name: "admin",
|
|
|
|
Run: peer.Admin.Server.Run,
|
|
|
|
Close: peer.Admin.Server.Close,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-07 15:56:59 +00:00
|
|
|
return peer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run runs satellite until it's either closed or it errors.
|
|
|
|
func (peer *Admin) Run(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
group, ctx := errgroup.WithContext(ctx)
|
|
|
|
|
2022-05-06 22:04:07 +01:00
|
|
|
pprof.Do(ctx, pprof.Labels("subsystem", "admin"), func(ctx context.Context) {
|
|
|
|
peer.Servers.Run(ctx, group)
|
|
|
|
peer.Services.Run(ctx, group)
|
2020-02-07 15:56:59 +00:00
|
|
|
|
2022-05-06 22:04:07 +01:00
|
|
|
pprof.Do(ctx, pprof.Labels("name", "subsystem-wait"), func(ctx context.Context) {
|
|
|
|
err = group.Wait()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return err
|
2020-02-07 15:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes all the resources.
|
|
|
|
func (peer *Admin) Close() error {
|
|
|
|
return errs.Combine(
|
|
|
|
peer.Servers.Close(),
|
|
|
|
peer.Services.Close(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns the peer ID.
|
|
|
|
func (peer *Admin) ID() storj.NodeID { return peer.Identity.ID }
|