2022-09-15 15:23:10 +01:00
|
|
|
// Copyright (C) 2022 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-12-03 18:12:05 +00:00
|
|
|
"context"
|
|
|
|
"encoding/csv"
|
2022-09-15 15:23:10 +01:00
|
|
|
"encoding/hex"
|
2022-12-03 18:12:05 +00:00
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2022-09-15 15:23:10 +01:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/common/fpath"
|
|
|
|
"storj.io/common/peertls/tlsopts"
|
|
|
|
"storj.io/common/rpc"
|
|
|
|
"storj.io/common/signing"
|
|
|
|
"storj.io/common/uuid"
|
|
|
|
"storj.io/private/cfgstruct"
|
|
|
|
"storj.io/private/process"
|
|
|
|
"storj.io/storj/private/revocation"
|
|
|
|
"storj.io/storj/satellite"
|
|
|
|
"storj.io/storj/satellite/metabase"
|
|
|
|
"storj.io/storj/satellite/orders"
|
|
|
|
"storj.io/storj/satellite/overlay"
|
|
|
|
"storj.io/storj/satellite/satellitedb"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Satellite defines satellite configuration.
|
|
|
|
type Satellite struct {
|
|
|
|
Database string `help:"satellite database connection string" releaseDefault:"postgres://" devDefault:"postgres://"`
|
|
|
|
|
|
|
|
satellite.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
rootCmd = &cobra.Command{
|
|
|
|
Use: "segment-verify",
|
|
|
|
Short: "segment-verify",
|
|
|
|
}
|
|
|
|
|
|
|
|
runCmd = &cobra.Command{
|
|
|
|
Use: "run",
|
|
|
|
Short: "commands to process segments",
|
|
|
|
}
|
|
|
|
|
|
|
|
rangeCmd = &cobra.Command{
|
|
|
|
Use: "range",
|
|
|
|
Short: "runs the command on a range of segments",
|
2022-12-03 18:12:05 +00:00
|
|
|
RunE: verifySegments,
|
|
|
|
}
|
|
|
|
|
|
|
|
bucketsCmd = &cobra.Command{
|
|
|
|
Use: "buckets",
|
|
|
|
Short: "runs the command on segments from specified buckets",
|
|
|
|
RunE: verifySegments,
|
2022-09-15 15:23:10 +01:00
|
|
|
}
|
|
|
|
|
2023-01-18 17:34:19 +00:00
|
|
|
readCSVCmd = &cobra.Command{
|
|
|
|
Use: "read-csv",
|
|
|
|
Short: "runs the command on segments from an input CSV file",
|
|
|
|
RunE: verifySegments,
|
|
|
|
}
|
|
|
|
|
2022-10-10 17:59:35 +01:00
|
|
|
summarizeCmd = &cobra.Command{
|
|
|
|
Use: "summarize-log",
|
|
|
|
Short: "summarizes verification log",
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
RunE: summarizeVerificationLog,
|
|
|
|
}
|
|
|
|
|
2022-12-13 22:14:39 +00:00
|
|
|
nodeCheckCmd = &cobra.Command{
|
|
|
|
Use: "node-check",
|
|
|
|
Short: "checks segments for too many duplicate or unvetted nodes",
|
|
|
|
RunE: verifySegmentsNodeCheck,
|
2022-12-09 17:04:05 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 22:14:39 +00:00
|
|
|
satelliteCfg Satellite
|
|
|
|
rangeCfg RangeConfig
|
|
|
|
bucketsCfg BucketConfig
|
2023-01-18 17:34:19 +00:00
|
|
|
readCSVCfg ReadCSVConfig
|
2022-12-13 22:14:39 +00:00
|
|
|
nodeCheckCfg NodeCheckConfig
|
2022-09-15 15:23:10 +01:00
|
|
|
|
|
|
|
confDir string
|
|
|
|
identityDir string
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
defaultConfDir := fpath.ApplicationDir("storj", "satellite")
|
|
|
|
defaultIdentityDir := fpath.ApplicationDir("storj", "identity", "satellite")
|
|
|
|
cfgstruct.SetupFlag(zap.L(), rootCmd, &confDir, "config-dir", defaultConfDir, "main directory for satellite configuration")
|
|
|
|
cfgstruct.SetupFlag(zap.L(), rootCmd, &identityDir, "identity-dir", defaultIdentityDir, "main directory for satellite identity credentials")
|
|
|
|
defaults := cfgstruct.DefaultsFlag(rootCmd)
|
|
|
|
|
|
|
|
rootCmd.AddCommand(runCmd)
|
2022-10-10 17:59:35 +01:00
|
|
|
rootCmd.AddCommand(summarizeCmd)
|
2022-12-13 22:14:39 +00:00
|
|
|
rootCmd.AddCommand(nodeCheckCmd)
|
2022-09-15 15:23:10 +01:00
|
|
|
runCmd.AddCommand(rangeCmd)
|
2022-12-03 18:12:05 +00:00
|
|
|
runCmd.AddCommand(bucketsCmd)
|
2023-01-18 17:34:19 +00:00
|
|
|
runCmd.AddCommand(readCSVCmd)
|
2022-09-15 15:23:10 +01:00
|
|
|
|
|
|
|
process.Bind(runCmd, &satelliteCfg, defaults, cfgstruct.ConfDir(confDir), cfgstruct.IdentityDir(identityDir))
|
2022-12-09 17:04:05 +00:00
|
|
|
|
2022-10-05 14:17:19 +01:00
|
|
|
process.Bind(rangeCmd, &satelliteCfg, defaults, cfgstruct.ConfDir(confDir), cfgstruct.IdentityDir(identityDir))
|
2022-09-15 15:23:10 +01:00
|
|
|
process.Bind(rangeCmd, &rangeCfg, defaults, cfgstruct.ConfDir(confDir), cfgstruct.IdentityDir(identityDir))
|
2022-12-03 18:12:05 +00:00
|
|
|
process.Bind(bucketsCmd, &satelliteCfg, defaults, cfgstruct.ConfDir(confDir), cfgstruct.IdentityDir(identityDir))
|
|
|
|
process.Bind(bucketsCmd, &bucketsCfg, defaults, cfgstruct.ConfDir(confDir), cfgstruct.IdentityDir(identityDir))
|
2023-01-18 17:34:19 +00:00
|
|
|
process.Bind(readCSVCmd, &satelliteCfg, defaults, cfgstruct.ConfDir(confDir), cfgstruct.IdentityDir(identityDir))
|
|
|
|
process.Bind(readCSVCmd, &readCSVCfg, defaults, cfgstruct.ConfDir(confDir), cfgstruct.IdentityDir(identityDir))
|
2022-12-09 17:04:05 +00:00
|
|
|
|
2022-12-13 22:14:39 +00:00
|
|
|
process.Bind(nodeCheckCmd, &satelliteCfg, defaults, cfgstruct.ConfDir(confDir), cfgstruct.IdentityDir(identityDir))
|
|
|
|
process.Bind(nodeCheckCmd, &nodeCheckCfg, defaults, cfgstruct.ConfDir(confDir), cfgstruct.IdentityDir(identityDir))
|
2022-09-15 15:23:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// RangeConfig defines configuration for verifying segment existence.
|
|
|
|
type RangeConfig struct {
|
|
|
|
Service ServiceConfig
|
|
|
|
Verify VerifierConfig
|
|
|
|
|
|
|
|
Low string `help:"hex lowest segment id prefix to verify"`
|
|
|
|
High string `help:"hex highest segment id prefix to verify (excluded)"`
|
|
|
|
}
|
|
|
|
|
2022-12-03 18:12:05 +00:00
|
|
|
// BucketConfig defines configuration for verifying segment existence within a list of buckets.
|
|
|
|
type BucketConfig struct {
|
|
|
|
Service ServiceConfig
|
|
|
|
Verify VerifierConfig
|
|
|
|
|
|
|
|
BucketsCSV string `help:"csv file of project_id,bucket_name of buckets to verify" default:""`
|
|
|
|
}
|
|
|
|
|
2023-01-18 17:34:19 +00:00
|
|
|
// ReadCSVConfig defines configuration for verifying existence of specific segments.
|
|
|
|
type ReadCSVConfig struct {
|
|
|
|
Service ServiceConfig
|
|
|
|
Verify VerifierConfig
|
|
|
|
|
|
|
|
InputFile string `help:"csv file of segment_id,position for segments to verify"`
|
|
|
|
}
|
|
|
|
|
2022-12-03 18:12:05 +00:00
|
|
|
func verifySegments(cmd *cobra.Command, args []string) error {
|
|
|
|
|
2022-09-15 15:23:10 +01:00
|
|
|
ctx, _ := process.Ctx(cmd)
|
|
|
|
log := zap.L()
|
|
|
|
|
|
|
|
// open default satellite database
|
|
|
|
db, err := satellitedb.Open(ctx, log.Named("db"), satelliteCfg.Database, satellitedb.Options{
|
|
|
|
ApplicationName: "segment-verify",
|
|
|
|
SaveRollupBatchSize: satelliteCfg.Tally.SaveRollupBatchSize,
|
|
|
|
ReadRollupBatchSize: satelliteCfg.Tally.ReadRollupBatchSize,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return errs.New("Error starting master database on satellite: %+v", err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, db.Close())
|
|
|
|
}()
|
|
|
|
|
|
|
|
// open metabase
|
2022-10-28 15:56:20 +01:00
|
|
|
metabaseDB, err := metabase.Open(ctx, log.Named("metabase"), satelliteCfg.Metainfo.DatabaseURL,
|
2023-01-12 12:16:36 +00:00
|
|
|
satelliteCfg.Config.Metainfo.Metabase("segment-verify"))
|
2022-09-15 15:23:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
defer func() { _ = metabaseDB.Close() }()
|
|
|
|
|
|
|
|
// check whether satellite and metabase versions match
|
|
|
|
versionErr := db.CheckVersion(ctx)
|
|
|
|
if versionErr != nil {
|
|
|
|
log.Error("versions skewed", zap.Error(versionErr))
|
|
|
|
return Error.Wrap(versionErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
versionErr = metabaseDB.CheckVersion(ctx)
|
|
|
|
if versionErr != nil {
|
|
|
|
log.Error("versions skewed", zap.Error(versionErr))
|
|
|
|
return Error.Wrap(versionErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup dialer
|
|
|
|
identity, err := satelliteCfg.Identity.Load()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to load identity.", zap.Error(err))
|
|
|
|
return errs.New("Failed to load identity: %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
revocationDB, err := revocation.OpenDBFromCfg(ctx, satelliteCfg.Server.Config)
|
|
|
|
if err != nil {
|
|
|
|
return errs.New("Error creating revocation database: %+v", err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, revocationDB.Close())
|
|
|
|
}()
|
|
|
|
|
|
|
|
tlsOptions, err := tlsopts.NewOptions(identity, satelliteCfg.Server.Config, revocationDB)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dialer := rpc.NewDefaultDialer(tlsOptions)
|
|
|
|
|
|
|
|
// setup dependencies for verification
|
2023-03-08 12:25:10 +00:00
|
|
|
overlay, err := overlay.NewService(log.Named("overlay"), db.OverlayCache(), db.NodeEvents(), "", "", satelliteCfg.Overlay)
|
2022-09-15 15:23:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2023-01-12 12:16:36 +00:00
|
|
|
ordersService, err := orders.NewService(log.Named("orders"), signing.SignerFromFullIdentity(identity), overlay, orders.NewNoopDB(), satelliteCfg.Orders)
|
2022-09-15 15:23:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2023-01-19 23:44:06 +00:00
|
|
|
var (
|
|
|
|
verifyConfig VerifierConfig
|
|
|
|
serviceConfig ServiceConfig
|
|
|
|
commandFunc func(ctx context.Context, service *Service) error
|
|
|
|
)
|
|
|
|
switch cmd.Name() {
|
|
|
|
case "range":
|
|
|
|
verifyConfig = rangeCfg.Verify
|
|
|
|
serviceConfig = rangeCfg.Service
|
|
|
|
commandFunc = func(ctx context.Context, service *Service) error {
|
|
|
|
return verifySegmentsRange(ctx, service, rangeCfg)
|
|
|
|
}
|
|
|
|
case "buckets":
|
|
|
|
verifyConfig = bucketsCfg.Verify
|
|
|
|
serviceConfig = bucketsCfg.Service
|
|
|
|
commandFunc = func(ctx context.Context, service *Service) error {
|
|
|
|
return verifySegmentsBuckets(ctx, service, bucketsCfg)
|
|
|
|
}
|
|
|
|
case "read-csv":
|
|
|
|
verifyConfig = readCSVCfg.Verify
|
|
|
|
serviceConfig = readCSVCfg.Service
|
|
|
|
commandFunc = func(ctx context.Context, service *Service) error {
|
|
|
|
return verifySegmentsCSV(ctx, service, readCSVCfg)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return errors.New("unknown command: " + cmd.Name())
|
|
|
|
}
|
|
|
|
|
2022-09-15 15:23:10 +01:00
|
|
|
// setup verifier
|
2023-01-19 23:44:06 +00:00
|
|
|
verifier := NewVerifier(log.Named("verifier"), dialer, ordersService, verifyConfig)
|
|
|
|
service, err := NewService(log.Named("service"), metabaseDB, verifier, overlay, serviceConfig)
|
2022-09-16 16:35:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
2023-01-05 17:32:18 +00:00
|
|
|
verifier.reportPiece = service.problemPieces.Write
|
2022-09-16 16:35:19 +01:00
|
|
|
defer func() { err = errs.Combine(err, service.Close()) }()
|
2023-01-19 23:44:06 +00:00
|
|
|
|
|
|
|
log.Debug("starting", zap.Any("config", service.config), zap.String("command", cmd.Name()))
|
|
|
|
return commandFunc(ctx, service)
|
2022-12-03 18:12:05 +00:00
|
|
|
}
|
2022-09-15 15:23:10 +01:00
|
|
|
|
2022-12-03 18:12:05 +00:00
|
|
|
func verifySegmentsRange(ctx context.Context, service *Service, rangeCfg RangeConfig) error {
|
2022-09-15 15:23:10 +01:00
|
|
|
// parse arguments
|
|
|
|
var low, high uuid.UUID
|
|
|
|
|
|
|
|
lowBytes, err := hex.DecodeString(rangeCfg.Low)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
highBytes, err := hex.DecodeString(rangeCfg.High)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
copy(low[:], lowBytes)
|
2022-10-05 14:17:19 +01:00
|
|
|
copy(high[:], highBytes)
|
2022-09-15 15:23:10 +01:00
|
|
|
|
|
|
|
if high.IsZero() {
|
|
|
|
return Error.New("high argument not specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
return service.ProcessRange(ctx, low, high)
|
|
|
|
}
|
|
|
|
|
2022-12-03 18:12:05 +00:00
|
|
|
func verifySegmentsBuckets(ctx context.Context, service *Service, bucketCfg BucketConfig) error {
|
|
|
|
if bucketsCfg.BucketsCSV == "" {
|
|
|
|
return Error.New("bucket list file path not provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
bucketList, err := service.ParseBucketFile(bucketsCfg.BucketsCSV)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
return service.ProcessBuckets(ctx, bucketList.Buckets)
|
|
|
|
}
|
|
|
|
|
2023-01-18 17:34:19 +00:00
|
|
|
func verifySegmentsCSV(ctx context.Context, service *Service, readCSVCfg ReadCSVConfig) (err error) {
|
|
|
|
if readCSVCfg.InputFile == "" {
|
|
|
|
return Error.New("input CSV file not provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
segmentSource, err := OpenSegmentCSVFile(readCSVCfg.InputFile)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, segmentSource.Close()) }()
|
|
|
|
return service.ProcessSegmentsFromCSV(ctx, segmentSource)
|
|
|
|
}
|
|
|
|
|
2022-09-15 15:23:10 +01:00
|
|
|
func main() {
|
|
|
|
process.Exec(rootCmd)
|
|
|
|
}
|
2022-12-03 18:12:05 +00:00
|
|
|
|
|
|
|
// ParseBucketFile parses a csv file containing project_id and bucket names.
|
|
|
|
func (service *Service) ParseBucketFile(path string) (_ BucketList, err error) {
|
|
|
|
csvFile, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return BucketList{}, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, csvFile.Close())
|
|
|
|
}()
|
|
|
|
|
|
|
|
csvReader := csv.NewReader(csvFile)
|
|
|
|
allEntries, err := csvReader.ReadAll()
|
|
|
|
if err != nil {
|
|
|
|
return BucketList{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bucketList := BucketList{}
|
|
|
|
for _, entry := range allEntries {
|
|
|
|
if len(entry) < 2 {
|
|
|
|
return BucketList{}, Error.New("unable to parse buckets file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
projectId, err := projectIdFromCompactString(strings.TrimSpace(entry[0]))
|
|
|
|
if err != nil {
|
|
|
|
return BucketList{}, Error.New("unable to parse buckets file: %w", err)
|
|
|
|
}
|
|
|
|
bucketList.Add(projectId, strings.TrimSpace(entry[1]))
|
|
|
|
}
|
|
|
|
return bucketList, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func projectIdFromCompactString(s string) (uuid.UUID, error) {
|
|
|
|
decoded, err := hex.DecodeString(s)
|
|
|
|
if err != nil {
|
|
|
|
return uuid.UUID{}, Error.New("invalid string")
|
|
|
|
}
|
|
|
|
|
|
|
|
return uuid.FromBytes(decoded)
|
|
|
|
}
|