Skip to content

Commit 3e7f5a0

Browse files
committed
Fix AMQP 1.0 SASL CR Demo
``` switch_callback(State1, {frame_header, sasl}, 8); ``` was missing. Tidy up various other small things.
1 parent 74121d7 commit 3e7f5a0

File tree

3 files changed

+42
-42
lines changed

3 files changed

+42
-42
lines changed

deps/rabbit/src/rabbit_amqp_reader.erl

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
incoming_max_frame_size :: pos_integer(),
5151
outgoing_max_frame_size :: unlimited | pos_integer(),
5252
channel_max :: non_neg_integer(),
53-
auth_mechanism :: none | {binary(), module()},
53+
auth_mechanism :: sasl_init_unprocessed | {binary(), module()},
5454
auth_state :: term(),
5555
properties :: undefined | {map, list(tuple())}
5656
}).
@@ -85,23 +85,23 @@
8585
%%--------------------------------------------------------------------------
8686

8787
unpack_from_0_9_1(
88-
{Sock,RecvLen, PendingRecv, SupPid, Buf, BufLen, ProxySocket,
88+
{Sock, PendingRecv, SupPid, Buf, BufLen, ProxySocket,
8989
ConnectionName, Host, PeerHost, Port, PeerPort, ConnectedAt},
90-
Parent, HandshakeTimeout) ->
90+
Parent) ->
9191
logger:update_process_metadata(#{connection => ConnectionName}),
92-
#v1{parent = Parent,
93-
sock = Sock,
94-
callback = handshake,
95-
recv_len = RecvLen,
96-
pending_recv = PendingRecv,
97-
connection_state = received_amqp3100,
98-
heartbeater = none,
99-
helper_sup = SupPid,
100-
buf = Buf,
101-
buf_len = BufLen,
102-
proxy_socket = ProxySocket,
103-
tracked_channels = maps:new(),
104-
writer = none,
92+
#v1{parent = Parent,
93+
sock = Sock,
94+
callback = {frame_header, sasl},
95+
recv_len = 8,
96+
pending_recv = PendingRecv,
97+
heartbeater = none,
98+
helper_sup = SupPid,
99+
buf = Buf,
100+
buf_len = BufLen,
101+
proxy_socket = ProxySocket,
102+
tracked_channels = maps:new(),
103+
writer = none,
104+
connection_state = received_amqp3100,
105105
connection = #v1_connection{
106106
name = ConnectionName,
107107
vhost = none,
@@ -111,12 +111,12 @@ unpack_from_0_9_1(
111111
peer_port = PeerPort,
112112
connected_at = ConnectedAt,
113113
user = unauthenticated,
114-
timeout = HandshakeTimeout,
114+
timeout = ?NORMAL_TIMEOUT,
115115
incoming_max_frame_size = ?INITIAL_MAX_FRAME_SIZE,
116116
outgoing_max_frame_size = ?INITIAL_MAX_FRAME_SIZE,
117117
channel_max = 0,
118-
auth_mechanism = none,
119-
auth_state = none}}.
118+
auth_mechanism = sasl_init_unprocessed,
119+
auth_state = unauthenticated}}.
120120

121121
-spec system_continue(pid(), [sys:dbg_opt()], state()) -> no_return() | ok.
122122
system_continue(Parent, Deb, State) ->
@@ -142,7 +142,9 @@ inet_op(F) -> rabbit_misc:throw_on_error(inet_error, F).
142142

143143
recvloop(Deb, State = #v1{pending_recv = true}) ->
144144
mainloop(Deb, State);
145-
recvloop(Deb, State = #v1{sock = Sock, recv_len = RecvLen, buf_len = BufLen})
145+
recvloop(Deb, State = #v1{sock = Sock,
146+
recv_len = RecvLen,
147+
buf_len = BufLen})
146148
when BufLen < RecvLen ->
147149
case rabbit_net:setopts(Sock, [{active, once}]) of
148150
ok ->
@@ -203,10 +205,10 @@ handle_other({'EXIT', Parent, Reason}, State = #v1{parent = Parent}) ->
203205
exit(Reason);
204206
handle_other({{'DOWN', ChannelNum}, _MRef, process, SessionPid, Reason}, State) ->
205207
handle_session_exit(ChannelNum, SessionPid, Reason, State);
206-
handle_other(handshake_timeout, State)
207-
when ?IS_RUNNING(State) orelse
208-
State#v1.connection_state =:= closing orelse
209-
State#v1.connection_state =:= closed ->
208+
handle_other(handshake_timeout, State = #v1{connection_state = ConnState})
209+
when ConnState =:= running orelse
210+
ConnState =:= closing orelse
211+
ConnState =:= closed ->
210212
State;
211213
handle_other(handshake_timeout, State) ->
212214
throw({handshake_timeout, State#v1.callback});
@@ -573,13 +575,14 @@ handle_sasl_frame(#'v1_0.sasl_response'{response = {binary, Response}},
573575
handle_sasl_frame(Performative, State) ->
574576
throw({unexpected_1_0_sasl_frame, Performative, State}).
575577

576-
handle_input(handshake, <<"AMQP",0,1,0,0>>,
578+
handle_input(handshake,
579+
<<"AMQP",0,1,0,0>>,
577580
#v1{connection_state = waiting_amqp0100,
578581
sock = Sock,
579-
connection = Connection = #v1_connection{user = #user{}},
582+
connection = #v1_connection{user = #user{}},
580583
helper_sup = HelperSup
581584
} = State0) ->
582-
%% Client already got successfully authenticated by SASL.
585+
%% At this point, client already got successfully authenticated by SASL.
583586
send_handshake(Sock, <<"AMQP",0,1,0,0>>),
584587
ChildSpec = #{id => session_sup,
585588
start => {rabbit_amqp_session_sup, start_link, [self()]},
@@ -593,8 +596,7 @@ handle_input(handshake, <<"AMQP",0,1,0,0>>,
593596
%% "After establishing or accepting a TCP connection and sending
594597
%% the protocol header, each peer MUST send an open frame before
595598
%% sending any other frames." [2.4.1]
596-
connection_state = waiting_open,
597-
connection = Connection#v1_connection{timeout = ?NORMAL_TIMEOUT}},
599+
connection_state = waiting_open},
598600
switch_callback(State, {frame_header, amqp}, 8);
599601
handle_input({frame_header, Mode},
600602
Header = <<Size:32, DOff:8, Type:8, Channel:16>>,
@@ -620,7 +622,8 @@ handle_input({frame_header, Mode},
620622
handle_input({frame_header, _Mode}, Malformed, _State) ->
621623
throw({bad_1_0_header, Malformed});
622624
handle_input({frame_body, Mode, DOff, Channel},
623-
FrameBin, State) ->
625+
FrameBin,
626+
State) ->
624627
%% Figure 2.16
625628
%% DOff = 4-byte words minus 8 bytes we've already read
626629
ExtendedHeaderSize = (DOff * 32 - 64),
@@ -633,24 +636,21 @@ handle_input(Callback, Data, _State) ->
633636

634637
-spec init(tuple()) -> no_return().
635638
init(PackedState) ->
636-
{ok, HandshakeTimeout} = application:get_env(rabbit, handshake_timeout),
637639
{parent, Parent} = erlang:process_info(self(), parent),
638640
ok = rabbit_connection_sup:remove_connection_helper_sup(Parent, helper_sup_amqp_091),
639-
State0 = unpack_from_0_9_1(PackedState, Parent, HandshakeTimeout),
641+
State0 = unpack_from_0_9_1(PackedState, Parent),
640642
State = advertise_sasl_mechanism(State0),
641643
%% By invoking recvloop here we become 1.0.
642644
recvloop(sys:debug_options([]), State).
643645

644646
advertise_sasl_mechanism(State0 = #v1{connection_state = received_amqp3100,
645-
connection = Connection,
646647
sock = Sock}) ->
647648
send_handshake(Sock, <<"AMQP",3,1,0,0>>),
648649
Ms0 = [{symbol, atom_to_binary(M)} || M <- auth_mechanisms(Sock)],
649650
Ms1 = {array, symbol, Ms0},
650651
Ms = #'v1_0.sasl_mechanisms'{sasl_server_mechanisms = Ms1},
651652
ok = send_on_channel0(Sock, Ms, rabbit_amqp_sasl),
652-
State = State0#v1{connection_state = waiting_sasl_init,
653-
connection = Connection#v1_connection{timeout = ?NORMAL_TIMEOUT}},
653+
State = State0#v1{connection_state = waiting_sasl_init},
654654
switch_callback(State, {frame_header, sasl}, 8).
655655

656656
send_handshake(Sock, Handshake) ->
@@ -715,15 +715,16 @@ auth_phase(
715715
auth_fail(none, State),
716716
protocol_error(?V_1_0_AMQP_ERROR_DECODE_ERROR, Msg, Args);
717717
{challenge, Challenge, AuthState1} ->
718-
Secure = #'v1_0.sasl_challenge'{challenge = {binary, Challenge}},
719-
ok = send_on_channel0(Sock, Secure, rabbit_amqp_sasl),
720-
State#v1{connection = Conn#v1_connection{auth_state = AuthState1}};
718+
Challenge = #'v1_0.sasl_challenge'{challenge = {binary, Challenge}},
719+
ok = send_on_channel0(Sock, Challenge, rabbit_amqp_sasl),
720+
State1 = State#v1{connection = Conn#v1_connection{auth_state = AuthState1}},
721+
switch_callback(State1, {frame_header, sasl}, 8);
721722
{ok, User} ->
722723
Outcome = #'v1_0.sasl_outcome'{code = ?V_1_0_SASL_CODE_OK},
723724
ok = send_on_channel0(Sock, Outcome, rabbit_amqp_sasl),
724725
State1 = State#v1{connection_state = waiting_amqp0100,
725726
connection = Conn#v1_connection{user = User,
726-
auth_state = none}},
727+
auth_state = authenticated}},
727728
switch_callback(State1, handshake, 8)
728729
end.
729730

deps/rabbit/src/rabbit_auth_mechanism_anonymous.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ should_offer(_Sock) ->
3434
init(_Sock) ->
3535
?STATE.
3636

37-
handle_response(_Response, ?STATE) ->
37+
handle_response(_TraceInfo, ?STATE) ->
3838
{ok, User, Pass} = credentials(),
3939
rabbit_access_control:check_user_pass_login(User, Pass).
4040

deps/rabbit/src/rabbit_reader.erl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,7 +1644,6 @@ become_10(State) ->
16441644
State#v1{connection_state = {become, Fun}}.
16451645

16461646
pack_for_1_0(Buf, BufLen, #v1{sock = Sock,
1647-
recv_len = RecvLen,
16481647
pending_recv = PendingRecv,
16491648
helper_sup = {_HelperSup091, HelperSup10},
16501649
proxy_socket = ProxySocket,
@@ -1655,7 +1654,7 @@ pack_for_1_0(Buf, BufLen, #v1{sock = Sock,
16551654
port = Port,
16561655
peer_port = PeerPort,
16571656
connected_at = ConnectedAt}}) ->
1658-
{Sock, RecvLen, PendingRecv, HelperSup10, Buf, BufLen, ProxySocket,
1657+
{Sock, PendingRecv, HelperSup10, Buf, BufLen, ProxySocket,
16591658
Name, Host, PeerHost, Port, PeerPort, ConnectedAt}.
16601659

16611660
respond_and_close(State, Channel, Protocol, Reason, LogErr) ->

0 commit comments

Comments
 (0)