Merge pull request #78 from poanetwork/ferigis.77.read_credentials_from_overlay

[#77] reading the credentials from the overlay
This commit is contained in:
Joseph Yiasemides 2018-09-04 08:01:57 +02:00 committed by GitHub
commit e405b86259
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 158 additions and 38 deletions

View File

@ -26,8 +26,8 @@ config :poa_agent,
identifier: "elixirNodeJSIntegration", identifier: "elixirNodeJSIntegration",
# Authentication parameters # Authentication parameters
user: "rUN7afCO", user: "AhvK0DSj",
password: "_3IC09xfMtAW4Hr", password: "EkiuUkyOD6KLas8",
token_url: "https://localhost:4003/session" token_url: "https://localhost:4003/session"
] ]
} }
@ -41,5 +41,5 @@ config :poa_agent,
{:eth_stats, [:rest_transfer]}, {:eth_stats, [:rest_transfer]},
{:eth_pending, [:rest_transfer]}, {:eth_pending, [:rest_transfer]},
{:eth_information, [:rest_transfer]}, {:eth_information, [:rest_transfer]},
{:system_collector, []} {:system_collector, [:rest_transfer]}
] ]

View File

@ -24,22 +24,22 @@ defmodule POAAgent.Configuration do
defp config(overlay, default) do defp config(overlay, default) do
overlay overlay
|> Kernel.get_in(["POAAgent", "transfers"]) |> Kernel.get_in(["POAAgent", "transfers"])
|> Kernel.hd()
|> merge_overlay_into_config(default) |> merge_overlay_into_config(default)
end end
defp merge_overlay_into_config(overlay, [{id, module_name, default}]) do defp merge_overlay_into_config(overlay, default) do
keys = [ Enum.flat_map(default,
"address", fn {id, module_name, args} ->
"identifier", case POAAgent.Configuration.search(overlay, Atom.to_string(id)) do
"secret" [] ->
] []
^id = String.to_existing_atom(Map.fetch!(overlay, "id")) overlay_args ->
restricted = Map.take(overlay, keys) overlay_args = POAAgent.Configuration.to_keyword(overlay_args)
want = POAAgent.Configuration.to_keyword(restricted) final = Keyword.merge(args, overlay_args)
final = Keyword.merge(default, want)
[{id, module_name, final}] [{id, module_name, final}]
end end
end)
end
end end
defmodule Collectors do defmodule Collectors do
@ -65,28 +65,35 @@ defmodule POAAgent.Configuration do
defp config(overlay, default) do defp config(overlay, default) do
overlay overlay
|> Kernel.get_in(["POAAgent", "collectors"]) |> Kernel.get_in(["POAAgent", "collectors"])
|> Kernel.hd()
|> merge_overlay_into_config(default) |> merge_overlay_into_config(default)
end end
defp merge_overlay_into_config(overlay, [{id, module_name, freq, tag, default} | rest]) do defp merge_overlay_into_config(overlay, default) do
keys = [ Enum.flat_map(default,
"url", fn {id, module_name, freq, tag, args} ->
"name", case POAAgent.Configuration.search(overlay, Atom.to_string(id)) do
"contact" [] ->
] []
^id = String.to_existing_atom(Map.fetch!(overlay, "id")) overlay_args ->
restricted = Map.take(overlay, keys) overlay_args = POAAgent.Configuration.to_keyword(overlay_args)
want = POAAgent.Configuration.to_keyword(restricted) final = Keyword.merge(args, overlay_args)
final = Keyword.merge(default, want) [{id, module_name, freq, tag, final}]
[{id, module_name, freq, tag, final}] ++ rest end
end)
end end
end end
def to_keyword(nil), do: []
def to_keyword(x) do def to_keyword(x) do
f = fn {k, v} -> f = fn {k, v} ->
{String.to_existing_atom(k), v} {String.to_atom(k), v}
end end
Enum.into(x, [], f) Enum.into(x, [], f)
end end
def search(list, key) do
Enum.find(list, fn(map) ->
Map.get(map, "id") == key
end)
end
end end

View File

@ -1,6 +1,10 @@
{ {
"POAAgent":{ "POAAgent":{
"collectors":[ "collectors":[
{
"id": "other_collector",
"myfield": "myvalue"
},
{ {
"id": "eth_information", "id": "eth_information",
"url": "http://localhost:8546", "url": "http://localhost:8546",
@ -9,11 +13,17 @@
} }
], ],
"transfers":[ "transfers":[
{
"id": "transfer1",
"somefield": "somevalue",
"somefield2": "somevalue2"
},
{ {
"id": "rest_transfer", "id": "rest_transfer",
"address": "http://localhost:4003", "address": "http://localhost:4003",
"identifier": "NewIdentifier", "identifier": "NewIdentifier",
"secret": "newsecret" "user": "user1",
"password": "password1"
} }
] ]
} }

View File

@ -8,7 +8,9 @@ defmodule POAAgent.ConfigurationTest do
{:rest_transfer, POAAgent.Plugins.Transfers.HTTP.REST, [ {:rest_transfer, POAAgent.Plugins.Transfers.HTTP.REST, [
address: "http://localhost:4002", address: "http://localhost:4002",
identifier: "elixirNodeJSIntegration", identifier: "elixirNodeJSIntegration",
secret: "mysecret" user: "AhvK0DSj",
password: "EkiuUkyOD6KLas8",
token_url: "https://localhost:4003/session"
] ]
} }
] ]
@ -20,13 +22,59 @@ defmodule POAAgent.ConfigurationTest do
# 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_address = "http://localhost:4003" new_address = "http://localhost:4003"
new_identifier = "NewIdentifier" 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() [{:rest_transfer, POAAgent.Plugins.Transfers.HTTP.REST, new_args}] = POAAgent.Configuration.Transfers.config()
assert new_address == new_args[:address] assert new_address == new_args[:address]
assert new_identifier == new_args[:identifier] 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 # putting back the original value
:ok = Application.put_env(:poa_agent, :transfers, original_transfers) :ok = Application.put_env(:poa_agent, :transfers, original_transfers)
@ -50,7 +98,7 @@ defmodule POAAgent.ConfigurationTest do
assert collectors == Application.fetch_env!(:poa_agent, :collectors) 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_url = "http://localhost:8546"
new_name = "NewNodeName" new_name = "NewNodeName"
new_contact = "mynewemail@gmail.com" new_contact = "mynewemail@gmail.com"
@ -89,7 +137,7 @@ defmodule POAAgent.ConfigurationTest do
assert collectors == Application.fetch_env!(:poa_agent, :collectors) 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_url = "http://localhost:8546"
new_name = "NewNodeName" new_name = "NewNodeName"
new_contact = "mynewemail@gmail.com" new_contact = "mynewemail@gmail.com"
@ -105,4 +153,59 @@ defmodule POAAgent.ConfigurationTest do
assert original_collectors == Application.fetch_env!(:poa_agent, :collectors) assert original_collectors == Application.fetch_env!(:poa_agent, :collectors)
end end
test "test coverage for collectors" do
original_collectors = Application.fetch_env!(:poa_agent, :collectors)
original_config_overlay = Application.fetch_env!(:poa_agent, :config_overlay)
collectors = [
{:eth_information, POAAgent.Plugins.Collectors.Eth.Information, 60_000, :eth_information, [
url: "http://localhost:8545",
name: "Elixir-NodeJS-Integration",
contact: "myemail@gmail.com"
]
},
{:other_collector, POAAgent.Plugins.Collectors.Eth.Information, 60_000, :eth_information, [
url: "http://localhost:8545",
name: "Elixir-NodeJS-Integration",
contact: "myemail@gmail.com"
]
}
]
:ok = Application.put_env(:poa_agent, :collectors, collectors)
assert collectors == Application.fetch_env!(:poa_agent, :collectors)
:ok = Application.delete_env(:poa_agent, :config_overlay)
assert collectors == POAAgent.Configuration.Collectors.config()
# putting back the original value
:ok = Application.put_env(:poa_agent, :collectors, original_collectors)
:ok = Application.put_env(:poa_agent, :config_overlay, original_config_overlay)
assert original_collectors == Application.fetch_env!(:poa_agent, :collectors)
end
test "test coverage for transfers" do
original_transfers = Application.fetch_env!(:poa_agent, :transfers)
original_config_overlay = Application.fetch_env!(:poa_agent, :config_overlay)
transfers = []
:ok = Application.put_env(:poa_agent, :transfers, transfers)
assert transfers == Application.fetch_env!(:poa_agent, :transfers)
:ok = Application.delete_env(:poa_agent, :config_overlay)
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)
:ok = Application.put_env(:poa_agent, :config_overlay, original_config_overlay)
end
end end