2022-01-06 01:10:35 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module API
|
|
|
|
# This internal endpoint receives webhooks sent from the MailRoom component.
|
|
|
|
# This component constantly listens to configured email accounts. When it
|
|
|
|
# finds any incoming email or service desk email, it makes a POST request to
|
|
|
|
# this endpoint. The target mailbox type is indicated in the request path.
|
|
|
|
# The email raw content is attached to the request body.
|
|
|
|
#
|
|
|
|
# For more information, please visit https://gitlab.com/groups/gitlab-com/gl-infra/-/epics/644
|
|
|
|
module Internal
|
|
|
|
class MailRoom < ::API::Base
|
|
|
|
feature_category :service_desk
|
|
|
|
|
|
|
|
before do
|
|
|
|
authenticate_gitlab_mailroom_request!
|
|
|
|
end
|
|
|
|
|
|
|
|
helpers do
|
|
|
|
def authenticate_gitlab_mailroom_request!
|
|
|
|
unauthorized! unless Gitlab::MailRoom::Authenticator.verify_api_request(headers, params[:mailbox_type])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
namespace 'internal' do
|
|
|
|
namespace 'mail_room' do
|
|
|
|
params do
|
|
|
|
requires :mailbox_type, type: String,
|
|
|
|
desc: 'The destination mailbox type configuration. Must either be incoming_email or service_desk_email'
|
|
|
|
end
|
|
|
|
post "/*mailbox_type" do
|
|
|
|
worker = Gitlab::MailRoom.worker_for(params[:mailbox_type])
|
2022-01-13 07:14:38 -05:00
|
|
|
raw = request.body.read
|
2022-01-06 01:10:35 -05:00
|
|
|
begin
|
2022-01-13 07:14:38 -05:00
|
|
|
worker.perform_async(raw)
|
|
|
|
rescue Gitlab::SidekiqMiddleware::SizeLimiter::ExceedLimitError
|
|
|
|
receiver = Gitlab::Email::Receiver.new(raw)
|
|
|
|
reason = Gitlab::Email::FailureHandler.handle(receiver, Gitlab::Email::EmailTooLarge.new)
|
|
|
|
|
2022-01-06 01:10:35 -05:00
|
|
|
status 400
|
2022-01-13 07:14:38 -05:00
|
|
|
break { success: false, message: reason }
|
2022-01-06 01:10:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
status 200
|
|
|
|
{ success: true }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|