Fix notification unmarshalling, unmarshal only when size is > 0 (#5087)

Fixes #5085
master
Harshavardhana 7 years ago committed by Dee Koder
parent f598f4fd1b
commit d82a1da511
  1. 4
      cmd/bucket-notification-handlers.go
  2. 28
      cmd/event-notifier.go

@ -63,13 +63,13 @@ func (api objectAPIHandlers) GetBucketNotificationHandler(w http.ResponseWriter,
// Attempt to successfully load notification config. // Attempt to successfully load notification config.
nConfig, err := loadNotificationConfig(bucket, objAPI) nConfig, err := loadNotificationConfig(bucket, objAPI)
if err != nil && err != errNoSuchNotifications { if err != nil && errorCause(err) != errNoSuchNotifications {
errorIf(err, "Unable to read notification configuration.") errorIf(err, "Unable to read notification configuration.")
writeErrorResponse(w, toAPIErrorCode(err), r.URL) writeErrorResponse(w, toAPIErrorCode(err), r.URL)
return return
} }
// For no notifications we write a dummy XML. // For no notifications we write a dummy XML.
if err == errNoSuchNotifications { if errorCause(err) == errNoSuchNotifications {
// Complies with the s3 behavior in this regard. // Complies with the s3 behavior in this regard.
nConfig = &notificationConfig{} nConfig = &notificationConfig{}
} }

@ -377,18 +377,24 @@ func loadNotificationConfig(bucket string, objAPI ObjectLayer) (*notificationCon
// 'errNoSuchNotifications'. This is default when no // 'errNoSuchNotifications'. This is default when no
// bucket notifications are found on the bucket. // bucket notifications are found on the bucket.
if isErrObjectNotFound(err) || isErrIncompleteBody(err) { if isErrObjectNotFound(err) || isErrIncompleteBody(err) {
return nil, errNoSuchNotifications return nil, traceError(errNoSuchNotifications)
} }
errorIf(err, "Unable to load bucket-notification for bucket %s", bucket) errorIf(err, "Unable to load bucket-notification for bucket %s", bucket)
// Returns error for other errors. // Returns error for other errors.
return nil, err return nil, err
} }
// if `notifications.xml` is empty we should return NoSuchNotifications.
if buffer.Len() == 0 {
return nil, traceError(errNoSuchNotifications)
}
// Unmarshal notification bytes. // Unmarshal notification bytes.
notificationConfigBytes := buffer.Bytes() notificationConfigBytes := buffer.Bytes()
notificationCfg := &notificationConfig{} notificationCfg := &notificationConfig{}
if err = xml.Unmarshal(notificationConfigBytes, &notificationCfg); err != nil { // Unmarshal notification bytes only if we read data.
return nil, err if err = xml.Unmarshal(notificationConfigBytes, notificationCfg); err != nil {
return nil, traceError(err)
} }
// Return success. // Return success.
@ -418,23 +424,27 @@ func loadListenerConfig(bucket string, objAPI ObjectLayer) ([]listenerConfig, er
var buffer bytes.Buffer var buffer bytes.Buffer
err := objAPI.GetObject(minioMetaBucket, lcPath, 0, -1, &buffer) err := objAPI.GetObject(minioMetaBucket, lcPath, 0, -1, &buffer)
if err != nil { if err != nil {
// 'notification.xml' not found return // 'listener.json' not found return
// 'errNoSuchNotifications'. This is default when no // 'errNoSuchNotifications'. This is default when no
// bucket listeners are found on the bucket. // bucket listeners are found on the bucket
if isErrObjectNotFound(err) { if isErrObjectNotFound(err) || isErrIncompleteBody(err) {
return nil, errNoSuchNotifications return nil, traceError(errNoSuchNotifications)
} }
errorIf(err, "Unable to load bucket-listeners for bucket %s", bucket) errorIf(err, "Unable to load bucket-listeners for bucket %s", bucket)
// Returns error for other errors. // Returns error for other errors.
return nil, err return nil, err
} }
// Unmarshal notification bytes. // if `listener.json` is empty we should return NoSuchNotifications.
if buffer.Len() == 0 {
return nil, traceError(errNoSuchNotifications)
}
var lCfg []listenerConfig var lCfg []listenerConfig
lConfigBytes := buffer.Bytes() lConfigBytes := buffer.Bytes()
if err = json.Unmarshal(lConfigBytes, &lCfg); err != nil { if err = json.Unmarshal(lConfigBytes, &lCfg); err != nil {
errorIf(err, "Unable to unmarshal listener config from JSON.") errorIf(err, "Unable to unmarshal listener config from JSON.")
return nil, err return nil, traceError(err)
} }
// Return success. // Return success.

Loading…
Cancel
Save