helpers.rb 2.58 KB
Newer Older
1 2 3
module Ci
  module API
    module Helpers
Douwe Maan's avatar
Douwe Maan committed
4
      BUILD_TOKEN_HEADER = "HTTP_BUILD_TOKEN".freeze
5
      BUILD_TOKEN_PARAM = :token
6
      UPDATE_RUNNER_EVERY = 10 * 60
7

8
      def authenticate_runners!
9
        forbidden! unless runner_registration_token_valid?
10 11 12 13 14 15
      end

      def authenticate_runner!
        forbidden! unless current_runner
      end

16 17 18
      def authenticate_build!
        build = Ci::Build.find_by_id(params[:id])

19 20 21
        validate_build!(build) do
          forbidden! unless build_token_valid?(build)
        end
22 23

        build
24 25 26
      end

      def validate_build!(build)
27
        not_found! unless build
28 29 30

        yield if block_given?

31 32
        project = build.project
        forbidden!('Project has been deleted!') if project.nil? || project.pending_delete?
33
        forbidden!('Build has been erased!') if build.erased?
34 35
      end

36
      def runner_registration_token_valid?
37 38 39 40 41
        ActiveSupport::SecurityUtils.variable_size_secure_compare(
          params[:token],
          current_application_settings.runners_registration_token)
      end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
42
      def build_token_valid?(build)
43 44 45 46
        token = (params[BUILD_TOKEN_PARAM] || env[BUILD_TOKEN_HEADER]).to_s

        # We require to also check `runners_token` to maintain compatibility with old version of runners
        token && (build.valid_token?(token) || build.project.valid_runners_token?(token))
47 48
      end

49
      def update_runner_info
50
        return unless update_runner?
51 52 53 54

        current_runner.contacted_at = Time.now
        current_runner.assign_attributes(get_runner_version_from_params)
        current_runner.save if current_runner.changed?
55 56
      end

57 58 59 60 61 62 63 64 65 66
      def update_runner?
        # Use a random threshold to prevent beating DB updates.
        # It generates a distribution between [40m, 80m].
        #
        contacted_at_max_age = UPDATE_RUNNER_EVERY + Random.rand(UPDATE_RUNNER_EVERY)

        current_runner.contacted_at.nil? ||
          (Time.now - current_runner.contacted_at) >= contacted_at_max_age
      end

67
      def build_not_found!
68
        if headers['User-Agent'].to_s =~ /gitlab-ci-multi-runner \d+\.\d+\.\d+(~beta\.\d+\.g[0-9a-f]+)? /
69 70 71 72 73 74
          no_content!
        else
          not_found!
        end
      end

Valery Sizov's avatar
Valery Sizov committed
75 76 77 78
      def current_runner
        @runner ||= Runner.find_by_token(params[:token].to_s)
      end

79
      def get_runner_version_from_params
80
        return unless params["info"].present?
Douwe Maan's avatar
Douwe Maan committed
81
        attributes_for_keys(%w(name version revision platform architecture), params["info"])
82 83
      end

84 85 86
      def max_artifacts_size
        current_application_settings.max_artifacts_size.megabytes.to_i
      end
87 88 89
    end
  end
end