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
36 srv = WEBrick::HTTPServer.new(
38 Logger: WEBrick::Log.new(
39 Rails.root.join("log", "webrick.log").to_s,
41 AccessLog: [[File.open(Rails.root.join(
42 "log", "webrick_access.log").to_s, 'a+'),
43 WEBrick::AccessLog::COMBINED_LOG_FORMAT]],
45 SSLVerifyClient: OpenSSL::SSL::VERIFY_NONE,
46 SSLPrivateKey: OpenSSL::PKey::RSA.new(
47 File.open(Rails.root.join("tmp", "self-signed.key")).read),
48 SSLCertificate: OpenSSL::X509::Certificate.new(
49 File.open(Rails.root.join("tmp", "self-signed.pem")).read),
50 SSLCertName: [["CN", WEBrick::Utils::getservername]],
51 StartCallback: lambda { ready.push(true) })
52 srv.mount_proc '/discovery/v1/apis/arvados/v1/rest' do |req, res|
53 Rails.cache.delete 'arvados_v1_rest_discovery'
54 res.body = Arvados::V1::SchemaController.new.send(:discovery_doc).to_json
56 srv.mount_proc '/arvados/v1/users/current' do |req, res|
57 res.status = @stub_status
58 res.body = @stub_content.is_a?(String) ? @stub_content : @stub_content.to_json
65 @remote_host = "127.0.0.1:#{srv.config[:Port]}"
66 Rails.configuration.remote_hosts['zbbbb'] = @remote_host
67 Rails.configuration.remote_hosts['zbork'] = @remote_host
68 Arvados::V1::SchemaController.any_instance.stubs(:root_url).returns "https://#{@remote_host}"
71 uuid: 'zbbbb-tpzed-000000000000000',
78 @remote_server.andand.stop
81 test 'authenticate with remote token' do
82 get '/arvados/v1/users/current', {format: 'json'}, auth(remote: 'zbbbb')
83 assert_response :success
84 assert_equal 'zbbbb-tpzed-000000000000000', json_response['uuid']
85 assert_equal false, json_response['is_admin']
87 # revoke original token
90 # re-authorize before cache expires
91 get '/arvados/v1/users/current', {format: 'json'}, auth(remote: 'zbbbb')
92 assert_response :success
94 # simulate cache expiry
95 ApiClientAuthorization.where(
96 uuid: salted_active_token(remote: 'zbbbb').split('/')[1]).
97 update_all(expires_at: db_current_time - 1.minute)
99 # re-authorize after cache expires
100 get '/arvados/v1/users/current', {format: 'json'}, auth(remote: 'zbbbb')
103 # revive original token and re-authorize
105 get '/arvados/v1/users/current', {format: 'json'}, auth(remote: 'zbbbb')
106 assert_response :success
109 test 'authenticate with remote token from misbhehaving remote cluster' do
110 get '/arvados/v1/users/current', {format: 'json'}, auth(remote: 'zbork')
114 test 'authenticate with remote token that fails validate' do
117 error: 'not authorized',
119 get '/arvados/v1/users/current', {format: 'json'}, auth(remote: 'zbbbb')
127 "v2/'; delete from users where 1=1; commit; select '/lol",
129 'v2/zzzzz-gj3su-077z32aux8dg2s1',
130 'v2/zzzzz-gj3su-077z32aux8dg2s1/',
131 'v2/3kg6k6lzmp9kj5cpkcoxie963cmvjahbt2fod9zru30k1jqdmi',
132 'v2/3kg6k6lzmp9kj5cpkcoxie963cmvjahbt2fod9zru30k1jqdmi/zzzzz-gj3su-077z32aux8dg2s1',
133 'v2//3kg6k6lzmp9kj5cpkcoxie963cmvjahbt2fod9zru30k1jqdmi',
134 'v8/zzzzz-gj3su-077z32aux8dg2s1/3kg6k6lzmp9kj5cpkcoxie963cmvjahbt2fod9zru30k1jqdmi',
135 '/zzzzz-gj3su-077z32aux8dg2s1/3kg6k6lzmp9kj5cpkcoxie963cmvjahbt2fod9zru30k1jqdmi',
136 '"v2/zzzzz-gj3su-077z32aux8dg2s1/3kg6k6lzmp9kj5cpkcoxie963cmvjahbt2fod9zru30k1jqdmi"',
141 test "authenticate with malformed remote token #{token}" do
142 get '/arvados/v1/users/current', {format: 'json'}, {"HTTP_AUTHORIZATION" => "Bearer #{token}"}
147 test "ignore extra fields in remote token" do
148 token = salted_active_token(remote: 'zbbbb') + '/foo/bar/baz/*'
149 get '/arvados/v1/users/current', {format: 'json'}, {"HTTP_AUTHORIZATION" => "Bearer #{token}"}
150 assert_response :success
153 test 'remote api server is not an api server' do
155 @stub_content = '<html>bad</html>'
156 get '/arvados/v1/users/current', {format: 'json'}, auth(remote: 'zbbbb')
160 ['zbbbb', 'z0000'].each do |token_valid_for|
161 test "validate #{token_valid_for}-salted token for remote cluster zbbbb" do
162 salted_token = salt_token(fixture: :active, remote: token_valid_for)
163 get '/arvados/v1/users/current', {format: 'json', remote: 'zbbbb'}, {
164 "HTTP_AUTHORIZATION" => "Bearer #{salted_token}"
166 if token_valid_for == 'zbbbb'
168 assert_equal(users(:active).uuid, json_response['uuid'])
175 test "list readable groups with salted token" do
176 salted_token = salt_token(fixture: :active, remote: 'zbbbb')
177 get '/arvados/v1/groups', {
182 "HTTP_AUTHORIZATION" => "Bearer #{salted_token}"
185 group_uuids = json_response['items'].collect { |i| i['uuid'] }
186 assert_includes(group_uuids, 'zzzzz-j7d0g-fffffffffffffff')
187 refute_includes(group_uuids, 'zzzzz-j7d0g-000000000000000')
188 assert_includes(group_uuids, groups(:aproject).uuid)
189 refute_includes(group_uuids, groups(:trashed_project).uuid)
190 refute_includes(group_uuids, groups(:testusergroup_admins).uuid)