Module: API::Helpers::InternalHelpers

Defined in:
lib/api/helpers/internal_helpers.rb

Constant Summary collapse

UNKNOWN_CHECK_RESULT_ERROR =
'Unknown check result'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#redirected_pathObject (readonly)

Returns the value of attribute redirected_path.



6
7
8
# File 'lib/api/helpers/internal_helpers.rb', line 6

def redirected_path
  @redirected_path
end

Instance Method Details

#access_check!(actor, params) ⇒ Object

rubocop:disable Gitlab/ModuleWithInstanceVariables



52
53
54
55
56
57
58
59
60
61
# File 'lib/api/helpers/internal_helpers.rb', line 52

def access_check!(actor, params)
  access_checker = access_checker_for(actor, params[:protocol])
  access_checker.check(params[:action], params[:changes]).tap do |result|
    break result if @project || !repo_type.project?

    # If we have created a project directly from a git push
    # we have to assign its value to both @project and @container
    @project = @container = access_checker.container
  end
end

#access_check_resultObject

rubocop:enable Gitlab/ModuleWithInstanceVariables



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/api/helpers/internal_helpers.rb', line 33

def access_check_result
  with_admin_mode_bypass!(actor.user&.id) do
    access_check!(actor, params)
  end
rescue Gitlab::GitAccess::ForbiddenError => e
  # The return code needs to be 401. If we return 403
  # the custom message we return won't be shown to the user
  # and, instead, the default message 'GitLab: API is not accessible'
  # will be displayed
  response_with_status(code: 401, success: false, message: e.message)
rescue Gitlab::GitAccess::TimeoutError => e
  response_with_status(code: 503, success: false, message: e.message)
rescue Gitlab::GitAccess::NotFoundError => e
  response_with_status(code: 404, success: false, message: e.message)
rescue Gitlab::GitAccessProject::CreationError => e
  response_with_status(code: 422, success: false, message: e.message)
end

#access_checker_for(actor, protocol) ⇒ Object

rubocop:enable Gitlab/ModuleWithInstanceVariables



64
65
66
67
68
69
70
71
72
# File 'lib/api/helpers/internal_helpers.rb', line 64

def access_checker_for(actor, protocol)
  access_checker_klass.new(actor.key_or_user, container, protocol,
    authentication_abilities: ssh_authentication_abilities,
    repository_path: repository_path,
    redirected_path: redirected_path,
    push_options: params[:push_options],
    gitaly_context: gitaly_context(params)
  )
end

#access_checker_klassObject



74
75
76
# File 'lib/api/helpers/internal_helpers.rb', line 74

def access_checker_klass
  repo_type.access_checker_class
end

#actorObject



12
13
14
# File 'lib/api/helpers/internal_helpers.rb', line 12

def actor
  @actor ||= Support::GitAccessActor.from_params(params)
end

#containerObject



27
28
29
30
# File 'lib/api/helpers/internal_helpers.rb', line 27

def container
  parse_repo_path unless defined?(@container)
  @container
end

#include_ip_address_in_audit_event?(ip_address) ⇒ Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/api/helpers/internal_helpers.rb', line 143

def include_ip_address_in_audit_event?(ip_address)
  params[:protocol] == 'ssh' && ip_address
end

#log_user_activity(actor) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/api/helpers/internal_helpers.rb', line 94

def log_user_activity(actor)
  commands = Gitlab::GitAccess::DOWNLOAD_COMMANDS

  return unless commands.include?(params[:action])

  ::Users::ActivityService.new(author: actor, namespace: project&.namespace, project: project).execute

  return unless project && actor

  Gitlab::EventStore.publish(
    ::Users::ActivityEvent.new(data: {
      user_id: actor.id,
      namespace_id: project.root_ancestor.id
    })
  )
end

#need_git_audit_event?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/api/helpers/internal_helpers.rb', line 139

def need_git_audit_event?
  false
end

#parse_envObject



86
87
88
89
90
91
92
# File 'lib/api/helpers/internal_helpers.rb', line 86

def parse_env
  return {} if params[:env].blank?

  Gitlab::Json.parse(params[:env])
rescue JSON::ParserError
  {}
end

#projectObject



22
23
24
25
# File 'lib/api/helpers/internal_helpers.rb', line 22

def project
  parse_repo_path unless defined?(@project)
  @project
end

#redis_pingObject



111
112
113
114
115
116
117
118
# File 'lib/api/helpers/internal_helpers.rb', line 111

def redis_ping
  result = Gitlab::Redis::SharedState.with { |redis| redis.ping }

  result == 'PONG'
rescue StandardError => e
  Gitlab::AppLogger.warn("GitLab: An unexpected error occurred in pinging to Redis: #{e}")
  false
end

#repo_typeObject

rubocop:disable Gitlab/ModuleWithInstanceVariables



17
18
19
20
# File 'lib/api/helpers/internal_helpers.rb', line 17

def repo_type
  parse_repo_path unless defined?(@repo_type)
  @repo_type
end

#response_with_status(code: 200, success: true, message: nil, **extra_options) ⇒ Object



120
121
122
123
# File 'lib/api/helpers/internal_helpers.rb', line 120

def response_with_status(code: 200, success: true, message: nil, **extra_options)
  status code
  { status: success, message: message }.merge(extra_options).compact
end

#send_git_audit_streaming_event(msg) ⇒ Object



135
136
137
# File 'lib/api/helpers/internal_helpers.rb', line 135

def send_git_audit_streaming_event(msg)
  # Defined in EE
end

#ssh_authentication_abilitiesObject



78
79
80
81
82
83
84
# File 'lib/api/helpers/internal_helpers.rb', line 78

def ssh_authentication_abilities
  [
    :read_project,
    :download_code,
    :push_code
  ]
end

#unsuccessful_response?(response) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/api/helpers/internal_helpers.rb', line 125

def unsuccessful_response?(response)
  response.is_a?(Hash) && response[:status] == false
end

#with_admin_mode_bypass!(actor_id, &block) ⇒ Object



129
130
131
132
133
# File 'lib/api/helpers/internal_helpers.rb', line 129

def with_admin_mode_bypass!(actor_id, &block)
  return yield unless Gitlab::CurrentSettings.admin_mode

  Gitlab::Auth::CurrentUserMode.bypass_session!(actor_id, &block)
end