2023-04-15 10:34:02 -04:00
|
|
|
// Copyright (c) 2015-2023 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 (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"crypto/subtle"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2024-04-19 08:23:42 -04:00
|
|
|
"path"
|
2023-04-15 10:34:02 -04:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-06-19 20:53:08 -04:00
|
|
|
"github.com/minio/madmin-go/v3"
|
2023-04-15 10:34:02 -04:00
|
|
|
"github.com/minio/minio-go/v7"
|
|
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
|
|
"github.com/minio/minio/internal/auth"
|
2024-01-28 13:04:17 -05:00
|
|
|
xioutil "github.com/minio/minio/internal/ioutil"
|
2024-05-24 19:05:23 -04:00
|
|
|
"github.com/minio/pkg/v3/mimedb"
|
2023-04-15 10:34:02 -04:00
|
|
|
ftp "goftp.io/server/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ ftp.Driver = &ftpDriver{}
|
|
|
|
|
|
|
|
// ftpDriver implements ftpDriver to store files in minio
|
|
|
|
type ftpDriver struct {
|
|
|
|
endpoint string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFTPDriver implements ftp.Driver interface
|
|
|
|
func NewFTPDriver() ftp.Driver {
|
|
|
|
return &ftpDriver{endpoint: fmt.Sprintf("127.0.0.1:%s", globalMinioPort)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildMinioPath(p string) string {
|
|
|
|
return strings.TrimPrefix(p, SlashSeparator)
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildMinioDir(p string) string {
|
|
|
|
v := buildMinioPath(p)
|
|
|
|
if !strings.HasSuffix(v, SlashSeparator) {
|
|
|
|
return v + SlashSeparator
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
type minioFileInfo struct {
|
|
|
|
p string
|
|
|
|
info minio.ObjectInfo
|
|
|
|
isDir bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *minioFileInfo) Name() string {
|
|
|
|
return m.p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *minioFileInfo) Size() int64 {
|
|
|
|
return m.info.Size
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *minioFileInfo) Mode() os.FileMode {
|
|
|
|
if m.isDir {
|
|
|
|
return os.ModeDir
|
|
|
|
}
|
|
|
|
return os.ModePerm
|
|
|
|
}
|
|
|
|
|
2024-04-16 20:35:55 -04:00
|
|
|
var minFileDate = time.Date(1980, 1, 1, 0, 0, 0, 0, time.UTC) // Workaround for Filezilla
|
|
|
|
|
2023-04-15 10:34:02 -04:00
|
|
|
func (m *minioFileInfo) ModTime() time.Time {
|
2024-04-16 20:35:55 -04:00
|
|
|
if !m.info.LastModified.IsZero() {
|
|
|
|
return m.info.LastModified
|
|
|
|
}
|
|
|
|
return minFileDate
|
2023-04-15 10:34:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *minioFileInfo) IsDir() bool {
|
|
|
|
return m.isDir
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *minioFileInfo) Sys() interface{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//msgp:ignore ftpMetrics
|
|
|
|
type ftpMetrics struct{}
|
|
|
|
|
|
|
|
var globalFtpMetrics ftpMetrics
|
|
|
|
|
2024-06-03 11:45:54 -04:00
|
|
|
func ftpTrace(s *ftp.Context, startTime time.Time, source, objPath string, err error, sz int64) madmin.TraceInfo {
|
2023-04-15 10:34:02 -04:00
|
|
|
var errStr string
|
|
|
|
if err != nil {
|
|
|
|
errStr = err.Error()
|
|
|
|
}
|
|
|
|
return madmin.TraceInfo{
|
|
|
|
TraceType: madmin.TraceFTP,
|
|
|
|
Time: startTime,
|
|
|
|
NodeName: globalLocalNodeName,
|
2024-08-14 13:11:51 -04:00
|
|
|
FuncName: s.Cmd,
|
2023-04-15 10:34:02 -04:00
|
|
|
Duration: time.Since(startTime),
|
2024-04-19 08:23:42 -04:00
|
|
|
Path: objPath,
|
2023-04-15 10:34:02 -04:00
|
|
|
Error: errStr,
|
2024-06-03 11:45:54 -04:00
|
|
|
Bytes: sz,
|
|
|
|
Custom: map[string]string{
|
|
|
|
"user": s.Sess.LoginUser(),
|
|
|
|
"cmd": s.Cmd,
|
|
|
|
"param": s.Param,
|
|
|
|
"login": fmt.Sprintf("%t", s.Sess.IsLogin()),
|
|
|
|
"source": source,
|
|
|
|
},
|
2023-04-15 10:34:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-03 11:45:54 -04:00
|
|
|
func (m *ftpMetrics) log(s *ftp.Context, paths ...string) func(sz int64, err error) {
|
2023-04-15 10:34:02 -04:00
|
|
|
startTime := time.Now()
|
|
|
|
source := getSource(2)
|
2024-06-03 11:45:54 -04:00
|
|
|
return func(sz int64, err error) {
|
|
|
|
globalTrace.Publish(ftpTrace(s, startTime, source, strings.Join(paths, " "), err, sz))
|
2023-04-15 10:34:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stat implements ftpDriver
|
2024-04-19 08:23:42 -04:00
|
|
|
func (driver *ftpDriver) Stat(ctx *ftp.Context, objPath string) (fi os.FileInfo, err error) {
|
|
|
|
stopFn := globalFtpMetrics.log(ctx, objPath)
|
2024-06-03 11:45:54 -04:00
|
|
|
defer stopFn(0, err)
|
2023-04-15 10:34:02 -04:00
|
|
|
|
2024-04-19 08:23:42 -04:00
|
|
|
if objPath == SlashSeparator {
|
2023-04-15 10:34:02 -04:00
|
|
|
return &minioFileInfo{
|
|
|
|
p: SlashSeparator,
|
|
|
|
isDir: true,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-04-19 08:23:42 -04:00
|
|
|
bucket, object := path2BucketObject(objPath)
|
2023-04-15 10:34:02 -04:00
|
|
|
if bucket == "" {
|
|
|
|
return nil, errors.New("bucket name cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
clnt, err := driver.getMinIOClient(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if object == "" {
|
|
|
|
ok, err := clnt.BucketExists(context.Background(), bucket)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return nil, os.ErrNotExist
|
|
|
|
}
|
|
|
|
return &minioFileInfo{
|
|
|
|
p: pathClean(bucket),
|
|
|
|
info: minio.ObjectInfo{Key: bucket},
|
|
|
|
isDir: true,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
objInfo, err := clnt.StatObject(context.Background(), bucket, object, minio.StatObjectOptions{})
|
|
|
|
if err != nil {
|
|
|
|
if minio.ToErrorResponse(err).Code == "NoSuchKey" {
|
|
|
|
// dummy return to satisfy LIST (stat -> list) behavior.
|
|
|
|
return &minioFileInfo{
|
|
|
|
p: pathClean(object),
|
|
|
|
info: minio.ObjectInfo{Key: object},
|
|
|
|
isDir: true,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
isDir := strings.HasSuffix(objInfo.Key, SlashSeparator)
|
|
|
|
return &minioFileInfo{
|
|
|
|
p: pathClean(object),
|
|
|
|
info: objInfo,
|
|
|
|
isDir: isDir,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListDir implements ftpDriver
|
2024-04-19 08:23:42 -04:00
|
|
|
func (driver *ftpDriver) ListDir(ctx *ftp.Context, objPath string, callback func(os.FileInfo) error) (err error) {
|
|
|
|
stopFn := globalFtpMetrics.log(ctx, objPath)
|
2024-06-03 11:45:54 -04:00
|
|
|
defer stopFn(0, err)
|
2023-04-15 10:34:02 -04:00
|
|
|
|
|
|
|
clnt, err := driver.getMinIOClient(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2024-04-19 08:23:42 -04:00
|
|
|
bucket, prefix := path2BucketObject(objPath)
|
2023-04-15 10:34:02 -04:00
|
|
|
if bucket == "" {
|
|
|
|
buckets, err := clnt.ListBuckets(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, bucket := range buckets {
|
|
|
|
info := minioFileInfo{
|
|
|
|
p: pathClean(bucket.Name),
|
|
|
|
info: minio.ObjectInfo{Key: retainSlash(bucket.Name), LastModified: bucket.CreationDate},
|
|
|
|
isDir: true,
|
|
|
|
}
|
|
|
|
if err := callback(&info); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix = retainSlash(prefix)
|
|
|
|
|
|
|
|
for object := range clnt.ListObjects(cctx, bucket, minio.ListObjectsOptions{
|
|
|
|
Prefix: prefix,
|
|
|
|
Recursive: false,
|
|
|
|
}) {
|
|
|
|
if object.Err != nil {
|
|
|
|
return object.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
if object.Key == prefix {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
isDir := strings.HasSuffix(object.Key, SlashSeparator)
|
|
|
|
info := minioFileInfo{
|
|
|
|
p: pathClean(strings.TrimPrefix(object.Key, prefix)),
|
|
|
|
info: object,
|
|
|
|
isDir: isDir,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := callback(&info); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (driver *ftpDriver) CheckPasswd(c *ftp.Context, username, password string) (ok bool, err error) {
|
|
|
|
stopFn := globalFtpMetrics.log(c, username)
|
2024-06-03 11:45:54 -04:00
|
|
|
defer stopFn(0, err)
|
2023-04-15 10:34:02 -04:00
|
|
|
|
|
|
|
if globalIAMSys.LDAPConfig.Enabled() {
|
2023-12-06 07:31:35 -05:00
|
|
|
sa, _, err := globalIAMSys.getServiceAccount(context.Background(), username)
|
|
|
|
if err != nil && !errors.Is(err, errNoSuchServiceAccount) {
|
2023-04-15 10:34:02 -04:00
|
|
|
return false, err
|
|
|
|
}
|
2023-12-06 07:31:35 -05:00
|
|
|
if errors.Is(err, errNoSuchServiceAccount) {
|
2024-05-24 19:05:23 -04:00
|
|
|
lookupRes, groupDistNames, err := globalIAMSys.LDAPConfig.Bind(username, password)
|
2023-12-06 07:31:35 -05:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2024-05-24 19:05:23 -04:00
|
|
|
ldapPolicies, _ := globalIAMSys.PolicyDBGet(lookupRes.NormDN, groupDistNames...)
|
2023-12-06 07:31:35 -05:00
|
|
|
return len(ldapPolicies) > 0, nil
|
|
|
|
}
|
|
|
|
return subtle.ConstantTimeCompare([]byte(sa.Credentials.SecretKey), []byte(password)) == 1, nil
|
2023-04-15 10:34:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ui, ok := globalIAMSys.GetUser(context.Background(), username)
|
|
|
|
if !ok {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return subtle.ConstantTimeCompare([]byte(ui.Credentials.SecretKey), []byte(password)) == 1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (driver *ftpDriver) getMinIOClient(ctx *ftp.Context) (*minio.Client, error) {
|
|
|
|
ui, ok := globalIAMSys.GetUser(context.Background(), ctx.Sess.LoginUser())
|
|
|
|
if !ok && !globalIAMSys.LDAPConfig.Enabled() {
|
|
|
|
return nil, errNoSuchUser
|
|
|
|
}
|
|
|
|
if !ok && globalIAMSys.LDAPConfig.Enabled() {
|
2023-12-06 07:31:35 -05:00
|
|
|
sa, _, err := globalIAMSys.getServiceAccount(context.Background(), ctx.Sess.LoginUser())
|
|
|
|
if err != nil && !errors.Is(err, errNoSuchServiceAccount) {
|
2023-04-15 10:34:02 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-12-06 07:31:35 -05:00
|
|
|
var mcreds *credentials.Credentials
|
|
|
|
if errors.Is(err, errNoSuchServiceAccount) {
|
2024-05-24 19:05:23 -04:00
|
|
|
lookupResult, targetGroups, err := globalIAMSys.LDAPConfig.LookupUserDN(ctx.Sess.LoginUser())
|
2023-12-06 07:31:35 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-05-24 19:05:23 -04:00
|
|
|
ldapPolicies, _ := globalIAMSys.PolicyDBGet(lookupResult.NormDN, targetGroups...)
|
2023-12-06 07:31:35 -05:00
|
|
|
if len(ldapPolicies) == 0 {
|
|
|
|
return nil, errAuthentication
|
|
|
|
}
|
|
|
|
expiryDur, err := globalIAMSys.LDAPConfig.GetExpiryDuration("")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
claims := make(map[string]interface{})
|
|
|
|
claims[expClaim] = UTCNow().Add(expiryDur).Unix()
|
2024-05-24 19:05:23 -04:00
|
|
|
|
|
|
|
claims[ldapUser] = lookupResult.NormDN
|
2024-05-25 09:43:06 -04:00
|
|
|
claims[ldapActualUser] = lookupResult.ActualDN
|
2023-12-06 07:31:35 -05:00
|
|
|
claims[ldapUserN] = ctx.Sess.LoginUser()
|
|
|
|
|
2024-05-24 19:05:23 -04:00
|
|
|
// Add LDAP attributes that were looked up into the claims.
|
|
|
|
for attribKey, attribValue := range lookupResult.Attributes {
|
|
|
|
claims[ldapAttribPrefix+attribKey] = attribValue
|
|
|
|
}
|
|
|
|
|
2023-12-06 07:31:35 -05:00
|
|
|
cred, err := auth.GetNewCredentialsWithMetadata(claims, globalActiveCred.SecretKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-15 10:34:02 -04:00
|
|
|
|
2023-12-06 07:31:35 -05:00
|
|
|
// Set the parent of the temporary access key, this is useful
|
|
|
|
// in obtaining service accounts by this cred.
|
2024-05-24 19:05:23 -04:00
|
|
|
cred.ParentUser = lookupResult.NormDN
|
2023-04-15 10:34:02 -04:00
|
|
|
|
2023-12-06 07:31:35 -05:00
|
|
|
// Set this value to LDAP groups, LDAP user can be part
|
|
|
|
// of large number of groups
|
|
|
|
cred.Groups = targetGroups
|
2023-04-15 10:34:02 -04:00
|
|
|
|
2023-12-06 07:31:35 -05:00
|
|
|
// Set the newly generated credentials, policyName is empty on purpose
|
|
|
|
// LDAP policies are applied automatically using their ldapUser, ldapGroups
|
|
|
|
// mapping.
|
|
|
|
updatedAt, err := globalIAMSys.SetTempUser(context.Background(), cred.AccessKey, cred, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-15 10:34:02 -04:00
|
|
|
|
2023-12-06 07:31:35 -05:00
|
|
|
// Call hook for site replication.
|
2024-04-04 08:04:40 -04:00
|
|
|
replLogIf(context.Background(), globalSiteReplicationSys.IAMChangeHook(context.Background(), madmin.SRIAMItem{
|
2023-12-06 07:31:35 -05:00
|
|
|
Type: madmin.SRIAMItemSTSAcc,
|
|
|
|
STSCredential: &madmin.SRSTSCredential{
|
|
|
|
AccessKey: cred.AccessKey,
|
|
|
|
SecretKey: cred.SecretKey,
|
|
|
|
SessionToken: cred.SessionToken,
|
|
|
|
ParentUser: cred.ParentUser,
|
|
|
|
},
|
|
|
|
UpdatedAt: updatedAt,
|
|
|
|
}))
|
|
|
|
|
|
|
|
mcreds = credentials.NewStaticV4(cred.AccessKey, cred.SecretKey, cred.SessionToken)
|
|
|
|
} else {
|
|
|
|
mcreds = credentials.NewStaticV4(sa.Credentials.AccessKey, sa.Credentials.SecretKey, "")
|
|
|
|
}
|
2023-04-15 10:34:02 -04:00
|
|
|
|
|
|
|
return minio.New(driver.endpoint, &minio.Options{
|
2023-12-06 07:31:35 -05:00
|
|
|
Creds: mcreds,
|
2023-04-15 10:34:02 -04:00
|
|
|
Secure: globalIsTLS,
|
2023-06-30 11:04:13 -04:00
|
|
|
Transport: globalRemoteFTPClientTransport,
|
2023-04-15 10:34:02 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ok == true - at this point
|
|
|
|
|
|
|
|
if ui.Credentials.IsTemp() {
|
|
|
|
// Temporary credentials are not allowed.
|
|
|
|
return nil, errAuthentication
|
|
|
|
}
|
|
|
|
|
|
|
|
return minio.New(driver.endpoint, &minio.Options{
|
|
|
|
Creds: credentials.NewStaticV4(ui.Credentials.AccessKey, ui.Credentials.SecretKey, ""),
|
|
|
|
Secure: globalIsTLS,
|
2023-06-30 11:04:13 -04:00
|
|
|
Transport: globalRemoteFTPClientTransport,
|
2023-04-15 10:34:02 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteDir implements ftpDriver
|
2024-04-19 08:23:42 -04:00
|
|
|
func (driver *ftpDriver) DeleteDir(ctx *ftp.Context, objPath string) (err error) {
|
|
|
|
stopFn := globalFtpMetrics.log(ctx, objPath)
|
2024-06-03 11:45:54 -04:00
|
|
|
defer stopFn(0, err)
|
2023-04-15 10:34:02 -04:00
|
|
|
|
2024-04-19 08:23:42 -04:00
|
|
|
bucket, prefix := path2BucketObject(objPath)
|
2023-04-15 10:34:02 -04:00
|
|
|
if bucket == "" {
|
|
|
|
return errors.New("deleting all buckets not allowed")
|
|
|
|
}
|
|
|
|
|
|
|
|
clnt, err := driver.getMinIOClient(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2023-06-28 16:15:21 -04:00
|
|
|
if prefix == "" {
|
|
|
|
// if all objects are not deleted yet this call may fail.
|
|
|
|
return clnt.RemoveBucket(cctx, bucket)
|
|
|
|
}
|
|
|
|
|
2023-04-15 10:34:02 -04:00
|
|
|
objectsCh := make(chan minio.ObjectInfo)
|
|
|
|
|
|
|
|
// Send object names that are needed to be removed to objectsCh
|
|
|
|
go func() {
|
2024-01-28 13:04:17 -05:00
|
|
|
defer xioutil.SafeClose(objectsCh)
|
2023-06-28 16:15:21 -04:00
|
|
|
opts := minio.ListObjectsOptions{
|
|
|
|
Prefix: prefix,
|
|
|
|
Recursive: true,
|
|
|
|
}
|
2023-04-15 10:34:02 -04:00
|
|
|
for object := range clnt.ListObjects(cctx, bucket, opts) {
|
|
|
|
if object.Err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
objectsCh <- object
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Call RemoveObjects API
|
|
|
|
for err := range clnt.RemoveObjects(context.Background(), bucket, objectsCh, minio.RemoveObjectsOptions{}) {
|
|
|
|
if err.Err != nil {
|
|
|
|
return err.Err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteFile implements ftpDriver
|
2024-04-19 08:23:42 -04:00
|
|
|
func (driver *ftpDriver) DeleteFile(ctx *ftp.Context, objPath string) (err error) {
|
|
|
|
stopFn := globalFtpMetrics.log(ctx, objPath)
|
2024-06-03 11:45:54 -04:00
|
|
|
defer stopFn(0, err)
|
2023-04-15 10:34:02 -04:00
|
|
|
|
2024-04-19 08:23:42 -04:00
|
|
|
bucket, object := path2BucketObject(objPath)
|
2023-04-15 10:34:02 -04:00
|
|
|
if bucket == "" {
|
|
|
|
return errors.New("bucket name cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
clnt, err := driver.getMinIOClient(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return clnt.RemoveObject(context.Background(), bucket, object, minio.RemoveObjectOptions{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rename implements ftpDriver
|
2024-04-19 08:23:42 -04:00
|
|
|
func (driver *ftpDriver) Rename(ctx *ftp.Context, fromObjPath string, toObjPath string) (err error) {
|
|
|
|
stopFn := globalFtpMetrics.log(ctx, fromObjPath, toObjPath)
|
2024-06-03 11:45:54 -04:00
|
|
|
defer stopFn(0, err)
|
2023-04-15 10:34:02 -04:00
|
|
|
|
|
|
|
return NotImplemented{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MakeDir implements ftpDriver
|
2024-04-19 08:23:42 -04:00
|
|
|
func (driver *ftpDriver) MakeDir(ctx *ftp.Context, objPath string) (err error) {
|
|
|
|
stopFn := globalFtpMetrics.log(ctx, objPath)
|
2024-06-03 11:45:54 -04:00
|
|
|
defer stopFn(0, err)
|
2023-04-15 10:34:02 -04:00
|
|
|
|
2024-04-19 08:23:42 -04:00
|
|
|
bucket, prefix := path2BucketObject(objPath)
|
2023-04-15 10:34:02 -04:00
|
|
|
if bucket == "" {
|
|
|
|
return errors.New("bucket name cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
clnt, err := driver.getMinIOClient(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-28 16:15:21 -04:00
|
|
|
if prefix == "" {
|
2024-05-16 19:13:47 -04:00
|
|
|
return clnt.MakeBucket(context.Background(), bucket, minio.MakeBucketOptions{Region: globalSite.Region()})
|
2023-06-28 16:15:21 -04:00
|
|
|
}
|
|
|
|
|
2023-04-15 10:34:02 -04:00
|
|
|
dirPath := buildMinioDir(prefix)
|
|
|
|
|
2024-04-19 08:23:42 -04:00
|
|
|
_, err = clnt.PutObject(context.Background(), bucket, dirPath, bytes.NewReader([]byte("")), 0, minio.PutObjectOptions{
|
|
|
|
DisableContentSha256: true,
|
|
|
|
})
|
2023-04-15 10:34:02 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFile implements ftpDriver
|
2024-04-19 08:23:42 -04:00
|
|
|
func (driver *ftpDriver) GetFile(ctx *ftp.Context, objPath string, offset int64) (n int64, rc io.ReadCloser, err error) {
|
|
|
|
stopFn := globalFtpMetrics.log(ctx, objPath)
|
2024-06-03 11:45:54 -04:00
|
|
|
defer stopFn(n, err)
|
2023-04-15 10:34:02 -04:00
|
|
|
|
2024-04-19 08:23:42 -04:00
|
|
|
bucket, object := path2BucketObject(objPath)
|
2023-04-15 10:34:02 -04:00
|
|
|
if bucket == "" {
|
|
|
|
return 0, nil, errors.New("bucket name cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
clnt, err := driver.getMinIOClient(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := minio.GetObjectOptions{}
|
|
|
|
obj, err := clnt.GetObject(context.Background(), bucket, object, opts)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil && obj != nil {
|
|
|
|
obj.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
_, err = obj.Seek(offset, io.SeekStart)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := obj.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
2024-06-03 11:45:54 -04:00
|
|
|
n = info.Size - offset
|
|
|
|
return n, obj, nil
|
2023-04-15 10:34:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutFile implements ftpDriver
|
2024-04-19 08:23:42 -04:00
|
|
|
func (driver *ftpDriver) PutFile(ctx *ftp.Context, objPath string, data io.Reader, offset int64) (n int64, err error) {
|
|
|
|
stopFn := globalFtpMetrics.log(ctx, objPath)
|
2024-06-03 11:45:54 -04:00
|
|
|
defer stopFn(n, err)
|
2023-04-15 10:34:02 -04:00
|
|
|
|
2024-04-19 08:23:42 -04:00
|
|
|
bucket, object := path2BucketObject(objPath)
|
2023-04-15 10:34:02 -04:00
|
|
|
if bucket == "" {
|
|
|
|
return 0, errors.New("bucket name cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if offset != -1 {
|
|
|
|
// FTP - APPEND not implemented
|
|
|
|
return 0, NotImplemented{}
|
|
|
|
}
|
|
|
|
|
|
|
|
clnt, err := driver.getMinIOClient(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := clnt.PutObject(context.Background(), bucket, object, data, -1, minio.PutObjectOptions{
|
2024-04-19 08:23:42 -04:00
|
|
|
ContentType: mimedb.TypeByExtension(path.Ext(object)),
|
|
|
|
DisableContentSha256: true,
|
2023-04-15 10:34:02 -04:00
|
|
|
})
|
2024-06-03 11:45:54 -04:00
|
|
|
n = info.Size
|
|
|
|
return n, err
|
2023-04-15 10:34:02 -04:00
|
|
|
}
|