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}"}
18 @controller = Arvados::V1::UsersController.new
19 ready = Thread::Queue.new
20 srv = WEBrick::HTTPServer.new(
22 Logger: WEBrick::Log.new(
23 Rails.root.join("log", "webrick.log").to_s,
25 AccessLog: [[File.open(Rails.root.join(
26 "log", "webrick_access.log").to_s, 'a+'),
27 WEBrick::AccessLog::COMBINED_LOG_FORMAT]],
29 SSLVerifyClient: OpenSSL::SSL::VERIFY_NONE,
30 SSLPrivateKey: OpenSSL::PKey::RSA.new(
31 File.open(Rails.root.join("tmp", "self-signed.key")).read),
32 SSLCertificate: OpenSSL::X509::Certificate.new(
33 File.open(Rails.root.join("tmp", "self-signed.pem")).read),
34 SSLCertName: [["CN", WEBrick::Utils::getservername]],
35 StartCallback: lambda { ready.push(true) })
36 srv.mount_proc '/discovery/v1/apis/arvados/v1/rest' do |req, res|
37 Rails.cache.delete 'arvados_v1_rest_discovery'
38 res.body = Arvados::V1::SchemaController.new.send(:discovery_doc).to_json
40 srv.mount_proc '/arvados/v1/users/current' do |req, res|
41 res.status = @stub_status
42 res.body = @stub_content.is_a?(String) ? @stub_content : @stub_content.to_json
49 @remote_host = "127.0.0.1:#{srv.config[:Port]}"
50 Rails.configuration.remote_hosts['zbbbb'] = @remote_host
51 Rails.configuration.remote_hosts['zcccc'] = @remote_host
52 Arvados::V1::SchemaController.any_instance.stubs(:root_url).returns "https://#{@remote_host}"
55 uuid: 'zbbbb-tpzed-000000000000000',
62 @remote_server.andand.stop
65 test 'authenticate with remote token' do
66 get '/arvados/v1/users/current', {}, auth(remote: 'zbbbb')
67 assert_response :success
68 assert_equal 'zbbbb-tpzed-000000000000000', json_response['uuid']
69 assert_equal false, json_response['is_admin']
72 test 'authenticate with remote token from wrong site' do
73 @stub_content[:uuid] = 'zcccc-tpzed-000000000000000'
74 get '/arvados/v1/users/current', {}, auth(remote: 'zbbbb')
78 test 'authenticate with remote token that fails validate' do
81 error: 'not authorized',
83 get '/arvados/v1/users/current', {}, auth(remote: 'zbbbb')
87 test 'remote api server is not an api server' do
89 @stub_content = '<html>bad</html>'
90 get '/arvados/v1/users/current', {}, auth(remote: 'zbbbb')
94 ['zbbbb', 'z0000'].each do |token_valid_for|
95 test "validate #{token_valid_for}-salted token for remote cluster zbbbb" do
96 salted_token = salt_token(fixture: :active, remote: token_valid_for)
97 get '/arvados/v1/users/current', {format: 'json', remote: 'zbbbb'}, {
98 "HTTP_AUTHORIZATION" => "Bearer #{salted_token}"
100 if token_valid_for == 'zbbbb'
102 assert_equal(users(:active).uuid, json_response['uuid'])