github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/cmd/batch-replicate.go (about)

     1  // Copyright (c) 2015-2023 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package cmd
    19  
    20  import (
    21  	"time"
    22  
    23  	miniogo "github.com/minio/minio-go/v7"
    24  
    25  	"github.com/minio/minio/internal/auth"
    26  )
    27  
    28  //go:generate msgp -file $GOFILE
    29  
    30  // replicate:
    31  //   # source of the objects to be replicated
    32  //   source:
    33  //     type: "minio"
    34  //     bucket: "testbucket"
    35  //     prefix: "spark/"
    36  //
    37  //   # optional flags based filtering criteria
    38  //   # for source objects
    39  //   flags:
    40  //     filter:
    41  //       newerThan: "7d"
    42  //       olderThan: "7d"
    43  //       createdAfter: "date"
    44  //       createdBefore: "date"
    45  //       tags:
    46  //         - key: "name"
    47  //           value: "value*"
    48  //       metadata:
    49  //         - key: "content-type"
    50  //           value: "image/*"
    51  //     notify:
    52  //       endpoint: "https://splunk-hec.dev.com"
    53  //       token: "Splunk ..." # e.g. "Bearer token"
    54  //
    55  //   # target where the objects must be replicated
    56  //   target:
    57  //     type: "minio"
    58  //     bucket: "testbucket1"
    59  //     endpoint: "https://play.min.io"
    60  //     path: "on"
    61  //     credentials:
    62  //       accessKey: "minioadmin"
    63  //       secretKey: "minioadmin"
    64  //       sessionToken: ""
    65  
    66  // BatchReplicateFilter holds all the filters currently supported for batch replication
    67  type BatchReplicateFilter struct {
    68  	NewerThan     time.Duration `yaml:"newerThan,omitempty" json:"newerThan"`
    69  	OlderThan     time.Duration `yaml:"olderThan,omitempty" json:"olderThan"`
    70  	CreatedAfter  time.Time     `yaml:"createdAfter,omitempty" json:"createdAfter"`
    71  	CreatedBefore time.Time     `yaml:"createdBefore,omitempty" json:"createdBefore"`
    72  	Tags          []BatchJobKV  `yaml:"tags,omitempty" json:"tags"`
    73  	Metadata      []BatchJobKV  `yaml:"metadata,omitempty" json:"metadata"`
    74  }
    75  
    76  // BatchJobReplicateFlags various configurations for replication job definition currently includes
    77  // - filter
    78  // - notify
    79  // - retry
    80  type BatchJobReplicateFlags struct {
    81  	Filter BatchReplicateFilter `yaml:"filter" json:"filter"`
    82  	Notify BatchJobNotification `yaml:"notify" json:"notify"`
    83  	Retry  BatchJobRetry        `yaml:"retry" json:"retry"`
    84  }
    85  
    86  // BatchJobReplicateResourceType defines the type of batch jobs
    87  type BatchJobReplicateResourceType string
    88  
    89  // Validate validates if the replicate resource type is recognized and supported
    90  func (t BatchJobReplicateResourceType) Validate() error {
    91  	switch t {
    92  	case BatchJobReplicateResourceMinIO:
    93  	case BatchJobReplicateResourceS3:
    94  	default:
    95  		return errInvalidArgument
    96  	}
    97  	return nil
    98  }
    99  
   100  func (t BatchJobReplicateResourceType) isMinio() bool {
   101  	return t == BatchJobReplicateResourceMinIO
   102  }
   103  
   104  // Different types of batch jobs..
   105  const (
   106  	BatchJobReplicateResourceMinIO BatchJobReplicateResourceType = "minio"
   107  	BatchJobReplicateResourceS3    BatchJobReplicateResourceType = "s3"
   108  
   109  	// add future targets
   110  )
   111  
   112  // BatchJobReplicateCredentials access credentials for batch replication it may
   113  // be either for target or source.
   114  type BatchJobReplicateCredentials struct {
   115  	AccessKey    string `xml:"AccessKeyId" json:"accessKey,omitempty" yaml:"accessKey"`
   116  	SecretKey    string `xml:"SecretAccessKey" json:"secretKey,omitempty" yaml:"secretKey"`
   117  	SessionToken string `xml:"SessionToken" json:"sessionToken,omitempty" yaml:"sessionToken"`
   118  }
   119  
   120  // Empty indicates if credentials are not set
   121  func (c BatchJobReplicateCredentials) Empty() bool {
   122  	return c.AccessKey == "" && c.SecretKey == "" && c.SessionToken == ""
   123  }
   124  
   125  // Validate validates if credentials are valid
   126  func (c BatchJobReplicateCredentials) Validate() error {
   127  	if !auth.IsAccessKeyValid(c.AccessKey) || !auth.IsSecretKeyValid(c.SecretKey) {
   128  		return errInvalidArgument
   129  	}
   130  	return nil
   131  }
   132  
   133  // BatchJobReplicateTarget describes target element of the replication job that receives
   134  // the filtered data from source
   135  type BatchJobReplicateTarget struct {
   136  	Type     BatchJobReplicateResourceType `yaml:"type" json:"type"`
   137  	Bucket   string                        `yaml:"bucket" json:"bucket"`
   138  	Prefix   string                        `yaml:"prefix" json:"prefix"`
   139  	Endpoint string                        `yaml:"endpoint" json:"endpoint"`
   140  	Path     string                        `yaml:"path" json:"path"`
   141  	Creds    BatchJobReplicateCredentials  `yaml:"credentials" json:"credentials"`
   142  }
   143  
   144  // ValidPath returns true if path is valid
   145  func (t BatchJobReplicateTarget) ValidPath() bool {
   146  	return t.Path == "on" || t.Path == "off" || t.Path == "auto" || t.Path == ""
   147  }
   148  
   149  // BatchJobReplicateSource describes source element of the replication job that is
   150  // the source of the data for the target
   151  type BatchJobReplicateSource struct {
   152  	Type     BatchJobReplicateResourceType `yaml:"type" json:"type"`
   153  	Bucket   string                        `yaml:"bucket" json:"bucket"`
   154  	Prefix   string                        `yaml:"prefix" json:"prefix"`
   155  	Endpoint string                        `yaml:"endpoint" json:"endpoint"`
   156  	Path     string                        `yaml:"path" json:"path"`
   157  	Creds    BatchJobReplicateCredentials  `yaml:"credentials" json:"credentials"`
   158  	Snowball BatchJobSnowball              `yaml:"snowball" json:"snowball"`
   159  }
   160  
   161  // ValidPath returns true if path is valid
   162  func (s BatchJobReplicateSource) ValidPath() bool {
   163  	switch s.Path {
   164  	case "on", "off", "auto", "":
   165  		return true
   166  	default:
   167  		return false
   168  	}
   169  }
   170  
   171  // BatchJobReplicateV1 v1 of batch job replication
   172  type BatchJobReplicateV1 struct {
   173  	APIVersion string                  `yaml:"apiVersion" json:"apiVersion"`
   174  	Flags      BatchJobReplicateFlags  `yaml:"flags" json:"flags"`
   175  	Target     BatchJobReplicateTarget `yaml:"target" json:"target"`
   176  	Source     BatchJobReplicateSource `yaml:"source" json:"source"`
   177  
   178  	clnt *miniogo.Core `msg:"-"`
   179  }
   180  
   181  // RemoteToLocal returns true if source is remote and target is local
   182  func (r BatchJobReplicateV1) RemoteToLocal() bool {
   183  	return !r.Source.Creds.Empty()
   184  }