Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add protection support for destroy builds #219

Merged
merged 1 commit into from
Jan 27, 2023
Merged
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
5 changes: 4 additions & 1 deletion lib/console/plural/context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Console.Plural.Context do
import Console
alias Console.Deployer

defstruct [:configuration, :bundles, :smtp, :buckets, :domains]
defstruct [:configuration, :bundles, :smtp, :buckets, :domains, :protect]

defmodule Smtp do
defstruct [:user, :password, :server, :port, :sender]
Expand Down Expand Up @@ -34,6 +34,9 @@ defmodule Console.Plural.Context do
}
end

def protected?(%__MODULE__{protect: [_ | _] = protect}, repo), do: Enum.member?(protect, repo)
def protected?(_, _), do: false

def get() do
with {:ok, content} <- Deployer.file(location()),
{:ok, %{"spec" => spec}} <- YamlElixir.read_from_string(content) do
Expand Down
23 changes: 22 additions & 1 deletion lib/console/services/builds.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ defmodule Console.Services.Builds do
alias Kube.{Client, Application}
alias Console.Schema.{Build, Command, User, Lock}
alias Console.Services.{Changelogs, Rbac}
alias Console.Plural.Incidents
alias Console.Plural.{Incidents, Context}

@type error :: {:error, term}
@type build_resp :: {:ok, Build.t} | error

def get!(id), do: Repo.get!(Build, id)

Expand Down Expand Up @@ -68,6 +71,14 @@ defmodule Console.Services.Builds do
end
end

@doc """
Creates a new build and goes from there

fails if:
* there is no current application for that repo
* if its a destroy build, the repo is protected
"""
@spec create(map, User.t) :: build_resp
def create(attrs, %User{id: id} = user) do
start_transaction()
|> add_operation(:build, fn _ ->
Expand All @@ -83,6 +94,16 @@ defmodule Console.Services.Builds do
_ -> {:error, :invalid_repository}
end
end)
|> add_operation(:protect, fn
%{build: %Build{type: :destroy, repository: repo} = b} ->
with {:ok, ctx} <- Context.get(),
true <- Context.protected?(ctx, repo) do
{:error, "repo #{repo} is protected from being destroyed, update context.yaml if you really want to destroy it"}
else
_ -> {:ok, b}
end
%{build: b} -> {:ok, b}
end)
|> execute(extract: :build)
|> notify(:create, user)
end
Expand Down
8 changes: 8 additions & 0 deletions test/console/services/builds_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule Console.Services.BuildsTest do
use Console.DataCase, async: true
use Mimic
alias Console.Services.Builds
alias Console.Plural.Context
alias Console.{PubSub, Storage.Git}

describe "Command implements Collectable" do
Expand Down Expand Up @@ -56,6 +57,13 @@ defmodule Console.Services.BuildsTest do

assert_receive {:event, %PubSub.BuildCreated{item: ^build}}
end

test "destroy builds fail when protected" do
user = insert(:user)
expect(Kazan, :run, fn _ -> {:ok, %Kube.Application{}} end)
expect(Context, :get, fn -> {:ok, %Context{protect: ["repo"]}} end)
{:error, _} = Builds.create(%{type: :destroy, repository: "repo"}, user)
end
end

describe "#restart/2" do
Expand Down