Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,8 @@ jobs:
key: ${{ runner.os }}-dialyzer-${{ hashFiles('**/mix.lock') }}
restore-keys: ${{ runner.os }}-dialyzer-

- name: Create PLT directory
run: mkdir -p priv/plts

- name: Run Dialyzer
run: mix dialyzer --halt-exit-status
8 changes: 7 additions & 1 deletion Dockerfile.core
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ RUN mix deps.compile
RUN npm install -g bun

# Build assets using bun and tailwind
RUN mix assets.setup && \
# DATABASE_URL and SECRET_KEY_BASE are needed because mix assets.deploy
# triggers runtime.exs evaluation in prod; these are build-time placeholders only
RUN DATABASE_URL=ecto://placeholder/placeholder \
SECRET_KEY_BASE=placeholder_secret_key_base_for_asset_compilation_only_00 \
mix assets.setup && \
DATABASE_URL=ecto://placeholder/placeholder \
SECRET_KEY_BASE=placeholder_secret_key_base_for_asset_compilation_only_00 \
mix assets.deploy

# Compile application
Expand Down
8 changes: 7 additions & 1 deletion Dockerfile.core-standalone
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ RUN mix deps.compile
RUN npm install -g bun

# Build assets using bun and tailwind
RUN mix assets.setup && \
# DATABASE_URL and SECRET_KEY_BASE are needed because mix assets.deploy
# triggers runtime.exs evaluation in prod; these are build-time placeholders only
RUN DATABASE_URL=ecto://placeholder/placeholder \
SECRET_KEY_BASE=placeholder_secret_key_base_for_asset_compilation_only_00 \
mix assets.setup && \
DATABASE_URL=ecto://placeholder/placeholder \
SECRET_KEY_BASE=placeholder_secret_key_base_for_asset_compilation_only_00 \
mix assets.deploy

# Compile application
Expand Down
80 changes: 43 additions & 37 deletions apps/secrethub_web/lib/secret_hub/web/channels/agent_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -84,44 +84,50 @@ defmodule SecretHub.Web.AgentChannel do
def handle_in("authenticate", %{"role_id" => role_id, "secret_id" => secret_id}, socket) do
source_ip = get_source_ip(socket)

case AppRole.login(role_id, secret_id, source_ip) do
{:ok, auth_result} ->
# Create or update agent record
case Agents.bootstrap_agent(role_id, secret_id, %{
"name" => auth_result.role_name,
"ip_address" => source_ip,
"user_agent" => "SecretHub Agent v1.0"
}) do
{:ok, agent} ->
socket =
socket
|> assign(:authenticated, true)
|> assign(:agent_id, agent.agent_id)
|> assign(:role_name, auth_result.role_name)
|> assign(:policies, auth_result.policies)
|> assign(:token, auth_result.token)
|> assign(:last_heartbeat, DateTime.utc_now() |> DateTime.truncate(:second))

Logger.info("Agent authenticated: #{agent.agent_id} (#{auth_result.role_name})")

{:reply,
{:ok,
%{
status: "authenticated",
agent_id: agent.agent_id,
role_name: auth_result.role_name,
policies: auth_result.policies,
token: auth_result.token
}}, socket}

{:error, reason} ->
Logger.warning("Agent bootstrap failed: #{inspect(reason)}")
{:reply, {:error, %{reason: "authentication_failed"}}, socket}
end
try do
case AppRole.login(role_id, secret_id, source_ip) do
{:ok, auth_result} ->
# Create or update agent record
case Agents.bootstrap_agent(role_id, secret_id, %{
"name" => auth_result.role_name,
"ip_address" => source_ip,
"user_agent" => "SecretHub Agent v1.0"
}) do
{:ok, agent} ->
socket =
socket
|> assign(:authenticated, true)
|> assign(:agent_id, agent.agent_id)
|> assign(:role_name, auth_result.role_name)
|> assign(:policies, auth_result.policies)
|> assign(:token, auth_result.token)
|> assign(:last_heartbeat, DateTime.utc_now() |> DateTime.truncate(:second))

Logger.info("Agent authenticated: #{agent.agent_id} (#{auth_result.role_name})")

{:reply,
{:ok,
%{
status: "authenticated",
agent_id: agent.agent_id,
role_name: auth_result.role_name,
policies: auth_result.policies,
token: auth_result.token
}}, socket}

{:error, reason} ->
Logger.warning("Agent bootstrap failed: #{inspect(reason)}")
{:reply, {:error, %{reason: "authentication_failed"}}, socket}
end

{:error, reason} ->
Logger.warning("AppRole login failed: #{reason}")
{:reply, {:error, %{reason: "invalid_credentials"}}, socket}
{:error, reason} ->
Logger.warning("AppRole login failed: #{reason}")
{:reply, {:error, %{reason: "invalid_credentials"}}, socket}
end
rescue
error ->
Logger.error("Unexpected error during authentication: #{inspect(error)}")
{:reply, {:error, %{reason: "internal_error"}}, socket}
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ defmodule SecretHub.Web.AgentChannelTest do
assert socket.assigns.agent_id == nil
end

test "rejects joining specific agent channels", %{socket: socket} do
assert {:error, %{reason: "unauthorized"}} =
subscribe_and_join(socket, AgentChannel, "agent:some-agent-id", %{})
test "allows direct join to specific agent channels with auto-auth", %{socket: socket} do
{:ok, reply, socket} =
subscribe_and_join(socket, AgentChannel, "agent:some-agent-id", %{})

assert reply.status == "connected"
assert reply.authenticated == true
assert reply.agent_id == "some-agent-id"
assert socket.assigns.authenticated == true
assert socket.assigns.agent_id == "some-agent-id"
end
end

Expand All @@ -34,27 +40,27 @@ defmodule SecretHub.Web.AgentChannelTest do

test "rejects secret:request without authentication", %{socket: socket} do
ref = push(socket, "secret:request", %{"path" => "prod.db.password"})
assert_reply ref, :error, %{reason: "not_authenticated"}
assert_reply(ref, :error, %{reason: "not_authenticated"})
end

test "rejects heartbeat without authentication", %{socket: socket} do
ref = push(socket, "heartbeat", %{})
assert_reply ref, :error, %{reason: "not_authenticated"}
assert_reply(ref, :error, %{reason: "not_authenticated"})
end

test "rejects secret:renew without authentication", %{socket: socket} do
ref = push(socket, "secret:renew", %{"lease_id" => "test-lease"})
assert_reply ref, :error, %{reason: "not_authenticated"}
assert_reply(ref, :error, %{reason: "not_authenticated"})
end

test "returns error for invalid authentication payload", %{socket: socket} do
ref = push(socket, "authenticate", %{"invalid" => "data"})
assert_reply ref, :error, %{reason: "invalid_authentication_payload"}
assert_reply(ref, :error, %{reason: "invalid_authentication_payload"})
end

test "returns error for unknown event", %{socket: socket} do
ref = push(socket, "unknown:event", %{})
assert_reply ref, :error, %{reason: "unknown_event"}
assert_reply(ref, :error, %{reason: "unknown_event"})
end
end
end
Loading
Loading