Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set remote_ip from cf-connecting-ip header #1679

Merged
merged 11 commits into from
Nov 25, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ defmodule OMG.ChildChainRPC.Web.Endpoint do
use Phoenix.Endpoint, otp_app: :omg_child_chain_rpc
use Sentry.Phoenix.Endpoint

plug(RemoteIp)
plug(Plug.RequestId)
plug(Plug.Logger, log: :debug)

Expand Down
1 change: 1 addition & 0 deletions apps/omg_child_chain_rpc/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ defmodule OMG.ChildChainRPC.MixProject do
{:spandex_phoenix, "~> 0.4.1"},
{:spandex_datadog, "~> 0.4"},
{:telemetry, "~> 0.4.1"},
{:remote_ip, "~> 0.2.0"},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder, do we really need this dependency that brings two more dependencies?
Parsing an IP from the header in total three dependencies?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@InoMurko I created a custom plug

#
{:omg_bus, in_umbrella: true},
{:omg_status, in_umbrella: true},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
defmodule OMG.ChildChainRPC.Web.Controller.AlarmTest do
use ExUnitFixtures
use ExUnit.Case, async: true
use Phoenix.ConnTest

alias OMG.ChildChainRPC.Web.TestHelper

@endpoint OMG.ChildChainRPC.Web.Endpoint

setup do
{:ok, apps} = Application.ensure_all_started(:omg_status)

Expand All @@ -39,4 +42,15 @@ defmodule OMG.ChildChainRPC.Web.Controller.AlarmTest do
version = Map.get(response, "version")
%{"data" => [], "success" => true, "version" => ^version} = response
end

@tag fixtures: [:phoenix_sandbox]
test "sets remote ip from X-Forwarded-For header", _ do
response =
build_conn()
|> put_req_header("content-type", "application/json")
|> put_req_header("x-forwarded-for", "99.99.99.99")
|> get("alarm.get")

assert response.remote_ip == {99, 99, 99, 99}
end
end
1 change: 1 addition & 0 deletions apps/omg_watcher_rpc/lib/web/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ defmodule OMG.WatcherRPC.Web.Endpoint do
use Phoenix.Endpoint, otp_app: :omg_watcher_rpc
use Sentry.Phoenix.Endpoint

plug(RemoteIp)
plug(Plug.RequestId)
plug(Plug.Logger, log: :debug)

Expand Down
1 change: 1 addition & 0 deletions apps/omg_watcher_rpc/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ defmodule OMG.WatcherRPC.Mixfile do
{:spandex_phoenix, "~> 0.4.1"},
{:spandex_datadog, "~> 0.4"},
{:telemetry, "~> 0.4.1"},
{:remote_ip, "~> 0.2.0"},
# UMBRELLA
{:omg_bus, in_umbrella: true},
{:omg_utils, in_umbrella: true},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ defmodule OMG.WatcherRPC.Web.Controller.AlarmTest do
assert [] == get("alarm.get")
end

@tag fixtures: [:phoenix_ecto_sandbox, :db_initialized]
test "sets remote ip from X-Forwarded-For header", _ do
response =
build_conn()
|> put_req_header("content-type", "application/json")
|> put_req_header("x-forwarded-for", "99.99.99.99")
|> get("alarm.get")

assert response.remote_ip == {99, 99, 99, 99}
end

defp get(path) do
response_body = rpc_call_get(path, 200)
version = Map.get(response_body, "version")
Expand Down
3 changes: 3 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"briefly": {:hex, :briefly, "0.3.0", "16e6b76d2070ebc9cbd025fa85cf5dbaf52368c4bd896fb482b5a6b95a540c2f", [:mix], [], "hexpm", "c6ebf8fc3dcd4950dd10c03e953fb4f553a8bcf0ff4c8c40d71542434cd7e046"},
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"},
"certifi": {:hex, :certifi, "2.5.2", "b7cfeae9d2ed395695dd8201c57a2d019c0c43ecaf8b8bcb9320b40d6662f340", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm", "3b3b5f36493004ac3455966991eaf6e768ce9884693d9968055aeeeb1e575040"},
"combine": {:hex, :combine, "0.10.0", "eff8224eeb56498a2af13011d142c5e7997a80c8f5b97c499f84c841032e429f", [:mix], [], "hexpm", "1b1dbc1790073076580d0d1d64e42eae2366583e7aecd455d1215b0d16f2451b"},
"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm", "4a0850c9be22a43af9920a71ab17c051f5f7d45c209e40269a1938832510e4d9"},
"cors_plug": {:hex, :cors_plug, "2.0.2", "2b46083af45e4bc79632bd951550509395935d3e7973275b2b743bd63cc942ce", [:mix], [{:plug, "~> 1.8", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "f0d0e13f71c51fd4ef8b2c7e051388e4dfb267522a83a22392c856de7e46465f"},
"cowboy": {:hex, :cowboy, "1.1.2", "61ac29ea970389a88eca5a65601460162d370a70018afe6f949a29dca91f3bb0", [:rebar3], [{:cowlib, "~> 1.0.2", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.3.2", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "f4763bbe08233eceed6f24bc4fcc8d71c17cfeafa6439157c57349aa1bb4f17c"},
Expand Down Expand Up @@ -31,6 +32,7 @@
"hackney": {:hex, :hackney, "1.16.0", "5096ac8e823e3a441477b2d187e30dd3fff1a82991a806b2003845ce72ce2d84", [:rebar3], [{:certifi, "2.5.2", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.1", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.0", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.6", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm", "3bf0bebbd5d3092a3543b783bf065165fa5d3ad4b899b836810e513064134e18"},
"httpoison": {:hex, :httpoison, "1.6.2", "ace7c8d3a361cebccbed19c283c349b3d26991eff73a1eaaa8abae2e3c8089b6", [:mix], [{:hackney, "~> 1.15 and >= 1.15.2", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "aa2c74bd271af34239a3948779612f87df2422c2fdcfdbcec28d9c105f0773fe"},
"idna": {:hex, :idna, "6.0.1", "1d038fb2e7668ce41fbf681d2c45902e52b3cb9e9c77b55334353b222c2ee50c", [:rebar3], [{:unicode_util_compat, "0.5.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "a02c8a1c4fd601215bb0b0324c8a6986749f807ce35f25449ec9e69758708122"},
"inet_cidr": {:hex, :inet_cidr, "1.0.4", "a05744ab7c221ca8e395c926c3919a821eb512e8f36547c062f62c4ca0cf3d6e", [:mix], [], "hexpm", "64a2d30189704ae41ca7dbdd587f5291db5d1dda1414e0774c29ffc81088c1bc"},
"ink": {:hex, :ink, "1.1.1", "0fea8b923a2f9fe27f7a8ab77dc389b3cb12f860d80e680805be1ed653066cff", [:mix], [{:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "bba3c21115fa2bc13a6ab3d602f9859020ca19e6b0b58fff384fb1bd273b3ac9"},
"jason": {:hex, :jason, "1.2.1", "12b22825e22f468c02eb3e4b9985f3d0cb8dc40b9bd704730efa11abd2708c44", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "b659b8571deedf60f79c5a608e15414085fa141344e2716fbd6988a084b5f993"},
"keccakf1600": {:hex, :keccakf1600_orig, "2.0.0", "0a7217ddb3ee8220d449bbf7575ec39d4e967099f220a91e3dfca4dbaef91963", [:rebar3], [], "hexpm", "bdbbb02d67bea35605f95d4e3de48203347374e414da7945c4f2f7fd13ffe632"},
Expand Down Expand Up @@ -59,6 +61,7 @@
"propcheck": {:hex, :propcheck, "1.2.0", "e2b84f2f1a4c46b6b2aa22a0f6ddf97696f99d4a5c8f71d45f6519741e727eca", [:mix], [{:proper, "~> 1.3", [hex: :proper, repo: "hexpm", optional: false]}], "hexpm", "0f4fb2393fa5321ba7f23a8feabc861ac825e62d9e1db4aa35e39cace4c5c08d"},
"proper": {:hex, :proper, "1.3.0", "c1acd51c51da17a2fe91d7a6fc6a0c25a6a9849d8dc77093533109d1218d8457", [:make, :mix, :rebar3], [], "hexpm", "4aa192fccddd03fdbe50fef620be9d4d2f92635b54f55fb83aec185994403cbc"},
"ranch": {:hex, :ranch, "1.3.2", "e4965a144dc9fbe70e5c077c65e73c57165416a901bd02ea899cfd95aa890986", [:rebar3], [], "hexpm", "6e56493a862433fccc3aca3025c946d6720d8eedf6e3e6fb911952a7071c357f"},
"remote_ip": {:hex, :remote_ip, "0.2.1", "cd27cd8ea54ecaaf3532776ff4c5e353b3804e710302e88c01eadeaaf42e7e24", [:mix], [{:combine, "~> 0.10", [hex: :combine, repo: "hexpm", optional: false]}, {:inet_cidr, "~> 1.0", [hex: :inet_cidr, repo: "hexpm", optional: false]}, {:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "2e7ab1a461cc3cd5719f37e116a08f45c8b8493923063631b164315d6b7ee8e0"},
"rocksdb": {:hex, :rocksdb, "1.5.1", "016861fbfb1f76ec2c48f745fca1f6e140fc0f27adc8c389c4cabce4a79b5f2f", [:rebar3], [], "hexpm", "63db7dfd65082d61df92810f61bd934080c77bc57eb711b36bb56a8926bdf3b0"},
"sentry": {:hex, :sentry, "7.2.4", "b5bc90b594d40c2e653581e797a5fd2fdf994f2568f6bd66b7fa4971598be8d5", [:mix], [{:hackney, "~> 1.8 or 1.6.5", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix, "~> 1.3", [hex: :phoenix, repo: "hexpm", optional: true]}, {:plug, "~> 1.6", [hex: :plug, repo: "hexpm", optional: true]}, {:plug_cowboy, "~> 1.0 or ~> 2.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}], "hexpm", "4ee4d368b5013076afcc8b73ed028bdc8ee9db84ea987e3591101e194c1fc24b"},
"spandex": {:hex, :spandex, "2.4.4", "0178e797d941cd9f6b92c0db510f2cb6a7b967319c695f6ce3ba9c57a4cb1ace", [:mix], [{:decorator, "~> 1.2", [hex: :decorator, repo: "hexpm", optional: true]}, {:optimal, "~> 0.3.3", [hex: :optimal, repo: "hexpm", optional: false]}, {:plug, ">= 1.0.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "195bcaf5cbadaf88eb5afbe3a41403482504ff0c8e185cbb062fc56242a6402e"},
Expand Down