-
Notifications
You must be signed in to change notification settings - Fork 64
/
validations.ex
277 lines (216 loc) · 7.02 KB
/
validations.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
defmodule RigInboundGateway.ApiProxy.Validations do
@moduledoc """
Used to validate APIs configuration for reverse proxy.
Validates use cases such as setting "target" to "kafka" but not "topic", which is required in such case.
When any error occurs during RIG start -> process will exit.
When any error occurs during REST API request -> process won't exit, but instead API returns 400 -- bad request.
"""
use Rig.Config, [:system]
alias RigInboundGateway.ApiProxy.Api
require Logger
@type error_t :: [{:error, String.t() | atom, atom, String.t()}]
@type error_list_t :: [{String.t(), error_t()}]
@type error_map_t :: %{String.t() => [%{(String.t() | atom) => String.t()}]}
@spec validate_endpoint_target(Api.endpoint(), [String.t()]) :: boolean
def validate_endpoint_target(endpoint, targets) do
Vex.valid?(endpoint, %{"target" => [inclusion: targets]})
end
# ---
@spec with_nested_presence(boolean, String.t() | atom, map) :: error_t()
def with_nested_presence(true, key, map) do
Vex.errors(map, %{key => [presence: true]})
end
def with_nested_presence(_, _, _), do: []
# ---
@spec validate_auth_type(Api.t()) :: error_t()
def validate_auth_type(%{"auth" => %{"use_header" => true}} = api) do
Vex.errors(api, %{"auth_type" => [inclusion: ["jwt"]]})
end
def validate_auth_type(%{"auth" => %{"use_query" => true}} = api) do
Vex.errors(api, %{"auth_type" => [inclusion: ["jwt"]]})
end
def validate_auth_type(_), do: []
# ---
@spec validate_auth(error_list_t(), Api.t()) :: error_list_t()
def validate_auth(errors, %{"auth" => auth, "id" => id} = api) do
use_header_error =
Vex.errors(
auth,
%{
"header_name" => [
presence: [
if: fn _ -> Vex.valid?(auth, %{"use_header" => [presence: true]}) end
]
]
}
)
use_query_error =
Vex.errors(
auth,
%{
"query_name" => [
presence: [
if: fn _ -> Vex.valid?(auth, %{"use_query" => [presence: true]}) end
]
]
}
)
all_errors = use_header_error ++ use_query_error ++ validate_auth_type(api)
merge_errors(errors, [{id, all_errors}])
end
def validate_auth(errors, _), do: errors
# ---
@spec validate_secured_endpoint(Api.t(), Api.endpoint()) :: error_t()
def validate_secured_endpoint(api, %{"secured" => true}) do
Vex.errors(api, %{"auth_type" => [inclusion: ["jwt"]]})
end
def validate_secured_endpoint(_, _), do: []
# ---
@spec with_any_error(error_list_t(), integer) :: error_list_t()
def with_any_error(errors, min_errors \\ 1)
def with_any_error(errors, min_errors) when length(errors) > min_errors, do: errors
def with_any_error(_, _), do: []
# ---
@spec validate_endpoints(error_list_t(), Api.t()) :: error_list_t()
def validate_endpoints(
errors,
%{"id" => id, "version_data" => %{"default" => %{"endpoints" => endpoints}}} = api
) do
Enum.reduce(endpoints, [], fn endpoint, acc ->
topic_presence_config =
endpoint
|> validate_endpoint_target(["kafka", "kinesis"])
|> with_nested_presence("topic", endpoint)
schema_presence_config =
endpoint
|> Vex.valid?(%{"schema" => [presence: true]})
|> with_nested_presence("target", endpoint)
all_errors =
validate_secured_endpoint(api, endpoint) ++
topic_presence_config ++
schema_presence_config ++
validate_string(endpoint, "id") ++
validate_string(endpoint, "path_regex") ++
validate_string(endpoint, "method")
merge_errors(acc, [{"#{id}/#{endpoint["id"]}", all_errors}])
end)
|> Enum.concat(errors)
end
def validate_endpoints(errors, _), do: errors
# ---
@spec type_validation(map, String.t(), String.t(), function) :: error_list_t()
def type_validation(map, key, type, fun) do
Vex.errors(
map,
%{
key => fn key ->
if fun.(key) do
:ok
else
{:error, "must be #{type}"}
end
end
}
)
end
# ---
@spec validate_string(map, String.t()) :: error_list_t()
def validate_string(map, key) do
type_validation(map, key, "string", &is_binary/1) ++
Vex.errors(map, %{key => [length: [min: 1]]})
end
# ---
@spec validate_integer(map, String.t()) :: error_list_t()
def validate_integer(map, key) do
type_validation(map, key, "integer", &is_integer/1)
end
# ---
@spec validate_list(map, String.t()) :: error_list_t()
def validate_list(map, key) do
type_validation(map, key, "list", &is_list/1)
end
# ---
@spec validate_proxy(Api.t()) :: error_list_t()
def validate_proxy(%{"proxy" => proxy}) do
validate_string(proxy, "target_url") ++ validate_integer(proxy, "port")
end
def validate_proxy(_), do: []
# ---
@spec validate_version_data(Api.t()) :: error_list_t()
def validate_version_data(%{"version_data" => version_data}) do
keys = Map.keys(version_data)
if length(keys) > 0 do
Enum.reduce(keys, [], fn key, acc ->
endpoints_errors = version_data |> Map.get(key) |> validate_list("endpoints")
acc ++ endpoints_errors
end)
else
[{:error, "version_data", :presence, "must have at least one version, e.g. default"}]
end
end
def validate_version_data(_), do: []
# ---
@spec validate_required_props(error_list_t(), Api.t()) :: error_list_t()
def validate_required_props(
errors,
api
) do
api_errors =
validate_string(api, "id") ++
validate_string(api, "name") ++
Vex.errors(api, %{"proxy" => [presence: true]}) ++
validate_proxy(api) ++
Vex.errors(api, %{"version_data" => [presence: true]}) ++
validate_version_data(api)
merge_errors(errors, [{"api", api_errors}])
end
# ---
@spec validate_all(Api.t()) :: error_list_t()
def validate_all(api) do
[]
|> validate_required_props(api)
|> validate_auth(api)
|> validate_endpoints(api)
|> Enum.dedup()
end
# ---
@spec validate!(Api.t()) :: Api.t()
def validate!(api) do
conf = config()
errors = validate_all(api)
if errors != [] do
log_error(errors)
conf.system.stop()
end
api
end
# ---
@spec validate(Api.t()) :: {:error, error_list_t()} | {:ok, Api.t()}
def validate(api) do
errors = validate_all(api)
if errors != [] do
log_error(errors)
{:error, errors}
else
{:ok, api}
end
end
# ---
@spec to_map(error_list_t()) :: error_map_t()
def(to_map(errors)) do
errors
|> Enum.map(fn {key, value} ->
{key, Enum.map(value, fn {_, key, _, reason} -> %{key => reason} end)}
end)
|> Enum.into(%{})
end
# ---
defp log_error(errors) do
Logger.error(fn ->
"Wrong reverse proxy configuration: #{inspect(errors)}"
end)
end
# ---
defp merge_errors(current_errors, [{_key, []}]), do: current_errors
defp merge_errors(current_errors, new_errors), do: current_errors ++ new_errors
end