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.
25 @controller = Arvados::V1::UsersController.new
26 ready = Thread::Queue.new
27 srv = WEBrick::HTTPServer.new(
29 Logger: WEBrick::Log.new(
30 Rails.root.join("log", "webrick.log").to_s,
32 AccessLog: [[File.open(Rails.root.join(
33 "log", "webrick_access.log").to_s, 'a+'),
34 WEBrick::AccessLog::COMBINED_LOG_FORMAT]],
36 SSLVerifyClient: OpenSSL::SSL::VERIFY_NONE,
37 SSLPrivateKey: OpenSSL::PKey::RSA.new(
38 File.open(Rails.root.join("tmp", "self-signed.key")).read),
39 SSLCertificate: OpenSSL::X509::Certificate.new(
40 File.open(Rails.root.join("tmp", "self-signed.pem")).read),
41 SSLCertName: [["CN", WEBrick::Utils::getservername]],
42 StartCallback: lambda { ready.push(true) })
43 srv.mount_proc '/discovery/v1/apis/arvados/v1/rest' do |req, res|
44 Rails.cache.delete 'arvados_v1_rest_discovery'
45 res.body = Arvados::V1::SchemaController.new.send(:discovery_doc).to_json
47 srv.mount_proc '/arvados/v1/users/current' do |req, res|
48 res.status = @stub_status
49 res.body = @stub_content.is_a?(String) ? @stub_content : @stub_content.to_json
56 @remote_host = "127.0.0.1:#{srv.config[:Port]}"
57 Rails.configuration.remote_hosts['zbbbb'] = @remote_host
58 Rails.configuration.remote_hosts['zbork'] = @remote_host
59 Arvados::V1::SchemaController.any_instance.stubs(:root_url).returns "https://#{@remote_host}"
62 uuid: 'zbbbb-tpzed-000000000000000',
69 @remote_server.andand.stop
72 test 'authenticate with remote token' do
73 get '/arvados/v1/users/current', {}, auth(remote: 'zbbbb')
74 assert_response :success
75 assert_equal 'zbbbb-tpzed-000000000000000', json_response['uuid']
76 assert_equal false, json_response['is_admin']
79 test 'authenticate with remote token from misbhehaving remote cluster' do
80 get '/arvados/v1/users/current', {}, auth(remote: 'zbork')
84 test 'authenticate with remote token that fails validate' do
87 error: 'not authorized',
89 get '/arvados/v1/users/current', {}, auth(remote: 'zbbbb')
93 test 'remote api server is not an api server' do
95 @stub_content = '<html>bad</html>'
96 get '/arvados/v1/users/current', {}, auth(remote: 'zbbbb')
100 ['zbbbb', 'z0000'].each do |token_valid_for|
101 test "validate #{token_valid_for}-salted token for remote cluster zbbbb" do
102 salted_token = salt_token(fixture: :active, remote: token_valid_for)
103 get '/arvados/v1/users/current', {format: 'json', remote: 'zbbbb'}, {
104 "HTTP_AUTHORIZATION" => "Bearer #{salted_token}"
106 if token_valid_for == 'zbbbb'
108 assert_equal(users(:active).uuid, json_response['uuid'])