-
-
Notifications
You must be signed in to change notification settings - Fork 561
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(elixir): basic api and access control for relays
allow custom attributes when registering local addresses, for now only used for attaching metadata to relays. Use that metadata to store the identifier that creatd the relay, to enforce access control over it (who can take it over / delete it)
- Loading branch information
Showing
20 changed files
with
795 additions
and
240 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 0 additions & 53 deletions
53
implementations/elixir/ockam/ockam_services/lib/services/api/static_forwarding_api.ex
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
implementations/elixir/ockam/ockam_services/lib/services/relay/static_forwarding.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
defmodule Ockam.Services.Relay.StaticForwarding do | ||
@moduledoc """ | ||
Static forwarding service | ||
Subscribes workers (by return route) to a string forwarding alias | ||
Forwarding alias is parsed from the payload as a BARE `string` type | ||
New subscriptions update the forwarding route in the same forwarding alias | ||
Forwarder address is created from prefix and alias as <prefix>_<alias> | ||
e.g. if prefix is `forward_to_` and alias is `my_alias`, forwarder address will be: `forward_to_my_alias` | ||
Messages sent to the forwarder address will be forwarded to the forwarding route | ||
Options: | ||
`prefix` - address prefix | ||
""" | ||
use Ockam.Worker | ||
|
||
alias Ockam.Services.Relay.Types.CreateRelayRequest | ||
alias Ockam.Services.Relay.Types.Relay | ||
alias Ockam.Services.Relay.Worker, as: Forwarder | ||
|
||
alias Ockam.Message | ||
|
||
require Logger | ||
|
||
@spec list_running_relays() :: [{Ockam.Address.t(), map()}] | ||
def list_running_relays() do | ||
Ockam.Node.Registry.select_by_attribute(:service, :relay) | ||
|> Enum.map(&Relay.from_registry_attributes/1) | ||
end | ||
|
||
@spec relay_info(addr :: Ockam.Address.t()) :: {:ok, Relay.t()} | :error | ||
def relay_info(addr) do | ||
with {:ok, meta} <- Ockam.Node.Registry.lookup_meta(addr) do | ||
{:ok, Relay.from_registry_attributes({addr, meta.attributes})} | ||
end | ||
end | ||
|
||
@impl true | ||
def setup(options, state) do | ||
prefix = Keyword.get(options, :prefix, state.address) | ||
|
||
forwarder_options = Keyword.get(options, :forwarder_options, []) | ||
|
||
{:ok, | ||
Map.merge(state, %{ | ||
prefix: prefix, | ||
forwarder_options: forwarder_options | ||
})} | ||
end | ||
|
||
@impl true | ||
def handle_message(message, state) do | ||
payload = Message.payload(message) | ||
|
||
case parse_create_relay_req(payload) do | ||
{:ok, req} -> | ||
return_route = Message.return_route(message) | ||
target_identifier = Message.local_metadata_value(message, :identity_id) | ||
_ignored = subscribe(req.alias, req.tags, return_route, target_identifier, true, state) | ||
{:ok, state} | ||
|
||
{:error, reason} -> | ||
Logger.error("Invalid relay create msg: #{inspect(payload)}, reason #{inspect(reason)}") | ||
{:ok, state} | ||
end | ||
end | ||
|
||
def parse_create_relay_req(data) do | ||
case :bare.decode(data, :string) do | ||
{:ok, alias_str, ""} -> | ||
{:ok, %CreateRelayRequest{alias: alias_str, tags: %{}}} | ||
|
||
_err -> | ||
CreateRelayRequest.decode_strict(data) | ||
end | ||
end | ||
|
||
def subscribe(alias_str, tags, route, target_identifier, notify, state) do | ||
forwarder_address = forwarder_address(alias_str, state) | ||
forwarder_options = Map.fetch!(state, :forwarder_options) | ||
|
||
case Ockam.Node.whereis(forwarder_address) do | ||
nil -> | ||
Forwarder.create( | ||
Keyword.merge(forwarder_options, | ||
address: forwarder_address, | ||
relay_options: [ | ||
alias: alias_str, | ||
route: route, | ||
tags: tags, | ||
notify: notify, | ||
target_identifier: target_identifier | ||
] | ||
) | ||
) | ||
|
||
_pid -> | ||
with :ok <- | ||
Forwarder.update_route(forwarder_address, route, target_identifier, tags, notify) do | ||
{:ok, forwarder_address} | ||
end | ||
end | ||
end | ||
|
||
def forwarder_address(alias_str, state) do | ||
Map.get(state, :prefix, "") <> "_" <> alias_str | ||
end | ||
end |
Oops, something went wrong.