marshal retention XML in expected format (#15821)

This commit is contained in:
Harshavardhana 2022-10-08 13:11:00 -07:00 committed by GitHub
parent 64cf887b28
commit 3c4ef4338f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 26 deletions

View File

@ -309,9 +309,12 @@ func (rDate *RetentionDate) UnmarshalXML(d *xml.Decoder, startElement xml.StartE
// While AWS documentation mentions that the date specified
// must be present in ISO 8601 format, in reality they allow
// users to provide RFC 3339 compliant dates.
retDate, err := time.Parse(time.RFC3339, dateStr)
retDate, err := time.Parse(iso8601TimeFormat, dateStr)
if err != nil {
return ErrInvalidRetentionDate
retDate, err = time.Parse(time.RFC3339, dateStr)
if err != nil {
return ErrInvalidRetentionDate
}
}
*rDate = RetentionDate{retDate}
@ -321,10 +324,10 @@ func (rDate *RetentionDate) UnmarshalXML(d *xml.Decoder, startElement xml.StartE
// MarshalXML encodes expiration date if it is non-zero and encodes
// empty string otherwise
func (rDate *RetentionDate) MarshalXML(e *xml.Encoder, startElement xml.StartElement) error {
if *rDate == (RetentionDate{time.Time{}}) {
if rDate.IsZero() {
return nil
}
return e.EncodeElement(rDate.Format(time.RFC3339), startElement)
return e.EncodeElement(rDate.Format(iso8601TimeFormat), startElement)
}
// ObjectRetention specified in
@ -414,9 +417,12 @@ func ParseObjectLockRetentionHeaders(h http.Header) (rmode RetMode, r RetentionD
// While AWS documentation mentions that the date specified
// must be present in ISO 8601 format, in reality they allow
// users to provide RFC 3339 compliant dates.
retDate, err = time.Parse(time.RFC3339, dateStr)
retDate, err = time.Parse(iso8601TimeFormat, dateStr)
if err != nil {
return rmode, r, ErrInvalidRetentionDate
retDate, err = time.Parse(time.RFC3339, dateStr)
if err != nil {
return rmode, r, ErrInvalidRetentionDate
}
}
_, replReq := h[textproto.CanonicalMIMEHeaderKey(xhttp.MinIOSourceReplicationRequest)]
@ -459,6 +465,9 @@ func GetObjectRetentionMeta(meta map[string]string) ObjectRetention {
if t, e := time.Parse(iso8601TimeFormat, tillStr); e == nil {
retainTill = RetentionDate{t.UTC()}
}
if t, e := time.Parse(time.RFC3339, tillStr); e == nil {
retainTill = RetentionDate{t.UTC()}
}
}
return ObjectRetention{XMLNS: "http://s3.amazonaws.com/doc/2006-03-01/", Mode: mode, RetainUntilDate: retainTill}
}

View File

@ -174,17 +174,20 @@ func TestParseObjectLockConfig(t *testing.T) {
},
}
for _, tt := range tests {
_, err := ParseObjectLockConfig(strings.NewReader(tt.value))
//nolint:gocritic
if tt.expectedErr == nil {
if err != nil {
t.Fatalf("error: expected = <nil>, got = %v", err)
tt := tt
t.Run("", func(t *testing.T) {
_, err := ParseObjectLockConfig(strings.NewReader(tt.value))
//nolint:gocritic
if tt.expectedErr == nil {
if err != nil {
t.Fatalf("error: expected = <nil>, got = %v", err)
}
} else if err == nil {
t.Fatalf("error: expected = %v, got = <nil>", tt.expectedErr)
} else if tt.expectedErr.Error() != err.Error() {
t.Fatalf("error: expected = %v, got = %v", tt.expectedErr, err)
}
} else if err == nil {
t.Fatalf("error: expected = %v, got = <nil>", tt.expectedErr)
} else if tt.expectedErr.Error() != err.Error() {
t.Fatalf("error: expected = %v, got = %v", tt.expectedErr, err)
}
})
}
}
@ -209,19 +212,27 @@ func TestParseObjectRetention(t *testing.T) {
expectedErr: nil,
expectErr: false,
},
{
value: `<?xml version="1.0" encoding="UTF-8"?><Retention xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Mode>GOVERNANCE</Mode><RetainUntilDate>2057-01-02T15:04:05.000Z</RetainUntilDate></Retention>`,
expectedErr: nil,
expectErr: false,
},
}
for _, tt := range tests {
_, err := ParseObjectRetention(strings.NewReader(tt.value))
//nolint:gocritic
if tt.expectedErr == nil {
if err != nil {
t.Fatalf("error: expected = <nil>, got = %v", err)
tt := tt
t.Run("", func(t *testing.T) {
_, err := ParseObjectRetention(strings.NewReader(tt.value))
//nolint:gocritic
if tt.expectedErr == nil {
if err != nil {
t.Fatalf("error: expected = <nil>, got = %v", err)
}
} else if err == nil {
t.Fatalf("error: expected = %v, got = <nil>", tt.expectedErr)
} else if tt.expectedErr.Error() != err.Error() {
t.Fatalf("error: expected = %v, got = %v", tt.expectedErr, err)
}
} else if err == nil {
t.Fatalf("error: expected = %v, got = <nil>", tt.expectedErr)
} else if tt.expectedErr.Error() != err.Error() {
t.Fatalf("error: expected = %v, got = %v", tt.expectedErr, err)
}
})
}
}
@ -367,6 +378,13 @@ func TestParseObjectLockRetentionHeaders(t *testing.T) {
},
expectedErr: nil,
},
{
header: http.Header{
xhttp.AmzObjectLockMode: []string{"governance"},
xhttp.AmzObjectLockRetainUntilDate: []string{"2087-01-02T15:04:05.000Z"},
},
expectedErr: nil,
},
}
for i, tt := range tests {