X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/5c4d9d38dcee73a7ffb6221c80f707c3924da64f..1a599ec69ad8d533da1f12ad5d2c5789aa1c14e2:/services/api/test/integration/remote_user_test.rb diff --git a/services/api/test/integration/remote_user_test.rb b/services/api/test/integration/remote_user_test.rb index c38c230b22..04a45420fd 100644 --- a/services/api/test/integration/remote_user_test.rb +++ b/services/api/test/integration/remote_user_test.rb @@ -33,39 +33,50 @@ class RemoteUsersTest < ActionDispatch::IntegrationTest @controller = Arvados::V1::UsersController.new ready = Thread::Queue.new - srv = WEBrick::HTTPServer.new( - Port: 0, - Logger: WEBrick::Log.new( - Rails.root.join("log", "webrick.log").to_s, - WEBrick::Log::INFO), - AccessLog: [[File.open(Rails.root.join( - "log", "webrick_access.log").to_s, 'a+'), - WEBrick::AccessLog::COMBINED_LOG_FORMAT]], - SSLEnable: true, - SSLVerifyClient: OpenSSL::SSL::VERIFY_NONE, - SSLPrivateKey: OpenSSL::PKey::RSA.new( - File.open(Rails.root.join("tmp", "self-signed.key")).read), - SSLCertificate: OpenSSL::X509::Certificate.new( - File.open(Rails.root.join("tmp", "self-signed.pem")).read), - SSLCertName: [["CN", WEBrick::Utils::getservername]], - StartCallback: lambda { ready.push(true) }) - srv.mount_proc '/discovery/v1/apis/arvados/v1/rest' do |req, res| - Rails.cache.delete 'arvados_v1_rest_discovery' - res.body = Arvados::V1::SchemaController.new.send(:discovery_doc).to_json - end - srv.mount_proc '/arvados/v1/users/current' do |req, res| - res.status = @stub_status - res.body = @stub_content.is_a?(String) ? @stub_content : @stub_content.to_json - end - Thread.new do - srv.start + + @remote_server = [] + @remote_host = [] + + ['zbbbb', 'zbork'].each do |clusterid| + srv = WEBrick::HTTPServer.new( + Port: 0, + Logger: WEBrick::Log.new( + Rails.root.join("log", "webrick.log").to_s, + WEBrick::Log::INFO), + AccessLog: [[File.open(Rails.root.join( + "log", "webrick_access.log").to_s, 'a+'), + WEBrick::AccessLog::COMBINED_LOG_FORMAT]], + SSLEnable: true, + SSLVerifyClient: OpenSSL::SSL::VERIFY_NONE, + SSLPrivateKey: OpenSSL::PKey::RSA.new( + File.open(Rails.root.join("tmp", "self-signed.key")).read), + SSLCertificate: OpenSSL::X509::Certificate.new( + File.open(Rails.root.join("tmp", "self-signed.pem")).read), + SSLCertName: [["CN", WEBrick::Utils::getservername]], + StartCallback: lambda { ready.push(true) }) + srv.mount_proc '/discovery/v1/apis/arvados/v1/rest' do |req, res| + Rails.cache.delete 'arvados_v1_rest_discovery' + res.body = Arvados::V1::SchemaController.new.send(:discovery_doc).to_json + end + srv.mount_proc '/arvados/v1/users/current' do |req, res| + if clusterid == 'zbbbb' and req.header['authorization'][0][10..14] == 'zbork' + # asking zbbbb about zbork should yield an error, zbbbb doesn't trust zbork + res.status = 401 + return + end + res.status = @stub_status + res.body = @stub_content.is_a?(String) ? @stub_content : @stub_content.to_json + end + Thread.new do + srv.start + end + ready.pop + @remote_server << srv + @remote_host << "127.0.0.1:#{srv.config[:Port]}" end - ready.pop - @remote_server = srv - @remote_host = "127.0.0.1:#{srv.config[:Port]}" - Rails.configuration.remote_hosts['zbbbb'] = @remote_host - Rails.configuration.remote_hosts['zbork'] = @remote_host - Arvados::V1::SchemaController.any_instance.stubs(:root_url).returns "https://#{@remote_host}" + Rails.configuration.RemoteClusters = Rails.configuration.RemoteClusters.merge({zbbbb: ActiveSupport::InheritableOptions.new({Host: @remote_host[0]}), + zbork: ActiveSupport::InheritableOptions.new({Host: @remote_host[1]})}) + Arvados::V1::SchemaController.any_instance.stubs(:root_url).returns "https://#{@remote_host[0]}" @stub_status = 200 @stub_content = { uuid: 'zbbbb-tpzed-000000000000000', @@ -77,11 +88,15 @@ class RemoteUsersTest < ActionDispatch::IntegrationTest end teardown do - @remote_server.andand.stop + @remote_server.each do |srv| + srv.stop + end end test 'authenticate with remote token' do - get '/arvados/v1/users/current', {format: 'json'}, auth(remote: 'zbbbb') + get '/arvados/v1/users/current', + params: {format: 'json'}, + headers: auth(remote: 'zbbbb') assert_response :success assert_equal 'zbbbb-tpzed-000000000000000', json_response['uuid'] assert_equal false, json_response['is_admin'] @@ -93,7 +108,9 @@ class RemoteUsersTest < ActionDispatch::IntegrationTest @stub_status = 401 # re-authorize before cache expires - get '/arvados/v1/users/current', {format: 'json'}, auth(remote: 'zbbbb') + get '/arvados/v1/users/current', + params: {format: 'json'}, + headers: auth(remote: 'zbbbb') assert_response :success # simulate cache expiry @@ -102,7 +119,9 @@ class RemoteUsersTest < ActionDispatch::IntegrationTest update_all(expires_at: db_current_time - 1.minute) # re-authorize after cache expires - get '/arvados/v1/users/current', {format: 'json'}, auth(remote: 'zbbbb') + get '/arvados/v1/users/current', + params: {format: 'json'}, + headers: auth(remote: 'zbbbb') assert_response 401 # simulate cached token indicating wrong user (e.g., local user @@ -116,28 +135,60 @@ class RemoteUsersTest < ActionDispatch::IntegrationTest @stub_status = 200 @stub_content[:username] = 'blarney' @stub_content[:email] = 'blarney@example.com' - get '/arvados/v1/users/current', {format: 'json'}, auth(remote: 'zbbbb') + get '/arvados/v1/users/current', + params: {format: 'json'}, + headers: auth(remote: 'zbbbb') assert_response :success assert_equal 'barney', json_response['username'], 'local username should not change once assigned' assert_equal 'blarney@example.com', json_response['email'] end + test 'remote user is deactivated' do + Rails.configuration.RemoteClusters['zbbbb'].ActivateUsers = true + get '/arvados/v1/users/current', + params: {format: 'json'}, + headers: auth(remote: 'zbbbb') + assert_response :success + assert_equal true, json_response['is_active'] + + # revoke original token + @stub_content[:is_active] = false + + # simulate cache expiry + ApiClientAuthorization.where( + uuid: salted_active_token(remote: 'zbbbb').split('/')[1]). + update_all(expires_at: db_current_time - 1.minute) + + # re-authorize after cache expires + get '/arvados/v1/users/current', + params: {format: 'json'}, + headers: auth(remote: 'zbbbb') + assert_equal false, json_response['is_active'] + + end + test 'authenticate with remote token, remote username conflicts with local' do @stub_content[:username] = 'active' - get '/arvados/v1/users/current', {format: 'json'}, auth(remote: 'zbbbb') + get '/arvados/v1/users/current', + params: {format: 'json'}, + headers: auth(remote: 'zbbbb') assert_response :success assert_equal 'active2', json_response['username'] end test 'authenticate with remote token, remote username is nil' do @stub_content.delete :username - get '/arvados/v1/users/current', {format: 'json'}, auth(remote: 'zbbbb') + get '/arvados/v1/users/current', + params: {format: 'json'}, + headers: auth(remote: 'zbbbb') assert_response :success assert_equal 'foo', json_response['username'] end - test 'authenticate with remote token from misbhehaving remote cluster' do - get '/arvados/v1/users/current', {format: 'json'}, auth(remote: 'zbork') + test 'authenticate with remote token from misbehaving remote cluster' do + get '/arvados/v1/users/current', + params: {format: 'json'}, + headers: auth(remote: 'zbork') assert_response 401 end @@ -146,7 +197,9 @@ class RemoteUsersTest < ActionDispatch::IntegrationTest @stub_content = { error: 'not authorized', } - get '/arvados/v1/users/current', {format: 'json'}, auth(remote: 'zbbbb') + get '/arvados/v1/users/current', + params: {format: 'json'}, + headers: auth(remote: 'zbbbb') assert_response 401 end @@ -169,30 +222,36 @@ class RemoteUsersTest < ActionDispatch::IntegrationTest '///', ].each do |token| test "authenticate with malformed remote token #{token}" do - get '/arvados/v1/users/current', {format: 'json'}, {"HTTP_AUTHORIZATION" => "Bearer #{token}"} + get '/arvados/v1/users/current', + params: {format: 'json'}, + headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"} assert_response 401 end end test "ignore extra fields in remote token" do token = salted_active_token(remote: 'zbbbb') + '/foo/bar/baz/*' - get '/arvados/v1/users/current', {format: 'json'}, {"HTTP_AUTHORIZATION" => "Bearer #{token}"} + get '/arvados/v1/users/current', + params: {format: 'json'}, + headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"} assert_response :success end test 'remote api server is not an api server' do @stub_status = 200 @stub_content = 'bad' - get '/arvados/v1/users/current', {format: 'json'}, auth(remote: 'zbbbb') + get '/arvados/v1/users/current', + params: {format: 'json'}, + headers: auth(remote: 'zbbbb') assert_response 401 end ['zbbbb', 'z0000'].each do |token_valid_for| test "validate #{token_valid_for}-salted token for remote cluster zbbbb" do salted_token = salt_token(fixture: :active, remote: token_valid_for) - get '/arvados/v1/users/current', {format: 'json', remote: 'zbbbb'}, { - "HTTP_AUTHORIZATION" => "Bearer #{salted_token}" - } + get '/arvados/v1/users/current', + params: {format: 'json', remote: 'zbbbb'}, + headers: {"HTTP_AUTHORIZATION" => "Bearer #{salted_token}"} if token_valid_for == 'zbbbb' assert_response 200 assert_equal(users(:active).uuid, json_response['uuid']) @@ -204,13 +263,13 @@ class RemoteUsersTest < ActionDispatch::IntegrationTest test "list readable groups with salted token" do salted_token = salt_token(fixture: :active, remote: 'zbbbb') - get '/arvados/v1/groups', { - format: 'json', - remote: 'zbbbb', - limit: 10000, - }, { - "HTTP_AUTHORIZATION" => "Bearer #{salted_token}" - } + get '/arvados/v1/groups', + params: { + format: 'json', + remote: 'zbbbb', + limit: 10000, + }, + headers: {"HTTP_AUTHORIZATION" => "Bearer #{salted_token}"} assert_response 200 group_uuids = json_response['items'].collect { |i| i['uuid'] } assert_includes(group_uuids, 'zzzzz-j7d0g-fffffffffffffff') @@ -220,9 +279,29 @@ class RemoteUsersTest < ActionDispatch::IntegrationTest refute_includes(group_uuids, groups(:testusergroup_admins).uuid) end + test 'do not auto-activate user from untrusted cluster' do + Rails.configuration.RemoteClusters['zbbbb'].AutoSetupNewUsers = false + Rails.configuration.RemoteClusters['zbbbb'].ActivateUsers = false + get '/arvados/v1/users/current', + params: {format: 'json'}, + headers: auth(remote: 'zbbbb') + assert_response :success + assert_equal 'zbbbb-tpzed-000000000000000', json_response['uuid'] + assert_equal false, json_response['is_admin'] + assert_equal false, json_response['is_active'] + assert_equal 'foo@example.com', json_response['email'] + assert_equal 'barney', json_response['username'] + post '/arvados/v1/users/zbbbb-tpzed-000000000000000/activate', + params: {format: 'json'}, + headers: auth(remote: 'zbbbb') + assert_response 422 + end + test 'auto-activate user from trusted cluster' do - Rails.configuration.auto_activate_users_from = ['zbbbb'] - get '/arvados/v1/users/current', {format: 'json'}, auth(remote: 'zbbbb') + Rails.configuration.RemoteClusters['zbbbb'].ActivateUsers = true + get '/arvados/v1/users/current', + params: {format: 'json'}, + headers: auth(remote: 'zbbbb') assert_response :success assert_equal 'zbbbb-tpzed-000000000000000', json_response['uuid'] assert_equal false, json_response['is_admin'] @@ -231,24 +310,106 @@ class RemoteUsersTest < ActionDispatch::IntegrationTest assert_equal 'barney', json_response['username'] end + test 'get user from Login cluster' do + Rails.configuration.Login.LoginCluster = 'zbbbb' + get '/arvados/v1/users/current', + params: {format: 'json'}, + headers: auth(remote: 'zbbbb') + assert_response :success + assert_equal 'zbbbb-tpzed-000000000000000', json_response['uuid'] + assert_equal true, json_response['is_admin'] + assert_equal true, json_response['is_active'] + assert_equal 'foo@example.com', json_response['email'] + assert_equal 'barney', json_response['username'] + end + test 'pre-activate remote user' do - post '/arvados/v1/users', { - "user" => { - "uuid" => "zbbbb-tpzed-000000000000000", - "email" => 'foo@example.com', - "username" => 'barney', - "is_active" => true - } - }, {'HTTP_AUTHORIZATION' => "OAuth2 #{api_token(:admin)}"} + @stub_content = { + uuid: 'zbbbb-tpzed-000000000001234', + email: 'foo@example.com', + username: 'barney', + is_admin: true, + is_active: true, + } + + post '/arvados/v1/users', + params: { + "user" => { + "uuid" => "zbbbb-tpzed-000000000001234", + "email" => 'foo@example.com', + "username" => 'barney', + "is_active" => true, + "is_admin" => false + } + }, + headers: {'HTTP_AUTHORIZATION' => "OAuth2 #{api_token(:admin)}"} assert_response :success - get '/arvados/v1/users/current', {format: 'json'}, auth(remote: 'zbbbb') + get '/arvados/v1/users/current', + params: {format: 'json'}, + headers: auth(remote: 'zbbbb') assert_response :success - assert_equal 'zbbbb-tpzed-000000000000000', json_response['uuid'] - assert_equal nil, json_response['is_admin'] + assert_equal 'zbbbb-tpzed-000000000001234', json_response['uuid'] + assert_equal false, json_response['is_admin'] assert_equal true, json_response['is_active'] assert_equal 'foo@example.com', json_response['email'] assert_equal 'barney', json_response['username'] end + + test 'remote user inactive without pre-activation' do + @stub_content = { + uuid: 'zbbbb-tpzed-000000000001234', + email: 'foo@example.com', + username: 'barney', + is_admin: true, + is_active: true, + } + + get '/arvados/v1/users/current', + params: {format: 'json'}, + headers: auth(remote: 'zbbbb') + assert_response :success + assert_equal 'zbbbb-tpzed-000000000001234', json_response['uuid'] + assert_equal false, json_response['is_admin'] + assert_equal false, json_response['is_active'] + assert_equal 'foo@example.com', json_response['email'] + assert_equal 'barney', json_response['username'] + end + + test "validate unsalted v2 token for remote cluster zbbbb" do + auth = api_client_authorizations(:active) + token = "v2/#{auth.uuid}/#{auth.api_token}" + get '/arvados/v1/users/current', + params: {format: 'json', remote: 'zbbbb'}, + headers: {"HTTP_AUTHORIZATION" => "Bearer #{token}"} + assert_response :success + assert_equal(users(:active).uuid, json_response['uuid']) + end + + test 'container request with runtime_token' do + [["valid local", "v2/#{api_client_authorizations(:active).uuid}/#{api_client_authorizations(:active).api_token}"], + ["valid remote", "v2/zbbbb-gj3su-000000000000000/abc"], + ["invalid local", "v2/#{api_client_authorizations(:active).uuid}/fakefakefake"], + ["invalid remote", "v2/zbork-gj3su-000000000000000/abc"], + ].each do |label, runtime_token| + post '/arvados/v1/container_requests', + params: { + "container_request" => { + "command" => ["echo"], + "container_image" => "xyz", + "output_path" => "/", + "cwd" => "/", + "runtime_token" => runtime_token + } + }, + headers: {"HTTP_AUTHORIZATION" => "Bearer #{api_client_authorizations(:active).api_token}"} + if label.include? "invalid" + assert_response 422 + else + assert_response :success + end + end + end + end