2021-04-19 13:30:42 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2024-08-13 14:21:34 -04:00
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
|
2024-10-31 18:10:24 -04:00
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
|
2024-08-13 14:21:34 -04:00
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blob"
|
2023-06-19 20:53:08 -04:00
|
|
|
"github.com/minio/madmin-go/v3"
|
2021-04-19 13:30:42 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type warmBackendAzure struct {
|
2024-08-13 14:21:34 -04:00
|
|
|
clnt *azblob.Client
|
2021-04-19 13:30:42 -04:00
|
|
|
Bucket string
|
|
|
|
Prefix string
|
|
|
|
StorageClass string
|
|
|
|
}
|
|
|
|
|
2024-08-13 14:21:34 -04:00
|
|
|
func (az *warmBackendAzure) tier() *blob.AccessTier {
|
|
|
|
if az.StorageClass == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, t := range blob.PossibleAccessTierValues() {
|
|
|
|
if strings.EqualFold(az.StorageClass, string(t)) {
|
|
|
|
return &t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-19 13:30:42 -04:00
|
|
|
func (az *warmBackendAzure) getDest(object string) string {
|
|
|
|
destObj := object
|
|
|
|
if az.Prefix != "" {
|
|
|
|
destObj = fmt.Sprintf("%s/%s", az.Prefix, object)
|
|
|
|
}
|
|
|
|
return destObj
|
|
|
|
}
|
2022-01-02 12:15:06 -05:00
|
|
|
|
2024-10-31 18:10:24 -04:00
|
|
|
func (az *warmBackendAzure) PutWithMeta(ctx context.Context, object string, r io.Reader, length int64, meta map[string]string) (remoteVersionID, error) {
|
|
|
|
azMeta := map[string]*string{}
|
|
|
|
for k, v := range meta {
|
|
|
|
azMeta[k] = to.Ptr(v)
|
|
|
|
}
|
2024-08-13 14:21:34 -04:00
|
|
|
resp, err := az.clnt.UploadStream(ctx, az.Bucket, az.getDest(object), io.LimitReader(r, length), &azblob.UploadStreamOptions{
|
|
|
|
Concurrency: 4,
|
|
|
|
AccessTier: az.tier(), // set tier if specified
|
2024-10-31 18:10:24 -04:00
|
|
|
Metadata: azMeta,
|
2024-08-13 14:21:34 -04:00
|
|
|
})
|
2021-09-21 18:47:30 -04:00
|
|
|
if err != nil {
|
2024-08-13 14:21:34 -04:00
|
|
|
return "", azureToObjectError(err, az.Bucket, az.getDest(object))
|
2021-09-21 18:47:30 -04:00
|
|
|
}
|
2024-08-13 14:21:34 -04:00
|
|
|
vid := ""
|
|
|
|
if resp.VersionID != nil {
|
|
|
|
vid = *resp.VersionID
|
|
|
|
}
|
|
|
|
return remoteVersionID(vid), nil
|
2021-04-19 13:30:42 -04:00
|
|
|
}
|
|
|
|
|
2024-10-31 18:10:24 -04:00
|
|
|
func (az *warmBackendAzure) Put(ctx context.Context, object string, r io.Reader, length int64) (remoteVersionID, error) {
|
|
|
|
return az.PutWithMeta(ctx, object, r, length, map[string]string{})
|
|
|
|
}
|
|
|
|
|
2021-06-03 17:26:51 -04:00
|
|
|
func (az *warmBackendAzure) Get(ctx context.Context, object string, rv remoteVersionID, opts WarmBackendGetOpts) (r io.ReadCloser, err error) {
|
2021-04-19 13:30:42 -04:00
|
|
|
if opts.startOffset < 0 {
|
|
|
|
return nil, InvalidRange{}
|
|
|
|
}
|
2024-08-13 14:21:34 -04:00
|
|
|
resp, err := az.clnt.DownloadStream(ctx, az.Bucket, az.getDest(object), &azblob.DownloadStreamOptions{
|
|
|
|
Range: blob.HTTPRange{Offset: opts.startOffset, Count: opts.length},
|
|
|
|
})
|
2021-04-19 13:30:42 -04:00
|
|
|
if err != nil {
|
2024-08-13 14:21:34 -04:00
|
|
|
return nil, azureToObjectError(err, az.Bucket, az.getDest(object))
|
2021-04-19 13:30:42 -04:00
|
|
|
}
|
|
|
|
|
2024-08-13 14:21:34 -04:00
|
|
|
return resp.Body, nil
|
2021-04-19 13:30:42 -04:00
|
|
|
}
|
|
|
|
|
2021-06-03 17:26:51 -04:00
|
|
|
func (az *warmBackendAzure) Remove(ctx context.Context, object string, rv remoteVersionID) error {
|
2024-08-13 14:21:34 -04:00
|
|
|
_, err := az.clnt.DeleteBlob(ctx, az.Bucket, az.getDest(object), &azblob.DeleteBlobOptions{})
|
|
|
|
return azureToObjectError(err, az.Bucket, az.getDest(object))
|
2021-04-19 13:30:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (az *warmBackendAzure) InUse(ctx context.Context) (bool, error) {
|
2024-08-13 14:21:34 -04:00
|
|
|
maxResults := int32(1)
|
|
|
|
pager := az.clnt.NewListBlobsFlatPager(az.Bucket, &azblob.ListBlobsFlatOptions{
|
|
|
|
Prefix: &az.Prefix,
|
|
|
|
MaxResults: &maxResults,
|
2021-04-19 13:30:42 -04:00
|
|
|
})
|
2024-08-13 14:21:34 -04:00
|
|
|
if !pager.More() {
|
|
|
|
return false, nil
|
2023-12-12 00:51:53 -05:00
|
|
|
}
|
|
|
|
|
2024-08-13 14:21:34 -04:00
|
|
|
resp, err := pager.NextPage(ctx)
|
2023-12-12 00:51:53 -05:00
|
|
|
if err != nil {
|
2024-08-13 14:21:34 -04:00
|
|
|
if strings.Contains(err.Error(), "no more pages") {
|
|
|
|
return false, nil
|
2023-12-12 00:51:53 -05:00
|
|
|
}
|
2024-08-13 14:21:34 -04:00
|
|
|
return false, azureToObjectError(err, az.Bucket, az.Prefix)
|
|
|
|
}
|
2023-12-12 00:51:53 -05:00
|
|
|
|
2024-08-13 14:21:34 -04:00
|
|
|
return len(resp.Segment.BlobItems) > 0, nil
|
2023-12-12 00:51:53 -05:00
|
|
|
}
|
|
|
|
|
2024-08-13 14:21:34 -04:00
|
|
|
type azureConf struct {
|
|
|
|
madmin.TierAzure
|
|
|
|
}
|
2023-12-12 00:51:53 -05:00
|
|
|
|
2024-08-13 14:21:34 -04:00
|
|
|
func (conf azureConf) Validate() error {
|
2023-12-21 19:58:10 -05:00
|
|
|
switch {
|
|
|
|
case conf.AccountName == "":
|
2024-08-13 14:21:34 -04:00
|
|
|
return errors.New("the account name is required")
|
2023-12-21 19:58:10 -05:00
|
|
|
case conf.AccountKey != "" && (conf.SPAuth.TenantID != "" || conf.SPAuth.ClientID != "" || conf.SPAuth.ClientSecret != ""):
|
2024-08-13 14:21:34 -04:00
|
|
|
return errors.New("multiple authentication mechanisms are provided")
|
2023-12-21 19:58:10 -05:00
|
|
|
case conf.AccountKey == "" && (conf.SPAuth.TenantID == "" || conf.SPAuth.ClientID == "" || conf.SPAuth.ClientSecret == ""):
|
2024-08-13 14:21:34 -04:00
|
|
|
return errors.New("no authentication mechanism was provided")
|
2023-12-21 19:58:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if conf.Bucket == "" {
|
2024-08-13 14:21:34 -04:00
|
|
|
return errors.New("no bucket name was provided")
|
2023-12-21 19:58:10 -05:00
|
|
|
}
|
|
|
|
|
2024-08-13 14:21:34 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (conf azureConf) NewClient() (clnt *azblob.Client, clntErr error) {
|
|
|
|
if err := conf.Validate(); err != nil {
|
2021-04-19 13:30:42 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-08-13 14:21:34 -04:00
|
|
|
|
|
|
|
ep := conf.Endpoint
|
|
|
|
if ep == "" {
|
|
|
|
ep = fmt.Sprintf("https://%s.blob.core.windows.net", conf.AccountName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.IsSPEnabled() {
|
|
|
|
credential, err := azidentity.NewClientSecretCredential(conf.SPAuth.TenantID, conf.SPAuth.ClientID, conf.SPAuth.ClientSecret, &azidentity.ClientSecretCredentialOptions{})
|
2024-03-05 11:44:08 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-08-13 14:21:34 -04:00
|
|
|
return azblob.NewClient(ep, credential, &azblob.ClientOptions{})
|
|
|
|
}
|
|
|
|
credential, err := azblob.NewSharedKeyCredential(conf.AccountName, conf.AccountKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return azblob.NewClientWithSharedKeyCredential(ep, credential, &azblob.ClientOptions{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func newWarmBackendAzure(conf madmin.TierAzure, _ string) (*warmBackendAzure, error) {
|
|
|
|
clnt, err := azureConf{conf}.NewClient()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-04-19 13:30:42 -04:00
|
|
|
}
|
2024-08-13 14:21:34 -04:00
|
|
|
|
2021-04-19 13:30:42 -04:00
|
|
|
return &warmBackendAzure{
|
2024-08-13 14:21:34 -04:00
|
|
|
clnt: clnt,
|
2021-04-19 13:30:42 -04:00
|
|
|
Bucket: conf.Bucket,
|
|
|
|
Prefix: strings.TrimSuffix(conf.Prefix, slashSeparator),
|
|
|
|
StorageClass: conf.StorageClass,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert azure errors to minio object layer errors.
|
|
|
|
func azureToObjectError(err error, params ...string) error {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket := ""
|
|
|
|
object := ""
|
|
|
|
if len(params) >= 1 {
|
|
|
|
bucket = params[0]
|
|
|
|
}
|
|
|
|
if len(params) == 2 {
|
|
|
|
object = params[1]
|
|
|
|
}
|
|
|
|
|
2024-08-13 14:21:34 -04:00
|
|
|
azureErr, ok := err.(*azcore.ResponseError)
|
2021-04-19 13:30:42 -04:00
|
|
|
if !ok {
|
|
|
|
// We don't interpret non Azure errors. As azure errors will
|
|
|
|
// have StatusCode to help to convert to object errors.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-08-13 14:21:34 -04:00
|
|
|
serviceCode := azureErr.ErrorCode
|
|
|
|
statusCode := azureErr.StatusCode
|
2021-04-19 13:30:42 -04:00
|
|
|
|
|
|
|
return azureCodesToObjectError(err, serviceCode, statusCode, bucket, object)
|
|
|
|
}
|
|
|
|
|
|
|
|
func azureCodesToObjectError(err error, serviceCode string, statusCode int, bucket string, object string) error {
|
|
|
|
switch serviceCode {
|
|
|
|
case "ContainerNotFound", "ContainerBeingDeleted":
|
|
|
|
err = BucketNotFound{Bucket: bucket}
|
|
|
|
case "ContainerAlreadyExists":
|
|
|
|
err = BucketExists{Bucket: bucket}
|
|
|
|
case "InvalidResourceName":
|
|
|
|
err = BucketNameInvalid{Bucket: bucket}
|
|
|
|
case "RequestBodyTooLarge":
|
|
|
|
err = PartTooBig{}
|
|
|
|
case "InvalidMetadata":
|
|
|
|
err = UnsupportedMetadata{}
|
|
|
|
case "BlobAccessTierNotSupportedForAccountType":
|
|
|
|
err = NotImplemented{}
|
|
|
|
case "OutOfRangeInput":
|
|
|
|
err = ObjectNameInvalid{
|
|
|
|
Bucket: bucket,
|
|
|
|
Object: object,
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
switch statusCode {
|
|
|
|
case http.StatusNotFound:
|
|
|
|
if object != "" {
|
|
|
|
err = ObjectNotFound{
|
|
|
|
Bucket: bucket,
|
|
|
|
Object: object,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = BucketNotFound{Bucket: bucket}
|
|
|
|
}
|
|
|
|
case http.StatusBadRequest:
|
|
|
|
err = BucketNameInvalid{Bucket: bucket}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|