Skip to content

Commit

Permalink
[#77] reding the credentials from the overlay
Browse files Browse the repository at this point in the history
  • Loading branch information
ferigis committed Aug 31, 2018
1 parent 2206dc7 commit 4665fc1
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 38 deletions.
6 changes: 3 additions & 3 deletions config/prod.exs
Expand Up @@ -26,8 +26,8 @@ config :poa_agent,
identifier: "elixirNodeJSIntegration",

# Authentication parameters
user: "rUN7afCO",
password: "_3IC09xfMtAW4Hr",
user: "AhvK0DSj",
password: "EkiuUkyOD6KLas8",
token_url: "https://localhost:4003/session"
]
}
Expand All @@ -41,5 +41,5 @@ config :poa_agent,
{:eth_stats, [:rest_transfer]},
{:eth_pending, [:rest_transfer]},
{:eth_information, [:rest_transfer]},
{:system_collector, []}
{:system_collector, [:rest_transfer]}
]
57 changes: 32 additions & 25 deletions lib/poa_agent/configuration.ex
Expand Up @@ -24,21 +24,21 @@ defmodule POAAgent.Configuration do
defp config(overlay, default) do
overlay
|> Kernel.get_in(["POAAgent", "transfers"])
|> Kernel.hd()
|> merge_overlay_into_config(default)
end

defp merge_overlay_into_config(overlay, [{id, module_name, default}]) do
keys = [
"address",
"identifier",
"secret"
]
^id = String.to_existing_atom(Map.fetch!(overlay, "id"))
restricted = Map.take(overlay, keys)
want = POAAgent.Configuration.to_keyword(restricted)
final = Keyword.merge(default, want)
[{id, module_name, final}]
defp merge_overlay_into_config(overlay, default) do
Enum.flat_map(default,
fn {id, module_name, args} ->
case POAAgent.Configuration.search(overlay, Atom.to_string(id)) do
[] ->
[]
overlay_args ->
overlay_args = POAAgent.Configuration.to_keyword(overlay_args)
final = Keyword.merge(args, overlay_args)
[{id, module_name, final}]
end
end)
end
end

Expand All @@ -65,28 +65,35 @@ defmodule POAAgent.Configuration do
defp config(overlay, default) do
overlay
|> Kernel.get_in(["POAAgent", "collectors"])
|> Kernel.hd()
|> merge_overlay_into_config(default)
end

defp merge_overlay_into_config(overlay, [{id, module_name, freq, tag, default} | rest]) do
keys = [
"url",
"name",
"contact"
]
^id = String.to_existing_atom(Map.fetch!(overlay, "id"))
restricted = Map.take(overlay, keys)
want = POAAgent.Configuration.to_keyword(restricted)
final = Keyword.merge(default, want)
[{id, module_name, freq, tag, final}] ++ rest
defp merge_overlay_into_config(overlay, default) do
Enum.flat_map(default,
fn {id, module_name, freq, tag, args} ->
case POAAgent.Configuration.search(overlay, Atom.to_string(id)) do
[] ->
[]
overlay_args ->
overlay_args = POAAgent.Configuration.to_keyword(overlay_args)
final = Keyword.merge(args, overlay_args)
[{id, module_name, freq, tag, final}]
end
end)
end
end

def to_keyword(nil), do: []
def to_keyword(x) do
f = fn {k, v} ->
{String.to_existing_atom(k), v}
{String.to_atom(k), v}
end
Enum.into(x, [], f)
end

def search(list, key) do
Enum.find(list, fn(map) ->
Map.get(map, "id") == key
end)
end
end
12 changes: 11 additions & 1 deletion test/poa_agent/config_overlay.json
@@ -1,6 +1,10 @@
{
"POAAgent":{
"collectors":[
{
"id": "other_collector",
"myfield": "myvalue"
},
{
"id": "eth_information",
"url": "http://localhost:8546",
Expand All @@ -9,11 +13,17 @@
}
],
"transfers":[
{
"id": "transfer1",
"somefield": "somevalue",
"somefield2": "somevalue2"
},
{
"id": "rest_transfer",
"address": "http://localhost:4003",
"identifier": "NewIdentifier",
"secret": "newsecret"
"user": "user1",
"password": "password1"
}
]
}
Expand Down
66 changes: 57 additions & 9 deletions test/poa_agent/configuration_test.exs
Expand Up @@ -6,11 +6,13 @@ defmodule POAAgent.ConfigurationTest do

transfers = [
{:rest_transfer, POAAgent.Plugins.Transfers.HTTP.REST, [
address: "http://localhost:4002",
identifier: "elixirNodeJSIntegration",
secret: "mysecret"
]
}
address: "http://localhost:4002",
identifier: "elixirNodeJSIntegration",
user: "AhvK0DSj",
password: "EkiuUkyOD6KLas8",
token_url: "https://localhost:4003/session"
]
}
]

:ok = Application.put_env(:poa_agent, :transfers, transfers)
Expand All @@ -20,13 +22,59 @@ defmodule POAAgent.ConfigurationTest do
# those values are in the test/poa_agent/config_overlay.json file
new_address = "http://localhost:4003"
new_identifier = "NewIdentifier"
new_secret = "newsecret"
new_user = "user1"
new_password = "password1"

[{:rest_transfer, POAAgent.Plugins.Transfers.HTTP.REST, new_args}] = POAAgent.Configuration.Transfers.config()

assert new_address == new_args[:address]
assert new_identifier == new_args[:identifier]
assert new_secret == new_args[:secret]
assert new_user == new_args[:user]
assert new_password == new_args[:password]
assert "https://localhost:4003/session" == new_args[:token_url]

# putting back the original value
:ok = Application.put_env(:poa_agent, :transfers, original_transfers)

assert original_transfers == Application.fetch_env!(:poa_agent, :transfers)
end

test "Transfers overlay doesn't exist in default" do
original_transfers = Application.fetch_env!(:poa_agent, :transfers)

transfers = [
{:rest_transfer2, POAAgent.Plugins.Transfers.HTTP.REST, [
address: "http://localhost:4002",
identifier: "elixirNodeJSIntegration",
user: "AhvK0DSj",
password: "EkiuUkyOD6KLas8",
token_url: "https://localhost:4003/session"
]
}
]

:ok = Application.put_env(:poa_agent, :transfers, transfers)

assert transfers == Application.fetch_env!(:poa_agent, :transfers)

assert transfers == POAAgent.Configuration.Transfers.config()

# putting back the original value
:ok = Application.put_env(:poa_agent, :transfers, original_transfers)

assert original_transfers == Application.fetch_env!(:poa_agent, :transfers)
end

test "Transfers overlay, default is an empty list" do
original_transfers = Application.fetch_env!(:poa_agent, :transfers)

transfers = []

:ok = Application.put_env(:poa_agent, :transfers, transfers)

assert transfers == Application.fetch_env!(:poa_agent, :transfers)

assert transfers == POAAgent.Configuration.Transfers.config()

# putting back the original value
:ok = Application.put_env(:poa_agent, :transfers, original_transfers)
Expand All @@ -50,7 +98,7 @@ defmodule POAAgent.ConfigurationTest do

assert collectors == Application.fetch_env!(:poa_agent, :collectors)

# # those values are in the test/poa_agent/config_overlay.json file
# those values are in the test/poa_agent/config_overlay.json file
new_url = "http://localhost:8546"
new_name = "NewNodeName"
new_contact = "mynewemail@gmail.com"
Expand Down Expand Up @@ -89,7 +137,7 @@ defmodule POAAgent.ConfigurationTest do

assert collectors == Application.fetch_env!(:poa_agent, :collectors)

# # those values are in the test/poa_agent/config_overlay.json file
# those values are in the test/poa_agent/config_overlay.json file
new_url = "http://localhost:8546"
new_name = "NewNodeName"
new_contact = "mynewemail@gmail.com"
Expand Down

0 comments on commit 4665fc1

Please sign in to comment.