Skip to content

Commit 89fbd4a

Browse files
kaiwensundljvette
authored andcommitted
load correct credentials in FileCredentials using v3 sdk
1 parent ec2a3a7 commit 89fbd4a

File tree

4 files changed

+62
-7
lines changed

4 files changed

+62
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ codedeploy-local.*.log
1010
deployment/
1111
.idea/
1212
.DS_STORE
13+
*.iml

lib/instance_agent/file_credentials.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ def initialize(path)
1414
private
1515

1616
def refresh
17-
@credentials = Aws::SharedCredentials.new(path: @path)
17+
@credentials = Aws::SharedCredentials.new(path: @path).credentials
18+
raise "Failed to load credentials from path #{@path}" if @credentials.nil?
1819
@expiration = Time.new + 1800
1920
end
2021
end
Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,60 @@
11
require 'test_helper'
22

33
class FileCredentialsTest < InstanceAgentTestCase
4-
context 'The file credentials' do
5-
should 'pass the path to SharedCredentials' do
6-
credentials = InstanceAgent::FileCredentials.new("/tmp/credentials_path")
7-
Aws::SharedCredentials.expects(:new).with(path: "/tmp/credentials_path")
4+
context 'With the file credentials' do
5+
6+
access_key_id = "fake-aws-access-key-id"
7+
secret_access_key = "fake-aws-secret-key"
8+
credentials_path = "/tmp/credentials_path"
9+
session_token_1 = "fake-aws-session-token-1"
10+
session_token_2 = "fake-aws-session-token-2"
11+
credential_file_pattern = <<-END
12+
[default]
13+
aws_access_key_id = #{access_key_id}
14+
aws_secret_access_key = #{secret_access_key}
15+
aws_session_token = %s
16+
END
17+
18+
setup do
19+
File.stubs(:exist?).with(credentials_path).returns(true)
20+
File.stubs(:exist?).with(Not(equals(credentials_path))).returns(false)
21+
File.stubs(:readable?).with(credentials_path).returns(true)
22+
File.expects(:read).with(credentials_path).returns(credential_file_pattern % session_token_2)
23+
File.expects(:read).with(credentials_path).returns(credential_file_pattern % session_token_1)
24+
end
25+
26+
should 'load and refresh the credentials from the path to SharedCredentials' do
27+
credentials = InstanceAgent::FileCredentials.new(credentials_path)
28+
assert_equal access_key_id, credentials.credentials.access_key_id
29+
assert_equal secret_access_key, credentials.credentials.secret_access_key
30+
assert_equal session_token_1, credentials.credentials.session_token
831
credentials.refresh!
32+
assert_equal access_key_id, credentials.credentials.access_key_id
33+
assert_equal secret_access_key, credentials.credentials.secret_access_key
34+
assert_equal session_token_2, credentials.credentials.session_token
935
end
1036

1137
should 'set the refresh time to 30 minutes' do
12-
credentials = InstanceAgent::FileCredentials.new("/tmp/credentials_path")
38+
credentials = InstanceAgent::FileCredentials.new(credentials_path)
1339
credentials.refresh!
1440
# Around 30 minutes
1541
expected_time = Time.now + 1800
1642
assert_in_delta(expected_time, credentials.expiration, 5, "Expiration time did not fall within 5 seconds of expected expiration")
1743
end
1844
end
45+
46+
context 'Without the file credentials' do
47+
48+
credentials_path = "/tmp/invalid_credentials_path"
49+
50+
setup do
51+
File.stubs(:exist?).with(credentials_path).returns(false)
52+
end
53+
54+
should 'raise error when credential file is missing' do
55+
assert_raised_with_message("Failed to load credentials from path #{credentials_path}", RuntimeError) do
56+
InstanceAgent::FileCredentials.new(credentials_path)
57+
end
58+
end
59+
end
1960
end

test/instance_agent/plugins/codedeploy/onpremise_config_test.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,33 @@ class OnPremiseConfigTest < InstanceAgentTestCase
7373
end
7474

7575
context "config file with session configuration" do
76+
credentials_path = "/etc/codedeploy-agent/conf/.aws_credentials"
7677
linux_file = <<-END
7778
region: us-east-test
7879
iam_session_arn: test:arn
79-
aws_credentials_file: /etc/codedeploy-agent/conf/.aws_credentials
80+
aws_credentials_file: #{credentials_path}
8081
END
82+
access_key_id = "fake-access-key-id-#{rand 1000}"
83+
credentials_file = <<-END
84+
[default]
85+
aws_access_key_id = #{access_key_id}
86+
aws_secret_access_key = fake-secret-access-key
87+
aws_session_token = fake-session-token
88+
END
8189

8290
setup do
8391
File.stubs(:read).with(linux_path).returns(linux_file)
92+
File.stubs(:read).with(credentials_path).returns(credentials_file)
93+
File.stubs(:exist?).with(credentials_path).returns(true)
94+
File.stubs(:readable?).with(credentials_path).returns(true)
8495
end
8596

8697
should "set the ENV variables correctly" do
8798
OnPremisesConfig.configure
8899
assert_equal 'us-east-test', ENV['AWS_REGION']
89100
assert_equal 'test:arn', ENV['AWS_HOST_IDENTIFIER']
90101
assert_equal '/etc/codedeploy-agent/conf/.aws_credentials', ENV['AWS_CREDENTIALS_FILE']
102+
assert_equal access_key_id, Aws.config[:credentials].credentials.access_key_id
91103
end
92104
end
93105

0 commit comments

Comments
 (0)