Module: API::Helpers::Runner

Includes:
Gitlab::Utils::StrongMemoize
Defined in:
lib/api/helpers/runner.rb

Constant Summary collapse

JOB_TOKEN_HEADER =
'HTTP_JOB_TOKEN'
JOB_TOKEN_PARAM =
:token

Instance Method Summary collapse

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Instance Method Details

#authenticate_job!(require_running: true) ⇒ Object


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/api/helpers/runner.rb', line 41

def authenticate_job!(require_running: true)
  job = current_job

  not_found! unless job
  forbidden! unless job_token_valid?(job)

  forbidden!('Project has been deleted!') if job.project.nil? || job.project.pending_delete?
  forbidden!('Job has been erased!') if job.erased?

  if require_running
    job_forbidden!(job, 'Job is not running') unless job.running?
  end

  if Gitlab::Ci::Features.job_heartbeats_runner?(job.project)
    job.runner&.heartbeat(get_runner_ip)
  end

  job
end

#authenticate_runner!Object


17
18
19
20
21
22
# File 'lib/api/helpers/runner.rb', line 17

def authenticate_runner!
  forbidden! unless current_runner

  current_runner
    .heartbeat(get_runner_details_from_request)
end

#current_jobObject


61
62
63
64
65
# File 'lib/api/helpers/runner.rb', line 61

def current_job
  strong_memoize(:current_job) do
    ::Ci::Build.find_by_id(params[:id])
  end
end

#current_runnerObject


35
36
37
38
39
# File 'lib/api/helpers/runner.rb', line 35

def current_runner
  strong_memoize(:current_runner) do
    ::Ci::Runner.find_by_token(params[:token].to_s)
  end
end

#get_runner_details_from_requestObject


24
25
26
27
28
29
# File 'lib/api/helpers/runner.rb', line 24

def get_runner_details_from_request
  return get_runner_ip unless params['info'].present?

  attributes_for_keys(%w(name version revision platform architecture), params['info'])
    .merge(get_runner_ip)
end

#get_runner_ipObject


31
32
33
# File 'lib/api/helpers/runner.rb', line 31

def get_runner_ip
  { ip_address: ip_address }
end

#job_forbidden!(job, reason) ⇒ Object


72
73
74
75
# File 'lib/api/helpers/runner.rb', line 72

def job_forbidden!(job, reason)
  header 'Job-Status', job.status
  forbidden!(reason)
end

#job_token_valid?(job) ⇒ Boolean

Returns:

  • (Boolean)

67
68
69
70
# File 'lib/api/helpers/runner.rb', line 67

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

#runner_registration_token_valid?Boolean

Returns:

  • (Boolean)

13
14
15
# File 'lib/api/helpers/runner.rb', line 13

def runner_registration_token_valid?
  ActiveSupport::SecurityUtils.secure_compare(params[:token], Gitlab::CurrentSettings.runners_registration_token)
end