From 44b9979f02c32958b5596fe28cc549c1faaffd72 Mon Sep 17 00:00:00 2001 From: Cesar N Date: Thu, 3 Dec 2020 13:45:45 -0600 Subject: [PATCH] Add api to set object lock config on a bucket (#463) --- models/object_retention_unit.go | 80 +++++++++++ models/put_bucket_retention_request.go | 121 +++++++++++++++++ restapi/client.go | 5 + restapi/embedded_spec.go | 126 +++++++++++++++++ restapi/operations/console_api.go | 12 ++ .../user_api/set_bucket_retention_config.go | 90 ++++++++++++ .../set_bucket_retention_config_parameters.go | 120 ++++++++++++++++ .../set_bucket_retention_config_responses.go | 113 ++++++++++++++++ .../set_bucket_retention_config_urlbuilder.go | 116 ++++++++++++++++ restapi/user_buckets.go | 58 ++++++++ restapi/user_buckets_test.go | 128 ++++++++++++++++++ restapi/user_objects.go | 5 +- swagger.yml | 45 ++++++ 13 files changed, 1017 insertions(+), 2 deletions(-) create mode 100644 models/object_retention_unit.go create mode 100644 models/put_bucket_retention_request.go create mode 100644 restapi/operations/user_api/set_bucket_retention_config.go create mode 100644 restapi/operations/user_api/set_bucket_retention_config_parameters.go create mode 100644 restapi/operations/user_api/set_bucket_retention_config_responses.go create mode 100644 restapi/operations/user_api/set_bucket_retention_config_urlbuilder.go diff --git a/models/object_retention_unit.go b/models/object_retention_unit.go new file mode 100644 index 000000000..a31e2bac4 --- /dev/null +++ b/models/object_retention_unit.go @@ -0,0 +1,80 @@ +// Code generated by go-swagger; DO NOT EDIT. + +// This file is part of MinIO Console Server +// Copyright (c) 2020 MinIO, Inc. +// +// 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 . +// + +package models + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "encoding/json" + + "github.com/go-openapi/errors" + "github.com/go-openapi/strfmt" + "github.com/go-openapi/validate" +) + +// ObjectRetentionUnit object retention unit +// +// swagger:model objectRetentionUnit +type ObjectRetentionUnit string + +const ( + + // ObjectRetentionUnitDays captures enum value "days" + ObjectRetentionUnitDays ObjectRetentionUnit = "days" + + // ObjectRetentionUnitYears captures enum value "years" + ObjectRetentionUnitYears ObjectRetentionUnit = "years" +) + +// for schema +var objectRetentionUnitEnum []interface{} + +func init() { + var res []ObjectRetentionUnit + if err := json.Unmarshal([]byte(`["days","years"]`), &res); err != nil { + panic(err) + } + for _, v := range res { + objectRetentionUnitEnum = append(objectRetentionUnitEnum, v) + } +} + +func (m ObjectRetentionUnit) validateObjectRetentionUnitEnum(path, location string, value ObjectRetentionUnit) error { + if err := validate.EnumCase(path, location, value, objectRetentionUnitEnum, true); err != nil { + return err + } + return nil +} + +// Validate validates this object retention unit +func (m ObjectRetentionUnit) Validate(formats strfmt.Registry) error { + var res []error + + // value enum + if err := m.validateObjectRetentionUnitEnum("", "body", m); err != nil { + return err + } + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} diff --git a/models/put_bucket_retention_request.go b/models/put_bucket_retention_request.go new file mode 100644 index 000000000..62b7b5373 --- /dev/null +++ b/models/put_bucket_retention_request.go @@ -0,0 +1,121 @@ +// Code generated by go-swagger; DO NOT EDIT. + +// This file is part of MinIO Console Server +// Copyright (c) 2020 MinIO, Inc. +// +// 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 . +// + +package models + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "github.com/go-openapi/errors" + "github.com/go-openapi/strfmt" + "github.com/go-openapi/swag" + "github.com/go-openapi/validate" +) + +// PutBucketRetentionRequest put bucket retention request +// +// swagger:model putBucketRetentionRequest +type PutBucketRetentionRequest struct { + + // mode + // Required: true + Mode ObjectRetentionMode `json:"mode"` + + // unit + // Required: true + Unit ObjectRetentionUnit `json:"unit"` + + // validity + // Required: true + Validity *int32 `json:"validity"` +} + +// Validate validates this put bucket retention request +func (m *PutBucketRetentionRequest) Validate(formats strfmt.Registry) error { + var res []error + + if err := m.validateMode(formats); err != nil { + res = append(res, err) + } + + if err := m.validateUnit(formats); err != nil { + res = append(res, err) + } + + if err := m.validateValidity(formats); err != nil { + res = append(res, err) + } + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} + +func (m *PutBucketRetentionRequest) validateMode(formats strfmt.Registry) error { + + if err := m.Mode.Validate(formats); err != nil { + if ve, ok := err.(*errors.Validation); ok { + return ve.ValidateName("mode") + } + return err + } + + return nil +} + +func (m *PutBucketRetentionRequest) validateUnit(formats strfmt.Registry) error { + + if err := m.Unit.Validate(formats); err != nil { + if ve, ok := err.(*errors.Validation); ok { + return ve.ValidateName("unit") + } + return err + } + + return nil +} + +func (m *PutBucketRetentionRequest) validateValidity(formats strfmt.Registry) error { + + if err := validate.Required("validity", "body", m.Validity); err != nil { + return err + } + + return nil +} + +// MarshalBinary interface implementation +func (m *PutBucketRetentionRequest) MarshalBinary() ([]byte, error) { + if m == nil { + return nil, nil + } + return swag.WriteJSON(m) +} + +// UnmarshalBinary interface implementation +func (m *PutBucketRetentionRequest) UnmarshalBinary(b []byte) error { + var res PutBucketRetentionRequest + if err := swag.ReadJSON(b, &res); err != nil { + return err + } + *m = res + return nil +} diff --git a/restapi/client.go b/restapi/client.go index 0657e90f2..46b0b2916 100644 --- a/restapi/client.go +++ b/restapi/client.go @@ -68,6 +68,7 @@ type MinioClient interface { getBucketEncryption(ctx context.Context, bucketName string) (*sse.Configuration, error) putObjectTagging(ctx context.Context, bucketName, objectName string, otags *tags.Tags, opts minio.PutObjectTaggingOptions) error getObjectTagging(ctx context.Context, bucketName, objectName string, opts minio.GetObjectTaggingOptions) (*tags.Tags, error) + setObjectLockConfig(ctx context.Context, bucketName string, mode *minio.RetentionMode, validity *uint, unit *minio.ValidityUnit) error } // Interface implementation @@ -169,6 +170,10 @@ func (c minioClient) getObjectTagging(ctx context.Context, bucketName, objectNam return c.client.GetObjectTagging(ctx, bucketName, objectName, opts) } +func (c minioClient) setObjectLockConfig(ctx context.Context, bucketName string, mode *minio.RetentionMode, validity *uint, unit *minio.ValidityUnit) error { + return c.client.SetObjectLockConfig(ctx, bucketName, mode, validity, unit) +} + // MCClient interface with all functions to be implemented // by mock when testing, it should include all mc/S3Client respective api calls // that are used within this project. diff --git a/restapi/embedded_spec.go b/restapi/embedded_spec.go index ec147bb24..bcad60c8f 100644 --- a/restapi/embedded_spec.go +++ b/restapi/embedded_spec.go @@ -868,6 +868,42 @@ func init() { } } }, + "/buckets/{bucket_name}/retention": { + "put": { + "tags": [ + "UserAPI" + ], + "summary": "Set Bucket's retention config", + "operationId": "SetBucketRetentionConfig", + "parameters": [ + { + "type": "string", + "name": "bucket_name", + "in": "path", + "required": true + }, + { + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/putBucketRetentionRequest" + } + } + ], + "responses": { + "200": { + "description": "A successful response." + }, + "default": { + "description": "Generic error response.", + "schema": { + "$ref": "#/definitions/error" + } + } + } + } + }, "/buckets/{bucket_name}/versioning": { "get": { "tags": [ @@ -4057,6 +4093,13 @@ func init() { "compliance" ] }, + "objectRetentionUnit": { + "type": "string", + "enum": [ + "days", + "years" + ] + }, "parityResponse": { "type": "array", "items": { @@ -4187,6 +4230,26 @@ func init() { } } }, + "putBucketRetentionRequest": { + "type": "object", + "required": [ + "mode", + "unit", + "validity" + ], + "properties": { + "mode": { + "$ref": "#/definitions/objectRetentionMode" + }, + "unit": { + "$ref": "#/definitions/objectRetentionUnit" + }, + "validity": { + "type": "integer", + "format": "int32" + } + } + }, "putObjectLegalHoldRequest": { "type": "object", "required": [ @@ -5827,6 +5890,42 @@ func init() { } } }, + "/buckets/{bucket_name}/retention": { + "put": { + "tags": [ + "UserAPI" + ], + "summary": "Set Bucket's retention config", + "operationId": "SetBucketRetentionConfig", + "parameters": [ + { + "type": "string", + "name": "bucket_name", + "in": "path", + "required": true + }, + { + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/putBucketRetentionRequest" + } + } + ], + "responses": { + "200": { + "description": "A successful response." + }, + "default": { + "description": "Generic error response.", + "schema": { + "$ref": "#/definitions/error" + } + } + } + } + }, "/buckets/{bucket_name}/versioning": { "get": { "tags": [ @@ -9495,6 +9594,13 @@ func init() { "compliance" ] }, + "objectRetentionUnit": { + "type": "string", + "enum": [ + "days", + "years" + ] + }, "parityResponse": { "type": "array", "items": { @@ -9603,6 +9709,26 @@ func init() { } } }, + "putBucketRetentionRequest": { + "type": "object", + "required": [ + "mode", + "unit", + "validity" + ], + "properties": { + "mode": { + "$ref": "#/definitions/objectRetentionMode" + }, + "unit": { + "$ref": "#/definitions/objectRetentionUnit" + }, + "validity": { + "type": "integer", + "format": "int32" + } + } + }, "putObjectLegalHoldRequest": { "type": "object", "required": [ diff --git a/restapi/operations/console_api.go b/restapi/operations/console_api.go index 1ce58c13c..89c24331f 100644 --- a/restapi/operations/console_api.go +++ b/restapi/operations/console_api.go @@ -266,6 +266,9 @@ func NewConsoleAPI(spec *loads.Document) *ConsoleAPI { UserAPISetBucketQuotaHandler: user_api.SetBucketQuotaHandlerFunc(func(params user_api.SetBucketQuotaParams, principal *models.Principal) middleware.Responder { return middleware.NotImplemented("operation user_api.SetBucketQuota has not yet been implemented") }), + UserAPISetBucketRetentionConfigHandler: user_api.SetBucketRetentionConfigHandlerFunc(func(params user_api.SetBucketRetentionConfigParams, principal *models.Principal) middleware.Responder { + return middleware.NotImplemented("operation user_api.SetBucketRetentionConfig has not yet been implemented") + }), UserAPISetBucketVersioningHandler: user_api.SetBucketVersioningHandlerFunc(func(params user_api.SetBucketVersioningParams, principal *models.Principal) middleware.Responder { return middleware.NotImplemented("operation user_api.SetBucketVersioning has not yet been implemented") }), @@ -494,6 +497,8 @@ type ConsoleAPI struct { UserAPISessionCheckHandler user_api.SessionCheckHandler // UserAPISetBucketQuotaHandler sets the operation handler for the set bucket quota operation UserAPISetBucketQuotaHandler user_api.SetBucketQuotaHandler + // UserAPISetBucketRetentionConfigHandler sets the operation handler for the set bucket retention config operation + UserAPISetBucketRetentionConfigHandler user_api.SetBucketRetentionConfigHandler // UserAPISetBucketVersioningHandler sets the operation handler for the set bucket versioning operation UserAPISetBucketVersioningHandler user_api.SetBucketVersioningHandler // AdminAPISetConfigHandler sets the operation handler for the set config operation @@ -799,6 +804,9 @@ func (o *ConsoleAPI) Validate() error { if o.UserAPISetBucketQuotaHandler == nil { unregistered = append(unregistered, "user_api.SetBucketQuotaHandler") } + if o.UserAPISetBucketRetentionConfigHandler == nil { + unregistered = append(unregistered, "user_api.SetBucketRetentionConfigHandler") + } if o.UserAPISetBucketVersioningHandler == nil { unregistered = append(unregistered, "user_api.SetBucketVersioningHandler") } @@ -1214,6 +1222,10 @@ func (o *ConsoleAPI) initHandlerCache() { if o.handlers["PUT"] == nil { o.handlers["PUT"] = make(map[string]http.Handler) } + o.handlers["PUT"]["/buckets/{bucket_name}/retention"] = user_api.NewSetBucketRetentionConfig(o.context, o.UserAPISetBucketRetentionConfigHandler) + if o.handlers["PUT"] == nil { + o.handlers["PUT"] = make(map[string]http.Handler) + } o.handlers["PUT"]["/buckets/{bucket_name}/versioning"] = user_api.NewSetBucketVersioning(o.context, o.UserAPISetBucketVersioningHandler) if o.handlers["PUT"] == nil { o.handlers["PUT"] = make(map[string]http.Handler) diff --git a/restapi/operations/user_api/set_bucket_retention_config.go b/restapi/operations/user_api/set_bucket_retention_config.go new file mode 100644 index 000000000..b6f0f380f --- /dev/null +++ b/restapi/operations/user_api/set_bucket_retention_config.go @@ -0,0 +1,90 @@ +// Code generated by go-swagger; DO NOT EDIT. + +// This file is part of MinIO Console Server +// Copyright (c) 2020 MinIO, Inc. +// +// 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 . +// + +package user_api + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the generate command + +import ( + "net/http" + + "github.com/go-openapi/runtime/middleware" + + "github.com/minio/console/models" +) + +// SetBucketRetentionConfigHandlerFunc turns a function with the right signature into a set bucket retention config handler +type SetBucketRetentionConfigHandlerFunc func(SetBucketRetentionConfigParams, *models.Principal) middleware.Responder + +// Handle executing the request and returning a response +func (fn SetBucketRetentionConfigHandlerFunc) Handle(params SetBucketRetentionConfigParams, principal *models.Principal) middleware.Responder { + return fn(params, principal) +} + +// SetBucketRetentionConfigHandler interface for that can handle valid set bucket retention config params +type SetBucketRetentionConfigHandler interface { + Handle(SetBucketRetentionConfigParams, *models.Principal) middleware.Responder +} + +// NewSetBucketRetentionConfig creates a new http.Handler for the set bucket retention config operation +func NewSetBucketRetentionConfig(ctx *middleware.Context, handler SetBucketRetentionConfigHandler) *SetBucketRetentionConfig { + return &SetBucketRetentionConfig{Context: ctx, Handler: handler} +} + +/*SetBucketRetentionConfig swagger:route PUT /buckets/{bucket_name}/retention UserAPI setBucketRetentionConfig + +Set Bucket's retention config + +*/ +type SetBucketRetentionConfig struct { + Context *middleware.Context + Handler SetBucketRetentionConfigHandler +} + +func (o *SetBucketRetentionConfig) ServeHTTP(rw http.ResponseWriter, r *http.Request) { + route, rCtx, _ := o.Context.RouteInfo(r) + if rCtx != nil { + r = rCtx + } + var Params = NewSetBucketRetentionConfigParams() + + uprinc, aCtx, err := o.Context.Authorize(r, route) + if err != nil { + o.Context.Respond(rw, r, route.Produces, route, err) + return + } + if aCtx != nil { + r = aCtx + } + var principal *models.Principal + if uprinc != nil { + principal = uprinc.(*models.Principal) // this is really a models.Principal, I promise + } + + if err := o.Context.BindValidRequest(r, route, &Params); err != nil { // bind params + o.Context.Respond(rw, r, route.Produces, route, err) + return + } + + res := o.Handler.Handle(Params, principal) // actually handle the request + + o.Context.Respond(rw, r, route.Produces, route, res) + +} diff --git a/restapi/operations/user_api/set_bucket_retention_config_parameters.go b/restapi/operations/user_api/set_bucket_retention_config_parameters.go new file mode 100644 index 000000000..73ff5d6fc --- /dev/null +++ b/restapi/operations/user_api/set_bucket_retention_config_parameters.go @@ -0,0 +1,120 @@ +// Code generated by go-swagger; DO NOT EDIT. + +// This file is part of MinIO Console Server +// Copyright (c) 2020 MinIO, Inc. +// +// 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 . +// + +package user_api + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "io" + "net/http" + + "github.com/go-openapi/errors" + "github.com/go-openapi/runtime" + "github.com/go-openapi/runtime/middleware" + "github.com/go-openapi/strfmt" + + "github.com/minio/console/models" +) + +// NewSetBucketRetentionConfigParams creates a new SetBucketRetentionConfigParams object +// no default values defined in spec. +func NewSetBucketRetentionConfigParams() SetBucketRetentionConfigParams { + + return SetBucketRetentionConfigParams{} +} + +// SetBucketRetentionConfigParams contains all the bound params for the set bucket retention config operation +// typically these are obtained from a http.Request +// +// swagger:parameters SetBucketRetentionConfig +type SetBucketRetentionConfigParams struct { + + // HTTP Request Object + HTTPRequest *http.Request `json:"-"` + + /* + Required: true + In: body + */ + Body *models.PutBucketRetentionRequest + /* + Required: true + In: path + */ + BucketName string +} + +// BindRequest both binds and validates a request, it assumes that complex things implement a Validatable(strfmt.Registry) error interface +// for simple values it will use straight method calls. +// +// To ensure default values, the struct must have been initialized with NewSetBucketRetentionConfigParams() beforehand. +func (o *SetBucketRetentionConfigParams) BindRequest(r *http.Request, route *middleware.MatchedRoute) error { + var res []error + + o.HTTPRequest = r + + if runtime.HasBody(r) { + defer r.Body.Close() + var body models.PutBucketRetentionRequest + if err := route.Consumer.Consume(r.Body, &body); err != nil { + if err == io.EOF { + res = append(res, errors.Required("body", "body", "")) + } else { + res = append(res, errors.NewParseError("body", "body", "", err)) + } + } else { + // validate body object + if err := body.Validate(route.Formats); err != nil { + res = append(res, err) + } + + if len(res) == 0 { + o.Body = &body + } + } + } else { + res = append(res, errors.Required("body", "body", "")) + } + rBucketName, rhkBucketName, _ := route.Params.GetOK("bucket_name") + if err := o.bindBucketName(rBucketName, rhkBucketName, route.Formats); err != nil { + res = append(res, err) + } + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} + +// bindBucketName binds and validates parameter BucketName from path. +func (o *SetBucketRetentionConfigParams) bindBucketName(rawData []string, hasKey bool, formats strfmt.Registry) error { + var raw string + if len(rawData) > 0 { + raw = rawData[len(rawData)-1] + } + + // Required: true + // Parameter is provided by construction from the route + + o.BucketName = raw + + return nil +} diff --git a/restapi/operations/user_api/set_bucket_retention_config_responses.go b/restapi/operations/user_api/set_bucket_retention_config_responses.go new file mode 100644 index 000000000..6f84fca65 --- /dev/null +++ b/restapi/operations/user_api/set_bucket_retention_config_responses.go @@ -0,0 +1,113 @@ +// Code generated by go-swagger; DO NOT EDIT. + +// This file is part of MinIO Console Server +// Copyright (c) 2020 MinIO, Inc. +// +// 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 . +// + +package user_api + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "net/http" + + "github.com/go-openapi/runtime" + + "github.com/minio/console/models" +) + +// SetBucketRetentionConfigOKCode is the HTTP code returned for type SetBucketRetentionConfigOK +const SetBucketRetentionConfigOKCode int = 200 + +/*SetBucketRetentionConfigOK A successful response. + +swagger:response setBucketRetentionConfigOK +*/ +type SetBucketRetentionConfigOK struct { +} + +// NewSetBucketRetentionConfigOK creates SetBucketRetentionConfigOK with default headers values +func NewSetBucketRetentionConfigOK() *SetBucketRetentionConfigOK { + + return &SetBucketRetentionConfigOK{} +} + +// WriteResponse to the client +func (o *SetBucketRetentionConfigOK) WriteResponse(rw http.ResponseWriter, producer runtime.Producer) { + + rw.Header().Del(runtime.HeaderContentType) //Remove Content-Type on empty responses + + rw.WriteHeader(200) +} + +/*SetBucketRetentionConfigDefault Generic error response. + +swagger:response setBucketRetentionConfigDefault +*/ +type SetBucketRetentionConfigDefault struct { + _statusCode int + + /* + In: Body + */ + Payload *models.Error `json:"body,omitempty"` +} + +// NewSetBucketRetentionConfigDefault creates SetBucketRetentionConfigDefault with default headers values +func NewSetBucketRetentionConfigDefault(code int) *SetBucketRetentionConfigDefault { + if code <= 0 { + code = 500 + } + + return &SetBucketRetentionConfigDefault{ + _statusCode: code, + } +} + +// WithStatusCode adds the status to the set bucket retention config default response +func (o *SetBucketRetentionConfigDefault) WithStatusCode(code int) *SetBucketRetentionConfigDefault { + o._statusCode = code + return o +} + +// SetStatusCode sets the status to the set bucket retention config default response +func (o *SetBucketRetentionConfigDefault) SetStatusCode(code int) { + o._statusCode = code +} + +// WithPayload adds the payload to the set bucket retention config default response +func (o *SetBucketRetentionConfigDefault) WithPayload(payload *models.Error) *SetBucketRetentionConfigDefault { + o.Payload = payload + return o +} + +// SetPayload sets the payload to the set bucket retention config default response +func (o *SetBucketRetentionConfigDefault) SetPayload(payload *models.Error) { + o.Payload = payload +} + +// WriteResponse to the client +func (o *SetBucketRetentionConfigDefault) WriteResponse(rw http.ResponseWriter, producer runtime.Producer) { + + rw.WriteHeader(o._statusCode) + if o.Payload != nil { + payload := o.Payload + if err := producer.Produce(rw, payload); err != nil { + panic(err) // let the recovery middleware deal with this + } + } +} diff --git a/restapi/operations/user_api/set_bucket_retention_config_urlbuilder.go b/restapi/operations/user_api/set_bucket_retention_config_urlbuilder.go new file mode 100644 index 000000000..a167dd7f2 --- /dev/null +++ b/restapi/operations/user_api/set_bucket_retention_config_urlbuilder.go @@ -0,0 +1,116 @@ +// Code generated by go-swagger; DO NOT EDIT. + +// This file is part of MinIO Console Server +// Copyright (c) 2020 MinIO, Inc. +// +// 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 . +// + +package user_api + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the generate command + +import ( + "errors" + "net/url" + golangswaggerpaths "path" + "strings" +) + +// SetBucketRetentionConfigURL generates an URL for the set bucket retention config operation +type SetBucketRetentionConfigURL struct { + BucketName string + + _basePath string + // avoid unkeyed usage + _ struct{} +} + +// WithBasePath sets the base path for this url builder, only required when it's different from the +// base path specified in the swagger spec. +// When the value of the base path is an empty string +func (o *SetBucketRetentionConfigURL) WithBasePath(bp string) *SetBucketRetentionConfigURL { + o.SetBasePath(bp) + return o +} + +// SetBasePath sets the base path for this url builder, only required when it's different from the +// base path specified in the swagger spec. +// When the value of the base path is an empty string +func (o *SetBucketRetentionConfigURL) SetBasePath(bp string) { + o._basePath = bp +} + +// Build a url path and query string +func (o *SetBucketRetentionConfigURL) Build() (*url.URL, error) { + var _result url.URL + + var _path = "/buckets/{bucket_name}/retention" + + bucketName := o.BucketName + if bucketName != "" { + _path = strings.Replace(_path, "{bucket_name}", bucketName, -1) + } else { + return nil, errors.New("bucketName is required on SetBucketRetentionConfigURL") + } + + _basePath := o._basePath + if _basePath == "" { + _basePath = "/api/v1" + } + _result.Path = golangswaggerpaths.Join(_basePath, _path) + + return &_result, nil +} + +// Must is a helper function to panic when the url builder returns an error +func (o *SetBucketRetentionConfigURL) Must(u *url.URL, err error) *url.URL { + if err != nil { + panic(err) + } + if u == nil { + panic("url can't be nil") + } + return u +} + +// String returns the string representation of the path with query string +func (o *SetBucketRetentionConfigURL) String() string { + return o.Must(o.Build()).String() +} + +// BuildFull builds a full url with scheme, host, path and query string +func (o *SetBucketRetentionConfigURL) BuildFull(scheme, host string) (*url.URL, error) { + if scheme == "" { + return nil, errors.New("scheme is required for a full url on SetBucketRetentionConfigURL") + } + if host == "" { + return nil, errors.New("host is required for a full url on SetBucketRetentionConfigURL") + } + + base, err := o.Build() + if err != nil { + return nil, err + } + + base.Scheme = scheme + base.Host = host + return base, nil +} + +// StringFull returns the string representation of a complete url +func (o *SetBucketRetentionConfigURL) StringFull(scheme, host string) string { + return o.Must(o.BuildFull(scheme, host)).String() +} diff --git a/restapi/user_buckets.go b/restapi/user_buckets.go index 77d331777..928982564 100644 --- a/restapi/user_buckets.go +++ b/restapi/user_buckets.go @@ -24,9 +24,12 @@ import ( "strings" "time" + "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/replication" "github.com/minio/minio-go/v7/pkg/sse" + "errors" + "github.com/go-openapi/runtime/middleware" "github.com/go-openapi/swag" "github.com/minio/console/models" @@ -124,6 +127,13 @@ func registerBucketsHandlers(api *operations.ConsoleAPI) { } return user_api.NewGetBucketEncryptionInfoOK().WithPayload(response) }) + // set bucket retention config + api.UserAPISetBucketRetentionConfigHandler = user_api.SetBucketRetentionConfigHandlerFunc(func(params user_api.SetBucketRetentionConfigParams, session *models.Principal) middleware.Responder { + if err := getSetBucketRetentionConfigResponse(session, params); err != nil { + return user_api.NewSetBucketRetentionConfigDefault(int(err.Code)).WithPayload(err) + } + return user_api.NewSetBucketRetentionConfigOK() + }) } func getAddBucketReplicationdResponse(session *models.Principal, bucketName string, params *user_api.AddBucketReplicationParams) error { @@ -560,3 +570,51 @@ func getBucketEncryptionInfoResponse(session *models.Principal, params user_api. } return bucketInfo, 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 getSetBucketRetentionConfigResponse(session *models.Principal, params user_api.SetBucketRetentionConfigParams) *models.Error { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + defer cancel() + mClient, err := newMinioClient(session) + if err != nil { + return prepareError(err) + } + // create a minioClient interface implementation + // defining the client to be used + minioClient := minioClient{client: mClient} + + err = setBucketRetentionConfig(ctx, minioClient, params.BucketName, params.Body.Mode, params.Body.Unit, params.Body.Validity) + if err != nil { + return prepareError(err) + } + return nil +} diff --git a/restapi/user_buckets_test.go b/restapi/user_buckets_test.go index 89ddceeee..5ed246682 100644 --- a/restapi/user_buckets_test.go +++ b/restapi/user_buckets_test.go @@ -42,6 +42,7 @@ var minioGetBucketPolicyMock func(bucketName string) (string, error) var minioSetBucketEncryptionMock func(ctx context.Context, bucketName string, config *sse.Configuration) error var minioRemoveBucketEncryptionMock func(ctx context.Context, bucketName string) error var minioGetBucketEncryptionMock func(ctx context.Context, bucketName string) (*sse.Configuration, error) +var minioSetObjectLockConfig func(ctx context.Context, bucketName string, mode *minio.RetentionMode, validity *uint, unit *minio.ValidityUnit) error // Define a mock struct of minio Client interface implementation type minioClientMock struct { @@ -84,6 +85,10 @@ func (mc minioClientMock) getBucketEncryption(ctx context.Context, bucketName st return minioGetBucketEncryptionMock(ctx, bucketName) } +func (mc minioClientMock) setObjectLockConfig(ctx context.Context, bucketName string, mode *minio.RetentionMode, validity *uint, unit *minio.ValidityUnit) error { + return minioSetObjectLockConfig(ctx, bucketName, mode, validity, unit) +} + var minioAccountInfoMock func(ctx context.Context) (madmin.AccountInfo, error) // mock function of dataUsageInfo() needed for list bucket's usage @@ -515,3 +520,126 @@ func Test_getBucketEncryptionInfo(t *testing.T) { }) } } + +func Test_SetBucketRetentionConfig(t *testing.T) { + assert := assert.New(t) + ctx := context.Background() + minClient := minioClientMock{} + type args struct { + ctx context.Context + client MinioClient + 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, + client: minClient, + bucketName: "test", + mode: models.ObjectRetentionModeCompliance, + unit: models.ObjectRetentionUnitDays, + validity: swag.Int32(2), + mockBucketRetentionFunc: func(ctx context.Context, bucketName string, mode *minio.RetentionMode, validity *uint, unit *minio.ValidityUnit) error { + return nil + }, + }, + expectedError: nil, + }, + { + name: "Set Bucket Retention Config 2", + args: args{ + ctx: ctx, + client: minClient, + bucketName: "test", + mode: models.ObjectRetentionModeGovernance, + unit: models.ObjectRetentionUnitYears, + validity: swag.Int32(2), + mockBucketRetentionFunc: func(ctx context.Context, bucketName string, mode *minio.RetentionMode, validity *uint, unit *minio.ValidityUnit) error { + return nil + }, + }, + expectedError: nil, + }, + { + name: "Invalid validity", + args: args{ + ctx: ctx, + client: minClient, + bucketName: "test", + mode: models.ObjectRetentionModeCompliance, + unit: models.ObjectRetentionUnitDays, + validity: nil, + mockBucketRetentionFunc: func(ctx context.Context, bucketName string, mode *minio.RetentionMode, validity *uint, unit *minio.ValidityUnit) error { + return nil + }, + }, + expectedError: errors.New("retention validity can't be nil"), + }, + { + name: "Invalid retention mode", + args: args{ + ctx: ctx, + client: minClient, + bucketName: "test", + mode: models.ObjectRetentionMode("othermode"), + unit: models.ObjectRetentionUnitDays, + validity: swag.Int32(2), + mockBucketRetentionFunc: func(ctx context.Context, bucketName string, mode *minio.RetentionMode, validity *uint, unit *minio.ValidityUnit) error { + return nil + }, + }, + expectedError: errors.New("invalid retention mode"), + }, + { + name: "Invalid retention unit", + args: args{ + ctx: ctx, + client: minClient, + bucketName: "test", + mode: models.ObjectRetentionModeCompliance, + unit: models.ObjectRetentionUnit("otherunit"), + validity: swag.Int32(2), + mockBucketRetentionFunc: func(ctx context.Context, bucketName string, mode *minio.RetentionMode, validity *uint, unit *minio.ValidityUnit) error { + return nil + }, + }, + expectedError: errors.New("invalid retention unit"), + }, + { + name: "Handle error on objec lock function", + args: args{ + ctx: ctx, + client: minClient, + bucketName: "test", + mode: models.ObjectRetentionModeCompliance, + unit: models.ObjectRetentionUnitDays, + validity: swag.Int32(2), + mockBucketRetentionFunc: func(ctx context.Context, bucketName string, mode *minio.RetentionMode, validity *uint, unit *minio.ValidityUnit) error { + return errors.New("error func") + }, + }, + expectedError: errors.New("error func"), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + minioSetObjectLockConfig = tt.args.mockBucketRetentionFunc + err := setBucketRetentionConfig(tt.args.ctx, tt.args.client, 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() error: `%s`, wantErr: `%s`", err, tt.expectedError)) + } else { + assert.Nil(err, fmt.Sprintf("setBucketRetentionConfig() error: %v, wantErr: %v", err, tt.expectedError)) + } + }) + } +} diff --git a/restapi/user_objects.go b/restapi/user_objects.go index 95c6d5879..03edf58de 100644 --- a/restapi/user_objects.go +++ b/restapi/user_objects.go @@ -478,20 +478,21 @@ func getSetObjectRetentionResponse(session *models.Principal, params user_api.Pu // create a minioClient interface implementation // defining the client to be used minioClient := minioClient{client: mClient} - err = setObjectRetention(ctx, minioClient, params.BucketName, params.Prefix, params.VersionID, params.Body) + err = setObjectRetention(ctx, minioClient, params.BucketName, params.VersionID, params.Prefix, params.Body) if err != nil { return prepareError(err) } return nil } -func setObjectRetention(ctx context.Context, client MinioClient, bucketName, prefix, versionID string, retentionOps *models.PutObjectRetentionRequest) error { +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 diff --git a/swagger.yml b/swagger.yml index 8eeb521f3..16bebfe86 100644 --- a/swagger.yml +++ b/swagger.yml @@ -223,6 +223,30 @@ paths: tags: - UserAPI + /buckets/{bucket_name}/retention: + put: + summary: Set Bucket's retention config + operationId: SetBucketRetentionConfig + parameters: + - name: bucket_name + in: path + required: true + type: string + - name: body + in: body + required: true + schema: + $ref: "#/definitions/putObjectRetentionRequest" + responses: + 200: + description: A successful response. + default: + description: Generic error response. + schema: + $ref: "#/definitions/error" + tags: + - UserAPI + /buckets/{bucket_name}/objects: get: summary: List Objects @@ -3590,3 +3614,24 @@ definitions: tags: additionalProperties: type: string + + objectRetentionUnit: + type: string + enum: + - days + - years + + putBucketRetentionRequest: + type: object + required: + - mode + - unit + - validity + properties: + mode: + $ref: "#/definitions/objectRetentionMode" + unit: + $ref: "#/definitions/objectRetentionUnit" + validity: + type: integer + format: int32 \ No newline at end of file