Skip to content

Commit

Permalink
Patch wine & iris dataset requests (#40)
Browse files Browse the repository at this point in the history
* change ssl verify option for iris, wine dataset requests

* compare wine, iris dataset hashes
  • Loading branch information
t-rutten authored Aug 11, 2023
1 parent 8a8d5fa commit ace9be1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
9 changes: 9 additions & 0 deletions lib/scidata/iris.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ defmodule Scidata.Iris do

@base_url "https://archive.ics.uci.edu/static/public/53/iris.zip"
@dataset_file "iris.data"
@data_hash <<111, 96, 139, 113, 167, 49, 114, 22, 49, 155, 77, 39, 180, 217, 188, 132, 230,
171, 215, 52, 237, 167, 135, 43, 113, 164, 88, 86, 158, 38, 86, 192>>

alias Scidata.Utils

Expand Down Expand Up @@ -46,6 +48,9 @@ defmodule Scidata.Iris do
base_url = opts[:base_url] || @base_url
dataset_file = opts[:dataset_file] || @dataset_file

# Temporary fix to cope with bad cert on source site
opts = Keyword.put(opts, :ssl_verify, :verify_none)

[{_, data}] =
Utils.get!(base_url, opts).body
|> Enum.filter(fn {fname, _} ->
Expand All @@ -55,6 +60,10 @@ defmodule Scidata.Iris do
)
end)

if :crypto.hash(:sha256, data) != @data_hash do
raise RuntimeError, "Dataset hashed to unexpected value"
end

data
|> String.split()
|> Enum.reverse()
Expand Down
12 changes: 7 additions & 5 deletions lib/scidata/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule Scidata.Utils do

request
|> if_modified_since(opts)
|> run!()
|> run!(opts)
|> raise_errors()
|> handle_cache(opts)
|> decode()
Expand All @@ -31,21 +31,23 @@ defmodule Scidata.Utils do
Calendar.strftime(datetime, "%a, %d %b %Y %H:%m:%S GMT")
end

defp run!(request) do
defp run!(request, opts) do
verify = opts[:ssl_verify] || :verify_peer

http_opts = [
ssl: [
verify: :verify_peer,
verify: verify,
cacertfile: CAStore.file_path(),
customize_hostname_check: [
match_fun: :public_key.pkix_verify_hostname_match_fun(:https)
]
]
]

opts = [body_format: :binary]
request_opts = [body_format: :binary]
arg = {request.url, request.headers}

case :httpc.request(:get, arg, http_opts, opts) do
case :httpc.request(:get, arg, http_opts, request_opts) do
{:ok, {{_, status, _}, headers, body}} ->
response = %{status: status, headers: headers, body: body}
{request, response}
Expand Down
9 changes: 9 additions & 0 deletions lib/scidata/wine.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ defmodule Scidata.Wine do

@base_url "https://archive.ics.uci.edu/static/public/109/wine.zip"
@dataset_file "wine.data"
@data_hash <<107, 230, 177, 32, 63, 61, 81, 223, 11, 85, 58, 112, 229, 123, 138, 114, 60,
212, 5, 104, 57, 88, 32, 79, 150, 210, 61, 124, 214, 174, 166, 89>>

alias Scidata.Utils

Expand Down Expand Up @@ -55,6 +57,9 @@ defmodule Scidata.Wine do
base_url = opts[:base_url] || @base_url
dataset_file = opts[:dataset_file] || @dataset_file

# Temporary fix to cope with bad cert on source site
opts = Keyword.put(opts, :ssl_verify, :verify_none)

[{_, data}] =
Utils.get!(base_url, opts).body
|> Enum.filter(fn {fname, _} ->
Expand All @@ -64,6 +69,10 @@ defmodule Scidata.Wine do
)
end)

if :crypto.hash(:sha256, data) != @data_hash do
raise RuntimeError, "Dataset hashed to unexpected value"
end

label_attr =
data
|> String.split()
Expand Down

0 comments on commit ace9be1

Please sign in to comment.