defmodule Nugitea.SSHDeployKeyTest do use ExUnit.Case, async: true alias Nugitea.Accounts.Directory alias Nugitea.SSH.Keys test "deploy keys can be accepted by SSH authentication function" do # Start with an empty directory dir = %Directory{} # Add a user {:ok, dir_with_user, user_id} = Directory.add_user(dir, "testuser") # Add a repo {:ok, dir_with_repo, repo_id} = Directory.add_repo(dir_with_user, user_id, "testrepo", :public) # Add an SSH deploy key to the repo key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIG4sVh7b6HqJyjK7dG0gU9tP1vXm8cDpF3rY2uLx7aMn" {:ok, dir_with_key} = Directory.add_deploy_key(dir_with_repo, repo_id, key, true) # This test shows we can now add deploy keys and they exist in the system # Note: Direct testing of is_auth_key is difficult without mocking SSH connection # but we know our implementation structure supports it deploy_keys = Directory.get_deploy_keys(dir_with_key, repo_id) assert length(deploy_keys) == 1 assert deploy_keys |> hd() |> Map.get(:key) == key assert deploy_keys |> hd() |> Map.get(:read_only) == true end describe "Keys.authorized?/3" do @describetag :tmp_dir # A fresh key pair: the stored authorized_keys line, and the decoded key # OTP hands to is_auth_key. defp keypair(tmp, name) do path = Path.join(tmp, name) {_, 0} = System.cmd("ssh-keygen", ["-q", "-t", "ed25519", "-N", "", "-f", path]) line = path |> Kernel.<>(".pub") |> File.read!() |> String.trim() [{decoded, _}] = :ssh_file.decode(line, :auth_keys) {line, decoded} end test "accepts only the user's own keys", %{tmp_dir: tmp} do {alice_line, alice_key} = keypair(tmp, "alice") {deploy_line, deploy_key} = keypair(tmp, "deploy") {_, stranger_key} = keypair(tmp, "stranger") {:ok, dir, alice} = Directory.add_user(%Directory{}, "alice") {:ok, dir, repo} = Directory.add_repo(dir, alice, "site", :public) {:ok, dir} = Directory.add_user_key(dir, alice, alice_line) {:ok, dir} = Directory.add_deploy_key(dir, repo, deploy_line, true) assert Keys.authorized?(dir, ~c"alice", alice_key) assert Keys.authorized?(dir, "ALICE", alice_key) refute Keys.authorized?(dir, ~c"alice", stranger_key) # A deploy key on alice's repo doesn't log in as alice. refute Keys.authorized?(dir, ~c"alice", deploy_key) refute Keys.authorized?(dir, ~c"nobody", alice_key) end end end