gcauchon

gcauchon

Good afternoon folks

I’ve been playing around with LiveView for a few days now and I have to admit it’s changing my approach to many use cases for client projets!

There is one thing I can wrap my head around: configuration for the production release; let me explain…

  1. In our actual way of packaging releases in Docker images, following 12-Factor App principles, the Plug.Session gets it’s key and signing_salt options from environment variables with Application.get_env/3.

https://github.com/mirego/elixir-boilerplate/blob/master/lib/elixir_boilerplate_web/endpoint.ex#L125-L134

  1. The LiveView documentation says to extract the @session_options to a module attribute…

https://github.com/phoenixframework/phoenix_live_view/blob/master/guides/introduction/installation.md#L84-L101

As module attribute, options are expanded at compile time! It works locally for development purposes since the same variables are used at both compile time and runtime, but breaks during the docker build… of the CI workflow because SESSION_KEY and SIGNING_SALT are not available!

I looked at the code in Phoenix and Plug.Session to understand if there is a different way to configure the socket session callback, but came out clear the
session needs to be a Map…

https://github.com/phoenixframework/phoenix_live_view/blob/master/lib/phoenix_live_view/socket.ex#L52-L53

Is it considered good practice to bake the session key and signing_salt in a version binaries, meaning we would have to build a new Docker image to change those value?

Showing Posts 1 to 10

gcauchon

gcauchon OP

@chrismccord, @josevalim, @snewcomer I’m curious to get your opinion on ^this?

chrismccord

chrismccord

Creator of Phoenix

On Phoenix master we allow the session connect_info to be an MFA, so stay tuned.

gcauchon

gcauchon OP

Did a little research for reference to anyone finding this post in the future!

Here’s the code in Phoenix.Socket.Transport to support a {MFA} 3-tuple to initialize the session configuration:

phoenix/lib/phoenix/socket/transport.ex at main · phoenixframework/phoenix · GitHub

The original issue: Missing capability of loading runtime config for Phoenix.Endpoint.socket · Issue #3659 · phoenixframework/phoenix · GitHub

And the pull request adding the feature: Allow setting mfa for loading socket session config by kafaichoi · Pull Request #3668 · phoenixframework/phoenix · GitHub

gcauchon

gcauchon OP

@chrismccord Even with MFA, it looks like the configuration still is compile time!

The before_compile macro expands to private do_handler/3 functions to match on every HTTP/Socket paths.

To generate the list of sockets to loop on, socket_paths/4 invokes Phoenix.Socket.Transport.load_config/2 explicitly, thus resolving the MFA, at compile time…

gcauchon

gcauchon OP

A little update on my configuration issue with MFA!

I opened an issue on the Phoenix repo yesterday…

https://github.com/phoenixframework/phoenix/issues/3754

it was fixed 6h later by @josevalim :tada:

i-n-g-m-a-r

i-n-g-m-a-r

@gcauchon How do you solve the plug Plug.Session compile-time problem?

It seems this part was solved:

@session_options {MyModule, :get_session_config, []}
socket "/live", Phoenix.LiveView.Socket,
  websocket: [connect_info: [session: @session_options]] 

But it does not seem to play nicely with this part:

plug Plug.Session, @session_options

I try to understand the explanation here:

This part:

{:session, session_config} - the session information from Plug.Conn . The session_config is an exact copy of the arguments given to Plug.Session . This requires the “_csrf_token” to be given as request parameter with the value of URI.encode_www_form(Plug.CSRFProtection.get_csrf_token()) when connecting to the socket. It can also be a MFA to allow loading config in runtime {MyAppWeb.Auth, :get_session_config, []} . Otherwise the session will be nil .

But to me this is abracadabra and I’m not sure how this relates to configuring Plug.Session at runtime.
Do you have an actual working example of configuring the session at runtime?

(probably startup time is a better term)

gcauchon

gcauchon OP

I’m not using the “vanilla” Plug.Session configuration! Like for many other plugs in the default Phoenix configuration, you hate to use init/1 and call/2 explicitly to bypass the default macro expansion at compile time.

I also extracted a get_options/0 function to a separate module to be {m, f, a} compatible and am not using the module attribute because it is not required anymore!

lib/foo_web/endpoint.ex
defmodule FooWeb.Endpoint do
  use Phoenix.Endpoint, otp_app: :foo
  …

  socket("/live", Phoenix.LiveView.Socket, 
    websocket: [
      connect_info: [
        {:session, {FooWeb.Session, :get_options, []}}
      ]
    ])
  …

  plug(Plug.MethodOverride)
  plug(Plug.Head)
  plug(:session)
  plug(FooWeb.Router)

  …

  defp session(conn, _opts) do
    opts = Plug.Session.init(FooWeb.Session.get_options())

    Plug.Session.call(conn, opts)
  end
 …

end
lib/foo_web/session.ex
defmodule FooWeb.Session do
  def get_options do
    [
      store: :cookie,
      key: System.get_env("SESSION_KEY"),
      signing_salt: System.get_env("SIGNING_SALT")
    ]
  end
end
i-n-g-m-a-r

i-n-g-m-a-r

Thank you for this explanation, very useful indeed.
I wonder how System.get_env/2 performance compares to populating the application environment on startup and calling Application.get_env/3 on every request.
Not sure whether System.get_env/2 caches results and is also a single ets read once a value becomes available.

i-n-g-m-a-r

i-n-g-m-a-r

Thanks again @gcauchon, your solution works like a charm, awesome!
I decided to load session options into the application environment.
Also an inline session module will do the trick.

# MyApp.Application
...

def start(_type, _args) do
  Application.put_env(:my_app, :session_options, [
    store: :cookie,
    key: "session",
    signing_salt: System.fetch_env!("SESSION_SIGNING_SALT")
  ])
  children = [
  ...
end

...

Configure session at runtime.

# MyApp.Endpoint
...

defmodule Session do
  def options do
    Application.get_env(:my_app, :session_options)
  end
end

defp session(conn, _opts) do
  Plug.Session.call(conn, Plug.Session.init(
    Session.options()
  ))
end

...

socket "/live", Phoenix.LiveView.Socket, websocket: [
  connect_info: [session: {Session, :options, []}]
]

...

plug Plug.MethodOverride
plug Plug.Head
plug(:session)
plug MyApp.Router

...
gcauchon

gcauchon OP

I really like the idea of reading environment variables from Application, not System. I will adopt a similar approach for sure :+1:

Where Next? Top

Trending in Questions Top

water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New

Other Trending Topics Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews