-
Notifications
You must be signed in to change notification settings - Fork 15
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
Conversation
There was a problem hiding this 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) |
There was a problem hiding this comment.
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?
|
||
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 |
There was a problem hiding this comment.
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 👀
There was a problem hiding this 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!
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 |
There was a problem hiding this comment.
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
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hackney: [cookie: [auth]] ++ maybe_provide_ssl_options(use_ca_cert) | |
hackney: [cookie: [auth]] ++ ssl_options(use_ca_cert) |
defp maybe_provide_ssl_options(true), | ||
do: [ssl: [verify: :verify_peer, certfile: SumaApi.ca_cert_path()]] | ||
|
||
defp maybe_provide_ssl_options(_), do: [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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: [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, great!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Super! 🚀
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.