Skip to content

Commit d7c30ae

Browse files
authored
Merge pull request #335 from Accenture/334-fix-endpoint-validation
334 fix endpoint validation
2 parents 099a3bf + 2a753c0 commit d7c30ae

File tree

7 files changed

+292
-12
lines changed

7 files changed

+292
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3535
### Fixed
3636

3737
- Fixed a bug where distributed set processes would crash when one of their peers has died but hasn't been removed yet from the pg2 group.
38+
- Fixed wrong endpoint validation for reverse proxy. Now it should correctly check for `path` or `path_regex`. Before it would require `path` even with `path_regex` in place. [#334](https://github.com/Accenture/reactive-interaction-gateway/issues/334)
3839
3940
<!-- ### Deprecated -->
4041

lib/rig_api/v1/apis_test.exs

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ defmodule RigApi.V1.APIsTest do
357357
}
358358
end
359359

360-
test "should return 400 when 'endpoint' doesn't have required properties 'id', 'method' and 'path'" do
360+
test "should return 400 when 'endpoint' doesn't have required properties 'id', 'method' and 'path' or 'path_regex'" do
361361
endpoints = [%{}]
362362
new_api = ProxyConfig.create_proxy_config(@invalid_config_id, endpoints)
363363
conn = build_conn() |> put("/v1/apis/#{@invalid_config_id}", new_api)
@@ -367,14 +367,74 @@ defmodule RigApi.V1.APIsTest do
367367
"invalid-config/" => [
368368
%{"id" => "must be string"},
369369
%{"id" => "must have a length of at least 1"},
370-
%{"path" => "must be string"},
371-
%{"path" => "must have a length of at least 1"},
370+
%{"path, path_regex" => "Either path or path_regex must be set"},
372371
%{"method" => "must be string"},
373372
%{"method" => "must have a length of at least 1"}
374373
]
375374
}
376375
end
377376

377+
test "should return 400 when 'endpoint' has 'path' and 'path_regex' set at the same time" do
378+
endpoints = [
379+
%{
380+
"id" => @invalid_config_id <> "1",
381+
"method" => "GET",
382+
"path" => "/",
383+
"path_regex" => "/"
384+
}
385+
]
386+
387+
new_api = ProxyConfig.create_proxy_config(@invalid_config_id, endpoints)
388+
conn = build_conn() |> put("/v1/apis/#{@invalid_config_id}", new_api)
389+
response = json_response(conn, 400)
390+
391+
assert response == %{
392+
"invalid-config/invalid-config1" => [
393+
%{"path, path_regex" => "You can't set path and path_regex at the same time"}
394+
]
395+
}
396+
end
397+
398+
test "should return 400 when when 'path' is set, but incorrect" do
399+
endpoints = [
400+
%{
401+
"id" => @invalid_config_id <> "1",
402+
"method" => "GET",
403+
"path" => ""
404+
}
405+
]
406+
407+
new_api = ProxyConfig.create_proxy_config(@invalid_config_id, endpoints)
408+
conn = build_conn() |> put("/v1/apis/#{@invalid_config_id}", new_api)
409+
response = json_response(conn, 400)
410+
411+
assert response == %{
412+
"invalid-config/invalid-config1" => [
413+
%{"path" => "must have a length of at least 1"}
414+
]
415+
}
416+
end
417+
418+
test "should return 400 when when 'path_regex' is set, but incorrect" do
419+
endpoints = [
420+
%{
421+
"id" => @invalid_config_id <> "1",
422+
"method" => "GET",
423+
"path_regex" => ""
424+
}
425+
]
426+
427+
new_api = ProxyConfig.create_proxy_config(@invalid_config_id, endpoints)
428+
conn = build_conn() |> put("/v1/apis/#{@invalid_config_id}", new_api)
429+
response = json_response(conn, 400)
430+
431+
assert response == %{
432+
"invalid-config/invalid-config1" => [
433+
%{"path_regex" => "must have a length of at least 1"}
434+
]
435+
}
436+
end
437+
378438
test "should return 400 when target is set to kafka or kinesis, but topic is not" do
379439
# kinesis topic not set
380440
endpoints = [

lib/rig_api/v2/apis_test.exs

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ defmodule RigApi.V2.APIsTest do
357357
}
358358
end
359359

360-
test "should return 400 when 'endpoint' doesn't have required properties 'id', 'method' and 'path'" do
360+
test "should return 400 when 'endpoint' doesn't have required properties 'id', 'method' and 'path' or 'path_regex'" do
361361
endpoints = [%{}]
362362
new_api = ProxyConfig.create_proxy_config(@invalid_config_id, endpoints)
363363
conn = build_conn() |> put("/v2/apis/#{@invalid_config_id}", new_api)
@@ -367,14 +367,74 @@ defmodule RigApi.V2.APIsTest do
367367
"invalid-config/" => [
368368
%{"id" => "must be string"},
369369
%{"id" => "must have a length of at least 1"},
370-
%{"path" => "must be string"},
371-
%{"path" => "must have a length of at least 1"},
370+
%{"path, path_regex" => "Either path or path_regex must be set"},
372371
%{"method" => "must be string"},
373372
%{"method" => "must have a length of at least 1"}
374373
]
375374
}
376375
end
377376

377+
test "should return 400 when 'endpoint' has 'path' and 'path_regex' set at the same time" do
378+
endpoints = [
379+
%{
380+
"id" => @invalid_config_id <> "1",
381+
"method" => "GET",
382+
"path" => "/",
383+
"path_regex" => "/"
384+
}
385+
]
386+
387+
new_api = ProxyConfig.create_proxy_config(@invalid_config_id, endpoints)
388+
conn = build_conn() |> put("/v2/apis/#{@invalid_config_id}", new_api)
389+
response = json_response(conn, 400)
390+
391+
assert response == %{
392+
"invalid-config/invalid-config1" => [
393+
%{"path, path_regex" => "You can't set path and path_regex at the same time"}
394+
]
395+
}
396+
end
397+
398+
test "should return 400 when when 'path' is set, but incorrect" do
399+
endpoints = [
400+
%{
401+
"id" => @invalid_config_id <> "1",
402+
"method" => "GET",
403+
"path" => ""
404+
}
405+
]
406+
407+
new_api = ProxyConfig.create_proxy_config(@invalid_config_id, endpoints)
408+
conn = build_conn() |> put("/v2/apis/#{@invalid_config_id}", new_api)
409+
response = json_response(conn, 400)
410+
411+
assert response == %{
412+
"invalid-config/invalid-config1" => [
413+
%{"path" => "must have a length of at least 1"}
414+
]
415+
}
416+
end
417+
418+
test "should return 400 when when 'path_regex' is set, but incorrect" do
419+
endpoints = [
420+
%{
421+
"id" => @invalid_config_id <> "1",
422+
"method" => "GET",
423+
"path_regex" => ""
424+
}
425+
]
426+
427+
new_api = ProxyConfig.create_proxy_config(@invalid_config_id, endpoints)
428+
conn = build_conn() |> put("/v2/apis/#{@invalid_config_id}", new_api)
429+
response = json_response(conn, 400)
430+
431+
assert response == %{
432+
"invalid-config/invalid-config1" => [
433+
%{"path_regex" => "must have a length of at least 1"}
434+
]
435+
}
436+
end
437+
378438
test "should return 400 when target is set to kafka or kinesis, but topic is not" do
379439
# kinesis topic not set
380440
endpoints = [

lib/rig_api/v3/apis_test.exs

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ defmodule RigApi.V3.APIsTest do
357357
}
358358
end
359359

360-
test "should return 400 when 'endpoint' doesn't have required properties 'id', 'method' and 'path'" do
360+
test "should return 400 when 'endpoint' doesn't have required properties 'id', 'method' and 'path' or 'path_regex'" do
361361
endpoints = [%{}]
362362
new_api = ProxyConfig.create_proxy_config(@invalid_config_id, endpoints)
363363
conn = build_conn() |> put("/v3/apis/#{@invalid_config_id}", new_api)
@@ -367,14 +367,74 @@ defmodule RigApi.V3.APIsTest do
367367
"invalid-config/" => [
368368
%{"id" => "must be string"},
369369
%{"id" => "must have a length of at least 1"},
370-
%{"path" => "must be string"},
371-
%{"path" => "must have a length of at least 1"},
370+
%{"path, path_regex" => "Either path or path_regex must be set"},
372371
%{"method" => "must be string"},
373372
%{"method" => "must have a length of at least 1"}
374373
]
375374
}
376375
end
377376

377+
test "should return 400 when 'endpoint' has 'path' and 'path_regex' set at the same time" do
378+
endpoints = [
379+
%{
380+
"id" => @invalid_config_id <> "1",
381+
"method" => "GET",
382+
"path" => "/",
383+
"path_regex" => "/"
384+
}
385+
]
386+
387+
new_api = ProxyConfig.create_proxy_config(@invalid_config_id, endpoints)
388+
conn = build_conn() |> put("/v3/apis/#{@invalid_config_id}", new_api)
389+
response = json_response(conn, 400)
390+
391+
assert response == %{
392+
"invalid-config/invalid-config1" => [
393+
%{"path, path_regex" => "You can't set path and path_regex at the same time"}
394+
]
395+
}
396+
end
397+
398+
test "should return 400 when when 'path' is set, but incorrect" do
399+
endpoints = [
400+
%{
401+
"id" => @invalid_config_id <> "1",
402+
"method" => "GET",
403+
"path" => ""
404+
}
405+
]
406+
407+
new_api = ProxyConfig.create_proxy_config(@invalid_config_id, endpoints)
408+
conn = build_conn() |> put("/v3/apis/#{@invalid_config_id}", new_api)
409+
response = json_response(conn, 400)
410+
411+
assert response == %{
412+
"invalid-config/invalid-config1" => [
413+
%{"path" => "must have a length of at least 1"}
414+
]
415+
}
416+
end
417+
418+
test "should return 400 when when 'path_regex' is set, but incorrect" do
419+
endpoints = [
420+
%{
421+
"id" => @invalid_config_id <> "1",
422+
"method" => "GET",
423+
"path_regex" => ""
424+
}
425+
]
426+
427+
new_api = ProxyConfig.create_proxy_config(@invalid_config_id, endpoints)
428+
conn = build_conn() |> put("/v3/apis/#{@invalid_config_id}", new_api)
429+
response = json_response(conn, 400)
430+
431+
assert response == %{
432+
"invalid-config/invalid-config1" => [
433+
%{"path_regex" => "must have a length of at least 1"}
434+
]
435+
}
436+
end
437+
378438
test "should return 400 when target is set to kafka or kinesis, but topic is not" do
379439
# kinesis topic not set
380440
endpoints = [

lib/rig_inbound_gateway/api_proxy/validations.ex

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ defmodule RigInboundGateway.ApiProxy.Validations do
1212

1313
require Logger
1414

15+
@endpoint_paths ["path", "path_regex"]
16+
@endpoint_paths_error_key "path, path_regex"
17+
1518
@type error_t :: [{:error, String.t() | atom, atom, String.t()}]
1619
@type error_list_t :: [{String.t(), error_t()}]
1720
@type error_map_t :: %{String.t() => [%{(String.t() | atom) => String.t()}]}
@@ -88,6 +91,29 @@ defmodule RigInboundGateway.ApiProxy.Validations do
8891

8992
# ---
9093

94+
@spec validate_endpoint_path(Api.endpoint()) :: error_list_t()
95+
def validate_endpoint_path(endpoint) do
96+
present_paths =
97+
@endpoint_paths
98+
|> Enum.filter(fn key -> Map.has_key?(endpoint, key) end)
99+
100+
case present_paths do
101+
[] ->
102+
[{:error, @endpoint_paths_error_key, :by, "Either path or path_regex must be set"}]
103+
104+
[path] ->
105+
validate_string(endpoint, path)
106+
107+
_ ->
108+
[
109+
{:error, @endpoint_paths_error_key, :by,
110+
"You can't set path and path_regex at the same time"}
111+
]
112+
end
113+
end
114+
115+
# ---
116+
91117
@spec with_any_error(error_list_t(), integer) :: error_list_t()
92118
def with_any_error(errors, min_errors \\ 1)
93119
def with_any_error(errors, min_errors) when length(errors) > min_errors, do: errors
@@ -127,13 +153,15 @@ defmodule RigInboundGateway.ApiProxy.Validations do
127153
|> Vex.valid?(%{"schema" => [presence: true]})
128154
|> with_nested_presence("target", endpoint)
129155

156+
endpoint_path_errors = validate_endpoint_path(endpoint)
157+
130158
all_errors =
131159
validate_secured_endpoint(api, endpoint) ++
132160
with_any_error(topic_presence) ++
133161
with_any_error(stream_presence) ++
134162
schema_presence_config ++
135163
validate_string(endpoint, "id") ++
136-
validate_string(endpoint, "path") ++ validate_string(endpoint, "method")
164+
endpoint_path_errors ++ validate_string(endpoint, "method")
137165

138166
merge_errors(acc, [{"#{id}/#{endpoint["id"]}", all_errors}])
139167
end)

0 commit comments

Comments
 (0)