iamapi_handlers.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package iamapi
  2. import (
  3. "net/http"
  4. "github.com/aws/aws-sdk-go/service/iam"
  5. "github.com/seaweedfs/seaweedfs/weed/glog"
  6. "github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
  7. )
  8. func newErrorResponse(errCode string, errMsg string) ErrorResponse {
  9. errorResp := ErrorResponse{}
  10. errorResp.Error.Type = "Sender"
  11. errorResp.Error.Code = &errCode
  12. errorResp.Error.Message = &errMsg
  13. return errorResp
  14. }
  15. func writeIamErrorResponse(w http.ResponseWriter, r *http.Request, iamError *IamError) {
  16. if iamError == nil {
  17. // Do nothing if there is no error
  18. glog.Errorf("No error found")
  19. return
  20. }
  21. errCode := iamError.Code
  22. errMsg := iamError.Error.Error()
  23. glog.Errorf("Response %+v", errMsg)
  24. errorResp := newErrorResponse(errCode, errMsg)
  25. internalErrorResponse := newErrorResponse(iam.ErrCodeServiceFailureException, "Internal server error")
  26. switch errCode {
  27. case iam.ErrCodeNoSuchEntityException:
  28. s3err.WriteXMLResponse(w, r, http.StatusNotFound, errorResp)
  29. case iam.ErrCodeMalformedPolicyDocumentException:
  30. s3err.WriteXMLResponse(w, r, http.StatusBadRequest, errorResp)
  31. case iam.ErrCodeServiceFailureException:
  32. // We do not want to expose internal server error to the client
  33. s3err.WriteXMLResponse(w, r, http.StatusInternalServerError, internalErrorResponse)
  34. default:
  35. s3err.WriteXMLResponse(w, r, http.StatusInternalServerError, internalErrorResponse)
  36. }
  37. }