Welcome to mirror list, hosted at ThFree Co, Russian Federation.

authorize_role_service.rb « aws « clusters « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6eafce0597e849f43be04692143f367c9504dcbf (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
# frozen_string_literal: true

module Clusters
  module Aws
    class AuthorizeRoleService
      attr_reader :user

      Response = Struct.new(:status, :body)

      ERRORS = [
        ActiveRecord::RecordInvalid,
        Clusters::Aws::FetchCredentialsService::MissingRoleError,
        ::Aws::Errors::MissingCredentialsError,
        ::Aws::STS::Errors::ServiceError
      ].freeze

      def initialize(user, params:)
        @user = user
        @params = params
      end

      def execute
        @role = create_or_update_role!

        Response.new(:ok, credentials)
      rescue *ERRORS
        Response.new(:unprocessable_entity, {})
      end

      private

      attr_reader :role, :params

      def create_or_update_role!
        if role = user.aws_role
          role.update!(params)

          role
        else
          user.create_aws_role!(params)
        end
      end

      def credentials
        Clusters::Aws::FetchCredentialsService.new(role).execute
      end
    end
  end
end