Vacuum Swagger (#3533)

This commit is contained in:
Daniel Valdivia
2025-05-09 16:41:27 -07:00
committed by GitHub
parent ee974a5961
commit e2bbf91e8a
138 changed files with 69 additions and 21974 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -31,7 +31,6 @@ import (
"github.com/minio/mc/cmd"
"github.com/minio/mc/pkg/probe"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/minio/minio-go/v7/pkg/sse"
"github.com/minio/minio-go/v7/pkg/tags"
"github.com/go-openapi/runtime/middleware"
@@ -480,66 +479,11 @@ func consoleAccess2policyAccess(bucketAccess models.BucketAccess) (bucketPolicy
return bucketPolicy
}
// enableBucketEncryption will enable bucket encryption based on two encryption algorithms, sse-s3 (server side encryption with external KMS) or sse-kms (aws s3 kms key)
func enableBucketEncryption(ctx context.Context, client MinioClient, bucketName string, encryptionType models.BucketEncryptionType, kmsKeyID string) error {
var config *sse.Configuration
switch encryptionType {
case models.BucketEncryptionTypeSseDashKms:
config = sse.NewConfigurationSSEKMS(kmsKeyID)
case models.BucketEncryptionTypeSseDashS3:
config = sse.NewConfigurationSSES3()
default:
return ErrInvalidEncryptionAlgorithm
}
return client.setBucketEncryption(ctx, bucketName, config)
}
// disableBucketEncryption will disable bucket for the provided bucket name
func disableBucketEncryption(ctx context.Context, client MinioClient, bucketName string) error {
return client.removeBucketEncryption(ctx, bucketName)
}
func getBucketEncryptionInfo(ctx context.Context, client MinioClient, bucketName string) (*models.BucketEncryptionInfo, error) {
bucketInfo, err := client.getBucketEncryption(ctx, bucketName)
if err != nil {
return nil, err
}
if len(bucketInfo.Rules) == 0 {
return nil, ErrDefault
}
return &models.BucketEncryptionInfo{Algorithm: bucketInfo.Rules[0].Apply.SSEAlgorithm, KmsMasterKeyID: bucketInfo.Rules[0].Apply.KmsMasterKeyID}, nil
}
// setBucketRetentionConfig sets object lock configuration on a bucket
func setBucketRetentionConfig(ctx context.Context, client MinioClient, bucketName string, mode models.ObjectRetentionMode, unit models.ObjectRetentionUnit, validity *int32) error {
if validity == nil {
return errors.New("retention validity can't be nil")
}
var retentionMode minio.RetentionMode
switch mode {
case models.ObjectRetentionModeGovernance:
retentionMode = minio.Governance
case models.ObjectRetentionModeCompliance:
retentionMode = minio.Compliance
default:
return errors.New("invalid retention mode")
}
var retentionUnit minio.ValidityUnit
switch unit {
case models.ObjectRetentionUnitDays:
retentionUnit = minio.Days
case models.ObjectRetentionUnitYears:
retentionUnit = minio.Years
default:
return errors.New("invalid retention unit")
}
retentionValidity := uint(*validity)
return client.setObjectLockConfig(ctx, bucketName, &retentionMode, &retentionValidity, &retentionUnit)
}
func getBucketRetentionConfig(ctx context.Context, client MinioClient, bucketName string) (*models.GetBucketRetentionConfig, error) {
m, v, u, err := client.getBucketObjectLockConfig(ctx, bucketName)
if err != nil {

View File

@@ -362,56 +362,6 @@ func TestSetBucketAccess(t *testing.T) {
}
}
func Test_enableBucketEncryption(t *testing.T) {
ctx := context.Background()
type args struct {
ctx context.Context
bucketName string
encryptionType models.BucketEncryptionType
kmsKeyID string
mockEnableBucketEncryptionFunc func(ctx context.Context, bucketName string, config *sse.Configuration) error
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Bucket encryption enabled correctly",
args: args{
ctx: ctx,
bucketName: "test",
encryptionType: "sse-s3",
mockEnableBucketEncryptionFunc: func(_ context.Context, _ string, _ *sse.Configuration) error {
return nil
},
},
wantErr: false,
},
{
name: "Error when enabling bucket encryption",
args: args{
ctx: ctx,
bucketName: "test",
encryptionType: "sse-s3",
mockEnableBucketEncryptionFunc: func(_ context.Context, _ string, _ *sse.Configuration) error {
return ErrInvalidSession
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(_ *testing.T) {
minClient := minioClientMock{}
minClient.setBucketEncryptionMock = tt.args.mockEnableBucketEncryptionFunc
if err := enableBucketEncryption(tt.args.ctx, minClient, tt.args.bucketName, tt.args.encryptionType, tt.args.kmsKeyID); (err != nil) != tt.wantErr {
t.Errorf("enableBucketEncryption() errors = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func Test_disableBucketEncryption(t *testing.T) {
ctx := context.Background()
type args struct {
@@ -458,197 +408,6 @@ func Test_disableBucketEncryption(t *testing.T) {
}
}
func Test_getBucketEncryptionInfo(t *testing.T) {
ctx := context.Background()
type args struct {
ctx context.Context
bucketName string
mockBucketEncryptionGet func(ctx context.Context, bucketName string) (*sse.Configuration, error)
}
tests := []struct {
name string
args args
want *models.BucketEncryptionInfo
wantErr bool
}{
{
name: "Bucket encryption info returned correctly",
args: args{
ctx: ctx,
bucketName: "test",
mockBucketEncryptionGet: func(_ context.Context, _ string) (*sse.Configuration, error) {
return &sse.Configuration{
Rules: []sse.Rule{
{
Apply: sse.ApplySSEByDefault{SSEAlgorithm: "AES256", KmsMasterKeyID: ""},
},
},
}, nil
},
},
wantErr: false,
want: &models.BucketEncryptionInfo{
Algorithm: "AES256",
KmsMasterKeyID: "",
},
},
{
name: "Bucket encryption info with no rules",
args: args{
ctx: ctx,
bucketName: "test",
mockBucketEncryptionGet: func(_ context.Context, _ string) (*sse.Configuration, error) {
return &sse.Configuration{
Rules: []sse.Rule{},
}, nil
},
},
wantErr: true,
},
{
name: "Error when obtaining bucket encryption info",
args: args{
ctx: ctx,
bucketName: "test",
mockBucketEncryptionGet: func(_ context.Context, _ string) (*sse.Configuration, error) {
return nil, ErrSSENotConfigured
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(_ *testing.T) {
minClient := minioClientMock{}
minClient.getBucketEncryptionMock = tt.args.mockBucketEncryptionGet
got, err := getBucketEncryptionInfo(tt.args.ctx, minClient, tt.args.bucketName)
if (err != nil) != tt.wantErr {
t.Errorf("getBucketEncryptionInfo() errors = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("getBucketEncryptionInfo() got = %v, want %v", got, tt.want)
}
})
}
}
func Test_SetBucketRetentionConfig(t *testing.T) {
assert := assert.New(t)
ctx := context.Background()
type args struct {
ctx context.Context
bucketName string
mode models.ObjectRetentionMode
unit models.ObjectRetentionUnit
validity *int32
mockBucketRetentionFunc func(ctx context.Context, bucketName string, mode *minio.RetentionMode, validity *uint, unit *minio.ValidityUnit) error
}
tests := []struct {
name string
args args
expectedError error
}{
{
name: "Set Bucket Retention Config",
args: args{
ctx: ctx,
bucketName: "test",
mode: models.ObjectRetentionModeCompliance,
unit: models.ObjectRetentionUnitDays,
validity: swag.Int32(2),
mockBucketRetentionFunc: func(_ context.Context, _ string, _ *minio.RetentionMode, _ *uint, _ *minio.ValidityUnit) error {
return nil
},
},
expectedError: nil,
},
{
name: "Set Bucket Retention Config 2",
args: args{
ctx: ctx,
bucketName: "test",
mode: models.ObjectRetentionModeGovernance,
unit: models.ObjectRetentionUnitYears,
validity: swag.Int32(2),
mockBucketRetentionFunc: func(_ context.Context, _ string, _ *minio.RetentionMode, _ *uint, _ *minio.ValidityUnit) error {
return nil
},
},
expectedError: nil,
},
{
name: "Invalid validity",
args: args{
ctx: ctx,
bucketName: "test",
mode: models.ObjectRetentionModeCompliance,
unit: models.ObjectRetentionUnitDays,
validity: nil,
mockBucketRetentionFunc: func(_ context.Context, _ string, _ *minio.RetentionMode, _ *uint, _ *minio.ValidityUnit) error {
return nil
},
},
expectedError: errors.New("retention validity can't be nil"),
},
{
name: "Invalid retention mode",
args: args{
ctx: ctx,
bucketName: "test",
mode: models.ObjectRetentionMode("othermode"),
unit: models.ObjectRetentionUnitDays,
validity: swag.Int32(2),
mockBucketRetentionFunc: func(_ context.Context, _ string, _ *minio.RetentionMode, _ *uint, _ *minio.ValidityUnit) error {
return nil
},
},
expectedError: errors.New("invalid retention mode"),
},
{
name: "Invalid retention unit",
args: args{
ctx: ctx,
bucketName: "test",
mode: models.ObjectRetentionModeCompliance,
unit: models.ObjectRetentionUnit("otherunit"),
validity: swag.Int32(2),
mockBucketRetentionFunc: func(_ context.Context, _ string, _ *minio.RetentionMode, _ *uint, _ *minio.ValidityUnit) error {
return nil
},
},
expectedError: errors.New("invalid retention unit"),
},
{
name: "Handle errors on objec lock function",
args: args{
ctx: ctx,
bucketName: "test",
mode: models.ObjectRetentionModeCompliance,
unit: models.ObjectRetentionUnitDays,
validity: swag.Int32(2),
mockBucketRetentionFunc: func(_ context.Context, _ string, _ *minio.RetentionMode, _ *uint, _ *minio.ValidityUnit) error {
return errors.New("error func")
},
},
expectedError: errors.New("error func"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(_ *testing.T) {
minClient := minioClientMock{}
minClient.setObjectLockConfigMock = tt.args.mockBucketRetentionFunc
err := setBucketRetentionConfig(tt.args.ctx, minClient, tt.args.bucketName, tt.args.mode, tt.args.unit, tt.args.validity)
if tt.expectedError != nil {
fmt.Println(t.Name())
assert.Equal(tt.expectedError.Error(), err.Error(), fmt.Sprintf("setObjectRetention() errors: `%s`, wantErr: `%s`", err, tt.expectedError))
} else {
assert.Nil(err, fmt.Sprintf("setBucketRetentionConfig() errors: %v, wantErr: %v", err, tt.expectedError))
}
})
}
}
func Test_GetBucketRetentionConfig(t *testing.T) {
assert := assert.New(t)
ctx := context.Background()

View File

@@ -1032,43 +1032,6 @@ func getRequestURLWithScheme(r *http.Request) string {
return fmt.Sprintf("%s://%s", scheme, r.Host)
}
func setObjectLegalHold(ctx context.Context, client MinioClient, bucketName, prefix, versionID string, status models.ObjectLegalHoldStatus) error {
var lstatus minio.LegalHoldStatus
if status == models.ObjectLegalHoldStatusEnabled {
lstatus = minio.LegalHoldEnabled
} else {
lstatus = minio.LegalHoldDisabled
}
return client.putObjectLegalHold(ctx, bucketName, prefix, minio.PutObjectLegalHoldOptions{VersionID: versionID, Status: &lstatus})
}
func setObjectRetention(ctx context.Context, client MinioClient, bucketName, versionID, prefix string, retentionOps *models.PutObjectRetentionRequest) error {
if retentionOps == nil {
return errors.New("object retention options can't be nil")
}
if retentionOps.Expires == nil {
return errors.New("object retention expires can't be nil")
}
var mode minio.RetentionMode
if *retentionOps.Mode == models.ObjectRetentionModeGovernance {
mode = minio.Governance
} else {
mode = minio.Compliance
}
retentionUntilDate, err := time.Parse(time.RFC3339, *retentionOps.Expires)
if err != nil {
return err
}
opts := minio.PutObjectRetentionOptions{
GovernanceBypass: retentionOps.GovernanceBypass,
RetainUntilDate: &retentionUntilDate,
Mode: &mode,
VersionID: versionID,
}
return client.putObjectRetention(ctx, bucketName, prefix, opts)
}
func deleteObjectRetention(ctx context.Context, client MinioClient, bucketName, prefix, versionID string) error {
opts := minio.PutObjectRetentionOptions{
GovernanceBypass: true,

View File

@@ -1083,206 +1083,6 @@ func Test_shareObject(t *testing.T) {
}
}
func Test_putObjectLegalHold(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
client := minioClientMock{}
type args struct {
bucket string
prefix string
versionID string
status models.ObjectLegalHoldStatus
legalHoldFunc func(ctx context.Context, bucketName, objectName string, opts minio.PutObjectLegalHoldOptions) error
}
tests := []struct {
test string
args args
wantError error
}{
{
test: "Put Object Legal hold enabled status",
args: args{
bucket: "buck1",
versionID: "someversion",
prefix: "folder/file.txt",
status: models.ObjectLegalHoldStatusEnabled,
legalHoldFunc: func(_ context.Context, _, _ string, _ minio.PutObjectLegalHoldOptions) error {
return nil
},
},
wantError: nil,
},
{
test: "Put Object Legal hold disabled status",
args: args{
bucket: "buck1",
versionID: "someversion",
prefix: "folder/file.txt",
status: models.ObjectLegalHoldStatusDisabled,
legalHoldFunc: func(_ context.Context, _, _ string, _ minio.PutObjectLegalHoldOptions) error {
return nil
},
},
wantError: nil,
},
{
test: "Handle error on legalhold func",
args: args{
bucket: "buck1",
versionID: "someversion",
prefix: "folder/file.txt",
status: models.ObjectLegalHoldStatusDisabled,
legalHoldFunc: func(_ context.Context, _, _ string, _ minio.PutObjectLegalHoldOptions) error {
return errors.New("new error")
},
},
wantError: errors.New("new error"),
},
}
for _, tt := range tests {
t.Run(tt.test, func(_ *testing.T) {
minioPutObjectLegalHoldMock = tt.args.legalHoldFunc
err := setObjectLegalHold(ctx, client, tt.args.bucket, tt.args.prefix, tt.args.versionID, tt.args.status)
if !reflect.DeepEqual(err, tt.wantError) {
t.Errorf("setObjectLegalHold() error: %v, wantErr: %v", err, tt.wantError)
return
}
})
}
}
func Test_putObjectRetention(t *testing.T) {
tAssert := assert.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
client := minioClientMock{}
type args struct {
bucket string
prefix string
versionID string
opts *models.PutObjectRetentionRequest
retentionFunc func(ctx context.Context, bucketName, objectName string, opts minio.PutObjectRetentionOptions) error
}
tests := []struct {
test string
args args
wantError error
}{
{
test: "Put Object retention governance",
args: args{
bucket: "buck1",
versionID: "someversion",
prefix: "folder/file.txt",
opts: &models.PutObjectRetentionRequest{
Expires: swag.String("2006-01-02T15:04:05Z"),
GovernanceBypass: false,
Mode: models.NewObjectRetentionMode(models.ObjectRetentionModeGovernance),
},
retentionFunc: func(_ context.Context, _, _ string, _ minio.PutObjectRetentionOptions) error {
return nil
},
},
wantError: nil,
},
{
test: "Put Object retention compliance",
args: args{
bucket: "buck1",
versionID: "someversion",
prefix: "folder/file.txt",
opts: &models.PutObjectRetentionRequest{
Expires: swag.String("2006-01-02T15:04:05Z"),
GovernanceBypass: false,
Mode: models.NewObjectRetentionMode(models.ObjectRetentionModeCompliance),
},
retentionFunc: func(_ context.Context, _, _ string, _ minio.PutObjectRetentionOptions) error {
return nil
},
},
wantError: nil,
},
{
test: "Empty opts should return error",
args: args{
bucket: "buck1",
versionID: "someversion",
prefix: "folder/file.txt",
opts: nil,
retentionFunc: func(_ context.Context, _, _ string, _ minio.PutObjectRetentionOptions) error {
return nil
},
},
wantError: errors.New("object retention options can't be nil"),
},
{
test: "Empty expire on opts should return error",
args: args{
bucket: "buck1",
versionID: "someversion",
prefix: "folder/file.txt",
opts: &models.PutObjectRetentionRequest{
Expires: nil,
GovernanceBypass: false,
Mode: models.NewObjectRetentionMode(models.ObjectRetentionModeCompliance),
},
retentionFunc: func(_ context.Context, _, _ string, _ minio.PutObjectRetentionOptions) error {
return nil
},
},
wantError: errors.New("object retention expires can't be nil"),
},
{
test: "Handle invalid expire time",
args: args{
bucket: "buck1",
versionID: "someversion",
prefix: "folder/file.txt",
opts: &models.PutObjectRetentionRequest{
Expires: swag.String("invalidtime"),
GovernanceBypass: false,
Mode: models.NewObjectRetentionMode(models.ObjectRetentionModeCompliance),
},
retentionFunc: func(_ context.Context, _, _ string, _ minio.PutObjectRetentionOptions) error {
return nil
},
},
wantError: errors.New("parsing time \"invalidtime\" as \"2006-01-02T15:04:05Z07:00\": cannot parse \"invalidtime\" as \"2006\""),
},
{
test: "Handle error on retention func",
args: args{
bucket: "buck1",
versionID: "someversion",
prefix: "folder/file.txt",
opts: &models.PutObjectRetentionRequest{
Expires: swag.String("2006-01-02T15:04:05Z"),
GovernanceBypass: false,
Mode: models.NewObjectRetentionMode(models.ObjectRetentionModeCompliance),
},
retentionFunc: func(_ context.Context, _, _ string, _ minio.PutObjectRetentionOptions) error {
return errors.New("new Error")
},
},
wantError: errors.New("new Error"),
},
}
for _, tt := range tests {
t.Run(tt.test, func(_ *testing.T) {
minioPutObjectRetentionMock = tt.args.retentionFunc
err := setObjectRetention(ctx, client, tt.args.bucket, tt.args.prefix, tt.args.versionID, tt.args.opts)
if tt.wantError != nil {
fmt.Println(t.Name())
tAssert.Equal(tt.wantError.Error(), err.Error(), fmt.Sprintf("setObjectRetention() error: `%s`, wantErr: `%s`", err, tt.wantError))
} else {
tAssert.Nil(err, fmt.Sprintf("setObjectRetention() error: %v, wantErr: %v", err, tt.wantError))
}
})
}
}
func Test_deleteObjectRetention(t *testing.T) {
tAssert := assert.New(t)
ctx, cancel := context.WithCancel(context.Background())