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 ability to provide a CA cert when making requests to SUMA #2391

Merged
merged 5 commits into from
Mar 7, 2024

Conversation

jamie-suse
Copy link
Contributor

Description

This change adds ability to provide a CA cert when making requests to SUMA.

Because the HTTPoison library uses Erlang library hackney underneath, whose API demands that we provide a path to a file containing the certificate; we need to save a certificate file on the filesystem somewhere. Note that if we save some new credentials that don't contain a certificate (i.e. the user clears the certificate), then the old certificate file is removed.

How was this tested?

Added unit tests for this new functionality.

@jamie-suse jamie-suse added enhancement New feature or request elixir Pull requests that update Elixir code labels Mar 6, 2024
@jamie-suse jamie-suse self-assigned this Mar 6, 2024
Copy link
Member

@nelsonkopliku nelsonkopliku left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job! Thanks!
Just a few comments.

response =
url
|> get_suma_api_url()
|> http_executor().get_system_id(auth, fully_qualified_domain_name)
|> http_executor().get_system_id(auth, fully_qualified_domain_name, ca_cert != nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

an alternative to this ca_cert != nil repetition could be expecting as an argument use_ca_cert as we do in the http executor, meaning we move the ca_cert != nil in the genserver. What do you think?

test/trento/infrastructure/software_updates/suma_test.exs Outdated Show resolved Hide resolved
test/trento/infrastructure/software_updates/suma_test.exs Outdated Show resolved Hide resolved
test/trento/infrastructure/software_updates/suma_test.exs Outdated Show resolved Hide resolved
test/trento/infrastructure/software_updates/suma_test.exs Outdated Show resolved Hide resolved

defp setup_auth(%State{} = state) do
with {:ok, %{url: url, username: username, password: password, ca_cert: ca_cert}} <-
SoftwareUpdates.get_settings(),
{:ok, auth_cookie} <- SumaApi.login(url, username, password) do
:ok <- maybe_write_ca_cert_file(ca_cert),
{:ok, auth_cookie} <- SumaApi.login(url, username, password, ca_cert) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we decide to have SumaApi functions accept a use_ca_cert as mentioned here we could also think of centralizing that ca_cert =! nil in the genserver state that we set in this function 👀

Copy link
Contributor

@dottorblaster dottorblaster left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides what Nelson commented, a couple namings that I would like to be a little bit more concise.

We can merge when we are ok with the changes 👍 good job!

Comment on lines 123 to 140
defp maybe_write_ca_cert_file(nil), do: File.rm(SumaApi.ca_cert_path())

defp maybe_write_ca_cert_file(ca_cert) do
SumaApi.ca_cert_path()
|> Path.dirname()
|> File.mkdir_p!()

File.write(SumaApi.ca_cert_path(), ca_cert)
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me File.rm and File.write are two different forms of writing, so this can just be names write, without the maybe

Suggested change
defp maybe_write_ca_cert_file(nil), do: File.rm(SumaApi.ca_cert_path())
defp maybe_write_ca_cert_file(ca_cert) do
SumaApi.ca_cert_path()
|> Path.dirname()
|> File.mkdir_p!()
File.write(SumaApi.ca_cert_path(), ca_cert)
end
defp write_ca_cert_file(nil), do: File.rm(SumaApi.ca_cert_path())
defp write_ca_cert_file(ca_cert) do
SumaApi.ca_cert_path()
|> Path.dirname()
|> File.mkdir_p!()
File.write(SumaApi.ca_cert_path(), ca_cert)
end

HTTPoison.get(
"#{base_url}/system/getRelevantErrata?sid=#{system_id}",
[{"Content-type", "application/json"}],
hackney: [cookie: [auth]],
ssl: [verify: :verify_none]
hackney: [cookie: [auth]] ++ maybe_provide_ssl_options(use_ca_cert)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
hackney: [cookie: [auth]] ++ maybe_provide_ssl_options(use_ca_cert)
hackney: [cookie: [auth]] ++ ssl_options(use_ca_cert)

Comment on lines 68 to 71
defp maybe_provide_ssl_options(true),
do: [ssl: [verify: :verify_peer, certfile: SumaApi.ca_cert_path()]]

defp maybe_provide_ssl_options(_), do: []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
defp maybe_provide_ssl_options(true),
do: [ssl: [verify: :verify_peer, certfile: SumaApi.ca_cert_path()]]
defp maybe_provide_ssl_options(_), do: []
defp ssl_options(true),
do: [ssl: [verify: :verify_peer, certfile: SumaApi.ca_cert_path()]]
defp ssl_options(_), do: []

Copy link
Contributor

@dottorblaster dottorblaster left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, great!

Copy link
Member

@nelsonkopliku nelsonkopliku left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super! 🚀

@jamie-suse jamie-suse merged commit d32d908 into main Mar 7, 2024
24 checks passed
@jamie-suse jamie-suse deleted the sum-ssl branch March 7, 2024 12:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
elixir Pull requests that update Elixir code enhancement New feature or request
Development

Successfully merging this pull request may close these issues.

3 participants