Skip to content

Commit 6ad0696

Browse files
chore: add more logging for user authentication (#726)
## πŸ“ Description Verbose logging for user authentication process and 404s from front service. ## βœ… Checklist - [ ] I have tested this change - [ ] This change requires documentation update
1 parent 7047857 commit 6ad0696

File tree

7 files changed

+73
-10
lines changed

7 files changed

+73
-10
lines changed

β€Žee/rbac/lib/rbac/utils/http.exβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ defmodule Rbac.Utils.Http do
5757
{:ok, Plug.Crypto.non_executable_binary_to_term(encoded_state, [:safe]), conn}
5858

5959
:error ->
60+
Logger.warning("State key: #{key} not found in cookies")
6061
{:error, "State key: #{key} not found in cookies"}
6162
end
6263
end

β€Žee/rbac/test/rbac/grpc_servers/okta_server_test.exsβ€Ž

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -601,8 +601,7 @@ defmodule Rbac.GrpcServers.OktaServer.Test do
601601
assert {:ok, response} = InternalApi.Okta.Okta.Stub.set_up_mapping(channel, request)
602602
assert %InternalApi.Okta.SetUpMappingResponse{} = response
603603

604-
{:ok, idp_mapping} =
605-
Rbac.Okta.IdpGroupMapping.get_for_organization(integration.org_id)
604+
{:ok, idp_mapping} = Rbac.Okta.IdpGroupMapping.get_for_organization(integration.org_id)
606605

607606
assert length(idp_mapping.group_mapping) == 2
608607
assert length(idp_mapping.role_mapping) == 1
@@ -800,8 +799,7 @@ defmodule Rbac.GrpcServers.OktaServer.Test do
800799

801800
assert {:ok, channel} = GRPC.Stub.connect("localhost:50051")
802801

803-
assert {:error, error} =
804-
InternalApi.Okta.Okta.Stub.set_up_mapping(channel, initial_request)
802+
assert {:error, error} = InternalApi.Okta.Okta.Stub.set_up_mapping(channel, initial_request)
805803

806804
assert error.message =~ "Invalid"
807805
end

β€Žee/rbac/test/rbac/grpc_servers/rbac_server_test.exsβ€Ž

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -616,8 +616,7 @@ defmodule Rbac.GrpcServers.RbacServer.Test do
616616

617617
1..3
618618
|> Enum.each(fn i ->
619-
{:ok, rbac_user} =
620-
Support.Factories.RbacUser.insert(UUID.generate(), "John Doe #{i}")
619+
{:ok, rbac_user} = Support.Factories.RbacUser.insert(UUID.generate(), "John Doe #{i}")
621620

622621
Support.Rbac.assign_org_role_by_name(@org_id, rbac_user.id, Enum.at(roles, i - 1))
623622
end)

β€Žfront/lib/front_web/plugs/fetch_permissions.exβ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ defmodule FrontWeb.Plugs.FetchPermissions do
2525
user_id = conn.assigns.user_id
2626
org_id = conn.assigns.organization_id
2727

28+
if is_nil(user_id) do
29+
Logger.info(
30+
"[FetchPermissions] Missing user_id while fetching permissions scope=#{scope} org_id=#{inspect(org_id)} path=#{conn.request_path}"
31+
)
32+
end
33+
2834
has_permissions =
2935
if scope == "org" do
3036
Front.RBAC.Permissions.has?(user_id, org_id, permissions)

β€Žfront/lib/front_web/plugs/public_page_access.exβ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ defmodule FrontWeb.Plugs.PublicPageAccess do
2020
Plug.Conn.assign(conn, :authorization, :guest)
2121

2222
true ->
23+
project = conn.assigns[:project]
24+
25+
Logger.info(
26+
"[PageAccess] Anonymous/unauthorized access blocked: path=#{conn.request_path} org_id=#{inspect(conn.assigns[:organization_id])} project_id=#{inspect(project && project.id)} public?=#{inspect(project && project.public)} user_id=#{inspect(conn.assigns[:user_id])}"
27+
)
28+
2329
conn |> render_404()
2430
end
2531
rescue

β€Žguard/lib/guard/grpc_servers/auth_server.exβ€Ž

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,23 @@ defmodule Guard.GrpcServers.AuthServer do
2424
@spec authenticate_with_cookie(Auth.AuthenticateWithCookieRequest.t(), GRPC.Server.Stream.t()) ::
2525
Auth.AuthenticateResponse.t()
2626
def authenticate_with_cookie(%Auth.AuthenticateWithCookieRequest{cookie: cookie}, _stream) do
27+
cookie_hash =
28+
:crypto.hash(:md5, cookie)
29+
|> Base.encode16(case: :lower)
30+
31+
Logger.debug("[AuthServer] authenticate_with_cookie start hash=#{cookie_hash}")
32+
2733
observe("grpc.authentication.authenticate_with_cookie", fn ->
2834
case find_user_by_cookie(cookie) do
2935
{:ok, {user, id_provider, ip_address, user_agent}} ->
36+
Logger.debug(
37+
"[AuthServer] authenticate_with_cookie user_id=#{user.id} provider=#{id_provider} ip=#{ip_address}"
38+
)
39+
3040
respond_with_user(user, id_provider, ip_address, user_agent)
3141

3242
{:error, :user, :not_found} ->
43+
Logger.debug("[AuthServer] authenticate_with_cookie not found hash=#{cookie_hash}")
3344
respond_false()
3445
end
3546
end)
@@ -70,23 +81,40 @@ defmodule Guard.GrpcServers.AuthServer do
7081
end
7182
end
7283

73-
defp find_user_by_cookie(""), do: {:error, :user, :not_found}
84+
defp find_user_by_cookie("") do
85+
Logger.debug("[AuthServer] find_user_by_cookie empty cookie")
86+
{:error, :user, :not_found}
87+
end
7488

7589
defp find_user_by_cookie(cookie) do
7690
case Guard.Session.deserialize_from_cookie(cookie) do
7791
{:ok, {id_provider, user_data, session_data, extras}} ->
92+
Logger.debug(
93+
"[AuthServer] find_user_by_cookie deserialized provider=#{id_provider} session_keys=#{inspect(Map.keys(session_data))} extras=#{inspect(Map.keys(extras))}"
94+
)
95+
7896
with {:ok, user_data, extras} <- process_session(session_data, user_data, extras),
7997
{:ok, user} <- get_user(user_data) do
98+
Logger.debug(
99+
"[AuthServer] find_user_by_cookie resolved user_id=#{user.id} provider=#{id_provider}"
100+
)
101+
80102
{:ok, {user, id_provider, extras.ip_address, extras.user_agent}}
81103
else
82104
{:error, :user_not_found} ->
105+
Logger.debug(
106+
"[AuthServer] find_user_by_cookie user not found after session processing"
107+
)
108+
83109
{:error, :user, :not_found}
84110

85111
{:error, :session_process_error} ->
112+
Logger.debug("[AuthServer] find_user_by_cookie session processing error")
86113
{:error, :user, :not_found}
87114
end
88115

89116
{:error, :invalid_cookie} ->
117+
Logger.debug("[AuthServer] find_user_by_cookie invalid cookie format")
90118
{:error, :user, :not_found}
91119
end
92120
end
@@ -111,30 +139,54 @@ defmodule Guard.GrpcServers.AuthServer do
111139
{:ok, Map.t(), Map.t()}
112140
| {:error, :session_process_error}
113141
defp process_session(%{id: session_id}, _, _) do
142+
Logger.debug("[AuthServer] process_session OIDC id=#{session_id}")
143+
114144
case Guard.Store.OIDCSession.get(session_id) do
115145
{:error, :not_found} ->
146+
Logger.debug("[AuthServer] process_session session not found id=#{session_id}")
116147
{:error, :session_process_error}
117148

118149
{:ok, %Guard.Repo.OIDCSession{refresh_token_enc: nil}} ->
150+
Logger.debug("[AuthServer] process_session refresh_token missing id=#{session_id}")
119151
{:error, :session_process_error}
120152

121153
{:ok, session} ->
122154
extras = %{ip_address: session.ip_address, user_agent: session.user_agent}
123155

124156
if Guard.Store.OIDCSession.expired?(session) do
157+
Logger.debug(
158+
"[AuthServer] process_session expired id=#{session_id} user_id=#{session.user_id}"
159+
)
160+
125161
case refresh_session(session) do
126-
{:ok, session} -> {:ok, %{id: session.user_id}, extras}
127-
{:error, _} -> {:error, :session_process_error}
162+
{:ok, session} ->
163+
{:ok, %{id: session.user_id}, extras}
164+
165+
{:error, reason} ->
166+
Logger.debug(
167+
"[AuthServer] process_session refresh failed id=#{session_id} user_id=#{session.user_id} reason=#{inspect(reason)}"
168+
)
169+
170+
{:error, :session_process_error}
128171
end
129172
else
173+
Logger.debug(
174+
"[AuthServer] process_session valid id=#{session_id} user_id=#{session.user_id}"
175+
)
176+
130177
{:ok, %{id: session.user_id}, extras}
131178
end
132179
end
133180
end
134181

135-
defp process_session(%{}, user_data, extras), do: {:ok, user_data, extras}
182+
defp process_session(%{}, user_data, extras) do
183+
Logger.debug("[AuthServer] process_session no session_id, skipping")
184+
{:ok, user_data, extras}
185+
end
136186

137187
defp refresh_session(session) do
188+
Logger.debug("[AuthServer] refresh_session id=#{session.id} user_id=#{session.user_id}")
189+
138190
with {:ok, refresh_token} <-
139191
Guard.OIDC.Token.decrypt(session.refresh_token_enc, session.user_id),
140192
{:ok, tokens} <- refresh_token(refresh_token, session.user),

β€Žguard/lib/guard/utils.exβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ defmodule Guard.Utils.Http do
226226
{:ok, Plug.Crypto.non_executable_binary_to_term(encoded_state, [:safe]), conn}
227227

228228
:error ->
229+
Logger.warn("State key: #{key} not found in cookies")
229230
{:error, "State key: #{key} not found in cookies"}
230231
end
231232
end

0 commit comments

Comments
Β (0)