Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

github(login): Add a way to read ~/.netrc.gpg encrypted files #624

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions lib/travis/tools/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'yaml'
require 'json'
require 'gh'
require 'open3'

module Travis
module Tools
Expand Down Expand Up @@ -171,7 +172,7 @@ def keychain_login
end

def netrc
file(netrc_path, []) do |contents|
gpg_file_or_file(netrc_path, []) do |contents|
contents.scan(/^\s*(\S+)\s+(\S+)\s*$/).inject([]) do |mapping, (key, value)|
mapping << {} if key == "machine"
mapping.last[key] = value if mapping.last
Expand Down Expand Up @@ -282,12 +283,37 @@ def security(type, key, arg, name)
raise e if explode
end

def gpg_file_or_file(path, default = nil)
gpg_path = File.expand_path("#{path}.gpg")

if File.file?(gpg_path)
path &&= gpg_path
else
path &&= File.expand_path(path)
end

file(path, default)
end

def file(path, default = nil)
path &&= File.expand_path(path)
@file ||= {}
@file[path] ||= if path and File.readable?(path)
debug "reading #{path}"
yield File.read(path)
if path.end_with?(".gpg")
stdout_str, stderr_str, status = Open3.capture3("gpg", "-d", path)
if status.success?
yield stdout_str
else
debug "unable to decrypt #{path} file because of:"
debug stderr_str
unencrypted_path = path.sub(/#{File.extname(path)}$/, '')
debug "falling back to reading #{unencrypted_path}"
yield File.read(unencrypted_path)
end
else
yield File.read(path)
end
end
@file[path] || default
rescue => e
Expand Down