Skip to content

Commit 9dcc4d9

Browse files
authored
Merge pull request #198 from pmzara/master
Syncing - Internal repo changes.
2 parents dc22129 + 3bbe54f commit 9dcc4d9

File tree

8 files changed

+166
-34
lines changed

8 files changed

+166
-34
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,17 @@ The integration test creates the following
3737
It terminates the test ec2 instance and deletes the CodeDeploy application at the end of each test run.
3838
It also terminates any test ec2 instances before starting up the test.
3939

40-
Create your default aws credentials file in the default location (~/.aws/credentials on linux/mac and %USERPROFILE%.awscredentials on windows). Add your AWS access key, secret key, and optionally your session token there. The access key should have permission to create the above mentioned resources. You can also change the default region. To run the integration test execute
40+
Create your default aws credentials file in the default location (~/.aws/credentials on linux/mac and %USERPROFILE%.awscredentials on windows). Add your AWS access key, secret key, and optionally your session token there. The access key should have permission to create the above mentioned resources. You can also change the default region. Note that temporary credentials won't work.
41+
42+
Sample format of the credentials file:
43+
44+
```
45+
[default]
46+
aws_access_key_id=<keyID>
47+
aws_secret_access_key=<key>
48+
```
49+
50+
To run the integration test execute:
4151

4252
```
4353
rake test-integration

lib/instance_agent/config.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# encoding: UTF-8
22
require 'process_manager/config'
3+
require 'set'
34

45
module InstanceAgent
56
class Config < ProcessManager::Config
7+
8+
FIPS_ENABLED_REGIONS = Set['us-east-1', 'us-east-2', 'us-west-1', 'us-west-2', 'us-gov-west-1', 'us-gov-east-1']
9+
610
def self.init
711
@config = Config.new
812
ProcessManager::Config.instance_variable_set("@config", @config)
@@ -11,6 +15,7 @@ def self.init
1115
def validate
1216
errors = super
1317
validate_children(errors)
18+
validate_use_fips_mode(errors)
1419
errors
1520
end
1621

@@ -35,14 +40,25 @@ def initialize
3540
:kill_agent_max_wait_time_seconds => 7200,
3641
:on_premises_config_file => '/etc/codedeploy-agent/conf/codedeploy.onpremises.yml',
3742
:proxy_uri => nil,
38-
:enable_deployments_log => true
43+
:enable_deployments_log => true,
44+
:use_fips_mode => false
3945
})
4046
end
4147

4248
def validate_children(errors = [])
4349
errors << 'children can only be set to 1' unless config[:children] == 1
44-
errors
50+
end
51+
52+
def validate_use_fips_mode errors
53+
if config[:use_fips_mode] && ! (FIPS_ENABLED_REGIONS.include? region)
54+
errors << 'use_fips_mode can be set to true only in regions located in the USA'
55+
end
4556
end
4657

58+
#Return the region we are currently in
59+
def region
60+
ENV['AWS_REGION'] || InstanceMetadata.region
61+
end
62+
4763
end
4864
end

lib/instance_agent/plugins/codedeploy/command_executor.rb

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -238,33 +238,8 @@ def most_recent_install_file_path(deployment_group)
238238
private
239239
def download_from_s3(deployment_spec, bucket, key, version, etag)
240240
log(:debug, "Downloading artifact bundle from bucket '#{bucket}' and key '#{key}', version '#{version}', etag '#{etag}'")
241-
region = ENV['AWS_REGION'] || InstanceMetadata.region
242-
243-
proxy_uri = nil
244-
if InstanceAgent::Config.config[:proxy_uri]
245-
proxy_uri = URI(InstanceAgent::Config.config[:proxy_uri])
246-
end
247-
248-
if InstanceAgent::Config.config[:log_aws_wire]
249-
s3 = Aws::S3::Client.new(
250-
:region => region,
251-
:ssl_ca_directory => ENV['AWS_SSL_CA_DIRECTORY'],
252-
# wire logs might be huge; customers should be careful about turning them on
253-
# allow 1GB of old wire logs in 64MB chunks
254-
:logger => Logger.new(
255-
File.join(InstanceAgent::Config.config[:log_dir], "#{InstanceAgent::Config.config[:program_name]}.aws_wire.log"),
256-
16,
257-
64 * 1024 * 1024),
258-
:http_wire_trace => true,
259-
:signature_version => 'v4',
260-
:http_proxy => proxy_uri)
261-
else
262-
s3 = Aws::S3::Client.new(
263-
:region => region,
264-
:ssl_ca_directory => ENV['AWS_SSL_CA_DIRECTORY'],
265-
:signature_version => 'v4',
266-
:http_proxy => proxy_uri)
267-
end
241+
242+
s3 = Aws::S3::Client.new(s3_options)
268243

269244
File.open(artifact_bundle(deployment_spec), 'wb') do |file|
270245

@@ -283,6 +258,39 @@ def download_from_s3(deployment_spec, bucket, key, version, etag)
283258
log(:debug, "Download complete from bucket #{bucket} and key #{key}")
284259
end
285260

261+
public
262+
def s3_options
263+
options = {}
264+
options[:ssl_ca_directory] = ENV['AWS_SSL_CA_DIRECTORY']
265+
options[:signature_version] = 'v4'
266+
267+
region = ENV['AWS_REGION'] || InstanceMetadata.region
268+
options[:region] = region
269+
if InstanceAgent::Config.config[:use_fips_mode]
270+
#S3 Fips pseudo-regions are not supported by the SDK yet
271+
#source for the URL: https://aws.amazon.com/compliance/fips/
272+
options[:endpoint] = "https://s3-fips.#{region}.amazonaws.com"
273+
end
274+
275+
proxy_uri = nil
276+
if InstanceAgent::Config.config[:proxy_uri]
277+
proxy_uri = URI(InstanceAgent::Config.config[:proxy_uri])
278+
end
279+
options[:http_proxy] = proxy_uri
280+
281+
if InstanceAgent::Config.config[:log_aws_wire]
282+
# wire logs might be huge; customers should be careful about turning them on
283+
# allow 1GB of old wire logs in 64MB chunks
284+
options[:logger] = Logger.new(
285+
File.join(InstanceAgent::Config.config[:log_dir], "#{InstanceAgent::Config.config[:program_name]}.aws_wire.log"),
286+
16,
287+
64 * 1024 * 1024)
288+
options[:http_wire_trace] = true
289+
end
290+
291+
options
292+
end
293+
286294
private
287295
def download_from_github(deployment_spec, account, repo, commit, anonymous, token)
288296

test/instance_agent/config_test.rb

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ class InstanceAgentConfigTest < InstanceAgentTestCase
3131
:ongoing_deployment_tracking => 'ongoing-deployment',
3232
:proxy_uri => nil,
3333
:enable_deployments_log => true,
34-
:kill_agent_max_wait_time_seconds => 7200
34+
:kill_agent_max_wait_time_seconds => 7200,
35+
:use_fips_mode => false
3536
}, InstanceAgent::Config.config)
3637
end
3738

@@ -41,9 +42,11 @@ class InstanceAgentConfigTest < InstanceAgentTestCase
4142
end
4243

4344
should 'execute all available validation methods' do
45+
InstanceMetadata.stubs(:region).returns('us-west-1') #without stubbing this, the test will fail in the build fleet because MetadataService is not available there
4446
validations = sequence('validation')
4547
err = []
4648
InstanceAgent::Config.any_instance.expects(:validate_children).with(err).in_sequence(validations)
49+
InstanceAgent::Config.any_instance.expects(:validate_use_fips_mode).with(err).in_sequence(validations)
4750
InstanceAgent::Config.validate_config
4851
end
4952

@@ -53,6 +56,8 @@ class InstanceAgentConfigTest < InstanceAgentTestCase
5356
InstanceAgent::Config.config[:instance_service_region] = 'eu-west-1'
5457
InstanceAgent::Config.config[:instance_service_endpoint] = 'api-endpoint.example.com'
5558
InstanceAgent::Config.config[:instance_service_port] = 123
59+
60+
InstanceMetadata.stubs(:region).returns('us-west-1') #without stubbing this, the test will fail in the build fleet because MetadataService is not available there
5661
end
5762

5863
should 'validate the children setting' do
@@ -65,5 +70,39 @@ class InstanceAgentConfigTest < InstanceAgentTestCase
6570
assert InstanceAgent::Config.validate_config.empty?, InstanceAgent::Config.validate_config.inspect
6671
end
6772
end
73+
74+
context 'validate use_fips_mode' do
75+
76+
error = 'use_fips_mode can be set to true only in regions located in the USA'
77+
78+
should 'error in eu-west-1' do
79+
InstanceAgent::Config.config[:use_fips_mode] = true
80+
ENV['AWS_REGION'] = 'eu-west-1'
81+
assert InstanceAgent::Config.validate_config.include? error
82+
end
83+
84+
should 'not error in eu-west-1 if not set' do
85+
InstanceAgent::Config.config[:use_fips_mode] = false
86+
ENV['AWS_REGION'] = 'eu-west-1'
87+
assert_false InstanceAgent::Config.validate_config.include? error
88+
end
89+
90+
should 'not error in us-east-1' do
91+
InstanceAgent::Config.config[:use_fips_mode] = true
92+
ENV['AWS_REGION'] = 'us-east-1'
93+
assert_false InstanceAgent::Config.validate_config.include? error
94+
end
95+
96+
should 'not error in us-gov-west-1' do
97+
InstanceAgent::Config.config[:use_fips_mode] = true
98+
ENV['AWS_REGION'] = 'us-gov-west-1'
99+
assert_false InstanceAgent::Config.validate_config.include? error
100+
end
101+
102+
cleanup do
103+
ENV['AWS_REGION'] = nil
104+
end
105+
106+
end
68107
end
69108
end

test/instance_agent/plugins/codedeploy/codedeploy_control_test.rb

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class CodeDeployControlTest < InstanceAgentTestCase
1111
ENV['AWS_ACCESS_KEY_ID'] = "Test Access Key"
1212
ENV['AWS_SECRET_ACCESS_KEY'] = "Test Secret Access Key"
1313
ENV['AWS_REGION'] = nil
14-
ENV['AWSDEPLOY_CONTROL_ENDPOINT'] = "https://tempuri"
1514
ENV['DEPLOYMENT_CREATOR'] = "User"
1615
ENV['DEPLOYMENT_TYPE'] = "IN_PLACE"
1716
end
@@ -51,6 +50,40 @@ class CodeDeployControlTest < InstanceAgentTestCase
5150
}
5251
end
5352
end
53+
54+
context "with ADCS endpoint set in an environment variable" do
55+
setup do
56+
ENV['AWS_DEPLOY_CONTROL_ENDPOINT'] = "https://tempuri"
57+
end
58+
59+
should "use endpoint from environment variable" do
60+
codedeploy_control_client = CodeDeployControl.new :region => "us-west-2"
61+
assert_equal "tempuri", codedeploy_control_client.get_client.config.endpoint.host
62+
end
63+
64+
cleanup do
65+
ENV['AWS_DEPLOY_CONTROL_ENDPOINT'] = nil
66+
end
67+
end
68+
69+
context "with use_fips_mode not set" do
70+
should "use non-Fips endpoint" do
71+
codedeploy_control_client = CodeDeployControl.new :region => "us-west-2"
72+
assert_equal "codedeploy-commands.us-west-2.amazonaws.com", codedeploy_control_client.get_client.config.endpoint.host
73+
end
74+
end
75+
76+
context "with use_fips_mode set" do
77+
setup do
78+
InstanceAgent::Config.config[:use_fips_mode] = true
79+
end
80+
81+
should "use Fips endpoint" do
82+
codedeploy_control_client = CodeDeployControl.new :region => "us-west-2"
83+
assert_equal "codedeploy-commands-fips.us-west-2.amazonaws.com", codedeploy_control_client.get_client.config.endpoint.host
84+
end
85+
end
86+
5487
end
5588
end
5689
end

test/instance_agent/plugins/codedeploy/command_executor_test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,28 @@ def generate_signed_message_for(map)
316316
end
317317
end
318318

319+
context "when creating S3 options" do
320+
321+
should "use right region" do
322+
assert_equal 'us-east-1', @command_executor.s3_options[:region]
323+
end
324+
325+
should "use right signature version" do
326+
assert_equal 'v4', @command_executor.s3_options[:signature_version]
327+
end
328+
329+
should "use right endpoint when using Fips" do
330+
InstanceAgent::Config.config[:use_fips_mode] = true
331+
assert_equal 'https://s3-fips.us-east-1.amazonaws.com', @command_executor.s3_options[:endpoint]
332+
InstanceAgent::Config.config[:use_fips_mode] = false
333+
end
334+
335+
should "use no endpoint when not using Fips" do
336+
assert_false @command_executor.s3_options.include? :endpoint
337+
end
338+
339+
end
340+
319341
context "downloading bundle from S3" do
320342
setup do
321343
File.expects(:open).with(File.join(@deployment_root_dir, 'bundle.tar'), 'wb').yields(@mock_file)

vendor/gems/codedeploy-commands-1.0.0/lib/aws/plugins/deploy_control_endpoint.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ class DeployControlEndpoint < Seahorse::Client::Plugin
66
option(:endpoint) do |cfg|
77
url = ENV['AWS_DEPLOY_CONTROL_ENDPOINT']
88
if url.nil?
9-
url = "https://codedeploy-commands.#{cfg.region}.amazonaws.com"
9+
url = "https://codedeploy-commands"
10+
if InstanceAgent::Config.config[:use_fips_mode]
11+
url.concat "-fips"
12+
end
13+
url.concat ".#{cfg.region}.amazonaws.com"
1014
if "cn" == cfg.region.split("-")[0]
1115
url.concat(".cn")
1216
end

vendor/gems/process_manager-0.0.13/lib/process_manager/master.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def process_matcher(pid)
167167
if pid == own_pid
168168
return false
169169
end
170-
File.read("/proc/#{pid}/cmdline").include?("codedeploy-agent: master")
170+
`ps -p #{pid} -o command`.include?("codedeploy-agent: master")
171171
end
172172

173173
def handle_pid_file

0 commit comments

Comments
 (0)