1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
6 require 'webrick/https'
8 require 'helpers/users_test_helper'
10 class RemoteUsersTest < ActionDispatch::IntegrationTest
13 def salted_active_token(remote:)
14 salt_token(fixture: :active, remote: remote).sub('/zzzzz-', '/'+remote+'-')
18 token = salted_active_token(remote: remote)
19 {"HTTP_AUTHORIZATION" => "Bearer #{token}"}
22 # For remote authentication tests, we bring up a simple stub server
23 # (on a port chosen by webrick) and configure the SUT so the stub is
24 # responsible for clusters "zbbbb" (a well-behaved cluster) and
25 # "zbork" (a misbehaving cluster).
27 # Test cases can override the stub's default response to
28 # .../users/current by changing @stub_status and @stub_content.
31 clnt.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
32 HTTPClient.stubs(:new).returns clnt
34 @controller = Arvados::V1::UsersController.new
35 ready = Thread::Queue.new
40 ['zbbbb', 'zbork'].each do |clusterid|
41 srv = WEBrick::HTTPServer.new(
43 Logger: WEBrick::Log.new(
44 Rails.root.join("log", "webrick.log").to_s,
46 AccessLog: [[File.open(Rails.root.join(
47 "log", "webrick_access.log").to_s, 'a+'),
48 WEBrick::AccessLog::COMBINED_LOG_FORMAT]],
50 SSLVerifyClient: OpenSSL::SSL::VERIFY_NONE,
51 SSLPrivateKey: OpenSSL::PKey::RSA.new(
52 File.open(Rails.root.join("tmp", "self-signed.key")).read),
53 SSLCertificate: OpenSSL::X509::Certificate.new(
54 File.open(Rails.root.join("tmp", "self-signed.pem")).read),
55 SSLCertName: [["CN", WEBrick::Utils::getservername]],
56 StartCallback: lambda { ready.push(true) })
57 srv.mount_proc '/discovery/v1/apis/arvados/v1/rest' do |req, res|
58 Rails.cache.delete 'arvados_v1_rest_discovery'
59 res.body = Arvados::V1::SchemaController.new.send(:discovery_doc).to_json
61 srv.mount_proc '/arvados/v1/users/current' do |req, res|
62 if clusterid == 'zbbbb' and req.header['authorization'][0][10..14] == 'zbork'
63 # asking zbbbb about zbork should yield an error, zbbbb doesn't trust zbork
67 res.status = @stub_status
68 res.body = @stub_content.is_a?(String) ? @stub_content : @stub_content.to_json
75 @remote_host << "127.0.0.1:#{srv.config[:Port]}"
77 Rails.configuration.RemoteClusters = Rails.configuration.RemoteClusters.merge({zbbbb: ActiveSupport::InheritableOptions.new({Host: @remote_host[0]}),
78 zbork: ActiveSupport::InheritableOptions.new({Host: @remote_host[1]})})
79 Arvados::V1::SchemaController.any_instance.stubs(:root_url).returns "https://#{@remote_host[0]}"
82 uuid: 'zbbbb-tpzed-000000000000001',
83 email: 'foo@example.com',
92 @remote_server.each do |srv|
97 test 'authenticate with remote token' do
98 get '/arvados/v1/users/current',
99 params: {format: 'json'},
100 headers: auth(remote: 'zbbbb')
101 assert_response :success
102 assert_equal 'zbbbb-tpzed-000000000000001', json_response['uuid']
103 assert_equal false, json_response['is_admin']
104 assert_equal false, json_response['is_active']
105 assert_equal 'foo@example.com', json_response['email']
106 assert_equal 'barney', json_response['username']
108 # revoke original token
111 # re-authorize before cache expires
112 get '/arvados/v1/users/current',
113 params: {format: 'json'},
114 headers: auth(remote: 'zbbbb')
115 assert_response :success
117 # simulate cache expiry
118 ApiClientAuthorization.where(
119 uuid: salted_active_token(remote: 'zbbbb').split('/')[1]).
120 update_all(expires_at: db_current_time - 1.minute)
122 # re-authorize after cache expires
123 get '/arvados/v1/users/current',
124 params: {format: 'json'},
125 headers: auth(remote: 'zbbbb')
128 # simulate cached token indicating wrong user (e.g., local user
129 # entry was migrated out of the way taking the cached token with
130 # it, or authorizing cluster reassigned auth to a different user)
131 ApiClientAuthorization.where(
132 uuid: salted_active_token(remote: 'zbbbb').split('/')[1]).
133 update_all(user_id: users(:active).id)
135 # revive original token and re-authorize
137 @stub_content[:username] = 'blarney'
138 @stub_content[:email] = 'blarney@example.com'
139 get '/arvados/v1/users/current',
140 params: {format: 'json'},
141 headers: auth(remote: 'zbbbb')
142 assert_response :success
143 assert_equal 'barney', json_response['username'], 'local username should not change once assigned'
144 assert_equal 'blarney@example.com', json_response['email']
147 test 'remote user is deactivated' do
148 Rails.configuration.RemoteClusters['zbbbb'].ActivateUsers = true
149 get '/arvados/v1/users/current',
150 params: {format: 'json'},
151 headers: auth(remote: 'zbbbb')
152 assert_response :success
153 assert_equal true, json_response['is_active']
155 # revoke original token
156 @stub_content[:is_active] = false
157 @stub_content[:is_invited] = false
159 # simulate cache expiry
160 ApiClientAuthorization.where(
161 uuid: salted_active_token(remote: 'zbbbb').split('/')[1]).
162 update_all(expires_at: db_current_time - 1.minute)
164 # re-authorize after cache expires
165 get '/arvados/v1/users/current',
166 params: {format: 'json'},
167 headers: auth(remote: 'zbbbb')
168 assert_equal false, json_response['is_active']
172 test 'authenticate with remote token, remote username conflicts with local' do
173 @stub_content[:username] = 'active'
174 get '/arvados/v1/users/current',
175 params: {format: 'json'},
176 headers: auth(remote: 'zbbbb')
177 assert_response :success
178 assert_equal 'active2', json_response['username']
181 test 'authenticate with remote token, remote username is nil' do
182 @stub_content.delete :username
183 get '/arvados/v1/users/current',
184 params: {format: 'json'},
185 headers: auth(remote: 'zbbbb')
186 assert_response :success
187 assert_equal 'foo', json_response['username']
190 test 'authenticate with remote token from misbehaving remote cluster' do
191 get '/arvados/v1/users/current',
192 params: {format: 'json'},
193 headers: auth(remote: 'zbork')
197 test 'authenticate with remote token that fails validate' do
200 error: 'not authorized',
202 get '/arvados/v1/users/current',
203 params: {format: 'json'},
204 headers: auth(remote: 'zbbbb')
212 "v2/'; delete from users where 1=1; commit; select '/lol",
214 'v2/zzzzz-gj3su-077z32aux8dg2s1',
215 'v2/zzzzz-gj3su-077z32aux8dg2s1/',
216 'v2/3kg6k6lzmp9kj5cpkcoxie963cmvjahbt2fod9zru30k1jqdmi',
217 'v2/3kg6k6lzmp9kj5cpkcoxie963cmvjahbt2fod9zru30k1jqdmi/zzzzz-gj3su-077z32aux8dg2s1',
218 'v2//3kg6k6lzmp9kj5cpkcoxie963cmvjahbt2fod9zru30k1jqdmi',
219 'v8/zzzzz-gj3su-077z32aux8dg2s1/3kg6k6lzmp9kj5cpkcoxie963cmvjahbt2fod9zru30k1jqdmi',
220 '/zzzzz-gj3su-077z32aux8dg2s1/3kg6k6lzmp9kj5cpkcoxie963cmvjahbt2fod9zru30k1jqdmi',
221 '"v2/zzzzz-gj3su-077z32aux8dg2s1/3kg6k6lzmp9kj5cpkcoxie963cmvjahbt2fod9zru30k1jqdmi"',
226 test "authenticate with malformed remote token #{token}" do
227 get '/arvados/v1/users/current',
228 params: {format: 'json'},
229 headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"}
234 test "ignore extra fields in remote token" do
235 token = salted_active_token(remote: 'zbbbb') + '/foo/bar/baz/*'
236 get '/arvados/v1/users/current',
237 params: {format: 'json'},
238 headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"}
239 assert_response :success
242 test 'remote api server is not an api server' do
244 @stub_content = '<html>bad</html>'
245 get '/arvados/v1/users/current',
246 params: {format: 'json'},
247 headers: auth(remote: 'zbbbb')
251 ['zbbbb', 'z0000'].each do |token_valid_for|
252 test "validate #{token_valid_for}-salted token for remote cluster zbbbb" do
253 salted_token = salt_token(fixture: :active, remote: token_valid_for)
254 get '/arvados/v1/users/current',
255 params: {format: 'json', remote: 'zbbbb'},
256 headers: {"HTTP_AUTHORIZATION" => "Bearer #{salted_token}"}
257 if token_valid_for == 'zbbbb'
259 assert_equal(users(:active).uuid, json_response['uuid'])
266 test "list readable groups with salted token" do
267 salted_token = salt_token(fixture: :active, remote: 'zbbbb')
268 get '/arvados/v1/groups',
274 headers: {"HTTP_AUTHORIZATION" => "Bearer #{salted_token}"}
276 group_uuids = json_response['items'].collect { |i| i['uuid'] }
277 assert_includes(group_uuids, 'zzzzz-j7d0g-fffffffffffffff')
278 refute_includes(group_uuids, 'zzzzz-j7d0g-000000000000000')
279 assert_includes(group_uuids, groups(:aproject).uuid)
280 refute_includes(group_uuids, groups(:trashed_project).uuid)
281 refute_includes(group_uuids, groups(:testusergroup_admins).uuid)
284 test 'do not auto-activate user from untrusted cluster' do
285 Rails.configuration.RemoteClusters['zbbbb'].AutoSetupNewUsers = false
286 Rails.configuration.RemoteClusters['zbbbb'].ActivateUsers = false
287 get '/arvados/v1/users/current',
288 params: {format: 'json'},
289 headers: auth(remote: 'zbbbb')
290 assert_response :success
291 assert_equal 'zbbbb-tpzed-000000000000001', json_response['uuid']
292 assert_equal false, json_response['is_admin']
293 assert_equal false, json_response['is_active']
294 assert_equal 'foo@example.com', json_response['email']
295 assert_equal 'barney', json_response['username']
296 post '/arvados/v1/users/zbbbb-tpzed-000000000000001/activate',
297 params: {format: 'json'},
298 headers: auth(remote: 'zbbbb')
302 test 'auto-activate user from trusted cluster' do
303 Rails.configuration.RemoteClusters['zbbbb'].ActivateUsers = true
304 get '/arvados/v1/users/current',
305 params: {format: 'json'},
306 headers: auth(remote: 'zbbbb')
307 assert_response :success
308 assert_equal 'zbbbb-tpzed-000000000000001', json_response['uuid']
309 assert_equal false, json_response['is_admin']
310 assert_equal true, json_response['is_active']
311 assert_equal 'foo@example.com', json_response['email']
312 assert_equal 'barney', json_response['username']
315 test 'get user from Login cluster' do
316 Rails.configuration.Login.LoginCluster = 'zbbbb'
317 get '/arvados/v1/users/current',
318 params: {format: 'json'},
319 headers: auth(remote: 'zbbbb')
320 assert_response :success
321 assert_equal 'zbbbb-tpzed-000000000000001', json_response['uuid']
322 assert_equal true, json_response['is_admin']
323 assert_equal true, json_response['is_active']
324 assert_equal 'foo@example.com', json_response['email']
325 assert_equal 'barney', json_response['username']
328 test 'get inactive user from Login cluster when AutoSetupNewUsers is set' do
329 Rails.configuration.Login.LoginCluster = 'zbbbb'
330 Rails.configuration.Users.AutoSetupNewUsers = true
332 uuid: 'zbbbb-tpzed-000000000000001',
333 email: 'foo@example.com',
339 get '/arvados/v1/users/current',
340 params: {format: 'json'},
341 headers: auth(remote: 'zbbbb')
342 assert_response :success
343 assert_equal 'zbbbb-tpzed-000000000000001', json_response['uuid']
344 assert_equal false, json_response['is_admin']
345 assert_equal false, json_response['is_active']
346 assert_equal false, json_response['is_invited']
347 assert_equal 'foo@example.com', json_response['email']
348 assert_equal 'barney', json_response['username']
351 test 'pre-activate remote user' do
353 uuid: 'zbbbb-tpzed-000000000001234',
354 email: 'foo@example.com',
361 post '/arvados/v1/users',
364 "uuid" => "zbbbb-tpzed-000000000001234",
365 "email" => 'foo@example.com',
366 "username" => 'barney',
371 headers: {'HTTP_AUTHORIZATION' => "OAuth2 #{api_token(:admin)}"}
372 assert_response :success
374 get '/arvados/v1/users/current',
375 params: {format: 'json'},
376 headers: auth(remote: 'zbbbb')
377 assert_response :success
378 assert_equal 'zbbbb-tpzed-000000000001234', json_response['uuid']
379 assert_equal false, json_response['is_admin']
380 assert_equal true, json_response['is_active']
381 assert_equal 'foo@example.com', json_response['email']
382 assert_equal 'barney', json_response['username']
386 test 'remote user inactive without pre-activation' do
388 uuid: 'zbbbb-tpzed-000000000001234',
389 email: 'foo@example.com',
396 get '/arvados/v1/users/current',
397 params: {format: 'json'},
398 headers: auth(remote: 'zbbbb')
399 assert_response :success
400 assert_equal 'zbbbb-tpzed-000000000001234', json_response['uuid']
401 assert_equal false, json_response['is_admin']
402 assert_equal false, json_response['is_active']
403 assert_equal 'foo@example.com', json_response['email']
404 assert_equal 'barney', json_response['username']
407 test "validate unsalted v2 token for remote cluster zbbbb" do
408 auth = api_client_authorizations(:active)
409 token = "v2/#{auth.uuid}/#{auth.api_token}"
410 get '/arvados/v1/users/current',
411 params: {format: 'json', remote: 'zbbbb'},
412 headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"}
413 assert_response :success
414 assert_equal(users(:active).uuid, json_response['uuid'])
417 test 'container request with runtime_token' do
418 [["valid local", "v2/#{api_client_authorizations(:active).uuid}/#{api_client_authorizations(:active).api_token}"],
419 ["valid remote", "v2/zbbbb-gj3su-000000000000000/abc"],
420 ["invalid local", "v2/#{api_client_authorizations(:active).uuid}/fakefakefake"],
421 ["invalid remote", "v2/zbork-gj3su-000000000000000/abc"],
422 ].each do |label, runtime_token|
423 post '/arvados/v1/container_requests',
425 "container_request" => {
426 "command" => ["echo"],
427 "container_image" => "xyz",
428 "output_path" => "/",
430 "runtime_token" => runtime_token
433 headers: {"HTTP_AUTHORIZATION" => "Bearer #{api_client_authorizations(:active).api_token}"}
434 if label.include? "invalid"
437 assert_response :success
442 test 'authenticate with remote token, remote user is system user' do
443 @stub_content[:uuid] = 'zbbbb-tpzed-000000000000000'
444 get '/arvados/v1/users/current',
445 params: {format: 'json'},
446 headers: auth(remote: 'zbbbb')
447 assert_equal 'from cluster zbbbb', json_response['last_name']
450 test 'authenticate with remote token, remote user is anonymous user' do
451 @stub_content[:uuid] = 'zbbbb-tpzed-anonymouspublic'
452 get '/arvados/v1/users/current',
453 params: {format: 'json'},
454 headers: auth(remote: 'zbbbb')
455 assert_response :success
456 assert_equal 'zzzzz-tpzed-anonymouspublic', json_response['uuid']