Skip to content

Commit 3673dda

Browse files
authored
Fix some git file storage issues (#3069)
1 parent 4eb9406 commit 3673dda

File tree

6 files changed

+32
-41
lines changed

6 files changed

+32
-41
lines changed

lib/livebook/file_system/git.ex

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,6 @@ defmodule Livebook.FileSystem.Git do
3838
message: "must be a valid repo URL"
3939
)
4040
|> validate_required([:repo_url, :branch, :key, :hub_id])
41-
|> update_change(:key, fn key ->
42-
if key =~ "\n" do
43-
key
44-
else
45-
String.replace(
46-
key,
47-
~r/ (?!(?:OPENSSH |RSA |EC |DSA |PRIVATE )?(?:PRIVATE )?KEY-----)/,
48-
"\n"
49-
)
50-
end
51-
end)
5241
|> put_id()
5342
end
5443

lib/livebook/file_system/git/client.ex

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ defmodule Livebook.FileSystem.Git.Client do
136136
end
137137

138138
defp env_opts(key_path) do
139-
[env: %{"GIT_SSH_COMMAND" => "ssh -i '#{key_path}'"}]
139+
[env: %{"GIT_SSH_COMMAND" => "ssh -o StrictHostKeyChecking=no -i '#{key_path}'"}]
140140
end
141141

142142
defp fetch_repository(file_system) do
@@ -151,10 +151,16 @@ defmodule Livebook.FileSystem.Git.Client do
151151

152152
defp with_ssh_key_file(file_system, fun) when is_function(fun, 1) do
153153
File.mkdir_p!(Livebook.Config.tmp_path())
154-
155154
key_path = FileSystem.Git.key_path(file_system)
156-
pem_entry = :public_key.pem_decode(file_system.key)
157-
ssh_key = :public_key.pem_encode(pem_entry)
155+
156+
ssh_key =
157+
file_system.key
158+
|> String.replace(
159+
~r/ (?!(?:OPENSSH |RSA |EC |DSA |PRIVATE )?(?:PRIVATE )?KEY-----)/,
160+
"\n"
161+
)
162+
|> :public_key.pem_decode()
163+
|> :public_key.pem_encode()
158164

159165
File.write!(key_path, ssh_key)
160166
File.chmod!(key_path, 0o600)

lib/livebook/file_system/mounter.ex

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
defmodule Livebook.FileSystem.Mounter do
22
# This server is responsible to handle file systems that are mountable
33
use GenServer
4+
require Logger
45

56
alias Livebook.{FileSystem, Hubs}
67

@@ -26,7 +27,7 @@ defmodule Livebook.FileSystem.Mounter do
2627

2728
@impl GenServer
2829
def handle_continue(:boot, state) do
29-
Hubs.Broadcasts.subscribe([:connection, :crud, :file_systems])
30+
Hubs.Broadcasts.subscribe([:crud, :file_systems])
3031
Process.send_after(self(), :remount, state.loop_delay)
3132

3233
{:noreply, mount_file_systems(state, Hubs.Personal.id())}
@@ -49,10 +50,6 @@ defmodule Livebook.FileSystem.Mounter do
4950
{:noreply, unmount_file_system(state, file_system)}
5051
end
5152

52-
def handle_info({:hub_changed, hub_id}, state) do
53-
{:noreply, mount_file_systems(state, hub_id)}
54-
end
55-
5653
def handle_info({:hub_deleted, hub_id}, state) do
5754
{:noreply, unmount_file_systems(state, hub_id)}
5855
end
@@ -98,7 +95,10 @@ defmodule Livebook.FileSystem.Mounter do
9895
broadcast({:file_system_mounted, file_system})
9996
put_hub_file_system(state, file_system)
10097

101-
{:error, _reason} ->
98+
{:error, reason} ->
99+
Logger.error("[file_system=#{name(file_system)}] failed to mount: #{reason}")
100+
Process.send_after(self(), {:file_system_created, file_system}, to_timeout(second: 10))
101+
102102
state
103103
end
104104
end
@@ -109,7 +109,10 @@ defmodule Livebook.FileSystem.Mounter do
109109
broadcast({:file_system_unmounted, file_system})
110110
remove_hub_file_system(state, file_system)
111111

112-
{:error, _reason} ->
112+
{:error, reason} ->
113+
Logger.error("[file_system=#{name(file_system)}] failed to unmount: #{reason}")
114+
Process.send_after(self(), {:file_system_deleted, file_system}, to_timeout(second: 10))
115+
113116
state
114117
end
115118
end
@@ -148,6 +151,10 @@ defmodule Livebook.FileSystem.Mounter do
148151
put_in(hub_data.file_systems, file_systems)
149152
end
150153

154+
defp name(file_system) do
155+
FileSystem.external_metadata(file_system).name
156+
end
157+
151158
if Mix.env() == :test do
152159
defp broadcast({_, %{external_id: _, hub_id: id}} = message) do
153160
Phoenix.PubSub.broadcast(Livebook.PubSub, "file_systems:#{id}", message)

test/livebook_teams/web/hub/edit_live_test.exs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -229,15 +229,6 @@ defmodule LivebookWeb.Integration.Hub.EditLiveTest do
229229
test "creates a Git file system", %{conn: conn, team: team} do
230230
id = Livebook.FileSystem.Utils.id("git", team.id, "[email protected]:livebook-dev/test.git")
231231
file_system = build(:fs_git, id: id, hub_id: team.id)
232-
233-
# When the user paste the ssh key to the password input, it will remove the break lines.
234-
# So, the changeset need to normalize it before persisting. That said, the ssh key from
235-
# factory must be "denormalized" so we can test the user scenario.
236-
file_system = %{
237-
file_system
238-
| key: file_system.key |> String.replace("\n", "\s") |> String.trim()
239-
}
240-
241232
attrs = %{file_system: Livebook.FileSystem.dump(file_system)}
242233

243234
{:ok, view, _html} = live(conn, ~p"/hub/#{team.id}")

test/livebook_web/live/hub/edit_live_test.exs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,6 @@ defmodule LivebookWeb.Hub.EditLiveTest do
201201
test "creates a Git file system", %{conn: conn, hub: hub} do
202202
file_system = build(:fs_git)
203203
id = file_system.id
204-
205-
# When the user paste the ssh key to the password input, it will remove the break lines.
206-
# So, the changeset need to normalize it before persisting. That said, the ssh key from
207-
# factory must be "denormalized" so we can test the user scenario.
208-
file_system = %{
209-
file_system
210-
| key: file_system.key |> String.replace("\n", "\s") |> String.trim()
211-
}
212-
213204
attrs = %{file_system: Livebook.FileSystem.dump(file_system)}
214205

215206
{:ok, view, _html} = live(conn, ~p"/hub/#{hub.id}")

test/support/factory.ex

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,14 @@ defmodule Livebook.Factory do
9696
def build(:fs_git) do
9797
repo_url = "[email protected]:livebook-dev/test.git"
9898
hub_id = Livebook.Hubs.Personal.id()
99-
key = System.get_env("TEST_GIT_SSH_KEY")
99+
100+
# When the user paste the ssh key to the password input, it will remove the break lines.
101+
# So, the changeset need to normalize it before persisting. That said, the ssh key from
102+
# factory must be "denormalized" so we can test the user scenario.
103+
key =
104+
System.get_env("TEST_GIT_SSH_KEY")
105+
|> String.replace("\n", "\s")
106+
|> String.trim()
100107

101108
%Livebook.FileSystem.Git{
102109
id: Livebook.FileSystem.Utils.id("git", hub_id, repo_url),

0 commit comments

Comments
 (0)