Skip to content

Commit

Permalink
Poc SUMA integration
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonkopliku committed Dec 4, 2023
1 parent 9883cad commit 12f4275
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 1 deletion.
5 changes: 5 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ config :trento,
api_key_authentication_enabled: true,
jwt_authentication_enabled: true

config :trento, :suma,
user: "",
password: "",
base_url: ""

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{config_env()}.exs"
5 changes: 5 additions & 0 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,9 @@ if config_env() in [:prod, :demo] do

config :trento,
api_key_authentication_enabled: System.get_env("ENABLE_API_KEY", "true") == "true"

config :trento, :suma,
user: System.get_env("SUMA_USER"),
password: System.get_env("SUMA_PASSWORD"),
public_url: System.get_env("SUMA_BASE_URL")
end
3 changes: 2 additions & 1 deletion lib/trento/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ defmodule Trento.Application do
Trento.ProjectorsSupervisor,
Trento.ProcessManagersSupervisor,
Trento.Infrastructure.Messaging.Adapter.AMQP.Publisher,
Trento.Infrastructure.Checks.AMQP.Consumer
Trento.Infrastructure.Checks.AMQP.Consumer,
{Trento.Infrastructure.SUMA, nil}
# Start a worker by calling: Trento.Worker.start_link(arg)
# {Trento.Worker, arg}
] ++
Expand Down
85 changes: 85 additions & 0 deletions lib/trento/infrastructure/software_updates/suma.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
defmodule Trento.Infrastructure.SUMA do
@moduledoc """
SUSE Manager integration service
"""

use Agent

require Logger

@suma_url Application.fetch_env!(:trento, :suma)[:base_url]
@suma_user Application.fetch_env!(:trento, :suma)[:user]
@suma_password Application.fetch_env!(:trento, :suma)[:password]

def start_link(initial_value) do
Agent.start_link(fn -> initial_value end, name: __MODULE__)
end

def login do
payload =
Jason.encode!(%{
"login" => @suma_user,
"password" => @suma_password
})

case HTTPoison.post(
"#{@suma_url}/auth/login",
payload,
[{"Content-type", "application/json"}],
ssl: [verify: :verify_none]
) do
{:ok, %HTTPoison.Response{headers: headers} = response} ->
Agent.update(__MODULE__, fn previouse_cookies ->
get_session_cookies(headers)
end)

Logger.debug("Successfully logged into suma #{inspect(response)}")
{:ok, :logged_in}

{:error, reason} ->
Logger.error("Failed to login to SUSE Manager. Error: #{inspect(reason)}")
{:error, :unable_to_login}
end
end

def list_affected_systems() do
case get_auth_cookies() do
nil ->
Logger.error("Unable to get auth cookies")
{:error, :unable_to_get_auth_cookies}

cookies ->
response =
HTTPoison.get(
"#{@suma_url}/errata/listAffectedSystems?advisoryName=SUSE-15-SP4-2023-4615",
[{"Content-type", "application/json"}],
hackney: [cookie: [cookies]],
ssl: [verify: :verify_none]
)

Logger.debug("Response from list_affected_systems #{inspect(response)}")

{:ok, :list_affected_systems}
end
end

def get_auth_cookies() do
Agent.get(__MODULE__, & &1)
end

defp get_session_cookies(login_response_headers) do
cookies =
Enum.filter(login_response_headers, fn {key, value} ->
String.match?(key, ~r/\Aset-cookie\z/i) &&
String.starts_with?(value, "pxt-session-cookie=")
end)
|> Enum.map(fn {_, value} ->
parts = String.split(value, ";")

Enum.find(parts, fn part ->
String.starts_with?(part, "pxt-session-cookie=")
end)
end)
|> List.last()
end
end

0 comments on commit 12f4275

Please sign in to comment.