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: Erlang -- Inet Configuration

dimitarvp

dimitarvp

You can also try Overview — extrace v0.5.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

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement