github.com/minio/madmin-go/v3@v3.0.51/tier-s3.go (about)

     1  //
     2  // Copyright (c) 2015-2023 MinIO, Inc.
     3  //
     4  // This file is part of MinIO Object Storage stack
     5  //
     6  // This program is free software: you can redistribute it and/or modify
     7  // it under the terms of the GNU Affero General Public License as
     8  // published by the Free Software Foundation, either version 3 of the
     9  // License, or (at your option) any later version.
    10  //
    11  // This program is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU Affero General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU Affero General Public License
    17  // along with this program. If not, see <http://www.gnu.org/licenses/>.
    18  //
    19  
    20  package madmin
    21  
    22  //go:generate msgp -file $GOFILE
    23  
    24  // TierS3 represents the remote tier configuration for AWS S3 compatible backend.
    25  type TierS3 struct {
    26  	Endpoint                    string `json:",omitempty"`
    27  	AccessKey                   string `json:",omitempty"`
    28  	SecretKey                   string `json:",omitempty"`
    29  	Bucket                      string `json:",omitempty"`
    30  	Prefix                      string `json:",omitempty"`
    31  	Region                      string `json:",omitempty"`
    32  	StorageClass                string `json:",omitempty"`
    33  	AWSRole                     bool   `json:",omitempty"`
    34  	AWSRoleWebIdentityTokenFile string `json:",omitempty"`
    35  	AWSRoleARN                  string `json:",omitempty"`
    36  	AWSRoleSessionName          string `json:",omitempty"`
    37  	AWSRoleDurationSeconds      int    `json:",omitempty"`
    38  }
    39  
    40  // S3Options supports NewTierS3 to take variadic options
    41  type S3Options func(*TierS3) error
    42  
    43  // S3Region helper to supply optional region to NewTierS3
    44  func S3Region(region string) func(s3 *TierS3) error {
    45  	return func(s3 *TierS3) error {
    46  		s3.Region = region
    47  		return nil
    48  	}
    49  }
    50  
    51  // S3Prefix helper to supply optional object prefix to NewTierS3
    52  func S3Prefix(prefix string) func(s3 *TierS3) error {
    53  	return func(s3 *TierS3) error {
    54  		s3.Prefix = prefix
    55  		return nil
    56  	}
    57  }
    58  
    59  // S3Endpoint helper to supply optional endpoint to NewTierS3
    60  func S3Endpoint(endpoint string) func(s3 *TierS3) error {
    61  	return func(s3 *TierS3) error {
    62  		s3.Endpoint = endpoint
    63  		return nil
    64  	}
    65  }
    66  
    67  // S3StorageClass helper to supply optional storage class to NewTierS3
    68  func S3StorageClass(storageClass string) func(s3 *TierS3) error {
    69  	return func(s3 *TierS3) error {
    70  		s3.StorageClass = storageClass
    71  		return nil
    72  	}
    73  }
    74  
    75  // S3AWSRole helper to use optional AWS Role to NewTierS3
    76  func S3AWSRole() func(s3 *TierS3) error {
    77  	return func(s3 *TierS3) error {
    78  		s3.AWSRole = true
    79  		return nil
    80  	}
    81  }
    82  
    83  // S3AWSRoleWebIdentityTokenFile helper to use optional AWS Role token file to NewTierS3
    84  func S3AWSRoleWebIdentityTokenFile(tokenFile string) func(s3 *TierS3) error {
    85  	return func(s3 *TierS3) error {
    86  		s3.AWSRoleWebIdentityTokenFile = tokenFile
    87  		return nil
    88  	}
    89  }
    90  
    91  // S3AWSRoleARN helper to use optional AWS RoleARN to NewTierS3
    92  func S3AWSRoleARN(roleARN string) func(s3 *TierS3) error {
    93  	return func(s3 *TierS3) error {
    94  		s3.AWSRoleARN = roleARN
    95  		return nil
    96  	}
    97  }
    98  
    99  // S3AWSRoleSessionName helper to use optional AWS RoleSessionName to NewTierS3
   100  func S3AWSRoleSessionName(roleSessionName string) func(s3 *TierS3) error {
   101  	return func(s3 *TierS3) error {
   102  		s3.AWSRoleSessionName = roleSessionName
   103  		return nil
   104  	}
   105  }
   106  
   107  // S3AWSRoleDurationSeconds helper to use optional token duration to NewTierS3
   108  func S3AWSRoleDurationSeconds(dsecs int) func(s3 *TierS3) error {
   109  	return func(s3 *TierS3) error {
   110  		s3.AWSRoleDurationSeconds = dsecs
   111  		return nil
   112  	}
   113  }
   114  
   115  // NewTierS3 returns a TierConfig of S3 type. Returns error if the given
   116  // parameters are invalid like name is empty etc.
   117  func NewTierS3(name, accessKey, secretKey, bucket string, options ...S3Options) (*TierConfig, error) {
   118  	if name == "" {
   119  		return nil, ErrTierNameEmpty
   120  	}
   121  	sc := &TierS3{
   122  		AccessKey: accessKey,
   123  		SecretKey: secretKey,
   124  		Bucket:    bucket,
   125  		// Defaults
   126  		Endpoint:     "https://s3.amazonaws.com",
   127  		Region:       "",
   128  		StorageClass: "",
   129  	}
   130  
   131  	for _, option := range options {
   132  		err := option(sc)
   133  		if err != nil {
   134  			return nil, err
   135  		}
   136  	}
   137  
   138  	return &TierConfig{
   139  		Version: TierConfigVer,
   140  		Type:    S3,
   141  		Name:    name,
   142  		S3:      sc,
   143  	}, nil
   144  }