Skip to content

Commit

Permalink
Need to b64 decode cockroach config secret
Browse files Browse the repository at this point in the history
forgot to add this as well
  • Loading branch information
michaeljguarino committed Aug 10, 2024
1 parent 14c08fb commit 0b3804a
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 16 deletions.
7 changes: 4 additions & 3 deletions apps/core/lib/core/clients/console.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ defmodule Core.Clients.Console do
def clusters(client) do
Req.post(client, graphql: @clusters_q)
|> case do
{:ok, %Req.Response{body: %{"clusters" => %{"edges" => edges}}}} -> {:ok, Enum.map(edges, & &1["node"])}
{:ok, %Req.Response{body: %{"data" => %{"clusters" => %{"edges" => edges}}}}} ->
{:ok, Enum.map(edges, & &1["node"])}
res ->
Logger.warn "Failed to fetch clusters: #{inspect(res)}"
{:error, "could not fetch clusters"}
Expand All @@ -59,7 +60,7 @@ defmodule Core.Clients.Console do
def repo(client, url) do
Req.post(client, graphql: {@repo_q, %{url: url}})
|> case do
{:ok, %Req.Response{body: %{"gitRepository" => %{"id" => id}}}} -> {:ok, id}
{:ok, %Req.Response{body: %{"data" => %{"gitRepository" => %{"id" => id}}}}} -> {:ok, id}
res ->
Logger.warn "Failed to fetch clusters: #{inspect(res)}"
{:error, "could not fetch repo"}
Expand All @@ -81,7 +82,7 @@ defmodule Core.Clients.Console do
|> service_resp("deleteServiceDeployment")
end

defp service_resp({:ok, %Req.Response{status: 200, body: body}}, field) do
defp service_resp({:ok, %Req.Response{status: 200, body: %{"data" => body}}}, field) do
case body[field] do
%{"id" => id} -> {:ok, id}
err ->
Expand Down
2 changes: 1 addition & 1 deletion apps/core/lib/core/schema/cockroach_cluster.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ defmodule Core.Schema.CockroachCluster do
model
|> cast(attrs, ~w(name cloud region url certificate endpoints)a)
|> unique_constraint(:name)
|> validate_required(~w(name cloud region url certificate endpoints)a)
|> validate_required(~w(name cloud url certificate endpoints)a)
end
end
3 changes: 2 additions & 1 deletion apps/core/lib/core/schema/console_instance.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ defmodule Core.Schema.ConsoleInstance do
field :dbpassword, EncryptedString
field :subdomain, :string
field :jwt_secret, EncryptedString
field :erlang_secret, EncryptedString
field :owner_name, :string
field :owner_email, :string
field :admin_password, EncryptedString
Expand Down Expand Up @@ -117,7 +118,7 @@ defmodule Core.Schema.ConsoleInstance do
end

@conf_valid ~w(
database dbuser dbpassword
database dbuser dbpassword erlang_secret
subdomain jwt_secret owner_name owner_email admin_password aes_key
encryption_key client_id client_secret plural_token
kas_api kas_private kas_redis
Expand Down
1 change: 1 addition & 0 deletions apps/core/lib/core/services/cloud.ex
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ defmodule Core.Services.Cloud do
owner_name: user.name,
owner_email: user.email,
admin_password: Core.random_alphanum(30),
erlang_secret: Core.random_alphanum(30),
client_id: oidc.client_id,
client_secret: oidc.client_secret,
plural_token: token,
Expand Down
5 changes: 4 additions & 1 deletion apps/core/lib/core/services/cloud/configuration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ defmodule Core.Services.Cloud.Configuration do
kas_api
kas_private
kas_redis
erlang_secret
)a)
|> Map.merge(%{
postgres_url: build_pg_url(inst),
cloud: "#{inst.cloud}",
cluster_name: inst.name,
size: "#{size}",
postgres_certificate: certificate(inst)
})
|> Map.put(:size, "#{size}")
|> Enum.map(fn {k, v} -> %{name: Macro.camelize("#{k}"), value: v} end)
|> Enum.map(fn {k, v} -> %{name: k, value: v} end)
end

defp certificate(%ConsoleInstance{cockroach: %CockroachCluster{certificate: cert}}), do: cert
Expand Down
12 changes: 9 additions & 3 deletions apps/core/lib/core/services/cloud/poller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ defmodule Core.Services.Cloud.Poller do

def repository(), do: GenServer.call(__MODULE__, :repo)

def handle_call(:repo, %{repo: id} = state) when is_binary(id),
def handle_call(:repo, _, %{repo: id} = state) when is_binary(id),
do: {:reply, {:ok, id}, state}
def handle_call(:repo, state), do: {:reply, {:error, "repo not pulled"}, state}
def handle_call(:repo, _, state), do: {:reply, {:error, "repo not pulled"}, state}

def handle_info(:repo, %{client: client} = state) do
case Console.repo(client, Core.conf(:mgmt_repo)) do
Expand Down Expand Up @@ -61,6 +61,7 @@ defmodule Core.Services.Cloud.Poller do
cloud: to_cloud(distro),
region: meta["region"]
}, name)
|> log_err("failed to insert cloud cluster")
end

defp upsert_roach(%{"name" => name} = roach) do
Expand All @@ -70,14 +71,16 @@ defmodule Core.Services.Cloud.Poller do
certificate: roach["certificate"],
endpoints: roach["endpoints"]
}, name)
|> log_err("failed to insert cockroach cluster")
end

defp read_secret() do
CoreV1.read_namespaced_secret!("plural", "plrl-cloud-config")
|> Kazan.run()
|> case do
{:ok, %CoreV1.Secret{data: %{"cockroaches" => roaches}}} ->
Jason.decode(roaches)
Base.decode64!(roaches)
|> Jason.decode()
_ -> {:error, "could not find secret"}
end
end
Expand All @@ -86,4 +89,7 @@ defmodule Core.Services.Cloud.Poller do
defp to_cloud("GKE"), do: :gcp
defp to_cloud("AKS"), do: :azure
defp to_cloud(_), do: :aws

defp log_err({:error, _} = err, msg), do: "#{msg}: #{inspect(err)}"
defp log_err(pass, _), do: pass
end
10 changes: 8 additions & 2 deletions apps/core/lib/core/services/cloud/workflow.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
defmodule Core.Services.Cloud.Workflow do
use Core.Services.Base
alias Core.Clients.Console
alias Core.Services.Cloud
alias Core.Services.{Cloud, Users}
alias Core.Services.Cloud.{Poller, Configuration}
alias Core.Schema.{ConsoleInstance, CockroachCluster}
alias Core.Schema.{ConsoleInstance, CockroachCluster, User}
alias Core.Repo

require Logger
Expand Down Expand Up @@ -122,6 +122,12 @@ defmodule Core.Services.Cloud.Workflow do
|> add_operation(:inst, fn _ -> Repo.delete(inst) end)
|> add_operation(:cluster, fn _ -> Cloud.dec(cluster) end)
|> add_operation(:roach, fn _ -> Cloud.dec(roach) end)
|> add_operation(:sa, fn %{inst: %{name: name}} ->
case Users.get_user_by_email("#{name}[email protected]") do
%User{} = u -> Repo.delete(u)
_ -> {:ok, nil}
end
end)
|> execute(extract: :inst)
end

Expand Down
8 changes: 6 additions & 2 deletions apps/core/test/services/cloud/workflow_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,19 @@ defmodule Core.Services.Cloud.WorkflowTest do
}, user)

expect(Core.Services.Cloud.Poller, :repository, fn -> {:ok, "some-id"} end)
expect(Core.Clients.Console, :create_service, fn _, ^cluster_id, _ -> {:ok, Ecto.UUID.generate()} end)
expect(Req, :post, fn _, [graphql: {_, %{clusterId: ^cluster_id}}] ->
{:ok, %Req.Response{status: 200, body: %{"data" => %{"createServiceDeployment" => %{"id" => Ecto.UUID.generate()}}}}}
end)

{:ok, %{external_id: svc_id} = instance} = Workflow.provision(instance)

assert instance.status == :provisioned
assert instance.instance_status.db
assert instance.instance_status.svc

expect(Core.Clients.Console, :delete_service, fn _, ^svc_id -> {:ok, svc_id} end)
expect(Req, :post, fn _, [graphql: {_, %{id: ^svc_id}}] ->
{:ok, %Req.Response{status: 200, body: %{"data" => %{"deleteServiceDeployment" => %{"id" => svc_id}}}}}
end)

{:ok, instance} = Workflow.deprovision(instance)

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 @@ -35,5 +35,6 @@ Mimic.copy(System)
Mimic.copy(Core.Clients.Vault)
Mimic.copy(Core.Clients.Console)
Mimic.copy(Core.Services.Cloud.Poller)
Mimic.copy(Req)

{:ok, _} = Application.ensure_all_started(:ex_machina)
2 changes: 1 addition & 1 deletion plural/helm/plural/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: v2
name: plural
description: A helm chart for installing plural
appVersion: 0.11.8
version: 0.10.93
version: 0.10.94
dependencies:
- name: hydra
version: 0.26.5
Expand Down
2 changes: 0 additions & 2 deletions plural/helm/plural/templates/chartmuseum.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ spec:
env:
- name: PORT
value: '8080'
- name: DEPLOYED_AT
value: {{ now | unixEpoch | quote }}
- name: DEPTH
value: '1'
- name: CONTEXT_PATH
Expand Down

0 comments on commit 0b3804a

Please sign in to comment.