fix(auth): authn & authz http support placeholder in HTTP path
This commit is contained in:
parent
dae418ae4a
commit
15ef9892c5
|
@ -161,17 +161,19 @@ create(_AuthenticatorID, Config) ->
|
||||||
create(
|
create(
|
||||||
#{
|
#{
|
||||||
method := Method,
|
method := Method,
|
||||||
url := RawURL,
|
url := RawUrl,
|
||||||
headers := Headers,
|
headers := Headers,
|
||||||
request_timeout := RequestTimeout
|
request_timeout := RequestTimeout
|
||||||
} = Config
|
} = Config
|
||||||
) ->
|
) ->
|
||||||
{BsaeUrlWithPath, Query} = parse_fullpath(RawURL),
|
{BaseUrl0, Path, Query} = parse_url(RawUrl),
|
||||||
URIMap = parse_url(BsaeUrlWithPath),
|
{ok, BaseUrl} = emqx_http_lib:uri_parse(BaseUrl0),
|
||||||
ResourceId = emqx_authn_utils:make_resource_id(?MODULE),
|
ResourceId = emqx_authn_utils:make_resource_id(?MODULE),
|
||||||
State = #{
|
State = #{
|
||||||
method => Method,
|
method => Method,
|
||||||
path => maps:get(path, URIMap),
|
path => Path,
|
||||||
|
headers => Headers,
|
||||||
|
base_path_templete => emqx_authn_utils:parse_str(Path),
|
||||||
base_query_template => emqx_authn_utils:parse_deep(
|
base_query_template => emqx_authn_utils:parse_deep(
|
||||||
cow_qs:parse_qs(to_bin(Query))
|
cow_qs:parse_qs(to_bin(Query))
|
||||||
),
|
),
|
||||||
|
@ -185,7 +187,7 @@ create(
|
||||||
?RESOURCE_GROUP,
|
?RESOURCE_GROUP,
|
||||||
emqx_connector_http,
|
emqx_connector_http,
|
||||||
Config#{
|
Config#{
|
||||||
base_url => maps:remove(query, URIMap),
|
base_url => BaseUrl,
|
||||||
pool_type => random
|
pool_type => random
|
||||||
},
|
},
|
||||||
#{}
|
#{}
|
||||||
|
@ -274,9 +276,6 @@ destroy(#{resource_id := ResourceId}) ->
|
||||||
%% Internal functions
|
%% Internal functions
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
parse_fullpath(RawURL) ->
|
|
||||||
cow_http:parse_fullpath(to_bin(RawURL)).
|
|
||||||
|
|
||||||
default_headers() ->
|
default_headers() ->
|
||||||
maps:put(
|
maps:put(
|
||||||
<<"content-type">>,
|
<<"content-type">>,
|
||||||
|
@ -303,14 +302,14 @@ transform_header_name(Headers) ->
|
||||||
).
|
).
|
||||||
|
|
||||||
check_ssl_opts(Conf) ->
|
check_ssl_opts(Conf) ->
|
||||||
{BaseUrlWithPath, _Query} = parse_fullpath(get_conf_val("url", Conf)),
|
{BaseUrl, _Path, _Query} = parse_url(get_conf_val("url", Conf)),
|
||||||
case parse_url(BaseUrlWithPath) of
|
case BaseUrl of
|
||||||
#{scheme := https} ->
|
<<"https://", _/binary>> ->
|
||||||
case get_conf_val("ssl.enable", Conf) of
|
case get_conf_val("ssl.enable", Conf) of
|
||||||
true -> ok;
|
true -> ok;
|
||||||
false -> false
|
false -> false
|
||||||
end;
|
end;
|
||||||
#{scheme := http} ->
|
<<"http://", _/binary>> ->
|
||||||
ok
|
ok
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
@ -319,39 +318,51 @@ check_headers(Conf) ->
|
||||||
Headers = get_conf_val("headers", Conf),
|
Headers = get_conf_val("headers", Conf),
|
||||||
Method =:= <<"post">> orelse (not maps:is_key(<<"content-type">>, Headers)).
|
Method =:= <<"post">> orelse (not maps:is_key(<<"content-type">>, Headers)).
|
||||||
|
|
||||||
parse_url(URL) ->
|
parse_url(Url) ->
|
||||||
{ok, URIMap} = emqx_http_lib:uri_parse(URL),
|
case string:split(Url, "//", leading) of
|
||||||
case maps:get(query, URIMap, undefined) of
|
[Scheme, UrlRem] ->
|
||||||
undefined ->
|
case string:split(UrlRem, "/", leading) of
|
||||||
URIMap#{query => ""};
|
[HostPort, Remaining] ->
|
||||||
_ ->
|
BaseUrl = iolist_to_binary([Scheme, "//", HostPort]),
|
||||||
URIMap
|
case string:split(Remaining, "?", leading) of
|
||||||
|
[Path, QueryString] ->
|
||||||
|
{BaseUrl, Path, QueryString};
|
||||||
|
[Path] ->
|
||||||
|
{BaseUrl, Path, <<>>}
|
||||||
|
end;
|
||||||
|
[HostPort] ->
|
||||||
|
{iolist_to_binary([Scheme, "//", HostPort]), <<>>, <<>>}
|
||||||
|
end;
|
||||||
|
[Url] ->
|
||||||
|
throw({invalid_url, Url})
|
||||||
end.
|
end.
|
||||||
|
|
||||||
generate_request(Credential, #{
|
generate_request(Credential, #{
|
||||||
method := Method,
|
method := Method,
|
||||||
path := Path,
|
headers := Headers0,
|
||||||
|
base_path_templete := BasePathTemplate,
|
||||||
base_query_template := BaseQueryTemplate,
|
base_query_template := BaseQueryTemplate,
|
||||||
headers := Headers,
|
|
||||||
body_template := BodyTemplate
|
body_template := BodyTemplate
|
||||||
}) ->
|
}) ->
|
||||||
|
Headers = maps:to_list(Headers0),
|
||||||
|
Path = emqx_authn_utils:render_str(BasePathTemplate, Credential),
|
||||||
|
Query = emqx_authn_utils:render_deep(BaseQueryTemplate, Credential),
|
||||||
Body = emqx_authn_utils:render_deep(BodyTemplate, Credential),
|
Body = emqx_authn_utils:render_deep(BodyTemplate, Credential),
|
||||||
NBaseQuery = emqx_authn_utils:render_deep(BaseQueryTemplate, Credential),
|
|
||||||
case Method of
|
case Method of
|
||||||
get ->
|
get ->
|
||||||
NPath = append_query(Path, NBaseQuery ++ Body),
|
NPathQuery = append_query(to_list(Path), to_list(Query) ++ maps:to_list(Body)),
|
||||||
{NPath, Headers};
|
{NPathQuery, Headers};
|
||||||
post ->
|
post ->
|
||||||
NPath = append_query(Path, NBaseQuery),
|
NPathQuery = append_query(to_list(Path), to_list(Query)),
|
||||||
ContentType = proplists:get_value(<<"content-type">>, Headers),
|
ContentType = proplists:get_value(<<"content-type">>, Headers),
|
||||||
NBody = serialize_body(ContentType, Body),
|
NBody = serialize_body(ContentType, Body),
|
||||||
{NPath, Headers, NBody}
|
{NPathQuery, Headers, NBody}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
append_query(Path, []) ->
|
append_query(Path, []) ->
|
||||||
Path;
|
encode_path(Path);
|
||||||
append_query(Path, Query) ->
|
append_query(Path, Query) ->
|
||||||
Path ++ "?" ++ binary_to_list(qs(Query)).
|
encode_path(Path) ++ "?" ++ binary_to_list(qs(Query)).
|
||||||
|
|
||||||
qs(KVs) ->
|
qs(KVs) ->
|
||||||
qs(KVs, []).
|
qs(KVs, []).
|
||||||
|
@ -388,12 +399,18 @@ may_append_body(Output, {ok, _, _}) ->
|
||||||
Output.
|
Output.
|
||||||
|
|
||||||
uri_encode(T) ->
|
uri_encode(T) ->
|
||||||
emqx_http_lib:uri_encode(to_bin(T)).
|
emqx_http_lib:uri_encode(to_list(T)).
|
||||||
|
|
||||||
|
encode_path(Path) ->
|
||||||
|
Parts = string:split(Path, "/", all),
|
||||||
|
lists:flatten(["/" ++ Part || Part <- lists:map(fun uri_encode/1, Parts)]).
|
||||||
|
|
||||||
to_list(A) when is_atom(A) ->
|
to_list(A) when is_atom(A) ->
|
||||||
atom_to_list(A);
|
atom_to_list(A);
|
||||||
to_list(B) when is_binary(B) ->
|
to_list(B) when is_binary(B) ->
|
||||||
binary_to_list(B).
|
binary_to_list(B);
|
||||||
|
to_list(L) when is_list(L) ->
|
||||||
|
L.
|
||||||
|
|
||||||
to_bin(A) when is_atom(A) ->
|
to_bin(A) when is_atom(A) ->
|
||||||
atom_to_binary(A);
|
atom_to_binary(A);
|
||||||
|
|
|
@ -94,45 +94,49 @@ authorize(
|
||||||
|
|
||||||
parse_config(
|
parse_config(
|
||||||
#{
|
#{
|
||||||
url := URL,
|
url := RawUrl,
|
||||||
method := Method,
|
method := Method,
|
||||||
headers := Headers,
|
headers := Headers,
|
||||||
request_timeout := ReqTimeout
|
request_timeout := ReqTimeout
|
||||||
} = Conf
|
} = Conf
|
||||||
) ->
|
) ->
|
||||||
{BaseURLWithPath, Query} = parse_fullpath(URL),
|
{BaseUrl0, Path, Query} = parse_url(RawUrl),
|
||||||
BaseURLMap = parse_url(BaseURLWithPath),
|
{ok, BaseUrl} = emqx_http_lib:uri_parse(BaseUrl0),
|
||||||
Conf#{
|
Conf#{
|
||||||
method => Method,
|
method => Method,
|
||||||
base_url => maps:remove(query, BaseURLMap),
|
base_url => BaseUrl,
|
||||||
|
headers => Headers,
|
||||||
|
base_path_templete => emqx_authz_utils:parse_str(Path, ?PLACEHOLDERS),
|
||||||
base_query_template => emqx_authz_utils:parse_deep(
|
base_query_template => emqx_authz_utils:parse_deep(
|
||||||
cow_qs:parse_qs(bin(Query)),
|
cow_qs:parse_qs(to_bin(Query)),
|
||||||
?PLACEHOLDERS
|
?PLACEHOLDERS
|
||||||
),
|
),
|
||||||
body_template => emqx_authz_utils:parse_deep(
|
body_template => emqx_authz_utils:parse_deep(
|
||||||
maps:to_list(maps:get(body, Conf, #{})),
|
maps:to_list(maps:get(body, Conf, #{})),
|
||||||
?PLACEHOLDERS
|
?PLACEHOLDERS
|
||||||
),
|
),
|
||||||
headers => Headers,
|
|
||||||
request_timeout => ReqTimeout,
|
request_timeout => ReqTimeout,
|
||||||
%% pool_type default value `random`
|
%% pool_type default value `random`
|
||||||
pool_type => random
|
pool_type => random
|
||||||
}.
|
}.
|
||||||
|
|
||||||
parse_fullpath(RawURL) ->
|
parse_url(Url) ->
|
||||||
cow_http:parse_fullpath(bin(RawURL)).
|
case string:split(Url, "//", leading) of
|
||||||
|
[Scheme, UrlRem] ->
|
||||||
parse_url(URL) when
|
case string:split(UrlRem, "/", leading) of
|
||||||
URL =:= undefined
|
[HostPort, Remaining] ->
|
||||||
->
|
BaseUrl = iolist_to_binary([Scheme, "//", HostPort]),
|
||||||
#{};
|
case string:split(Remaining, "?", leading) of
|
||||||
parse_url(URL) ->
|
[Path, QueryString] ->
|
||||||
{ok, URIMap} = emqx_http_lib:uri_parse(URL),
|
{BaseUrl, Path, QueryString};
|
||||||
case maps:get(query, URIMap, undefined) of
|
[Path] ->
|
||||||
undefined ->
|
{BaseUrl, Path, <<>>}
|
||||||
URIMap#{query => ""};
|
end;
|
||||||
_ ->
|
[HostPort] ->
|
||||||
URIMap
|
{iolist_to_binary([Scheme, "//", HostPort]), <<>>, <<>>}
|
||||||
|
end;
|
||||||
|
[Url] ->
|
||||||
|
throw({invalid_url, Url})
|
||||||
end.
|
end.
|
||||||
|
|
||||||
generate_request(
|
generate_request(
|
||||||
|
@ -141,21 +145,22 @@ generate_request(
|
||||||
Client,
|
Client,
|
||||||
#{
|
#{
|
||||||
method := Method,
|
method := Method,
|
||||||
base_url := #{path := Path},
|
|
||||||
base_query_template := BaseQueryTemplate,
|
|
||||||
headers := Headers,
|
headers := Headers,
|
||||||
|
base_path_templete := BasePathTemplate,
|
||||||
|
base_query_template := BaseQueryTemplate,
|
||||||
body_template := BodyTemplate
|
body_template := BodyTemplate
|
||||||
}
|
}
|
||||||
) ->
|
) ->
|
||||||
Values = client_vars(Client, PubSub, Topic),
|
Values = client_vars(Client, PubSub, Topic),
|
||||||
|
Path = emqx_authz_utils:render_str(BasePathTemplate, Values),
|
||||||
|
Query = emqx_authz_utils:render_deep(BaseQueryTemplate, Values),
|
||||||
Body = emqx_authz_utils:render_deep(BodyTemplate, Values),
|
Body = emqx_authz_utils:render_deep(BodyTemplate, Values),
|
||||||
NBaseQuery = emqx_authz_utils:render_deep(BaseQueryTemplate, Values),
|
|
||||||
case Method of
|
case Method of
|
||||||
get ->
|
get ->
|
||||||
NPath = append_query(Path, NBaseQuery ++ Body),
|
NPath = append_query(Path, Query ++ Body),
|
||||||
{NPath, Headers};
|
{NPath, Headers};
|
||||||
_ ->
|
_ ->
|
||||||
NPath = append_query(Path, NBaseQuery),
|
NPath = append_query(Path, Query),
|
||||||
NBody = serialize_body(
|
NBody = serialize_body(
|
||||||
proplists:get_value(<<"accept">>, Headers, <<"application/json">>),
|
proplists:get_value(<<"accept">>, Headers, <<"application/json">>),
|
||||||
Body
|
Body
|
||||||
|
@ -164,9 +169,9 @@ generate_request(
|
||||||
end.
|
end.
|
||||||
|
|
||||||
append_query(Path, []) ->
|
append_query(Path, []) ->
|
||||||
Path;
|
encode_path(Path);
|
||||||
append_query(Path, Query) ->
|
append_query(Path, Query) ->
|
||||||
Path ++ "?" ++ binary_to_list(query_string(Query)).
|
encode_path(Path) ++ "?" ++ to_list(query_string(Query)).
|
||||||
|
|
||||||
query_string(Body) ->
|
query_string(Body) ->
|
||||||
query_string(Body, []).
|
query_string(Body, []).
|
||||||
|
@ -179,13 +184,14 @@ query_string([], Acc) ->
|
||||||
<<>>
|
<<>>
|
||||||
end;
|
end;
|
||||||
query_string([{K, V} | More], Acc) ->
|
query_string([{K, V} | More], Acc) ->
|
||||||
query_string(
|
query_string(More, [["&", uri_encode(K), "=", uri_encode(V)] | Acc]).
|
||||||
More,
|
|
||||||
[
|
uri_encode(T) ->
|
||||||
["&", emqx_http_lib:uri_encode(K), "=", emqx_http_lib:uri_encode(V)]
|
emqx_http_lib:uri_encode(to_list(T)).
|
||||||
| Acc
|
|
||||||
]
|
encode_path(Path) ->
|
||||||
).
|
Parts = string:split(Path, "/", all),
|
||||||
|
lists:flatten(["/" ++ Part || Part <- lists:map(fun uri_encode/1, Parts)]).
|
||||||
|
|
||||||
serialize_body(<<"application/json">>, Body) ->
|
serialize_body(<<"application/json">>, Body) ->
|
||||||
jsx:encode(Body);
|
jsx:encode(Body);
|
||||||
|
@ -198,7 +204,13 @@ client_vars(Client, PubSub, Topic) ->
|
||||||
topic => Topic
|
topic => Topic
|
||||||
}.
|
}.
|
||||||
|
|
||||||
bin(A) when is_atom(A) -> atom_to_binary(A, utf8);
|
to_list(A) when is_atom(A) ->
|
||||||
bin(B) when is_binary(B) -> B;
|
atom_to_list(A);
|
||||||
bin(L) when is_list(L) -> list_to_binary(L);
|
to_list(B) when is_binary(B) ->
|
||||||
bin(X) -> X.
|
binary_to_list(B);
|
||||||
|
to_list(L) when is_list(L) ->
|
||||||
|
L.
|
||||||
|
|
||||||
|
to_bin(B) when is_binary(B) -> B;
|
||||||
|
to_bin(L) when is_list(L) -> list_to_binary(L);
|
||||||
|
to_bin(X) -> X.
|
||||||
|
|
|
@ -293,12 +293,12 @@ check_ssl_opts(Conf) ->
|
||||||
true;
|
true;
|
||||||
Url ->
|
Url ->
|
||||||
case emqx_authz_http:parse_url(Url) of
|
case emqx_authz_http:parse_url(Url) of
|
||||||
#{scheme := https} ->
|
{<<"https", _>>, _, _} ->
|
||||||
case hocon_maps:get("config.ssl.enable", Conf) of
|
case hocon_maps:get("config.ssl.enable", Conf) of
|
||||||
true -> true;
|
true -> true;
|
||||||
_ -> {error, ssl_not_enable}
|
_ -> {error, ssl_not_enable}
|
||||||
end;
|
end;
|
||||||
#{scheme := http} ->
|
{<<"http", _>>, _, _} ->
|
||||||
true;
|
true;
|
||||||
Bad ->
|
Bad ->
|
||||||
{bad_scheme, Url, Bad}
|
{bad_scheme, Url, Bad}
|
||||||
|
|
|
@ -25,8 +25,10 @@
|
||||||
create_resource/2,
|
create_resource/2,
|
||||||
update_config/2,
|
update_config/2,
|
||||||
parse_deep/2,
|
parse_deep/2,
|
||||||
|
parse_str/2,
|
||||||
parse_sql/3,
|
parse_sql/3,
|
||||||
render_deep/2,
|
render_deep/2,
|
||||||
|
render_str/2,
|
||||||
render_sql_params/2
|
render_sql_params/2
|
||||||
]).
|
]).
|
||||||
|
|
||||||
|
@ -69,6 +71,9 @@ update_config(Path, ConfigRequest) ->
|
||||||
parse_deep(Template, PlaceHolders) ->
|
parse_deep(Template, PlaceHolders) ->
|
||||||
emqx_placeholder:preproc_tmpl_deep(Template, #{placeholders => PlaceHolders}).
|
emqx_placeholder:preproc_tmpl_deep(Template, #{placeholders => PlaceHolders}).
|
||||||
|
|
||||||
|
parse_str(Template, PlaceHolders) ->
|
||||||
|
emqx_placeholder:preproc_tmpl(Template, #{placeholders => PlaceHolders}).
|
||||||
|
|
||||||
parse_sql(Template, ReplaceWith, PlaceHolders) ->
|
parse_sql(Template, ReplaceWith, PlaceHolders) ->
|
||||||
emqx_placeholder:preproc_sql(
|
emqx_placeholder:preproc_sql(
|
||||||
Template,
|
Template,
|
||||||
|
@ -85,6 +90,13 @@ render_deep(Template, Values) ->
|
||||||
#{return => full_binary, var_trans => fun handle_var/2}
|
#{return => full_binary, var_trans => fun handle_var/2}
|
||||||
).
|
).
|
||||||
|
|
||||||
|
render_str(Template, Values) ->
|
||||||
|
emqx_placeholder:proc_tmpl(
|
||||||
|
Template,
|
||||||
|
client_vars(Values),
|
||||||
|
#{return => full_binary, var_trans => fun handle_var/2}
|
||||||
|
).
|
||||||
|
|
||||||
render_sql_params(ParamList, Values) ->
|
render_sql_params(ParamList, Values) ->
|
||||||
emqx_placeholder:proc_tmpl(
|
emqx_placeholder:proc_tmpl(
|
||||||
ParamList,
|
ParamList,
|
||||||
|
|
Loading…
Reference in New Issue