forked from siassaj/heroku-buildpack-git-deploy-keys
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompile
executable file
·134 lines (103 loc) · 3.74 KB
/
compile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env ruby
require 'fileutils'
require 'pathname'
require 'tmpdir'
BUILD_DIR = ARGV[0]
CACHE_DIR = ARGV[1]
ENV_DIR = ARGV[2]
SSH_DIR = File.expand_path "#{ENV['HOME']}/.ssh"
def alert(str)
str.split('\n').each do |line|
puts " !!!! #{line}"
end
end
def arrow(str)
str.split("\n").each do |line|
puts ":::::> #{line}"
end
end
def read_env(env_file)
env_file_path = File.join(ENV_DIR, env_file)
if env_file.include?('GITHUB') && File.exist?(env_file_path)
alert "#{env_file} is deprecated, please switch to #{env_file.sub('GITHUB','GIT')}"
end
File.exist?(env_file_path) && File.read(env_file_path) || nil
end
arrow "############################################"
arrow " GIT DEPLOY KEY BUILDPACK "
arrow "############################################"
arrow "ssh dir is #{SSH_DIR}"
################# Get the key from heroku's environment config #################
ssh_key = read_env('GIT_DEPLOY_KEY')
key_file_path = File.join(ENV_DIR, 'GIT_DEPLOY_KEY') if ssh_key
ssh_key ||= read_env('GITHUB_DEPLOY_KEY')
key_file_path ||= File.join(ENV_DIR, 'GITHUB_DEPLOY_KEY')
if ssh_key.nil?
alert "GIT_DEPLOY_KEY not set"
alert " Try `heroku config:add GIT_DEPLOY_KEY=<your private token>`"
exit 1
end
############# Get the known host from heroku's environment config ##############
host_hash = read_env('GIT_HOST_HASH')
host_hash ||= read_env('GITHUB_HOST_HASH')
if host_hash.nil?
alert "GIT_HOST_HASH not set"
alert " Try `heroku config:add GIT_HOST_HASH=<hash>`"
exit 1
end
############# Get the git host from heroku's environment config ################
git_host = read_env('GIT_HOST')
if git_host.nil?
arrow 'GIT_HOST not set, assuming github.com'
git_user = 'github.com'
end
############# Get the git user from heroku's environment config ################
git_host = read_env('GIT_USER')
if git_host.nil?
arrow "GIT_USER not set, assuming 'git'"
git_user = 'git'
end
###################### Process and clean up the ssh keys #######################
fingerprint = nil
temp_key = nil
clean_host_hash = nil
Dir.mktmpdir 'ssh_buidpack' do |dir|
# Process key to standardise it's format
`ssh-keygen -e -P '' -f #{key_file_path} < /dev/null > #{dir}/ssh_buildpack_key.pub.rfc 2>/dev/null`
`ssh-keygen -i -P '' -f #{dir}/ssh_buildpack_key.pub.rfc > #{dir}/ssh_buildpack_key.pub 2>/dev/null`
# # Process host hash to standardise it's format
# `ssh-keygen -e -P '' -f #{key_file_path} < /dev/null > #{dir}/host_hash.pub.rfc 2>/dev/null`
# `ssh-keygen -i -P '' -f #{dir}/host_hash.pub.rfc > #{dir}/host_hash.pub 2>/dev/null`
fingerprint = `ssh-keygen -l -f #{dir}/ssh_buildpack_key.pub | awk '{print $2}'`
# only used to be sure the passed key was valid
temp_key = `echo "#{fingerprint}" | tr -ds ':' '' | egrep -ie "[a-f0-9]{32}" 2>/dev/null`
# clean_host_hash = `cat "#{dir}/host_hash.pub"`
if temp_key.strip == ''
alert "GIT_DEPLOY_KEY was invalid"
exit 1
else
arrow "Using GIT_DEPLOY_KEY #{fingerprint}"
end
end
# Create the ssh directory on the server
Dir.mkdir(SSH_DIR, 0700) unless File.exists?(SSH_DIR)
# Create id_rsa file and write contents
File.open "#{SSH_DIR}/id_rsa", 'w' do |f|
f.write ssh_key
end
FileUtils.chmod 0600, "#{SSH_DIR}/id_rsa"
arrow "Wrote ssh key to user's ssh dir"
#create known_hosts file and write contents
File.open "#{SSH_DIR}/known_hosts", 'w' do |f|
f.write host_hash
end
FileUtils.chmod 0600, "#{SSH_DIR}/known_hosts"
arrow "Wrote host hash to user's known hosts"
File.open "#{SSH_DIR}/config", 'w' do |f|
f.puts "IdentityFile #{SSH_DIR}/id_rsa"
f.puts "host #{git_host}"
f.puts " user #{git_user}"
f.puts " IdentityFile #{SSH_DIR}/id_rsa"
end
FileUtils.chmod 0600, "#{SSH_DIR}/config"
arrow "Wrote config to user's config"