Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix VPN session expiration query #1586

Merged
merged 3 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/fz_http/lib/fz_http/devices/device/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ defmodule FzHttp.Devices.Device.Query do
dynamic =
if FzHttp.Config.vpn_sessions_expire?() do
vpn_session_duration = FzHttp.Config.fetch_config!(:vpn_session_duration)
vpn_session_interval = %Postgrex.Interval{days: 0, months: 0, secs: vpn_session_duration}

dynamic(
[user: user],
is_nil(user.last_signed_in_at) or
user.last_signed_in_at < from_now(^vpn_session_duration, "second")
fragment("? + ?::interval", user.last_signed_in_at, ^vpn_session_interval) > fragment("now()")
)
else
true
Expand Down
10 changes: 7 additions & 3 deletions apps/fz_http/test/fz_http/devices_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -853,14 +853,18 @@ defmodule FzHttp.DevicesTest do
end

test "does not render peers for users with expired VPN session" do
FzHttp.Config.put_system_env_override(:vpn_session_duration, 1)
two_seconds_in_future = DateTime.utc_now() |> DateTime.add(2, :second)
FzHttp.Config.put_system_env_override(:vpn_session_duration, 5)
ten_seconds_in_past = DateTime.utc_now() |> DateTime.add(-10, :second)
user = UsersFixtures.create_user_with_role(:unprivileged)
DevicesFixtures.create_device(user: user)

user = UsersFixtures.update(user, last_signed_in_at: two_seconds_in_future)
user = UsersFixtures.update(user, last_signed_in_at: ten_seconds_in_past)
assert to_peer_list() == []

three_seconds_in_past = DateTime.utc_now() |> DateTime.add(-3, :second)
user = UsersFixtures.update(user, last_signed_in_at: three_seconds_in_past)
assert length(to_peer_list()) == 1

UsersFixtures.update(user, last_signed_in_at: nil)
assert length(to_peer_list()) == 1
end
Expand Down