Skip to content

Commit

Permalink
Merge pull request #227 from pluralsh/shell-git-oauth
Browse files Browse the repository at this point in the history
Auto-Infer git user via oauth information
  • Loading branch information
michaeljguarino authored May 11, 2022
2 parents ed92004 + 919eca7 commit 3890dd0
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 7 deletions.
15 changes: 15 additions & 0 deletions apps/core/lib/core/schema/cloud_shell.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ defmodule Core.Schema.CloudShell do
end
end

defmodule GitInfo do
use Piazza.Ecto.Schema

embedded_schema do
field :username, :string
field :email, :string
end

@valid ~w(username email)a

def changeset(model, attrs \\ %{}), do: cast(model, attrs, @valid)
end

schema "cloud_shells" do
field :provider, Provider
field :git_url, :string
Expand All @@ -93,6 +106,7 @@ defmodule Core.Schema.CloudShell do
field :ssh_public_key, EncryptedString
field :ssh_private_key, EncryptedString

embeds_one :git_info, GitInfo
embeds_one :workspace, Workspace
embeds_one :credentials, Credentials

Expand All @@ -109,6 +123,7 @@ defmodule Core.Schema.CloudShell do
|> cast(attrs, @valid)
|> cast_embed(:workspace)
|> cast_embed(:credentials)
|> cast_embed(:git_info)
|> foreign_key_constraint(:demo_id)
|> foreign_key_constraint(:user_id)
|> put_new_change(:pod_name, &pod_name/0)
Expand Down
4 changes: 2 additions & 2 deletions apps/core/lib/core/services/shell.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ defmodule Core.Services.Shell do
|> add_operation(:git, fn
%{fetch: nil, create: shell} ->
%{provider: p, token: t, name: n} = args = attrs[:scm]
with {:ok, url, pub, priv} <- Scm.setup_repository(p, user.email, t, args[:org], n) do
with {:ok, url, pub, priv, user} <- Scm.setup_repository(p, user.email, t, args[:org], n) do
shell
|> CloudShell.changeset(%{git_url: url, ssh_public_key: pub, ssh_private_key: priv})
|> CloudShell.changeset(%{git_url: url, ssh_public_key: pub, ssh_private_key: priv, git_info: user})
|> Core.Repo.update()
end
%{create: shell} -> {:ok, shell}
Expand Down
1 change: 0 additions & 1 deletion apps/core/lib/core/services/shell/demo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ defmodule Core.Services.Shell.Demo do
def poll_demo_project(%DemoProject{state: :ready, enabled_op_id: op_id} = proj) do
svcs_conn()
|> SvcsOperations.serviceusage_operations_get(op_id)
|> IO.inspect()
|> case do
{:error, %Tesla.Env{status: 404}} -> enable(proj)
{:ok, %{done: true}} -> enable(proj)
Expand Down
8 changes: 6 additions & 2 deletions apps/core/lib/core/services/shell/scm.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ defmodule Core.Shell.Scm do
@doc """
Sets up a repository against a common SCM system for use in the shell
"""
@spec setup_repository(provider, binary, binary, binary, binary) :: {:ok, binary, binary, binary} | error
@spec setup_repository(provider, binary, binary, binary, binary) :: {:ok, binary, binary, binary, map} | error
def setup_repository(:github, email, token, org, name) do
client = Github.client(token)
with {:ok, private, public} <- keypair(email),
{:ok, %{"ssh_url" => url} = repo} <- Github.create_repository(client, name, org),
:ok <- Github.register_keys(client, public, repo),
do: {:ok, url, public, private}
{:ok, user} <- Core.OAuth.Github.get_user(client),
do: {:ok, url, public, private, git_info(user)}
end

@doc """
Expand All @@ -41,4 +42,7 @@ defmodule Core.Shell.Scm do
def get_token(:github, code), do: Github.get_token(code)

defp authorize_url(:github), do: Github.authorize_url()

defp git_info(%{email: email} = user), do: %{username: user[:name], email: email}
defp git_info(_), do: nil
end
2 changes: 1 addition & 1 deletion apps/core/lib/core/services/shell/scm/github.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ defmodule Core.Shell.Scm.Github do

def authorize_url() do
oauth_client()
|> OAuth2.Client.authorize_url!(scope: "repo read:org")
|> OAuth2.Client.authorize_url!(scope: "user user:email user:name repo read:org")
end

def get_token(code) do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Core.Repo.Migrations.ShellGitInfo do
use Ecto.Migration

def change do
alter table(:cloud_shells) do
add :git_info, :map
end
end
end
7 changes: 7 additions & 0 deletions apps/core/test/services/shell_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ defmodule Core.Services.ShellTest do
{:ok, %HTTPoison.Response{status_code: 200, body: "OK"}}
end)

expect(OAuth2.Client, :get, 2, fn
_, "/user" -> {:ok, %OAuth2.Response{body: %{"name" => "name"}}}
_, "/user/emails" -> {:ok, %OAuth2.Response{body: [%{"primary" => true, "email" => "me@example.com"}]}}
end)

{:ok, shell} = Shell.create_shell(%{
provider: :aws,
credentials: %{
Expand All @@ -42,6 +47,8 @@ defmodule Core.Services.ShellTest do
assert shell.ssh_private_key
assert shell.git_url == "git@github.com:pluralsh/installations.git"
assert shell.provider == :aws
assert shell.git_info.username == "name"
assert shell.git_info.email == "me@example.com"
assert shell.credentials.aws.access_key_id == "access_key"
assert shell.credentials.aws.secret_access_key == "secret"
assert shell.workspace.cluster == "plural"
Expand Down
1 change: 1 addition & 0 deletions apps/core/test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ Mimic.copy(GoogleApi.IAM.V1.Api.Projects)
Mimic.copy(GoogleApi.CloudBilling.V1.Api.BillingAccounts)
Mimic.copy(GoogleApi.ServiceUsage.V1.Api.Services)
Mimic.copy(GoogleApi.ServiceUsage.V1.Api.Operations)
Mimic.copy(OAuth2.Client)

{:ok, _} = Application.ensure_all_started(:ex_machina)
2 changes: 1 addition & 1 deletion apps/graphql/test/mutations/shell_mutations_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule GraphQl.ShellMutationsTest do
expect(Pods, :fetch, fn _ -> {:ok, Pods.pod("plrl-shell-1", e)} end)
expect(Core.Shell.Scm, :setup_repository, fn
:github, ^e, "tok", nil, "demo" ->
{:ok, "git@github.com:pluralsh/demo.git", "pub-key", "priv-key"}
{:ok, "git@github.com:pluralsh/demo.git", "pub-key", "priv-key", nil}
end)

attrs = %{
Expand Down

0 comments on commit 3890dd0

Please sign in to comment.