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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
# frozen_string_literal: true
Shindo.tests('AWS | credentials', ['aws']) do
old_mock_value = Excon.defaults[:mock]
fog_was_mocked = Fog.mocking?
Excon.stubs.clear
Fog.unmock!
begin
Excon.defaults[:mock] = true
Excon.stub({ method: :put, path: '/latest/api/token' }, { status: 200, body: 'token1234' })
Excon.stub({ method: :get, path: '/latest/meta-data/iam/security-credentials/' }, { status: 200, body: 'arole' })
Excon.stub({ method: :get, path: '/latest/meta-data/placement/availability-zone/' }, { status: 200, body: 'us-west-1a' })
expires_at = Time.at(Time.now.to_i + 500)
credentials = {
'AccessKeyId' => 'dummykey',
'SecretAccessKey' => 'dummysecret',
'Token' => 'dummytoken',
'Expiration' => expires_at.xmlschema
}
Excon.stub({ method: :get, path: '/latest/meta-data/iam/security-credentials/arole' }, { status: 200, body: Fog::JSON.encode(credentials) })
tests('#fetch_credentials') do
returns(aws_access_key_id: 'dummykey',
aws_secret_access_key: 'dummysecret',
aws_session_token: 'dummytoken',
region: 'us-west-1',
aws_credentials_expire_at: expires_at) { Fog::AWS::Compute.fetch_credentials(use_iam_profile: true) }
end
tests('#fetch_credentials when the v2 token 404s') do
Excon.stub({ method: :put, path: '/latest/api/token' }, { status: 404, body: 'not found' })
returns(aws_access_key_id: 'dummykey',
aws_secret_access_key: 'dummysecret',
aws_session_token: 'dummytoken',
region: 'us-west-1',
aws_credentials_expire_at: expires_at) { Fog::AWS::Compute.fetch_credentials(use_iam_profile: true) }
end
tests('#fetch_credentials when the v2 disabled') do
returns(aws_access_key_id: 'dummykey',
aws_secret_access_key: 'dummysecret',
aws_session_token: 'dummytoken',
region: 'us-west-1',
aws_credentials_expire_at: expires_at) { Fog::AWS::Compute.fetch_credentials(use_iam_profile: true, disable_imds_v2: true) }
end
ENV['AWS_CONTAINER_CREDENTIALS_RELATIVE_URI'] = '/v1/credentials?id=task_id'
Excon.stub({ method: :get, path: '/v1/credentials?id=task_id' }, { status: 200, body: Fog::JSON.encode(credentials) })
tests('#fetch_credentials') do
returns(aws_access_key_id: 'dummykey',
aws_secret_access_key: 'dummysecret',
aws_session_token: 'dummytoken',
region: 'us-west-1',
aws_credentials_expire_at: expires_at) { Fog::AWS::Compute.fetch_credentials(use_iam_profile: true) }
end
ENV['AWS_CONTAINER_CREDENTIALS_RELATIVE_URI'] = nil
ENV['AWS_WEB_IDENTITY_TOKEN_FILE'] = File.dirname(__FILE__) + '/lorem.txt'
ENV['AWS_ROLE_ARN'] = "dummyrole"
ENV['AWS_ROLE_SESSION_NAME'] = "dummyrolesessionname"
document =
'<AssumeRoleWithWebIdentityResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/">'\
'<AssumeRoleWithWebIdentityResult>'\
'<Credentials>'\
'<SessionToken>dummytoken</SessionToken>'\
'<SecretAccessKey>dummysecret</SecretAccessKey>'\
"<Expiration>#{expires_at.xmlschema}</Expiration>"\
'<AccessKeyId>dummykey</AccessKeyId>'\
'</Credentials>'\
'</AssumeRoleWithWebIdentityResult>'\
'</AssumeRoleWithWebIdentityResponse>'
Excon.stub({method: :get, path: "/", idempotent: true}, { status: 200, body: document})
tests('#fetch_credentials token based') do
returns(
aws_access_key_id: 'dummykey',
aws_secret_access_key: 'dummysecret',
aws_session_token: 'dummytoken',
region: 'us-west-1',
sts_endpoint: "https://sts.amazonaws.com",
aws_credentials_expire_at: expires_at
) { Fog::AWS::Compute.fetch_credentials(use_iam_profile: true) }
end
ENV['AWS_ROLE_SESSION_NAME'] = nil
tests('#fetch_credentials token based without session name') do
returns(
aws_access_key_id: 'dummykey',
aws_secret_access_key: 'dummysecret',
aws_session_token: 'dummytoken',
region: 'us-west-1',
sts_endpoint: "https://sts.amazonaws.com",
aws_credentials_expire_at: expires_at
) { Fog::AWS::Compute.fetch_credentials(use_iam_profile: true, region: 'us-west-1') }
end
ENV["AWS_STS_REGIONAL_ENDPOINTS"] = "regional"
tests('#fetch_credentials with no region specified') do
returns(
aws_access_key_id: 'dummykey',
aws_secret_access_key: 'dummysecret',
aws_session_token: 'dummytoken',
region: 'us-west-1',
sts_endpoint: "https://sts.amazonaws.com",
aws_credentials_expire_at: expires_at
) { Fog::AWS::Compute.fetch_credentials(use_iam_profile: true) }
end
tests('#fetch_credentials with regional STS endpoint') do
returns(
aws_access_key_id: 'dummykey',
aws_secret_access_key: 'dummysecret',
aws_session_token: 'dummytoken',
region: 'us-west-1',
sts_endpoint: "https://sts.us-west-1.amazonaws.com",
aws_credentials_expire_at: expires_at
) { Fog::AWS::Compute.fetch_credentials(use_iam_profile: true, region: 'us-west-1') }
end
ENV["AWS_DEFAULT_REGION"] = "us-west-1"
tests('#fetch_credentials with regional STS endpoint with region in env') do
returns(
aws_access_key_id: 'dummykey',
aws_secret_access_key: 'dummysecret',
aws_session_token: 'dummytoken',
region: 'us-west-1',
sts_endpoint: "https://sts.us-west-1.amazonaws.com",
aws_credentials_expire_at: expires_at
) { Fog::AWS::Compute.fetch_credentials(use_iam_profile: true) }
end
ENV["AWS_STS_REGIONAL_ENDPOINTS"] = nil
ENV["AWS_DEFAULT_REGION"] = nil
ENV['AWS_WEB_IDENTITY_TOKEN_FILE'] = nil
storage = Fog::Storage.new(
:provider => 'AWS',
:region => 'us-east-1',
:use_iam_profile => true,
:aws_credentials_refresh_threshold_seconds => 30)
tests('#credentials_refresh_threshold') do
returns(30) { storage.send(:credentials_refresh_threshold) }
end
Fog::Time.now = storage.instance_variable_get(:@aws_credentials_expire_at) - 31
tests('#refresh_credentials_if_expired before credentials have expired and before refresh threshold') do
returns(nil) { storage.refresh_credentials_if_expired }
returns('dummykey') { storage.instance_variable_get(:@aws_access_key_id) }
returns('dummysecret') { storage.instance_variable_get(:@aws_secret_access_key) }
returns(expires_at) { storage.instance_variable_get(:@aws_credentials_expire_at) }
end
Fog::Time.now = Time.now
credentials['AccessKeyId'] = 'newkey-1'
credentials['SecretAccessKey'] = 'newsecret-1'
credentials['Expiration'] = (expires_at + 10).xmlschema
Excon.stub({ method: :get, path: '/latest/meta-data/iam/security-credentials/arole' }, { status: 200, body: Fog::JSON.encode(credentials) })
Fog::Time.now = storage.instance_variable_get(:@aws_credentials_expire_at) - 29
tests('#refresh_credentials_if_expired after refresh threshold is crossed but before expiration') do
returns(true) { storage.refresh_credentials_if_expired }
returns('newkey-1') { storage.instance_variable_get(:@aws_access_key_id) }
returns('newsecret-1') { storage.instance_variable_get(:@aws_secret_access_key) }
returns(expires_at + 10) { storage.instance_variable_get(:@aws_credentials_expire_at) }
end
Fog::Time.now = Time.now
credentials['AccessKeyId'] = 'newkey-2'
credentials['SecretAccessKey'] = 'newsecret-2'
credentials['Expiration'] = (expires_at + 20).xmlschema
Excon.stub({ method: :get, path: '/latest/meta-data/iam/security-credentials/arole' }, { status: 200, body: Fog::JSON.encode(credentials) })
Fog::Time.now = storage.instance_variable_get(:@aws_credentials_expire_at) + 1
tests('#refresh_credentials_if_expired after credentials have expired') do
returns(true) { storage.refresh_credentials_if_expired }
returns('newkey-2') { storage.instance_variable_get(:@aws_access_key_id) }
returns('newsecret-2') { storage.instance_variable_get(:@aws_secret_access_key) }
returns(expires_at + 20) { storage.instance_variable_get(:@aws_credentials_expire_at) }
end
Fog::Time.now = Time.now
compute = Fog::AWS::Compute.new(use_iam_profile: true)
tests('#credentials_refresh_threshold when "aws_credentials_refresh_threshold_seconds" is unspecified') do
returns(15) { compute.send(:credentials_refresh_threshold) }
end
default_credentials = Fog::AWS::Compute.fetch_credentials({})
tests('#fetch_credentials when the url 404s') do
Excon.stub({ method: :put, path: '/latest/api/token' }, { status: 404, body: 'not found' })
Excon.stub({ method: :get, path: '/latest/meta-data/iam/security-credentials/' }, { status: 404, body: 'not bound' })
Excon.stub({ method: :get, path: '/latest/meta-data/placement/availability-zone/' }, { status: 400, body: 'not found' })
returns(default_credentials) { Fog::AWS::Compute.fetch_credentials(use_iam_profile: true) }
end
mocked_credentials = {
aws_access_key_id: 'access-key-id',
aws_secret_access_key: 'secret-access-key',
aws_session_token: 'session-token',
aws_credentials_expire_at: Time.at(Time.now.to_i + 500).xmlschema
}
tests('#fetch_credentials when mocking') do
Fog.mock!
Fog::AWS::Compute::Mock.data[:iam_role_based_creds] = mocked_credentials
returns(mocked_credentials) { Fog::AWS::Compute.fetch_credentials(use_iam_profile: true) }
end
ensure
ENV['AWS_CONTAINER_CREDENTIALS_RELATIVE_URI'] = nil
ENV['AWS_WEB_IDENTITY_TOKEN_FILE'] = nil
Excon.stubs.clear
Excon.defaults[:mock] = old_mock_value
Fog.mock! if fog_was_mocked
end
end
|