| 1 |
# Licensed to the Apache Software Foundation (ASF) under one |
| 2 |
# or more contributor license agreements. See the NOTICE file |
| 3 |
# distributed with this work for additional information |
| 4 |
# regarding copyright ownership. The ASF licenses this file |
| 5 |
# to you under the Apache License, Version 2.0 (the |
| 6 |
# "License"); you may not use this file except in compliance |
| 7 |
# with the License. You may obtain a copy of the License at |
| 8 |
# |
| 9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
# |
| 11 |
# Unless required by applicable law or agreed to in writing, |
| 12 |
# software distributed under the License is distributed on an |
| 13 |
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 |
# KIND, either express or implied. See the License for the |
| 15 |
# specific language governing permissions and limitations |
| 16 |
# under the License. |
| 17 |
|
| 18 |
|
| 19 |
require 'test_tools' |
| 20 |
require 'minitest/unit' |
| 21 |
require 'socket' |
| 22 |
|
| 23 |
# Container that listens on a random port |
| 24 |
class TestContainer < Qpid::Proton::Container |
| 25 |
|
| 26 |
def initialize(handler, listener_opts, id) |
| 27 |
super handler, id |
| 28 |
@listener = listen_io(TCPServer.open(0), ListenOnceHandler.new(listener_opts)) |
| 29 |
end |
| 30 |
attr_reader :listener |
| 31 |
end |
| 32 |
|
| 33 |
class ContainerSASLTest < MiniTest::Test |
| 34 |
include Qpid::Proton |
| 35 |
|
| 36 |
# Handler for test client/server that sets up server and client SASL options |
| 37 |
class SASLHandler < TestHandler |
| 38 |
|
| 39 |
def initialize(url="amqp://", opts=nil) |
| 40 |
super() |
| 41 |
@url, @opts = url, opts |
| 42 |
end |
| 43 |
|
| 44 |
def on_container_start(container) |
| 45 |
@client = container.connect("#{@url}:#{container.listener.port}", @opts) |
| 46 |
end |
| 47 |
|
| 48 |
attr_reader :auth_user |
| 49 |
|
| 50 |
def on_connection_open(connection) |
| 51 |
super |
| 52 |
if connection == @client |
| 53 |
connection.close |
| 54 |
else |
| 55 |
@auth_user = connection.user |
| 56 |
end |
| 57 |
end |
| 58 |
end |
| 59 |
|
| 60 |
# Generate SASL server configuration files and database, initialize proton SASL |
| 61 |
class SASLConfig |
| 62 |
include Qpid::Proton |
| 63 |
attr_reader :conf_dir, :conf_file, :conf_name, :database, :error |
| 64 |
|
| 65 |
def initialize() |
| 66 |
if SASL.extended? # Configure cyrus SASL |
| 67 |
@conf_dir = File.expand_path('sasl_conf') |
| 68 |
@conf_name = "proton-server" |
| 69 |
@database = File.join(@conf_dir, "proton.sasldb") |
| 70 |
@conf_file = File.join(conf_dir,"#{@conf_name}.conf") |
| 71 |
Dir::mkdir(@conf_dir) unless File.directory?(@conf_dir) |
| 72 |
# Same user name in different realms |
| 73 |
make_user("user", "password", "proton") # proton realm |
| 74 |
make_user("user", "default_password") # Default realm |
| 75 |
File.open(@conf_file, 'w') do |f| |
| 76 |
f.write(" |
| 77 |
sasldb_path: #{database} |
| 78 |
mech_list: EXTERNAL DIGEST-MD5 SCRAM-SHA-1 CRAM-MD5 PLAIN ANONYMOUS |
| 79 |
") |
| 80 |
end |
| 81 |
# Tell proton library to use the new configuration |
| 82 |
SASL.config_path = conf_dir |
| 83 |
SASL.config_name = conf_name |
| 84 |
end |
| 85 |
rescue => e |
| 86 |
@error = e |
| 87 |
end |
| 88 |
|
| 89 |
private |
| 90 |
|
| 91 |
SASLPASSWD = (ENV['SASLPASSWD'] or 'saslpasswd2') |
| 92 |
|
| 93 |
def make_user(user, password, realm=nil) |
| 94 |
realm_opt = (realm ? "-u #{realm}" : "") |
| 95 |
cmd = "echo '#{password}' | #{SASLPASSWD} -c -p -f #{database} #{realm_opt} #{user}" |
| 96 |
system(cmd) or raise RuntimeError.new("saslpasswd2 failed: #{cmd}") |
| 97 |
end |
| 98 |
|
| 99 |
INSTANCE = SASLConfig.new |
| 100 |
end |
| 101 |
|
| 102 |
def begin_extended_test |
| 103 |
skip("Extended SASL not enabled") unless SASL.extended? |
| 104 |
skip("Extended SASL setup error: #{SASLConfig::INSTANCE.error}") if SASLConfig::INSTANCE.error |
| 105 |
end |
| 106 |
|
| 107 |
def test_sasl_anonymous() |
| 108 |
s = SASLHandler.new("amqp://", {:sasl_allowed_mechs => "ANONYMOUS"}) |
| 109 |
TestContainer.new(s, {:sasl_allowed_mechs => "ANONYMOUS"}, __method__).run |
| 110 |
assert_equal "anonymous", s.connections[0].user |
| 111 |
end |
| 112 |
|
| 113 |
def test_sasl_plain_url() |
| 114 |
begin_extended_test |
| 115 |
# Use default realm with URL, should authenticate with "default_password" |
| 116 |
opts = {:sasl_allowed_mechs => "PLAIN", :sasl_allow_insecure_mechs => true} |
| 117 |
s = SASLHandler.new("amqp://user:default_password@", opts) |
| 118 |
TestContainer.new(s, opts, __method__).run |
| 119 |
assert_equal(2, s.connections.size) |
| 120 |
assert_equal("user", s.auth_user) |
| 121 |
end |
| 122 |
|
| 123 |
def test_sasl_plain_options() |
| 124 |
begin_extended_test |
| 125 |
# Use default realm with connection options, should authenticate with "default_password" |
| 126 |
opts = {:sasl_allowed_mechs => "PLAIN",:sasl_allow_insecure_mechs => true, |
| 127 |
:user => 'user', :password => 'default_password' } |
| 128 |
s = SASLHandler.new("amqp://", opts) |
| 129 |
TestContainer.new(s, {:sasl_allowed_mechs => "PLAIN",:sasl_allow_insecure_mechs => true}, __method__).run |
| 130 |
assert_equal(2, s.connections.size) |
| 131 |
assert_equal("user", s.auth_user) |
| 132 |
end |
| 133 |
|
| 134 |
# Ensure we don't allow PLAIN if allow_insecure_mechs = true is not explicitly set |
| 135 |
def test_disallow_insecure() |
| 136 |
# Don't set allow_insecure_mechs, but try to use PLAIN |
| 137 |
s = SASLHandler.new("amqp://user:password@", {:sasl_allowed_mechs => "PLAIN", :sasl_allow_insecure_mechs => true}) |
| 138 |
e = assert_raises(TestError) { TestContainer.new(s, {:sasl_allowed_mechs => "PLAIN"}, __method__).run } |
| 139 |
assert_match(/amqp:unauthorized-access.*Authentication failed/, e.to_s) |
| 140 |
end |
| 141 |
end |