dwyl/phoenix-ecto-encryption-example

** (Protocol.UndefinedError) protocol Enumerable not implemented for nil

Opened this issue · 2 comments

Hello, following Phoenix Ecto Encryption Example step-by-step and errors on mix test test/lib/aes_test.exs run at end of step 3.3

  • Elixir 1.7.3 (compiled with Erlang/OTP 21) fresh install

image

I am new to Elixir thought this exercise would give me some insight, so no help / cannot debug, don't know if it is aes.ex or .exs issue

@corepay thank you for opening this issue to draw our attention.
will help to debug/investigate it as soon as I'm back at my desk.
(currently AFK and don't have Internet/Laptop to download/install latest Elixir ...)

@corepay the error is due to the environment variables (defined in the .env file) no properly loaded into the Phoenix application.
If you add the following code at the end of the config/config.exs the tests should ok then:

# run shell command to "source .env" to load the environment variables.
try do                                     # wrap in "try do"
  File.stream!("./.env")                   # in case .env file does not exist.
    |> Stream.map(&String.trim_trailing/1) # remove excess whitespace
    |> Enum.each(fn line -> line           # loop through each line
      |> String.replace("export ", "")     # remove "export" from line
      |> String.split("=", parts: 2)       # split on *first* "=" (equals sign)
      |> Enum.reduce(fn(value, key) ->     # stackoverflow.com/q/33055834/1148249
        System.put_env(key, value)         # set each environment variable
      end)
    end)
rescue
  _ -> IO.puts "no .env file found!"
end

# Set the Encryption Keys as an "Application Variable" accessible in aes.ex
config :encryption, Encryption.AES,
  keys: System.get_env("ENCRYPTION_KEYS") # get the ENCRYPTION_KEYS env variable
    |> String.replace("'", "")  # remove single-quotes around key list in .env
    |> String.split(",")        # split the CSV list of keys
    |> Enum.map(fn key -> :base64.decode(key) end) # decode the key.

You can see here for the config file also:

try do # wrap in "try do"
File.stream!("./.env") # in case .env file does not exist.
|> Stream.map(&String.trim_trailing/1) # remove excess whitespace
|> Enum.each(fn line -> line # loop through each line
|> String.replace("export ", "") # remove "export" from line
|> String.split("=", parts: 2) # split on *first* "=" (equals sign)
|> Enum.reduce(fn(value, key) -> # stackoverflow.com/q/33055834/1148249
System.put_env(key, value) # set each environment variable
end)
end)
rescue
_ -> IO.puts "no .env file found!"
end
# Set the Encryption Keys as an "Application Variable" accessible in aes.ex
config :encryption, Encryption.AES,
keys: System.get_env("ENCRYPTION_KEYS") # get the ENCRYPTION_KEYS env variable
|> String.replace("'", "") # remove single-quotes around key list in .env
|> String.split(",") # split the CSV list of keys
|> Enum.map(fn key -> :base64.decode(key) end) # decode the key.