2020-01-01 13:55:28 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Clusters
|
|
|
|
module Aws
|
|
|
|
class AuthorizeRoleService
|
|
|
|
attr_reader :user
|
|
|
|
|
|
|
|
Response = Struct.new(:status, :body)
|
|
|
|
|
|
|
|
ERRORS = [
|
|
|
|
ActiveRecord::RecordInvalid,
|
2020-09-03 11:15:55 +05:30
|
|
|
ActiveRecord::RecordNotFound,
|
2020-01-01 13:55:28 +05:30
|
|
|
Clusters::Aws::FetchCredentialsService::MissingRoleError,
|
|
|
|
::Aws::Errors::MissingCredentialsError,
|
|
|
|
::Aws::STS::Errors::ServiceError
|
|
|
|
].freeze
|
|
|
|
|
|
|
|
def initialize(user, params:)
|
|
|
|
@user = user
|
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2020-09-03 11:15:55 +05:30
|
|
|
ensure_role_exists!
|
|
|
|
update_role_arn!
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
Response.new(:ok, credentials)
|
|
|
|
rescue *ERRORS
|
|
|
|
Response.new(:unprocessable_entity, {})
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :role, :params
|
|
|
|
|
2020-09-03 11:15:55 +05:30
|
|
|
def ensure_role_exists!
|
|
|
|
@role = ::Aws::Role.find_by_user_id!(user.id)
|
|
|
|
end
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2020-09-03 11:15:55 +05:30
|
|
|
def update_role_arn!
|
|
|
|
role.update!(params)
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def credentials
|
|
|
|
Clusters::Aws::FetchCredentialsService.new(role).execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|