File: client_construction_aws_auth_spec.rb

package info (click to toggle)
ruby-mongo 2.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,764 kB
  • sloc: ruby: 108,806; makefile: 5; sh: 2
file content (203 lines) | stat: -rw-r--r-- 6,584 bytes parent folder | download
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
# frozen_string_literal: true
# rubocop:todo all

require 'spec_helper'

describe 'Client construction with AWS auth' do
  require_aws_auth

  let(:client) do
    new_local_client(SpecConfig.instance.addresses,
      SpecConfig.instance.ssl_options.merge(
        auth_mech: :aws,
        connect_timeout: 3.44, socket_timeout: 3.45,
        server_selection_timeout: 3.46))
  end

  let(:authenticated_user_info) do
    # https://stackoverflow.com/questions/21414608/mongodb-show-current-user
    info = client.database.command(connectionStatus: 1).documents.first
    info[:authInfo][:authenticatedUsers].first
  end

  let(:authenticated_user_name) { authenticated_user_info[:user] }

  shared_examples_for 'connects successfully' do
    it 'connects successfully' do
      client['foo'].insert_one(test: true)
    end
  end

  context 'credentials specified explicitly' do

    let(:username) { ENV.fetch('MONGO_RUBY_DRIVER_AWS_AUTH_ACCESS_KEY_ID') }
    let(:password) { ENV.fetch('MONGO_RUBY_DRIVER_AWS_AUTH_SECRET_ACCESS_KEY') }
    let(:session_token) { ENV.fetch('MONGO_RUBY_DRIVER_AWS_AUTH_SESSION_TOKEN') }
    let(:address_strs) { SpecConfig.instance.addresses.join(',') }

    context 'regular credentials' do
      require_auth 'aws-regular'

      context 'via Ruby options' do
        let(:client) do
          new_local_client(SpecConfig.instance.addresses,
            SpecConfig.instance.ssl_options.merge(
              auth_mech: :aws,
              user: username,
              password: password,
              connect_timeout: 3.34, socket_timeout: 3.35,
              server_selection_timeout: 3.36))
        end

        it_behaves_like 'connects successfully'

        it 'uses the expected user' do
          puts "Authenticated as #{authenticated_user_name}"
          authenticated_user_name.should =~ /^arn:aws:iam:/
        end
      end

      context 'via URI' do
        let(:client) do
          new_local_client("mongodb://#{CGI.escape(username)}:#{CGI.escape(password)}@#{address_strs}/test?authMechanism=MONGODB-AWS&serverSelectionTimeoutMS=3.26")
        end

        it_behaves_like 'connects successfully'

        it 'uses the expected user' do
          puts "Authenticated as #{authenticated_user_name}"
          authenticated_user_name.should =~ /^arn:aws:iam:/
        end
      end
    end

    context 'temporary credentials' do
      require_auth 'aws-assume-role'

      context 'via Ruby options' do
        let(:client) do
          new_local_client(SpecConfig.instance.addresses,
            SpecConfig.instance.ssl_options.merge(
              auth_mech: :aws,
              user: username,
              password: password,
              auth_mech_properties: {
                aws_session_token: session_token,
              },
              connect_timeout: 3.34, socket_timeout: 3.35,
              server_selection_timeout: 3.36))
        end

        it_behaves_like 'connects successfully'

        it 'uses the expected user' do
          puts "Authenticated as #{authenticated_user_name}"
          authenticated_user_name.should =~ /^arn:aws:sts:/
        end
      end

      context 'via URI' do
        let(:client) do
          new_local_client("mongodb://#{CGI.escape(username)}:#{CGI.escape(password)}@#{address_strs}/test?authMechanism=MONGODB-AWS&serverSelectionTimeoutMS=3.26&authMechanismProperties=AWS_SESSION_TOKEN:#{CGI.escape(session_token)}")
        end

        it_behaves_like 'connects successfully'

        it 'uses the expected user' do
          puts "Authenticated as #{authenticated_user_name}"
          authenticated_user_name.should =~ /^arn:aws:sts:/
        end
      end
    end
  end

  context 'credentials specified via environment' do
    require_auth 'aws-regular', 'aws-assume-role'

    context 'no credentials given explicitly to Client constructor' do
      context 'credentials not provided in environment' do
        local_env(
          'AWS_ACCESS_KEY_ID' => nil,
          'AWS_SECRET_ACCESS_KEY' => nil,
          'AWS_SESSION_TOKEN' => nil,
        )

        it 'does not connect' do
          lambda do
            client['foo'].insert_one(test: true)
          end.should raise_error(Mongo::Auth::Aws::CredentialsNotFound, /Could not locate AWS credentials/)
        end
      end

      context 'credentials provided in environment' do
        local_env do
          {
            'AWS_ACCESS_KEY_ID' => ENV.fetch('MONGO_RUBY_DRIVER_AWS_AUTH_ACCESS_KEY_ID'),
            'AWS_SECRET_ACCESS_KEY' => ENV.fetch('MONGO_RUBY_DRIVER_AWS_AUTH_SECRET_ACCESS_KEY'),
            'AWS_SESSION_TOKEN' => ENV['MONGO_RUBY_DRIVER_AWS_AUTH_SESSION_TOKEN'],
          }
        end

        it_behaves_like 'connects successfully'

        context 'when using regular credentials' do
          require_auth 'aws-regular'

          it 'uses the expected user' do
            puts "Authenticated as #{authenticated_user_name}"
            authenticated_user_name.should =~ /^arn:/
            authenticated_user_name.should_not =~ /^arn:.*assumed-role/
          end
        end

        context 'when using assume role credentials' do
          require_auth 'aws-assume-role'

          it 'uses the expected user' do
            puts "Authenticated as #{authenticated_user_name}"
            authenticated_user_name.should =~ /^arn:.*assumed-role/
          end
        end
      end
    end
  end

  context 'credentials specified via instance/task metadata' do
    require_auth 'aws-ec2', 'aws-ecs', 'aws-web-identity'

    before(:all) do
      # No explicit credentials are expected in the tested configurations
      ENV['AWS_ACCESS_KEY_ID'].should be_nil
    end

    it_behaves_like 'connects successfully'

    context 'when using ec2 instance role' do
      require_auth 'aws-ec2'

      it 'uses the expected user' do
        puts "Authenticated as #{authenticated_user_name}"
        authenticated_user_name.should =~ /^arn:aws:sts:.*assumed-role.*instance_profile_role/
      end
    end

    context 'when using ecs task role' do
      require_auth 'aws-ecs'

      it 'uses the expected user' do
        puts "Authenticated as #{authenticated_user_name}"
        authenticated_user_name.should =~ /^arn:aws:sts:.*assumed-role.*ecstaskexecutionrole/i
      end
    end

    context 'when using web identity' do
      require_auth 'aws-web-identity'

      it 'uses the expected user' do
        puts "Authenticated as #{authenticated_user_name}"
        authenticated_user_name.should =~ /^arn:aws:sts:.*assumed-role.*webIdentityTestRole/i
      end
    end
  end

end