30
30
socket_ready /2 ,
31
31
protocol_header_received /5 ,
32
32
begin_session /1 ,
33
- heartbeat /1 ]).
33
+ heartbeat /1 ,
34
+ encrypt_sasl /1 ,
35
+ decrypt_sasl /1 ]).
34
36
35
37
% % gen_fsm callbacks.
36
38
-export ([init /1 ,
56
58
57
59
-type address () :: inet :socket_address () | inet :hostname ().
58
60
61
+ -type encrypted_sasl () :: {plaintext , binary ()} | {encrypted , binary ()}.
62
+ -type decrypted_sasl () :: none | anon | {plain , User :: binary (), Pwd :: binary ()}.
63
+
59
64
-type connection_config () ::
60
65
#{container_id => binary (), % AMQP container id
61
66
hostname => binary (), % the dns name of the target host
72
77
% set to a negative value to allow a sender to "overshoot" the flow
73
78
% control by this margin
74
79
transfer_limit_margin => 0 | neg_integer (),
75
- sasl => none | anon | {plain , User :: binary (), Pwd :: binary ()},
80
+ % % These credentials_obfuscation-wrapped values have the type of
81
+ % % decrypted_sasl/0
82
+ sasl => encrypted_sasl () | decrypted_sasl (),
76
83
notify => pid (),
77
84
notify_when_opened => pid () | none ,
78
85
notify_when_closed => pid () | none
92
99
}).
93
100
94
101
-export_type ([connection_config / 0 ,
95
- amqp10_socket / 0 ]).
102
+ amqp10_socket / 0 ,
103
+ encrypted_sasl / 0 ,
104
+ decrypted_sasl / 0 ]).
96
105
97
106
% % -------------------------------------------------------------------
98
107
% % Public API.
@@ -125,6 +134,18 @@ open(Config) ->
125
134
close (Pid , Reason ) ->
126
135
gen_statem :cast (Pid , {close , Reason }).
127
136
137
+ -spec encrypt_sasl (decrypted_sasl ()) -> encrypted_sasl ().
138
+ encrypt_sasl (none ) ->
139
+ credentials_obfuscation :encrypt (none );
140
+ encrypt_sasl (DecryptedSasl ) ->
141
+ credentials_obfuscation :encrypt (term_to_binary (DecryptedSasl )).
142
+
143
+ -spec decrypt_sasl (encrypted_sasl ()) -> decrypted_sasl ().
144
+ decrypt_sasl (none ) ->
145
+ credentials_obfuscation :decrypt (none );
146
+ decrypt_sasl (EncryptedSasl ) ->
147
+ binary_to_term (credentials_obfuscation :decrypt (EncryptedSasl )).
148
+
128
149
% % -------------------------------------------------------------------
129
150
% % Private API.
130
151
% % -------------------------------------------------------------------
@@ -166,8 +187,9 @@ init([Sup, Config0]) ->
166
187
expecting_socket (_EvtType , {socket_ready , Socket },
167
188
State = # state {config = Cfg }) ->
168
189
State1 = State # state {socket = Socket },
169
- case Cfg of
170
- #{sasl := none } ->
190
+ Sasl = credentials_obfuscation :decrypt (maps :get (sasl , Cfg )),
191
+ case Sasl of
192
+ none ->
171
193
ok = socket_send (Socket , ? AMQP_PROTOCOL_HEADER ),
172
194
{next_state , hdr_sent , State1 };
173
195
_ ->
@@ -193,16 +215,17 @@ sasl_hdr_sent({call, From}, begin_session,
193
215
194
216
sasl_hdr_rcvds (_EvtType , # 'v1_0.sasl_mechanisms' {
195
217
sasl_server_mechanisms = {array , symbol , Mechs }},
196
- State = # state {config = #{sasl := Sasl }}) ->
197
- SaslBin = {symbol , sasl_to_bin (Sasl )},
218
+ State = # state {config = #{sasl := EncryptedSasl }}) ->
219
+ DecryptedSasl = decrypt_sasl (EncryptedSasl ),
220
+ SaslBin = {symbol , decrypted_sasl_to_bin (DecryptedSasl )},
198
221
case lists :any (fun (S ) when S =:= SaslBin -> true ;
199
222
(_ ) -> false
200
223
end , Mechs ) of
201
224
true ->
202
- ok = send_sasl_init (State , Sasl ),
225
+ ok = send_sasl_init (State , DecryptedSasl ),
203
226
{next_state , sasl_init_sent , State };
204
227
false ->
205
- {stop , {sasl_not_supported , Sasl }, State }
228
+ {stop , {sasl_not_supported , DecryptedSasl }, State }
206
229
end ;
207
230
sasl_hdr_rcvds ({call , From }, begin_session ,
208
231
# state {pending_session_reqs = PendingSessionReqs } = State ) ->
@@ -522,8 +545,9 @@ translate_err(#'v1_0.error'{condition = Cond, description = Desc}) ->
522
545
amqp10_event (Evt ) ->
523
546
{amqp10_event , {connection , self (), Evt }}.
524
547
525
- sasl_to_bin ({plain , _ , _ }) -> <<" PLAIN" >>;
526
- sasl_to_bin (anon ) -> <<" ANONYMOUS" >>.
548
+ decrypted_sasl_to_bin ({plain , _ , _ }) -> <<" PLAIN" >>;
549
+ decrypted_sasl_to_bin (anon ) -> <<" ANONYMOUS" >>;
550
+ decrypted_sasl_to_bin (none ) -> <<" ANONYMOUS" >>.
527
551
528
552
config_defaults () ->
529
553
#{sasl => none ,
0 commit comments