11453: Merge branch 'master' into 11453-federated-tokens
[arvados.git] / services / api / test / integration / remote_user_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'webrick'
6 require 'webrick/https'
7 require 'test_helper'
8 require 'helpers/users_test_helper'
9
10 class RemoteUsersTest < ActionDispatch::IntegrationTest
11   def auth(remote:)
12     token = salt_token(fixture: :active, remote: remote)
13     token.sub!('/zzzzz-', '/'+remote+'-')
14     {"HTTP_AUTHORIZATION" => "Bearer #{token}"}
15   end
16
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).
21   #
22   # Test cases can override the stub's default response to
23   # .../users/current by changing @stub_status and @stub_content.
24   setup do
25     clnt = HTTPClient.new
26     clnt.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
27     HTTPClient.stubs(:new).returns clnt
28
29     @controller = Arvados::V1::UsersController.new
30     ready = Thread::Queue.new
31     srv = WEBrick::HTTPServer.new(
32       Port: 0,
33       Logger: WEBrick::Log.new(
34         Rails.root.join("log", "webrick.log").to_s,
35         WEBrick::Log::INFO),
36       AccessLog: [[File.open(Rails.root.join(
37                               "log", "webrick_access.log").to_s, 'a+'),
38                    WEBrick::AccessLog::COMBINED_LOG_FORMAT]],
39       SSLEnable: true,
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
50     end
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
54     end
55     Thread.new do
56       srv.start
57     end
58     ready.pop
59     @remote_server = srv
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}"
64     @stub_status = 200
65     @stub_content = {
66       uuid: 'zbbbb-tpzed-000000000000000',
67       is_admin: true,
68       is_active: true,
69     }
70   end
71
72   teardown do
73     @remote_server.andand.stop
74   end
75
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']
81   end
82
83   test 'authenticate with remote token from misbhehaving remote cluster' do
84     get '/arvados/v1/users/current', {format: 'json'}, auth(remote: 'zbork')
85     assert_response 401
86   end
87
88   test 'authenticate with remote token that fails validate' do
89     @stub_status = 401
90     @stub_content = {
91       error: 'not authorized',
92     }
93     get '/arvados/v1/users/current', {format: 'json'}, auth(remote: 'zbbbb')
94     assert_response 401
95   end
96
97   test 'remote api server is not an api server' do
98     @stub_status = 200
99     @stub_content = '<html>bad</html>'
100     get '/arvados/v1/users/current', {format: 'json'}, auth(remote: 'zbbbb')
101     assert_response 401
102   end
103
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}"
109           }
110       if token_valid_for == 'zbbbb'
111         assert_response 200
112         assert_equal(users(:active).uuid, json_response['uuid'])
113       else
114         assert_response 401
115       end
116     end
117   end
118
119   test "list readable groups with salted token" do
120     salted_token = salt_token(fixture: :active, remote: 'zbbbb')
121     get '/arvados/v1/groups', {
122           format: 'json',
123           remote: 'zbbbb',
124           limit: 10000,
125         }, {
126           "HTTP_AUTHORIZATION" => "Bearer #{salted_token}"
127         }
128     assert_response 200
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')
132   end
133 end