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
12 token = salt_token(fixture: :active, remote: remote)
13 token.sub!('/zzzzz-', '/'+remote+'-')
14 {"HTTP_AUTHORIZATION" => "Bearer #{token}"}
17 # For remote authentication tests, we bring up a simple stub server
18 # (on a port chosen by webrick) and configure the SUT so the stub is
19 # responsible for clusters "zbbbb" (a well-behaved cluster) and
20 # "zbork" (a misbehaving cluster).
22 # Test cases can override the stub's default response to
23 # .../users/current by changing @stub_status and @stub_content.
26 clnt.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
27 HTTPClient.stubs(:new).returns clnt
29 @controller = Arvados::V1::UsersController.new
30 ready = Thread::Queue.new
31 srv = WEBrick::HTTPServer.new(
33 Logger: WEBrick::Log.new(
34 Rails.root.join("log", "webrick.log").to_s,
36 AccessLog: [[File.open(Rails.root.join(
37 "log", "webrick_access.log").to_s, 'a+'),
38 WEBrick::AccessLog::COMBINED_LOG_FORMAT]],
40 SSLVerifyClient: OpenSSL::SSL::VERIFY_NONE,
41 SSLPrivateKey: OpenSSL::PKey::RSA.new(
42 File.open(Rails.root.join("tmp", "self-signed.key")).read),
43 SSLCertificate: OpenSSL::X509::Certificate.new(
44 File.open(Rails.root.join("tmp", "self-signed.pem")).read),
45 SSLCertName: [["CN", WEBrick::Utils::getservername]],
46 StartCallback: lambda { ready.push(true) })
47 srv.mount_proc '/discovery/v1/apis/arvados/v1/rest' do |req, res|
48 Rails.cache.delete 'arvados_v1_rest_discovery'
49 res.body = Arvados::V1::SchemaController.new.send(:discovery_doc).to_json
51 srv.mount_proc '/arvados/v1/users/current' do |req, res|
52 res.status = @stub_status
53 res.body = @stub_content.is_a?(String) ? @stub_content : @stub_content.to_json
60 @remote_host = "127.0.0.1:#{srv.config[:Port]}"
61 Rails.configuration.remote_hosts['zbbbb'] = @remote_host
62 Rails.configuration.remote_hosts['zbork'] = @remote_host
63 Arvados::V1::SchemaController.any_instance.stubs(:root_url).returns "https://#{@remote_host}"
66 uuid: 'zbbbb-tpzed-000000000000000',
73 @remote_server.andand.stop
76 test 'authenticate with remote token' do
77 get '/arvados/v1/users/current', {format: 'json'}, auth(remote: 'zbbbb')
78 assert_response :success
79 assert_equal 'zbbbb-tpzed-000000000000000', json_response['uuid']
80 assert_equal false, json_response['is_admin']
83 test 'authenticate with remote token from misbhehaving remote cluster' do
84 get '/arvados/v1/users/current', {format: 'json'}, auth(remote: 'zbork')
88 test 'authenticate with remote token that fails validate' do
91 error: 'not authorized',
93 get '/arvados/v1/users/current', {format: 'json'}, auth(remote: 'zbbbb')
97 test 'remote api server is not an api server' do
99 @stub_content = '<html>bad</html>'
100 get '/arvados/v1/users/current', {format: 'json'}, auth(remote: 'zbbbb')
104 ['zbbbb', 'z0000'].each do |token_valid_for|
105 test "validate #{token_valid_for}-salted token for remote cluster zbbbb" do
106 salted_token = salt_token(fixture: :active, remote: token_valid_for)
107 get '/arvados/v1/users/current', {format: 'json', remote: 'zbbbb'}, {
108 "HTTP_AUTHORIZATION" => "Bearer #{salted_token}"
110 if token_valid_for == 'zbbbb'
112 assert_equal(users(:active).uuid, json_response['uuid'])
119 test "list readable groups with salted token" do
120 salted_token = salt_token(fixture: :active, remote: 'zbbbb')
121 get '/arvados/v1/groups', {
126 "HTTP_AUTHORIZATION" => "Bearer #{salted_token}"
129 group_uuids = json_response['items'].collect { |i| i['uuid'] }
130 assert_includes(group_uuids, 'zzzzz-j7d0g-fffffffffffffff')
131 refute_includes(group_uuids, 'zzzzz-j7d0g-000000000000000')