parent
6c62b1a2ea
commit
3773874cd3
@ -1,53 +0,0 @@ |
||||
/* |
||||
* MinIO Cloud Storage, (C) 2020 MinIO, Inc. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package tagging |
||||
|
||||
import ( |
||||
"fmt" |
||||
) |
||||
|
||||
// Error is the generic type for any error happening during tag
|
||||
// parsing.
|
||||
type Error struct { |
||||
err error |
||||
code string |
||||
} |
||||
|
||||
// Errorf - formats according to a format specifier and returns
|
||||
// the string as a value that satisfies error of type tagging.Error
|
||||
func Errorf(format, code string, a ...interface{}) error { |
||||
return Error{err: fmt.Errorf(format, a...), code: code} |
||||
} |
||||
|
||||
// Unwrap the internal error.
|
||||
func (e Error) Unwrap() error { return e.err } |
||||
|
||||
// Error 'error' compatible method.
|
||||
func (e Error) Error() string { |
||||
if e.err == nil { |
||||
return "tagging: cause <nil>" |
||||
} |
||||
return e.err.Error() |
||||
} |
||||
|
||||
// Code returns appropriate error code.
|
||||
func (e Error) Code() string { |
||||
if e.code == "" { |
||||
return "BadRequest" |
||||
} |
||||
return e.code |
||||
} |
@ -1,82 +0,0 @@ |
||||
/* |
||||
* MinIO Cloud Storage, (C) 2020 MinIO, Inc. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package tagging |
||||
|
||||
import ( |
||||
"encoding/xml" |
||||
"strings" |
||||
"unicode/utf8" |
||||
) |
||||
|
||||
// Tag - single tag
|
||||
type Tag struct { |
||||
XMLName xml.Name `xml:"Tag"` |
||||
Key string `xml:"Key,omitempty"` |
||||
Value string `xml:"Value,omitempty"` |
||||
} |
||||
|
||||
// Validate - validates the tag element
|
||||
func (t Tag) Validate() error { |
||||
if err := t.validateKey(); err != nil { |
||||
return err |
||||
} |
||||
if err := t.validateValue(); err != nil { |
||||
return err |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// validateKey - checks if key is valid or not.
|
||||
func (t Tag) validateKey() error { |
||||
// cannot be longer than maxTagKeyLength characters
|
||||
if utf8.RuneCountInString(t.Key) > maxTagKeyLength { |
||||
return ErrInvalidTagKey |
||||
} |
||||
// cannot be empty
|
||||
if len(t.Key) == 0 { |
||||
return ErrInvalidTagKey |
||||
} |
||||
// Tag key shouldn't have "&"
|
||||
if strings.Contains(t.Key, "&") { |
||||
return ErrInvalidTagKey |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// validateValue - checks if value is valid or not.
|
||||
func (t Tag) validateValue() error { |
||||
// cannot be longer than maxTagValueLength characters
|
||||
if utf8.RuneCountInString(t.Value) > maxTagValueLength { |
||||
return ErrInvalidTagValue |
||||
} |
||||
// Tag value shouldn't have "&"
|
||||
if strings.Contains(t.Value, "&") { |
||||
return ErrInvalidTagValue |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// IsEmpty - checks if tag is empty or not
|
||||
func (t Tag) IsEmpty() bool { |
||||
return t.Key == "" && t.Value == "" |
||||
} |
||||
|
||||
// String - returns a string in format "tag1=value1" for the
|
||||
// current Tag
|
||||
func (t Tag) String() string { |
||||
return t.Key + "=" + t.Value |
||||
} |
@ -1,112 +0,0 @@ |
||||
/* |
||||
* MinIO Cloud Storage, (C) 2020 MinIO, Inc. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package tagging |
||||
|
||||
import ( |
||||
"bytes" |
||||
"encoding/xml" |
||||
"io" |
||||
"net/url" |
||||
) |
||||
|
||||
// S3 API limits for tags
|
||||
// Ref: https://docs.aws.amazon.com/AmazonS3/latest/dev/object-tagging.html
|
||||
const ( |
||||
maxTags = 10 |
||||
maxTagKeyLength = 128 |
||||
maxTagValueLength = 256 |
||||
) |
||||
|
||||
// errors returned by tagging package
|
||||
var ( |
||||
ErrTooManyTags = Errorf("Object tags cannot be greater than 10", "BadRequest") |
||||
ErrInvalidTagKey = Errorf("The TagKey you have provided is invalid", "InvalidTag") |
||||
ErrInvalidTagValue = Errorf("The TagValue you have provided is invalid", "InvalidTag") |
||||
ErrInvalidTag = Errorf("Cannot provide multiple Tags with the same key", "InvalidTag") |
||||
) |
||||
|
||||
// Tagging - object tagging interface
|
||||
type Tagging struct { |
||||
XMLName xml.Name `xml:"Tagging"` |
||||
TagSet TagSet `xml:"TagSet"` |
||||
} |
||||
|
||||
// Validate - validates the tagging configuration
|
||||
func (t Tagging) Validate() error { |
||||
// Tagging can't have more than 10 tags
|
||||
if len(t.TagSet.Tags) > maxTags { |
||||
return ErrTooManyTags |
||||
} |
||||
// Validate all the rules in the tagging config
|
||||
for _, ts := range t.TagSet.Tags { |
||||
if err := ts.Validate(); err != nil { |
||||
return err |
||||
} |
||||
} |
||||
if t.TagSet.ContainsDuplicateTag() { |
||||
return ErrInvalidTag |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// String - returns a string in format "tag1=value1&tag2=value2" with all the
|
||||
// tags in this Tagging Struct
|
||||
func (t Tagging) String() string { |
||||
var buf bytes.Buffer |
||||
for _, tag := range t.TagSet.Tags { |
||||
if buf.Len() > 0 { |
||||
buf.WriteString("&") |
||||
} |
||||
buf.WriteString(tag.String()) |
||||
} |
||||
return buf.String() |
||||
} |
||||
|
||||
// FromString - returns a Tagging struct when given a string in format
|
||||
// "tag1=value1&tag2=value2"
|
||||
func FromString(tagStr string) (Tagging, error) { |
||||
tags, err := url.ParseQuery(tagStr) |
||||
if err != nil { |
||||
return Tagging{}, err |
||||
} |
||||
var idx = 0 |
||||
parsedTags := make([]Tag, len(tags)) |
||||
for k := range tags { |
||||
parsedTags[idx].Key = k |
||||
parsedTags[idx].Value = tags.Get(k) |
||||
idx++ |
||||
} |
||||
return Tagging{ |
||||
TagSet: TagSet{ |
||||
Tags: parsedTags, |
||||
}, |
||||
}, nil |
||||
} |
||||
|
||||
// ParseTagging - parses incoming xml data in given reader
|
||||
// into Tagging interface. After parsing, also validates the
|
||||
// parsed fields based on S3 API constraints.
|
||||
func ParseTagging(reader io.Reader) (*Tagging, error) { |
||||
var t Tagging |
||||
if err := xml.NewDecoder(reader).Decode(&t); err != nil { |
||||
return nil, err |
||||
} |
||||
if err := t.Validate(); err != nil { |
||||
return nil, err |
||||
} |
||||
return &t, nil |
||||
} |
@ -1,41 +0,0 @@ |
||||
/* |
||||
* MinIO Cloud Storage, (C) 2020 MinIO, Inc. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package tagging |
||||
|
||||
import ( |
||||
"encoding/xml" |
||||
) |
||||
|
||||
// TagSet - Set of tags under Tagging
|
||||
type TagSet struct { |
||||
XMLName xml.Name `xml:"TagSet"` |
||||
Tags []Tag `xml:"Tag"` |
||||
} |
||||
|
||||
// ContainsDuplicateTag - returns true if duplicate keys are present in TagSet
|
||||
func (t TagSet) ContainsDuplicateTag() bool { |
||||
x := make(map[string]struct{}, len(t.Tags)) |
||||
|
||||
for _, t := range t.Tags { |
||||
if _, has := x[t.Key]; has { |
||||
return true |
||||
} |
||||
x[t.Key] = struct{}{} |
||||
} |
||||
|
||||
return false |
||||
} |
Loading…
Reference in new issue