Module: Dependabot::SharedHelpers

Defined in:
lib/dependabot/shared_helpers.rb

Defined Under Namespace

Classes: ChildProcessFailed, HelperSubprocessFailed

Constant Summary collapse

BUMP_TMP_FILE_PREFIX =
"dependabot_"
BUMP_TMP_DIR_PATH =
"tmp"
GIT_CONFIG_GLOBAL_PATH =
File.expand_path("~/.gitconfig")

Class Method Summary collapse

Class Method Details

.configure_git_credentials(credentials) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/dependabot/shared_helpers.rb', line 157

def self.configure_git_credentials(credentials)
  # Then add a file-based credential store that loads a file in this repo.
  # Under the hood this uses git credential-store, but it's invoked through
  # an wrapper binary that only allows non-mutative commands. Without this,
  # whenever the credentials are deemed to be invalid, they're erased.
  credential_helper_path =
    File.join(__dir__, "../../bin/git-credential-store-immutable")
  run_shell_command(
    "git config --global credential.helper "\
    "'#{credential_helper_path} --file=#{Dir.pwd}/git.store'"
  )

  # Build the content for our credentials file
  git_store_content = ""
  credentials.each do |cred|
    next unless cred["type"] == "git_source"

    authenticated_url =
      "https://#{cred.fetch('username')}:#{cred.fetch('password')}"\
      "@#{cred.fetch('host')}"

    git_store_content += authenticated_url + "\n"
  end

  # Save the file
  File.write("git.store", git_store_content)
end

.configure_git_to_use_httpsObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/dependabot/shared_helpers.rb', line 140

def self.configure_git_to_use_https
  # Note: we use --global here (rather than --system) so that Dependabot
  # can be run without privileged access
  run_shell_command(
    'git config --global --replace-all url."https://github.com/".'\
    "insteadOf ssh://[email protected]/ && "\
    'git config --global --add url."https://github.com/".'\
    "insteadOf ssh://[email protected]: && "\
    'git config --global --add url."https://github.com/".'\
    "insteadOf [email protected]: && "\
    'git config --global --add url."https://github.com/".'\
    "insteadOf [email protected]/ && "\
    'git config --global --add url."https://github.com/".'\
    "insteadOf git://github.com/"
  )
end

.configure_git_to_use_https_with_credentials(credentials) ⇒ Object



135
136
137
138
# File 'lib/dependabot/shared_helpers.rb', line 135

def self.configure_git_to_use_https_with_credentials(credentials)
  configure_git_to_use_https
  configure_git_credentials(credentials)
end

.excon_defaultsObject



117
118
119
120
121
122
123
124
125
# File 'lib/dependabot/shared_helpers.rb', line 117

def self.excon_defaults
  {
    connect_timeout: 5,
    write_timeout: 5,
    read_timeout: 5,
    omit_default_port: true,
    middlewares: excon_middleware
  }
end

.excon_middlewareObject



113
114
115
# File 'lib/dependabot/shared_helpers.rb', line 113

def self.excon_middleware
  Excon.defaults[:middlewares] + [Excon::Middleware::RedirectFollower]
end

.in_a_forked_processObject

Raises:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dependabot/shared_helpers.rb', line 40

def self.in_a_forked_process
  read, write = IO.pipe

  pid = fork do
    read.close
    result = yield
  rescue Exception => error # rubocop:disable Lint/RescueException
    result = { _error_details: { error_class: error.class.to_s,
                                 error_message: error.message,
                                 error_backtrace: error.backtrace } }
  ensure
    Marshal.dump(result, write)
    exit!(0)
  end

  write.close
  result = read.read
  Process.wait(pid)
  result = Marshal.load(result) # rubocop:disable Security/MarshalLoad

  return result unless result.is_a?(Hash) && result[:_error_details]

  raise ChildProcessFailed, result[:_error_details]
end

.in_a_temporary_directory(directory = "/") ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/dependabot/shared_helpers.rb', line 31

def self.in_a_temporary_directory(directory = "/")
  Dir.mkdir(BUMP_TMP_DIR_PATH) unless Dir.exist?(BUMP_TMP_DIR_PATH)
  Dir.mktmpdir(BUMP_TMP_FILE_PREFIX, BUMP_TMP_DIR_PATH) do |dir|
    path = Pathname.new(File.join(dir, directory)).expand_path
    FileUtils.mkpath(path)
    Dir.chdir(path) { yield(path) }
  end
end

.reset_global_git_config(backup_path) ⇒ Object



196
197
198
199
200
201
# File 'lib/dependabot/shared_helpers.rb', line 196

def self.reset_global_git_config(backup_path)
  return if backup_path.nil?
  return unless File.exist?(backup_path)

  FileUtils.mv(backup_path, GIT_CONFIG_GLOBAL_PATH)
end

.run_helper_subprocess(command:, function:, args:, env: nil, stderr_to_stdout: false) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/dependabot/shared_helpers.rb', line 77

def self.run_helper_subprocess(command:, function:, args:, env: nil,
                               stderr_to_stdout: false)
  start = Time.now
  stdin_data = JSON.dump(function: function, args: args)
  env_cmd = [env, command].compact
  stdout, stderr, process = Open3.capture3(*env_cmd, stdin_data: stdin_data)
  time_taken = Time.now - start

  # Some package managers output useful stuff to stderr instead of stdout so
  # we want to parse this, most package manager will output garbage here so
  # would mess up json response from stdout
  stdout = "#{stderr}\n#{stdout}" if stderr_to_stdout

  error_context = {
    command: command,
    function: function,
    args: args,
    time_taken: time_taken,
    stderr_output: stderr ? stderr[0..50_000] : "", # Truncate to ~100kb
    process_exit_value: process.to_s
  }

  response = JSON.parse(stdout)
  return response["result"] if process.success?

  raise HelperSubprocessFailed.new(
    message: response["error"],
    error_context: error_context
  )
rescue JSON::ParserError
  raise HelperSubprocessFailed.new(
    message: stdout || "No output from command",
    error_context: error_context
  )
end

.run_shell_command(command) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/dependabot/shared_helpers.rb', line 203

def self.run_shell_command(command)
  start = Time.now
  stdout, process = Open3.capture2e(command)
  time_taken = Time.now - start

  # Raise an error with the output from the shell session if the
  # command returns a non-zero status
  return if process.success?

  error_context = {
    command: command,
    time_taken: time_taken,
    process_exit_value: process.to_s
  }

  raise SharedHelpers::HelperSubprocessFailed.new(
    message: stdout,
    error_context: error_context
  )
end

.stash_global_git_configObject



185
186
187
188
189
190
191
192
193
194
# File 'lib/dependabot/shared_helpers.rb', line 185

def self.stash_global_git_config
  return unless File.exist?(GIT_CONFIG_GLOBAL_PATH)

  contents = File.read(GIT_CONFIG_GLOBAL_PATH)
  digest = Digest::SHA2.hexdigest(contents)[0...10]
  backup_path = GIT_CONFIG_GLOBAL_PATH + ".backup-#{digest}"

  FileUtils.mv(GIT_CONFIG_GLOBAL_PATH, backup_path)
  backup_path
end

.with_git_configured(credentials:) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/dependabot/shared_helpers.rb', line 127

def self.with_git_configured(credentials:)
  backup_git_config_path = stash_global_git_config
  configure_git_to_use_https_with_credentials(credentials)
  yield
ensure
  reset_global_git_config(backup_git_config_path)
end