teamon

teamon

Disabling external network access

I have a fairly large codebase with many files and many tests.
For whatever reasons that happened over the years, some tests execute code that does call an external APIs. It’s not easy to spot these tests (they pop up only when something breaks), these calls often come from some deep code using different http libraries.

I wonder if there is a way to somehow setup beam to filter TCP traffic - disable all network access except for e.g. localhost:5432 (postgres) during tests. It could be useful especially on the CI (Locally one can disconnect internet and then try to find these bad tests)

tl;dr - Is there any way to block (or log) all external TCP traffic on the beam level?

Most Liked

teamon

teamon

Thanks!

I forgot about the amazing dbg - I’ve put this into test/test_helper.ex

require Logger

:dbg.start()

:dbg.tracer(
  :process,
  {fn {:trace, _pid, :call, {:gen_tcp, :connect, [host, port | _]}}, n ->
     case {host, port} do
       {~c"localhost", 5432} -> :ok
       {~c"localhost", 6379} -> :ok
       _ -> Logger.error("\n\n****\n\nConnection to #{host}:#{port} not allowed\n\n****\n")
     end

     n + 1
   end, 0}
)

:dbg.tp(:gen_tcp, :connect, [])
:dbg.p(:all, :c)

and it will log unwanted connections during tests.

garazdawi

garazdawi

Erlang Core Team

You can configure the inet dns resolver to never resolve anything. That way only ip addresses will be possible to use.

For example:

echo "{lookup,[]}." > ./erl_inetrc
ERL_INETRC=./erl_inetrc iex
Erlang/OTP 27 [RELEASE CANDIDATE 2] [erts-14.2.3] [source-f7fa414e3e] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit:ns]

Interactive Elixir (1.17.0-dev) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> :gen_tcp.connect(~c"localhost",80,[])
{:error, :nxdomain}

You can read more about how to configure inetrc here: Inet Configuration — OTP 29.0.2 (erts 17.0.2)

dimitarvp

dimitarvp

You can also try Overview — extrace v0.6.0 with some of the lower-level functions in :inet but I am not sure that would work at all.

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54092 488
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement