summaryrefslogtreecommitdiff
path: root/app/services/ci/runners/register_runner_service.rb
blob: ae9b8bc8a161239eddf71c959fdeba9146d79d18 (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
# frozen_string_literal: true

module Ci
  module Runners
    class RegisterRunnerService
      def execute(registration_token, attributes)
        runner_type_attrs = extract_runner_type_attrs(registration_token)

        return ServiceResponse.error(message: 'invalid token supplied', http_status: :forbidden) unless runner_type_attrs

        runner = ::Ci::Runner.new(attributes.merge(runner_type_attrs))

        Ci::BulkInsertableTags.with_bulk_insert_tags do
          Ci::Runner.transaction do
            if runner.save
              Gitlab::Ci::Tags::BulkInsert.bulk_insert_tags!([runner])
            else
              raise ActiveRecord::Rollback
            end
          end
        end

        ServiceResponse.success(payload: { runner: runner })
      end

      private

      def extract_runner_type_attrs(registration_token)
        @attrs_from_token ||= check_token(registration_token)

        return unless @attrs_from_token

        attrs = @attrs_from_token.clone
        case attrs[:runner_type]
        when :project_type
          attrs[:projects] = [attrs.delete(:scope)]
        when :group_type
          attrs[:groups] = [attrs.delete(:scope)]
        end

        attrs
      end

      def check_token(registration_token)
        if runner_registration_token_valid?(registration_token)
          # Create shared runner. Requires admin access
          { runner_type: :instance_type }
        elsif runner_registrar_valid?('project') && project = ::Project.find_by_runners_token(registration_token)
          # Create a specific runner for the project
          { runner_type: :project_type, scope: project }
        elsif runner_registrar_valid?('group') && group = ::Group.find_by_runners_token(registration_token)
          # Create a specific runner for the group
          { runner_type: :group_type, scope: group }
        end
      end

      def runner_registration_token_valid?(registration_token)
        ActiveSupport::SecurityUtils.secure_compare(registration_token, Gitlab::CurrentSettings.runners_registration_token)
      end

      def runner_registrar_valid?(type)
        Feature.disabled?(:runner_registration_control) || Gitlab::CurrentSettings.valid_runner_registrars.include?(type)
      end

      def token_scope
        @attrs_from_token[:scope]
      end
    end
  end
end

Ci::Runners::RegisterRunnerService.prepend_mod