Skip to content

Commit ad89a45

Browse files
committed
Source /etc/profile, use sigV4 on s3 download and a couple of minor bug fixes
1 parent a4e550b commit ad89a45

File tree

9 files changed

+37
-35
lines changed

9 files changed

+37
-35
lines changed

Rakefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Rake::TestTask.new(:test) do |t|
1212
t.verbose = true
1313
end
1414
task :default => :test
15+
task :release => :test
1516

1617
# Run units tests in test/instance_agent/
1718
Rake::TestTask.new(:test_instance_agent) do |t|

init.d/codedeploy-agent

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
# the deployment artifacts on to this instance.
1818
### END INIT INFO
1919

20+
2021
RETVAL=0
22+
[ -f /etc/profile ] && [ "`stat --format '%U %G' /etc/profile`" == "root root" ] && source /etc/profile
2123

2224
prog="codedeploy-agent"
2325
AGENT_ROOT="/opt/codedeploy-agent/"
@@ -52,12 +54,15 @@ update() {
5254

5355
case "$1" in
5456
start)
55-
update
5657
start
5758
;;
5859
start-no-update)
5960
start
6061
;;
62+
start-with-update)
63+
update
64+
start
65+
;;
6166
stop)
6267
stop
6368
;;

lib/instance_agent.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# encoding: UTF-8
22

33
Gem.use_paths(nil, Gem.path << "/opt/codedeploy-agent/vendor")
4+
45
require 'core_ext'
56

67
require 'rubygems'

lib/instance_agent/plugins/codedeploy/codedeploy_control.rb

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,7 @@ def verify_cert
7878
# Do minimal cert pinning
7979
def verify_subject
8080
InstanceAgent::Log.debug("#{self.class.to_s}: Actual certificate subject is '#{@cert.subject.to_s}'")
81-
82-
case @region
83-
when 'us-east-1'
84-
@cert.subject.to_s == "/C=US/ST=Washington/L=Seattle/O=Amazon.com, Inc./CN=codedeploy-commands.us-east-1.amazonaws.com"
85-
when 'us-west-2'
86-
@cert.subject.to_s == "/C=US/ST=Washington/L=Seattle/O=Amazon.com, Inc./CN=codedeploy-commands.us-west-2.amazonaws.com"
87-
when 'eu-west-1'
88-
@cert.subject.to_s == "/C=US/ST=Washington/L=Seattle/O=Amazon.com, Inc./CN=codedeploy-commands.eu-west-1.amazonaws.com"
89-
when 'ap-southeast-2'
90-
@cert.subject.to_s == "/C=US/ST=Washington/L=Seattle/O=Amazon.com, Inc./CN=codedeploy-commands.ap-southeast-2.amazonaws.com"
91-
else
92-
InstanceAgent::Log.debug("#{self.class.to_s}: Unsupported region '#{@region}'")
93-
false
94-
end
81+
@cert.subject.to_s == "/C=US/ST=Washington/L=Seattle/O=Amazon.com, Inc./CN=codedeploy-commands."+@region+".amazonaws.com"
9582
end
9683

9784
end

lib/instance_agent/plugins/codedeploy/command_executor.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,13 @@ def download_from_s3(deployment_spec, bucket, key, version, etag)
176176
File.join(InstanceAgent::Config.config[:log_dir], "#{InstanceAgent::Config.config[:program_name]}.aws_wire.log"),
177177
16,
178178
64 * 1024 * 1024),
179-
:http_wire_trace => true)
179+
:http_wire_trace => true,
180+
:signature_version => 'v4')
180181
else
181182
s3 = Aws::S3::Client.new(
182183
:region => region,
183-
:ssl_ca_directory => ENV['AWS_SSL_CA_DIRECTORY'])
184+
:ssl_ca_directory => ENV['AWS_SSL_CA_DIRECTORY'],
185+
:signature_version => 'v4')
184186
end
185187

186188
File.open(artifact_bundle(deployment_spec), 'wb') do |file|
@@ -316,16 +318,22 @@ def update_last_successful_install(deployment_spec)
316318

317319
private
318320
def cleanup_old_archives(deployment_group)
319-
deployment_archives = Dir[File.join(ProcessManager::Config.config[:root_dir], deployment_group, '*')]
320-
extra = deployment_archives.size - ARCHIVES_TO_RETAIN
321+
deployment_archives = Dir.entries(File.join(ProcessManager::Config.config[:root_dir], deployment_group))
322+
# remove . and ..
323+
deployment_archives.delete(".")
324+
deployment_archives.delete("..")
325+
326+
full_path_deployment_archives = deployment_archives.map{ |f| File.join(ProcessManager::Config.config[:root_dir], deployment_group, f)}
327+
328+
extra = full_path_deployment_archives.size - ARCHIVES_TO_RETAIN
321329
return unless extra > 0
322330

323331
# Never remove the last successful deployment
324332
last_success = last_successful_deployment_dir(deployment_group)
325-
deployment_archives.delete(last_success)
333+
full_path_deployment_archives.delete(last_success)
326334

327335
# Sort oldest -> newest, take first `extra` elements
328-
oldest_extra = deployment_archives.sort_by{ |f| File.mtime(f) }.take(extra)
336+
oldest_extra = full_path_deployment_archives.sort_by{ |f| File.mtime(f) }.take(extra)
329337

330338
# Absolute path takes care of relative root directories
331339
directories = oldest_extra.map{ |f| File.absolute_path(f) }

lib/instance_agent/plugins/codedeploy/command_poller.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ class CommandPoller < InstanceAgent::Agent::Base
88

99
VERSION = "2013-04-23"
1010
def initialize
11+
test_profile = InstanceAgent::Config.config[:codedeploy_test_profile]
12+
unless ["beta", "gamma"].include?(test_profile.downcase)
13+
# Remove any user overrides set in the environment.
14+
# The agent should always pull credentials from the EC2 instance
15+
# profile or the credentials in the OnPremises config file.
16+
ENV['AWS_ACCESS_KEY_ID'] = nil
17+
ENV['AWS_SECRET_ACCESS_KEY'] = nil
18+
ENV['AWS_CREDENTIAL_FILE'] = nil
19+
end
1120
CodeDeployPlugin::OnPremisesConfig.configure
1221
region = ENV['AWS_REGION'] || InstanceMetadata.region
1322
@host_identifier = ENV['AWS_HOST_IDENTIFIER'] || InstanceMetadata.host_identifier
@@ -151,4 +160,4 @@ def gather_diagnostics()
151160
end
152161
end
153162
end
154-
end
163+
end

lib/instance_agent/plugins/codedeploy/deployment_specification.rb

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'openssl'
22
require 'instance_metadata'
3+
require 'open-uri'
34

45
module InstanceAgent
56
module Plugins
@@ -132,18 +133,7 @@ def self.verify_pkcs7_signer_cert(cert)
132133
when 'beta', 'gamma'
133134
cert.subject.to_s == "/C=US/ST=Washington/L=Seattle/O=Amazon.com, Inc./CN=codedeploy-signer-integ.amazonaws.com"
134135
when 'prod'
135-
case @@region
136-
when 'us-east-1'
137-
cert.subject.to_s == "/C=US/ST=Washington/L=Seattle/O=Amazon.com, Inc./CN=codedeploy-signer-us-east-1.amazonaws.com"
138-
when 'us-west-2'
139-
cert.subject.to_s == "/C=US/ST=Washington/L=Seattle/O=Amazon.com, Inc./CN=codedeploy-signer-us-west-2.amazonaws.com"
140-
when 'eu-west-1'
141-
cert.subject.to_s == "/C=US/ST=Washington/L=Seattle/O=Amazon.com, Inc./CN=codedeploy-signer-eu-west-1.amazonaws.com"
142-
when 'ap-southeast-2'
143-
cert.subject.to_s == "/C=US/ST=Washington/L=Seattle/O=Amazon.com, Inc./CN=codedeploy-signer-ap-southeast-2.amazonaws.com"
144-
else
145-
raise "Unknown region '#{@region}'"
146-
end
136+
cert.subject.to_s == "/C=US/ST=Washington/L=Seattle/O=Amazon.com, Inc./CN=codedeploy-signer-"+@@region+".amazonaws.com"
147137
else
148138
raise "Unknown profile '#{Config.config()[:codedeploy_test_profile]}'"
149139
end

lib/instance_metadata.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class InstanceMetadata
66

77
IP_ADDRESS = '169.254.169.254'
88
PORT = 80
9-
9+
1010
def self.host_identifier
1111
doc = JSON.parse(http_get('/latest/dynamic/instance-identity/document').strip)
1212
"arn:aws:ec2:#{doc['region']}:#{doc['accountId']}:instance/#{doc['instanceId']}"

test/instance_agent/plugins/codedeploy/command_executor_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ def generate_signed_message_for(map)
184184
@mock_file = mock
185185
Net::HTTP.stubs(:start).yields(@http)
186186
File.stubs(:open).returns @mock_file
187+
Dir.stubs(:entries).returns []
187188
@mock_file.stubs(:close)
188189
@http.stubs(:request_get)
189190
@s3 = mock

0 commit comments

Comments
 (0)