1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
// Copyright (c) 2015-2025 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 ilm
import (
"errors"
"strconv"
"strings"
"time"
"github.com/minio/minio-go/v7/pkg/lifecycle"
)
// Used in tags. Ex: --tags "key1=value1&key2=value2&key3=value3"
const (
tagSeparator string = "&"
keyValSeparator string = "="
)
var (
errRuleAction = errors.New("at least one of Expiry, Transition, NoncurrentExpiry, NoncurrentVersionTransition, AllVersionsExpiration actions should be specified in a rule")
errZeroExpiryDays = errors.New("expiration days cannot be set to zero")
)
func extractILMTags(tagLabelVal string) []lifecycle.Tag {
var ilmTagKVList []lifecycle.Tag
for _, tag := range strings.Split(tagLabelVal, tagSeparator) {
if tag == "" {
// split returns empty for empty tagLabelVal, skip it.
continue
}
lfcTag := lifecycle.Tag{}
kvs := strings.SplitN(tag, keyValSeparator, 2)
if len(kvs) == 2 {
lfcTag.Key = kvs[0]
lfcTag.Value = kvs[1]
} else {
lfcTag.Key = kvs[0]
}
ilmTagKVList = append(ilmTagKVList, lfcTag)
}
return ilmTagKVList
}
// Some of these rules are enforced by Amazon S3 standards.
// For example: Transition has to happen before Expiry.
// Storage class must be specified if transition date/days is provided.
func validateTranExpDate(rule lifecycle.Rule) error {
expiryDateSet := !rule.Expiration.IsDateNull()
transitionSet := !rule.Transition.IsNull()
transitionDateSet := transitionSet && !rule.Transition.IsDateNull()
if transitionDateSet && expiryDateSet {
if rule.Expiration.Date.Before(rule.Transition.Date.Time) {
return errors.New("transition should apply before expiration")
}
}
if transitionDateSet && rule.Transition.StorageClass == "" {
return errors.New("missing transition storage-class")
}
return nil
}
func validateTranDays(rule lifecycle.Rule) error {
if rule.Transition.Days < 0 {
return errors.New("number of days to transition can't be negative")
}
if rule.Transition.Days < 30 && strings.ToLower(rule.Transition.StorageClass) == "standard_ia" {
return errors.New("number of days to transition should be >= 30 with STANDARD_IA storage-class")
}
return nil
}
// Amazon S3 requires a minimum of one action for a rule to be added.
func validateRuleAction(rule lifecycle.Rule) error {
expirySet := !rule.Expiration.IsNull()
allVersExpirySet := !rule.AllVersionsExpiration.IsNull()
transitionSet := !rule.Transition.IsNull()
noncurrentExpirySet := !rule.NoncurrentVersionExpiration.IsDaysNull()
newerNoncurrentVersionsExpiry := rule.NoncurrentVersionExpiration.NewerNoncurrentVersions > 0
noncurrentTransitionSet := rule.NoncurrentVersionTransition.StorageClass != ""
newerNoncurrentVersionsTransition := rule.NoncurrentVersionTransition.NewerNoncurrentVersions > 0
if !expirySet && !allVersExpirySet && !transitionSet && !noncurrentExpirySet && !noncurrentTransitionSet &&
!newerNoncurrentVersionsExpiry && !newerNoncurrentVersionsTransition {
return errRuleAction
}
return nil
}
func validateExpiration(rule lifecycle.Rule) error {
var i int
if !rule.Expiration.IsDaysNull() {
i++
}
if !rule.Expiration.IsDateNull() {
i++
}
if rule.Expiration.IsDeleteMarkerExpirationEnabled() {
i++
}
if i > 1 {
return errors.New("only one parameter under Expiration can be specified")
}
return nil
}
func validateTransition(rule lifecycle.Rule) error {
if !rule.Transition.IsDaysNull() && !rule.Transition.IsDateNull() {
return errors.New("only one parameter under Transition can be specified")
}
return nil
}
func validateAllVersionsExpiration(rule lifecycle.Rule) error {
if rule.AllVersionsExpiration.Days < 0 {
return errors.New("AllVersionsExpiration.Days is not a positive integer")
}
return nil
}
func validateNoncurrentExpiration(rule lifecycle.Rule) error {
days := rule.NoncurrentVersionExpiration.NoncurrentDays
if days < 0 {
return errors.New("NoncurrentVersionExpiration.NoncurrentDays is not a positive integer")
}
return nil
}
func validateNoncurrentTransition(rule lifecycle.Rule) error {
days := rule.NoncurrentVersionTransition.NoncurrentDays
storageClass := rule.NoncurrentVersionTransition.StorageClass
if days < 0 {
return errors.New("NoncurrentVersionTransition.NoncurrentDays is not a positive integer")
}
if days > 0 && storageClass == "" {
return errors.New("both NoncurrentVersionTransition NoncurrentDays and StorageClass need to be specified")
}
return nil
}
// Check if any date is before than cur date
func validateTranExpCurdate(rule lifecycle.Rule) error {
var e error
expirySet := !rule.Expiration.IsNull()
transitionSet := !rule.Transition.IsNull()
transitionDateSet := transitionSet && !rule.Transition.IsDateNull()
expiryDateSet := expirySet && !rule.Expiration.IsDateNull()
currentTime := time.Now()
curTimeStr := currentTime.Format(defaultILMDateFormat)
currentTime, e = time.Parse(defaultILMDateFormat, curTimeStr)
if e != nil {
return e
}
if expirySet && expiryDateSet && rule.Expiration.Date.Before(currentTime) {
e = errors.New("expiry date falls before or on today's date")
} else if transitionSet && transitionDateSet && rule.Transition.Date.Before(currentTime) {
e = errors.New("transition date falls before or on today's date")
}
return e
}
func validateILMRule(rule lifecycle.Rule) error {
if e := validateRuleAction(rule); e != nil {
return e
}
if e := validateExpiration(rule); e != nil {
return e
}
if e := validateTransition(rule); e != nil {
return e
}
if e := validateAllVersionsExpiration(rule); e != nil {
return e
}
if e := validateTranExpCurdate(rule); e != nil {
return e
}
if e := validateTranExpDate(rule); e != nil {
return e
}
if e := validateTranDays(rule); e != nil {
return e
}
if e := validateNoncurrentExpiration(rule); e != nil {
return e
}
if e := validateNoncurrentTransition(rule); e != nil {
return e
}
return nil
}
func parseTransitionDate(transitionDateStr string) (lifecycle.ExpirationDate, error) {
transitionDate, e := time.Parse(defaultILMDateFormat, transitionDateStr)
if e != nil {
return lifecycle.ExpirationDate{}, e
}
return lifecycle.ExpirationDate{Time: transitionDate}, nil
}
func parseTransitionDays(transitionDaysStr string) (lifecycle.ExpirationDays, error) {
transitionDays, e := strconv.Atoi(transitionDaysStr)
if e != nil {
return lifecycle.ExpirationDays(0), e
}
return lifecycle.ExpirationDays(transitionDays), nil
}
func parseTransition(storageClass, transitionDateStr, transitionDaysStr *string) (lifecycle.Transition, error) {
var transition lifecycle.Transition
if transitionDateStr != nil {
transitionDate, err := parseTransitionDate(*transitionDateStr)
if err != nil {
return lifecycle.Transition{}, err
}
transition.Date = transitionDate
}
if transitionDaysStr != nil {
transitionDays, err := parseTransitionDays(*transitionDaysStr)
if err != nil {
return lifecycle.Transition{}, err
}
transition.Days = transitionDays
}
if storageClass != nil {
transition.StorageClass = *storageClass
}
return transition, nil
}
func parseExpiryDate(expiryDateStr string) (lifecycle.ExpirationDate, error) {
date, e := time.Parse(defaultILMDateFormat, expiryDateStr)
if e != nil {
return lifecycle.ExpirationDate{}, e
}
if date.IsZero() {
return lifecycle.ExpirationDate{}, errors.New("expiration date cannot be set to zero")
}
return lifecycle.ExpirationDate{Time: date}, nil
}
func parseExpiryDays(expiryDayStr string) (lifecycle.ExpirationDays, error) {
days, e := strconv.Atoi(expiryDayStr)
if e != nil {
return lifecycle.ExpirationDays(0), e
}
if days == 0 {
return lifecycle.ExpirationDays(0), errZeroExpiryDays
}
return lifecycle.ExpirationDays(days), nil
}
func parseExpiry(expiryDate, expiryDays *string, expiredDeleteMarker *bool) (lfcExp lifecycle.Expiration, err error) {
if expiryDate != nil {
date, err := parseExpiryDate(*expiryDate)
if err != nil {
return lifecycle.Expiration{}, err
}
lfcExp.Date = date
}
if expiryDays != nil {
days, err := parseExpiryDays(*expiryDays)
if err != nil {
return lifecycle.Expiration{}, err
}
lfcExp.Days = days
}
if expiredDeleteMarker != nil {
lfcExp.DeleteMarker = lifecycle.ExpireDeleteMarker(*expiredDeleteMarker)
}
return lfcExp, nil
}
func parseAllVersionsExpiry(expiryDays *string, deleteMarker *bool) (lfcAllVersExp lifecycle.AllVersionsExpiration, err error) {
if expiryDays != nil {
days, err := parseExpiryDays(*expiryDays)
if err != nil {
return lifecycle.AllVersionsExpiration{}, err
}
lfcAllVersExp.Days = int(days)
}
if deleteMarker != nil {
lfcAllVersExp.DeleteMarker = lifecycle.ExpireDeleteMarker(*deleteMarker)
}
return lfcAllVersExp, nil
}
|