BigW Consortium Gitlab

runner.rb 1.55 KB
Newer Older
1 2
module API
  module Helpers
3
    module Runner
Tomasz Maczukin committed
4
      JOB_TOKEN_HEADER = 'HTTP_JOB_TOKEN'.freeze
5
      JOB_TOKEN_PARAM = :token
6

7
      def runner_registration_token_valid?
Tomasz Maczukin committed
8
        ActiveSupport::SecurityUtils.variable_size_secure_compare(params[:token],
9
                                                                  Gitlab::CurrentSettings.runners_registration_token)
10 11 12 13
      end

      def get_runner_version_from_params
        return unless params['info'].present?
14

15 16 17 18 19
        attributes_for_keys(%w(name version revision platform architecture), params['info'])
      end

      def authenticate_runner!
        forbidden! unless current_runner
20

21
        current_runner.update_cached_info(get_runner_version_from_params)
22 23 24 25 26
      end

      def current_runner
        @runner ||= ::Ci::Runner.find_by_token(params[:token].to_s)
      end
27

Tomasz Maczukin committed
28 29 30 31 32
      def validate_job!(job)
        not_found! unless job

        yield if block_given?

33 34
        project = job.project
        forbidden!('Project has been deleted!') if project.nil? || project.pending_delete?
Tomasz Maczukin committed
35 36
        forbidden!('Job has been erased!') if job.erased?
      end
37

38 39 40
      def authenticate_job!
        job = Ci::Build.find_by_id(params[:id])

41 42 43
        validate_job!(job) do
          forbidden! unless job_token_valid?(job)
        end
44 45

        job
46 47 48 49 50 51
      end

      def job_token_valid?(job)
        token = (params[JOB_TOKEN_PARAM] || env[JOB_TOKEN_HEADER]).to_s
        token && job.valid_token?(token)
      end
52 53

      def max_artifacts_size
54
        Gitlab::CurrentSettings.max_artifacts_size.megabytes.to_i
55
      end
56 57
    end
  end
Tomasz Maczukin committed
58
end