1414
1515-define (AUTH_PORT , 8000 ).
1616-define (USER_PATH , " /auth/user" ).
17- -define (BACKEND_CONFIG ,
18- [{http_method , get },
19- {user_path , " http://localhost:" ++ integer_to_list (? AUTH_PORT ) ++ ? USER_PATH },
20- {vhost_path , " http://localhost:" ++ integer_to_list (? AUTH_PORT ) ++ " /auth/vhost" },
21- {resource_path , " http://localhost:" ++ integer_to_list (? AUTH_PORT ) ++ " /auth/resource" },
22- {topic_path , " http://localhost:" ++ integer_to_list (? AUTH_PORT ) ++ " /auth/topic" }]).
2317-define (ALLOWED_USER , #{username => <<" Ala1" >>,
2418 password => <<" Kocur" >>,
2519 expected_credentials => [username , password ],
3327 password => <<" Cat" >>
3428 }).
3529
36- all () -> [grants_access_to_user ,
37- denies_access_to_user ,
38- grants_access_to_user_passing_additional_required_authprops ,
39- grants_access_to_user_skipping_internal_authprops ,
40- grants_access_to_user_with_credentials_in_rabbit_auth_backend_http ,
41- grants_access_to_user_with_credentials_in_rabbit_auth_backend_cache ].
42-
43- init_per_suite (Config ) ->
44- configure_http_auth_backend (),
30+ all () ->
31+ [
32+ {group , over_https },
33+ {group , over_http }
34+ ].
35+
36+ groups () ->
37+ [
38+ {over_http , [], shared ()},
39+ {over_https , [], shared ()}
40+ ].
41+
42+ shared () ->
43+ [
44+ grants_access_to_user ,
45+ denies_access_to_user ,
46+ grants_access_to_user_passing_additional_required_authprops ,
47+ grants_access_to_user_skipping_internal_authprops ,
48+ grants_access_to_user_with_credentials_in_rabbit_auth_backend_http ,
49+ grants_access_to_user_with_credentials_in_rabbit_auth_backend_cache
50+ ].
51+
52+ init_per_suite (Config ) ->
53+ rabbit_ct_helpers :run_setup_steps (Config ) ++
54+ [{allowed_user , ? ALLOWED_USER },
55+ {allowed_user_with_extra_credentials , ? ALLOWED_USER_WITH_EXTRA_CREDENTIALS },
56+ {denied_user , ? DENIED_USER }].
57+
58+ init_per_group (over_http , Config ) ->
59+ configure_http_auth_backend (" http" , Config ),
4560 {User1 , Tuple1 } = extractUserTuple (? ALLOWED_USER ),
46- {User2 , Tuple2 } = extractUserTuple (? ALLOWED_USER_WITH_EXTRA_CREDENTIALS ),
61+ {User2 , Tuple2 } = extractUserTuple (? ALLOWED_USER_WITH_EXTRA_CREDENTIALS ),
4762 start_http_auth_server (? AUTH_PORT , ? USER_PATH , #{User1 => Tuple1 , User2 => Tuple2 }),
48- [{allowed_user , ? ALLOWED_USER },
49- {allowed_user_with_extra_credentials , ? ALLOWED_USER_WITH_EXTRA_CREDENTIALS },
50- {denied_user , ? DENIED_USER } | Config ].
63+ Config ;
64+
65+ init_per_group (over_https , Config ) ->
66+ configure_http_auth_backend (" https" , Config ),
67+ {User1 , Tuple1 } = extractUserTuple (? ALLOWED_USER ),
68+ {User2 , Tuple2 } = extractUserTuple (? ALLOWED_USER_WITH_EXTRA_CREDENTIALS ),
69+ CertsDir = ? config (rmq_certsdir , Config ),
70+ start_https_auth_server (? AUTH_PORT , CertsDir , ? USER_PATH , #{User1 => Tuple1 , User2 => Tuple2 }),
71+ Config .
72+
73+
5174extractUserTuple (User ) ->
5275 #{username := Username , password := Password , tags := Tags , expected_credentials := ExpectedCredentials } = User ,
5376 {Username , {Password , Tags , ExpectedCredentials }}.
5477
55- end_per_suite (_Config ) ->
78+ end_per_suite (Config ) ->
79+ Config .
80+
81+ end_per_group (over_http , Config ) ->
82+ undo_configure_http_auth_backend (" http" , Config ),
83+ stop_http_auth_server ();
84+ end_per_group (over_https , Config ) ->
85+ undo_configure_http_auth_backend (" https" , Config ),
5686 stop_http_auth_server ().
5787
5888grants_access_to_user (Config ) ->
@@ -102,15 +132,49 @@ grants_access_to_user_with_credentials_in_rabbit_auth_backend_cache(Config) ->
102132
103133% %% HELPERS
104134
105- configure_http_auth_backend () ->
106- {ok , _ } = application :ensure_all_started (inets ),
107- [application :set_env (rabbitmq_auth_backend_http , K , V ) || {K , V } <- ? BACKEND_CONFIG ].
135+ configure_http_auth_backend (Scheme , Config ) ->
136+ [application :set_env (rabbitmq_auth_backend_http , K , V ) || {K , V } <- generate_backend_config (Scheme , Config )].
137+ undo_configure_http_auth_backend (Scheme , Config ) ->
138+ [application :unset_env (rabbitmq_auth_backend_http , K ) || {K , _V } <- generate_backend_config (Scheme , Config )].
108139
109140start_http_auth_server (Port , Path , Users ) ->
141+ {ok , _ } = application :ensure_all_started (inets ),
110142 application :ensure_all_started (cowboy ),
111143 Dispatch = cowboy_router :compile ([{'_' , [{Path , auth_http_mock , Users }]}]),
112144 {ok , _ } = cowboy :start_clear (
113145 mock_http_auth_listener , [{port , Port }], #{env => #{dispatch => Dispatch }}).
114146
147+ start_https_auth_server (Port , CertsDir , Path , Users ) ->
148+ {ok , _ } = application :ensure_all_started (inets ),
149+ {ok , _ } = application :ensure_all_started (ssl ),
150+ {ok , _ } = application :ensure_all_started (cowboy ),
151+
152+ Dispatch = cowboy_router :compile ([{'_' , [{Path , auth_http_mock , Users }]}]),
153+ {ok , _ } = cowboy :start_tls (mock_http_auth_listener ,
154+ [{port , Port },
155+ {certfile , filename :join ([CertsDir , " server" , " cert.pem" ])},
156+ {keyfile , filename :join ([CertsDir , " server" , " key.pem" ])}],
157+ #{env => #{dispatch => Dispatch }}).
158+
115159stop_http_auth_server () ->
116160 cowboy :stop_listener (mock_http_auth_listener ).
161+
162+ generate_backend_config (Scheme , Config ) ->
163+ Config0 = [{http_method , get },
164+ {user_path , Scheme ++ " ://localhost:" ++ integer_to_list (? AUTH_PORT ) ++ ? USER_PATH },
165+ {vhost_path , Scheme ++ " ://localhost:" ++ integer_to_list (? AUTH_PORT ) ++ " /auth/vhost" },
166+ {resource_path , Scheme ++ " ://localhost:" ++ integer_to_list (? AUTH_PORT ) ++ " /auth/resource" },
167+ {topic_path , Scheme ++ " ://localhost:" ++ integer_to_list (? AUTH_PORT ) ++ " /auth/topic" }],
168+ Config1 = case Scheme of
169+ " https" ->
170+ CertsDir = ? config (rmq_certsdir , Config ),
171+ [{ssl_options , [
172+ {cacertfile , filename :join ([CertsDir , " testca" , " cacert.pem" ])},
173+ {certfile , filename :join ([CertsDir , " server" , " cert.pem" ])},
174+ {keyfile , filename :join ([CertsDir , " server" , " key.pem" ])},
175+ {verify , verify_peer },
176+ {fail_if_no_peer_cert , false }]
177+ }];
178+ " http" -> []
179+ end ,
180+ Config0 ++ Config1 .
0 commit comments