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

     1  //
     2  // Copyright (c) 2015-2022 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  import "errors"
    23  
    24  //go:generate msgp -file $GOFILE
    25  
    26  // ServicePrincipalAuth holds fields for a successful SP authentication with Azure
    27  type ServicePrincipalAuth struct {
    28  	TenantID     string `json:",omitempty"`
    29  	ClientID     string `json:",omitempty"`
    30  	ClientSecret string `json:",omitempty"`
    31  }
    32  
    33  // TierAzure represents the remote tier configuration for Azure Blob Storage.
    34  type TierAzure struct {
    35  	Endpoint     string `json:",omitempty"`
    36  	AccountName  string `json:",omitempty"`
    37  	AccountKey   string `json:",omitempty"`
    38  	Bucket       string `json:",omitempty"`
    39  	Prefix       string `json:",omitempty"`
    40  	Region       string `json:",omitempty"`
    41  	StorageClass string `json:",omitempty"`
    42  
    43  	SPAuth ServicePrincipalAuth `json:",omitempty"`
    44  }
    45  
    46  // IsSPEnabled returns true if all SP related fields are provided
    47  func (ti TierAzure) IsSPEnabled() bool {
    48  	return ti.SPAuth.TenantID != "" && ti.SPAuth.ClientID != "" && ti.SPAuth.ClientSecret != ""
    49  }
    50  
    51  // AzureOptions supports NewTierAzure to take variadic options
    52  type AzureOptions func(*TierAzure) error
    53  
    54  // AzureServicePrincipal helper to supply optional service principal credentials
    55  func AzureServicePrincipal(tenantID, clientID, clientSecret string) func(az *TierAzure) error {
    56  	return func(az *TierAzure) error {
    57  		if tenantID == "" {
    58  			return errors.New("empty tenant ID unsupported")
    59  		}
    60  		if clientID == "" {
    61  			return errors.New("empty client ID unsupported")
    62  		}
    63  		if clientSecret == "" {
    64  			return errors.New("empty client secret unsupported")
    65  		}
    66  		az.SPAuth.TenantID = tenantID
    67  		az.SPAuth.ClientID = clientID
    68  		az.SPAuth.ClientSecret = clientSecret
    69  		return nil
    70  	}
    71  }
    72  
    73  // AzurePrefix helper to supply optional object prefix to NewTierAzure
    74  func AzurePrefix(prefix string) func(az *TierAzure) error {
    75  	return func(az *TierAzure) error {
    76  		az.Prefix = prefix
    77  		return nil
    78  	}
    79  }
    80  
    81  // AzureEndpoint helper to supply optional endpoint to NewTierAzure
    82  func AzureEndpoint(endpoint string) func(az *TierAzure) error {
    83  	return func(az *TierAzure) error {
    84  		az.Endpoint = endpoint
    85  		return nil
    86  	}
    87  }
    88  
    89  // AzureRegion helper to supply optional region to NewTierAzure
    90  func AzureRegion(region string) func(az *TierAzure) error {
    91  	return func(az *TierAzure) error {
    92  		az.Region = region
    93  		return nil
    94  	}
    95  }
    96  
    97  // AzureStorageClass helper to supply optional storage class to NewTierAzure
    98  func AzureStorageClass(sc string) func(az *TierAzure) error {
    99  	return func(az *TierAzure) error {
   100  		az.StorageClass = sc
   101  		return nil
   102  	}
   103  }
   104  
   105  // NewTierAzure returns a TierConfig of Azure type. Returns error if the given
   106  // parameters are invalid like name is empty etc.
   107  func NewTierAzure(name, accountName, accountKey, bucket string, options ...AzureOptions) (*TierConfig, error) {
   108  	if name == "" {
   109  		return nil, ErrTierNameEmpty
   110  	}
   111  
   112  	az := &TierAzure{
   113  		AccountName: accountName,
   114  		AccountKey:  accountKey,
   115  		Bucket:      bucket,
   116  		// Defaults
   117  		Endpoint:     "http://blob.core.windows.net",
   118  		Prefix:       "",
   119  		Region:       "",
   120  		StorageClass: "",
   121  	}
   122  
   123  	for _, option := range options {
   124  		err := option(az)
   125  		if err != nil {
   126  			return nil, err
   127  		}
   128  	}
   129  
   130  	return &TierConfig{
   131  		Version: TierConfigVer,
   132  		Type:    Azure,
   133  		Name:    name,
   134  		Azure:   az,
   135  	}, nil
   136  }