ueberauth / guardian_db

Guardian DB integration for tracking tokens and ensuring logout cannot be replayed.
MIT License
367 stars 87 forks source link

GuardianDb.ExpiredSweeper is not available in release #29

Closed AlanMcCann closed 7 years ago

AlanMcCann commented 7 years ago

Mix server works fine for me whether in dev or prod environments.

PORT=4000  MIX_ENV=prod mix phoenix.server
17:23:18.603 [info] Running App.Endpoint with Cowboy using http://localhost:4000
INITING THE SWEEPER 7200000

However, when I create a release and execute it, I get the following error. I'm likely missing a config variable - ie. this is likely not a bug.

Erlang/OTP 18 [erts-7.3] [source-d2a6d81] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]

17:29:59.556 [info] Running App.Endpoint with Cowboy using http://localhost:4000
17:29:59.558 [info] Application app exited: Appstart(:normal, []) returned an error: shutdown: failed to start child: GuardianDb.ExpiredSweeper
    ** (EXIT) an exception was raised:
        ** (UndefinedFunctionError) function GuardianDb.ExpiredSweeper.start_link/0 is undefined (module GuardianDb.ExpiredSweeper is not available)
            GuardianDb.ExpiredSweeper.start_link()
            (stdlib) supervisor.erl:358: :supervisor.do_start_child/2
            (stdlib) supervisor.erl:341: :supervisor.start_children/3
            (stdlib) supervisor.erl:307: :supervisor.init_children/2
            (stdlib) gen_server.erl:328: :gen_server.init_it/6
            (stdlib) proc_lib.erl:240: :proc_lib.init_p_do_apply/3
{"Kernel pid terminated",application_controller,"{application_start_failure,app,{{shutdown,{failed_to_start_child,'Elixir.GuardianDb.ExpiredSweeper',{'EXIT',{undef,[{'Elixir.GuardianDb.ExpiredSweeper',start_link,[],[]},{supervisor,do_start_child,2,[{file,\"supervisor.erl\"},{line,358}]},{supervisor,start_children,3,[{file,\"supervisor.erl\"},{line,341}]},{supervisor,init_children,2,[{file,\"supervisor.erl\"},{line,307}]},{gen_server,init_it,6,[{file,\"gen_server.erl\"},{line,328}]},{proc_lib,init_p_do_apply,3,[{file,\"proc_lib.erl\"},{line,240}]}]}}}},{'Elixir.App',start,[normal,[]]}}}"}

Crash dump is being written to: erl_crash.dump...done
Kernel pid terminated (application_controller) ({application_start_failure,app,{{shutdown,{failed_to_start_child,'Elixir.GuardianDb.ExpiredSweeper',{'EXIT',{undef,[{'Elixir.GuardianDb.ExpiredSwee

I have attached my app.ex and config.exs below (App name obfuscated)

defmodule App 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(App.Repo, []),
      # Start the endpoint when the application starts
      supervisor(App.Endpoint, []),
      # Start Presence when the application starts
      supervisor(App.Presence, []),
      # Start your own worker by calling: App.Worker.start_link(arg1, arg2, arg3)
      # worker(AppWorker, [arg1, arg2, arg3]),

      # https://github.com/hassox/guardian_db
      worker(GuardianDb.ExpiredSweeper, [])
    ]

    # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: App.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
    App.Endpoint.config_change(changed, removed)
    :ok
  end
end
# Configure Guardian
# https://github.com/ueberauth/guardian
config :guardian, Guardian,
  allowed_algos: ["HS512"], # optional
  verify_module: Guardian.JWT,  # optional
  issuer: "App.#{Mix.env}",
  ttl: { 30, :days },
  verify_issuer: true, # optional
  secret_key: to_string(Mix.env),
  serializer: App.GuardianSerializer,
  hooks: GuardianDb,
  Permissions: %{
    default: [
      :read_profile,
      :write_profile,
      :read_token,
      :revoke_token
    ]
  }

# https://github.com/hassox/guardian_db
config :guardian_db, GuardianDb,
       repo: App.Repo,
       # schema_name: "tokens",
       sweep_interval: 120
doomspork commented 7 years ago

@AlanMcCann have you resolved this?

AlanMcCann commented 7 years ago

I have had a blocking bug in ueberauth that has stopped from getting back to this.

doomspork commented 7 years ago

Thanks for the update @AlanMcCann, do you have an Issue on the Ueberauth repo I can peek at?

AlanMcCann commented 7 years ago

@doomspork Any help is appreciated - https://github.com/ueberauth/ueberauth/issues/43

st23am commented 7 years ago

I'm getting the same error but, I am not using uberauth..

I'm currently not running the sweeper and still getting this error. I'll try and reproduce in a clean repo and link it.

The error I'm getting:

** (exit) an exception was raised:
    ** (UndefinedFunctionError) function GuardianDb.before_encode_and_sign/3 is undefined (module GuardianDb is not available)
        GuardianDb.before_encode_and_sign(%Admin{__meta__: }, nil, %{"aud" => "Admin:1", "exp" => 1487366827, "iat" => 1484774827, "iss" => "MyApp", "jti" => "_my_jti_", "pem" => %{}, "sub" => "Admin:1", "typ" => "access"})
        (guardian) lib/guardian.ex:86: Guardian.encode_and_sign/3
        (guardian) lib/guardian/plug.ex:117: Guardian.Plug.sign_in/4
        (my_app) web/models/auth.ex:53: MyApp.Auth.build_authorization_payload/2
        (my_app) web/models/auth.ex:5: MyApp.Auth.browser_login/2
        (my_app) web/controllers/session_controller.ex:17: MyApp.SessionController.create/2
        (my_app) web/controllers/session_controller.ex:1: MyApp.SessionController.action/2
        (my_app) web/controllers/session_controller.ex:1: MyApp.SessionController.phoenix_controller_pipeline/2
st23am commented 7 years ago

I was able to solve this by adding guardian_db and distillery to my applications list. I could have sworn I had tried this previously but I guess not.

I created a example repo with steps to reproduce and the solution. I hope this helps @AlanMcCann *Example Reposistory: https://github.com/st23am/test_guardian_db_issue_29

doomspork commented 7 years ago

Ah ha @st23am! Good catch. @AlanMcCann can you try adding guardian_db to your applications list and see if that resolves the issue for you?

ybur-yug commented 7 years ago

@doomspork Do you think the docs/README could be clarified for this? Would be happy to throw up a PR.

doomspork commented 7 years ago

That would be a good idea @ybur-yug 👍

doomspork commented 7 years ago

This should be resolved as of #44

anasilva65 commented 3 years ago

did you manage to solve? give me the resolution please

Hanspagh commented 3 years ago

@anasilva65 Did you see the docs added in #44