123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- package minio
- import (
- "bytes"
- "context"
- "encoding/xml"
- "fmt"
- "net/http"
- "net/url"
- "time"
- "github.com/minio/minio-go/v7/pkg/s3utils"
- )
- type RetentionMode string
- const (
-
- Governance RetentionMode = "GOVERNANCE"
-
- Compliance RetentionMode = "COMPLIANCE"
- )
- func (r RetentionMode) String() string {
- return string(r)
- }
- func (r RetentionMode) IsValid() bool {
- return r == Governance || r == Compliance
- }
- type ValidityUnit string
- const (
-
- Days ValidityUnit = "DAYS"
-
- Years ValidityUnit = "YEARS"
- )
- func (unit ValidityUnit) String() string {
- return string(unit)
- }
- func (unit ValidityUnit) isValid() bool {
- return unit == Days || unit == Years
- }
- type Retention struct {
- Mode RetentionMode
- Validity time.Duration
- }
- func (r Retention) String() string {
- return fmt.Sprintf("{Mode:%v, Validity:%v}", r.Mode, r.Validity)
- }
- func (r Retention) IsEmpty() bool {
- return r.Mode == "" || r.Validity == 0
- }
- type objectLockConfig struct {
- XMLNS string `xml:"xmlns,attr,omitempty"`
- XMLName xml.Name `xml:"ObjectLockConfiguration"`
- ObjectLockEnabled string `xml:"ObjectLockEnabled"`
- Rule *struct {
- DefaultRetention struct {
- Mode RetentionMode `xml:"Mode"`
- Days *uint `xml:"Days"`
- Years *uint `xml:"Years"`
- } `xml:"DefaultRetention"`
- } `xml:"Rule,omitempty"`
- }
- func newObjectLockConfig(mode *RetentionMode, validity *uint, unit *ValidityUnit) (*objectLockConfig, error) {
- config := &objectLockConfig{
- ObjectLockEnabled: "Enabled",
- }
- if mode != nil && validity != nil && unit != nil {
- if !mode.IsValid() {
- return nil, fmt.Errorf("invalid retention mode `%v`", mode)
- }
- if !unit.isValid() {
- return nil, fmt.Errorf("invalid validity unit `%v`", unit)
- }
- config.Rule = &struct {
- DefaultRetention struct {
- Mode RetentionMode `xml:"Mode"`
- Days *uint `xml:"Days"`
- Years *uint `xml:"Years"`
- } `xml:"DefaultRetention"`
- }{}
- config.Rule.DefaultRetention.Mode = *mode
- if *unit == Days {
- config.Rule.DefaultRetention.Days = validity
- } else {
- config.Rule.DefaultRetention.Years = validity
- }
- return config, nil
- }
- if mode == nil && validity == nil && unit == nil {
- return config, nil
- }
- return nil, fmt.Errorf("all of retention mode, validity and validity unit must be passed")
- }
- func (c Client) SetBucketObjectLockConfig(ctx context.Context, bucketName string, mode *RetentionMode, validity *uint, unit *ValidityUnit) error {
-
- if err := s3utils.CheckValidBucketName(bucketName); err != nil {
- return err
- }
-
-
- urlValues := make(url.Values)
- urlValues.Set("object-lock", "")
- config, err := newObjectLockConfig(mode, validity, unit)
- if err != nil {
- return err
- }
- configData, err := xml.Marshal(config)
- if err != nil {
- return err
- }
- reqMetadata := requestMetadata{
- bucketName: bucketName,
- queryValues: urlValues,
- contentBody: bytes.NewReader(configData),
- contentLength: int64(len(configData)),
- contentMD5Base64: sumMD5Base64(configData),
- contentSHA256Hex: sum256Hex(configData),
- }
-
- resp, err := c.executeMethod(ctx, http.MethodPut, reqMetadata)
- defer closeResponse(resp)
- if err != nil {
- return err
- }
- if resp != nil {
- if resp.StatusCode != http.StatusOK {
- return httpRespToErrorResponse(resp, bucketName, "")
- }
- }
- return nil
- }
- func (c Client) GetObjectLockConfig(ctx context.Context, bucketName string) (objectLock string, mode *RetentionMode, validity *uint, unit *ValidityUnit, err error) {
-
- if err := s3utils.CheckValidBucketName(bucketName); err != nil {
- return "", nil, nil, nil, err
- }
- urlValues := make(url.Values)
- urlValues.Set("object-lock", "")
-
- resp, err := c.executeMethod(ctx, http.MethodGet, requestMetadata{
- bucketName: bucketName,
- queryValues: urlValues,
- contentSHA256Hex: emptySHA256Hex,
- })
- defer closeResponse(resp)
- if err != nil {
- return "", nil, nil, nil, err
- }
- if resp != nil {
- if resp.StatusCode != http.StatusOK {
- return "", nil, nil, nil, httpRespToErrorResponse(resp, bucketName, "")
- }
- }
- config := &objectLockConfig{}
- if err = xml.NewDecoder(resp.Body).Decode(config); err != nil {
- return "", nil, nil, nil, err
- }
- if config.Rule != nil {
- mode = &config.Rule.DefaultRetention.Mode
- if config.Rule.DefaultRetention.Days != nil {
- validity = config.Rule.DefaultRetention.Days
- days := Days
- unit = &days
- } else {
- validity = config.Rule.DefaultRetention.Years
- years := Years
- unit = &years
- }
- return config.ObjectLockEnabled, mode, validity, unit, nil
- }
- return config.ObjectLockEnabled, nil, nil, nil, nil
- }
- func (c Client) GetBucketObjectLockConfig(ctx context.Context, bucketName string) (mode *RetentionMode, validity *uint, unit *ValidityUnit, err error) {
- _, mode, validity, unit, err = c.GetObjectLockConfig(ctx, bucketName)
- return mode, validity, unit, err
- }
- func (c Client) SetObjectLockConfig(ctx context.Context, bucketName string, mode *RetentionMode, validity *uint, unit *ValidityUnit) error {
- return c.SetBucketObjectLockConfig(ctx, bucketName, mode, validity, unit)
- }
|