blob: 7ca20289bf7ca5ce7aaf33491ac990f9bacf8be6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# frozen_string_literal: true
module Clusters
module Aws
class AuthorizeRoleService
attr_reader :user
Response = Struct.new(:status, :body)
ERRORS = [
ActiveRecord::RecordInvalid,
ActiveRecord::RecordNotFound,
Clusters::Aws::FetchCredentialsService::MissingRoleError,
::Aws::Errors::MissingCredentialsError,
::Aws::STS::Errors::ServiceError
].freeze
def initialize(user, params:)
@user = user
@role_arn = params[:role_arn]
@region = params[:region]
end
def execute
ensure_role_exists!
update_role_arn!
Response.new(:ok, credentials)
rescue *ERRORS => e
Gitlab::ErrorTracking.track_exception(e)
Response.new(:unprocessable_entity, response_details(e))
end
private
attr_reader :role, :role_arn, :region
def ensure_role_exists!
@role = ::Aws::Role.find_by_user_id!(user.id)
end
def update_role_arn!
role.update!(role_arn: role_arn, region: region)
end
def credentials
Clusters::Aws::FetchCredentialsService.new(role).execute
end
def response_details(exception)
message =
case exception
when ::Aws::STS::Errors::AccessDenied
_("Access denied: %{error}") % { error: exception.message }
when ::Aws::STS::Errors::ServiceError
_("AWS service error: %{error}") % { error: exception.message }
when ActiveRecord::RecordNotFound
_("Error: Unable to find AWS role for current user")
when ActiveRecord::RecordInvalid
exception.message
when Clusters::Aws::FetchCredentialsService::MissingRoleError
_("Error: No AWS provision role found for user")
when ::Aws::Errors::MissingCredentialsError
_("Error: No AWS credentials were supplied")
else
_('An error occurred while authorizing your role')
end
{ message: message }.compact
end
end
end
end
|