michalmuskala / plug_attack

A plug building toolkit for blocking and throttling abusive requests
419 stars 20 forks source link

I did everything in the tutorial, but it gives me an error #2

Closed eduardonunesp closed 7 years ago

eduardonunesp commented 7 years ago
== Compilation error on file lib/myapp/plug_attack.ex ==
** (CompileError) lib/myapp/plug_attack.ex:2: undefined function halt/1
    (stdlib) lists.erl:1338: :lists.foreach/2
    (stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
    (elixir) lib/kernel/parallel_compiler.ex:117: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/1

part of the controller

defmodule MyApp.AuthenticationController do
  use MyApp.Web, :controller

  alias MyApp.{
    Authentication,
    Mailer,
    Password
  }

  plug MyApp.PlugAttack

Also I'm using Phoenix 1.2.1 and Elixir 1.4.1

michalmuskala commented 7 years ago

You didn't show the code where the error comes from.

The halt/1 function is from Plug.Conn.halt/1, but currently use PlugAttack does not import this module automatically. This should be probably changed. For now, adding import Plug.Conn should solve the issue.

eduardonunesp commented 7 years ago

Adding import Plug.Conn solved, the issue, but there's a another issue when start mix phoenix.server related to the params

defmodule MyApp.PlugAttack do
  import Plug.Conn # ADDED HERE !

  use PlugAttack

  rule "allow local", conn do
    allow conn.remote_ip == {127, 0, 0, 1}
  end

  rule "throttle by ip", conn do
    throttle conn.remote_ip,
      period: 60_000, limit: 10,
      storage: {PlugAttack.Storage.Ets, MyApp.PlugAttack.Storage}
  end
end

After mix phoenix.server

=INFO REPORT==== 12-Feb-2017::17:55:17 ===
    application: logger
    exited: stopped
    type: temporary
** (Mix) Could not start application myapp: MyApp.start(:normal, []) returned an error: shutdown: failed to start child: PlugAttack.Storage.Ets
    ** (EXIT) an exception was raised:
        ** (FunctionClauseError) no function clause matching in Keyword.get/3
            (elixir) lib/keyword.ex:150: Keyword.get({:clean_period, 60000}, :clean_period, 5000)
            (plug_attack) lib/storage/ets.ex:83: PlugAttack.Storage.Ets.start_link/2
            (stdlib) supervisor.erl:365: :supervisor.do_start_child/2
            (stdlib) supervisor.erl:348: :supervisor.start_children/3
            (stdlib) supervisor.erl:314: :supervisor.init_children/2
            (stdlib) gen_server.erl:328: :gen_server.init_it/6
            (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3

But the conf on supervisor looks ok

defmodule MyApp do
  use Application

  # See http://elixir-lang.org/docs/stable/elixir/Application.html
  # for more information on OTP Applications
  def start(_type, _args) do
    import Supervisor.Spec

    # Define workers and child supervisors to be supervised
    children = [
      # Start the Ecto repository
      supervisor(MyApp.Repo, []),
      # Start the endpoint when the application starts
      supervisor(MyApp.Endpoint, []),
      # Start your own worker by calling: MyApp.Worker.start_link(arg1, arg2, arg3)
      # worker(MyApp.Worker, [arg1, arg2, arg3]),
      worker(PlugAttack.Storage.Ets, [MyApp.PlugAttack.Storage, clean_period: 60_000])
    ]

    # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: MyApp.Supervisor]
    Supervisor.start_link(children, opts)
  end

  # Tell Phoenix to update the endpoint configuration
  # whenever the application is updated.
  def config_change(changed, _new, removed) do
    MyApp.Endpoint.config_change(changed, removed)
    :ok
  end
end
michalmuskala commented 7 years ago

Your arguments to the worker are wrong (as they are in the example).

It should be:

worker(PlugAttack.Storage.Ets, [MyApp.PlugAttack.Storage, [clean_period: 60_000]])

instead of

worker(PlugAttack.Storage.Ets, [MyApp.PlugAttack.Storage, clean_period: 60_000])

The second argument to the PlugAttack.Storage.Ets.start_link/2 function should be a keyword list.

eduardonunesp commented 7 years ago

Good catch ! it worked. Thanks