unmarshal both LegalHold and ObjectLockLegalHold XML types (#11921)

Because of silly AWS S3 behavior we to handle both types.

fixes #11920
This commit is contained in:
Harshavardhana 2021-03-29 23:52:30 -07:00
parent 8b76ba8d5d
commit 94018588fe
3 changed files with 57 additions and 2 deletions

View File

@ -18,6 +18,7 @@ package lifecycle
import ( import (
"encoding/xml" "encoding/xml"
"fmt"
"io" "io"
"strings" "strings"
"time" "time"
@ -71,7 +72,8 @@ func (lc *Lifecycle) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err e
switch start.Name.Local { switch start.Name.Local {
case "LifecycleConfiguration", "BucketLifecycleConfiguration": case "LifecycleConfiguration", "BucketLifecycleConfiguration":
default: default:
return errUnknownXMLTag return xml.UnmarshalError(fmt.Sprintf("expected element type <LifecycleConfiguration>/<BucketLifecycleConfiguration> but have <%s>",
start.Name.Local))
} }
for { for {
// Read tokens from the XML document in a stream. // Read tokens from the XML document in a stream.
@ -93,7 +95,7 @@ func (lc *Lifecycle) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err e
} }
lc.Rules = append(lc.Rules, r) lc.Rules = append(lc.Rules, r)
default: default:
return errUnknownXMLTag return xml.UnmarshalError(fmt.Sprintf("expected element type <Rule> but have <%s>", se.Name.Local))
} }
} }
} }

View File

@ -489,6 +489,41 @@ type ObjectLegalHold struct {
Status LegalHoldStatus `xml:"Status,omitempty"` Status LegalHoldStatus `xml:"Status,omitempty"`
} }
// UnmarshalXML - decodes XML data.
func (l *ObjectLegalHold) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
switch start.Name.Local {
case "LegalHold", "ObjectLockLegalHold":
default:
return xml.UnmarshalError(fmt.Sprintf("expected element type <LegalHold>/<ObjectLockLegalHold> but have <%s>",
start.Name.Local))
}
for {
// Read tokens from the XML document in a stream.
t, err := d.Token()
if err != nil {
if err == io.EOF {
break
}
return err
}
switch se := t.(type) {
case xml.StartElement:
switch se.Name.Local {
case "Status":
var st LegalHoldStatus
if err = d.DecodeElement(&st, &se); err != nil {
return err
}
l.Status = st
default:
return xml.UnmarshalError(fmt.Sprintf("expected element type <Status> but have <%s>", se.Name.Local))
}
}
}
return nil
}
// IsEmpty returns true if struct is empty // IsEmpty returns true if struct is empty
func (l *ObjectLegalHold) IsEmpty() bool { func (l *ObjectLegalHold) IsEmpty() bool {
return !l.Status.Valid() return !l.Status.Valid()

View File

@ -18,6 +18,7 @@ package lock
import ( import (
"encoding/xml" "encoding/xml"
"errors"
"fmt" "fmt"
"net/http" "net/http"
"reflect" "reflect"
@ -467,6 +468,23 @@ func TestParseObjectLegalHold(t *testing.T) {
expectedErr: nil, expectedErr: nil,
expectErr: false, expectErr: false,
}, },
{
value: `<?xml version="1.0" encoding="UTF-8"?><ObjectLockLegalHold xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Status>ON</Status></ObjectLockLegalHold>`,
expectedErr: nil,
expectErr: false,
},
// invalid Status key
{
value: `<?xml version="1.0" encoding="UTF-8"?><ObjectLockLegalHold xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><MyStatus>ON</MyStatus></ObjectLockLegalHold>`,
expectedErr: errors.New("expected element type <Status> but have <MyStatus>"),
expectErr: true,
},
// invalid XML attr
{
value: `<?xml version="1.0" encoding="UTF-8"?><UnknownLegalHold xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Status>ON</Status></UnknownLegalHold>`,
expectedErr: errors.New("expected element type <LegalHold>/<ObjectLockLegalHold> but have <UnknownLegalHold>"),
expectErr: true,
},
{ {
value: `<?xml version="1.0" encoding="UTF-8"?><LegalHold xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Status>On</Status></LegalHold>`, value: `<?xml version="1.0" encoding="UTF-8"?><LegalHold xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Status>On</Status></LegalHold>`,
expectedErr: ErrMalformedXML, expectedErr: ErrMalformedXML,