Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add support for loading zones remotely and from a zone server.
- Loading branch information
Showing
12 changed files
with
268 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
{application,erldns, | ||
[{description,"Erlang Authoritative DNS Server"}, | ||
{vsn,"de616b1"}, | ||
{vsn,"d5750c6"}, | ||
{mod,{erldns_app,[]}}, | ||
{applications,[kernel,stdlib]}, | ||
{applications,[kernel,stdlib,inets,crypto,ssl]}, | ||
{start_phases,[{post_start,[]}]}, | ||
{modules,[erldns,erldns_app,erldns_axfr,erldns_config, | ||
erldns_dnssec,erldns_edns,erldns_encoder, | ||
erldns_handler,erldns_metrics,erldns_packet_cache, | ||
erldns_query_throttle,erldns_records,erldns_sup, | ||
erldns_tcp_server,erldns_udp_server,erldns_worker, | ||
erldns_zone_cache,gen_nb_server, | ||
erldns_zone_cache,erldns_zone_client, | ||
erldns_zone_loader,erldns_zone_parser,gen_nb_server, | ||
sample_custom_handler]}]}. |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
-module(erldns_zone_client). | ||
|
||
-behaviour(websocket_client_handler). | ||
|
||
-include("dns.hrl"). | ||
-include("erldns.hrl"). | ||
|
||
-export([ | ||
start_link/0, | ||
fetch_zones/0, | ||
fetch_zone/1, | ||
init/1, | ||
websocket_handle/2, | ||
websocket_info/2, | ||
websocket_terminate/2 | ||
]). | ||
|
||
% Public API | ||
start_link() -> | ||
lager:info("Starting websocket client"), | ||
StartLinkResult = websocket_client:start_link(?MODULE, wss, zone_server_host(), 443, "/ws", []), | ||
{ok, StartLinkResult}. | ||
|
||
fetch_zones() -> | ||
case httpc:request(get, {zones_url(), [auth_header()]}, [], [{body_format, binary}]) of | ||
{ok, {{_Version, 200, _ReasonPhrase}, _Headers, Body}} -> | ||
lager:info("Parsing zones JSON"), | ||
Zones = erldns_zone_parser:zones_to_erlang(jsx:decode(Body)), | ||
lists:foreach( | ||
fun(Zone) -> | ||
erldns_zone_cache:put_zone(Zone) | ||
end, Zones), | ||
lager:info("Put ~p zones into cache", [length(Zones)]), | ||
{ok, length(Zones)}; | ||
{_, {{_Version, Status, ReasonPhrase}, _Headers, _Body}} -> | ||
lager:error("Failed to load zones: ~p (status: ~p)", [ReasonPhrase, Status]), | ||
{err, Status, ReasonPhrase} | ||
end. | ||
|
||
fetch_zone(Name) -> | ||
fetch_zone(Name, zones_url() ++ binary_to_list(Name)). | ||
|
||
fetch_zone(Name, Url) -> | ||
case httpc:request(get, {Url, [auth_header()]}, [], [{body_format, binary}]) of | ||
{ok, {{_Version, 200, _ReasonPhrase}, _Headers, Body}} -> | ||
lager:info("Parsing zone JSON"), | ||
Zone = erldns_zone_parser:zone_to_erlang(jsx:decode(Body)), | ||
lager:info("Putting ~p into zone cache", [Name]), | ||
erldns_zone_cache:put_zone(Zone); | ||
{_, {{_Version, Status, ReasonPhrase}, _Headers, _Body}} -> | ||
lager:error("Failed to load zone: ~p (status: ~p)", [ReasonPhrase, Status]), | ||
{err, Status, ReasonPhrase} | ||
end. | ||
|
||
% Websocket Callbacks | ||
|
||
init([]) -> | ||
lager:info("init() websocket client"), | ||
self() ! authenticate, | ||
{ok, 2}. | ||
|
||
websocket_handle({_Type, Msg}, State) -> | ||
ZoneNotification = jsx:decode(Msg), | ||
lager:info("Zone notification received: ~p", [ZoneNotification]), | ||
case ZoneNotification of | ||
[{<<"name">>, Name}, {<<"url">>, Url}, {<<"action">>, Action}] -> | ||
case Action of | ||
<<"create">> -> | ||
lager:debug("Creating zone ~p", [Name]), | ||
fetch_zone(Name, binary_to_list(Url)); | ||
<<"update">> -> | ||
lager:debug("Updating zone ~p", [Name]), | ||
fetch_zone(Name, binary_to_list(Url)); | ||
<<"delete">> -> | ||
erldns_zone_cache:delete_zone(Name), | ||
lager:debug("Deleting zone ~p", [Name]); | ||
_ -> | ||
lager:error("Unsupported action: ~p", [Action]) | ||
end; | ||
_ -> | ||
lager:error("Unsupported zone notification message: ~p", [ZoneNotification]) | ||
end, | ||
{ok, State}. | ||
|
||
websocket_info(authenticate, State) -> | ||
EncodedCredentials = encoded_credentials(), | ||
lager:debug("Authenticating with ~p", [EncodedCredentials]), | ||
{reply, {text, list_to_binary("Authorization: " ++ EncodedCredentials)}, State}; | ||
|
||
websocket_info(Atom, State) -> | ||
lager:debug("websocket_info(~p, ~p)", [Atom, State]), | ||
{ok, State}. | ||
|
||
websocket_terminate(Message, State) -> | ||
lager:debug("websocket_terminate(~p, ~p)", [Message, State]), | ||
ok. | ||
|
||
%% Internal functions | ||
zone_server_host() -> | ||
{ok, ZoneServerHost} = application:get_env(erldns, zone_server_host), | ||
ZoneServerHost. | ||
|
||
zones_url() -> | ||
"https://" ++ zone_server_host() ++ "/zones/". | ||
|
||
encoded_credentials() -> | ||
case application:get_env(erldns, credentials) of | ||
{ok, {Username, Password}} -> | ||
lager:debug("Sending ~p:~p for authentication", [Username, Password]), | ||
base64:encode_to_string(lists:append([Username,":",Password])) | ||
end. | ||
|
||
auth_header() -> | ||
{"Authorization","Basic " ++ encoded_credentials()}. | ||
|
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,20 @@ | ||
-module(erldns_zone_loader). | ||
|
||
-export([load_zones/0]). | ||
|
||
-define(FILENAME, "zones.json"). | ||
|
||
load_zones() -> | ||
case file:read_file(?FILENAME) of | ||
{ok, Binary} -> | ||
Zones = erldns_zone_parser:zones_to_erlang(jsx:decode(Binary)), | ||
lists:foreach( | ||
fun(Zone) -> | ||
erldns_zone_cache:put_zone(Zone) | ||
end, Zones), | ||
lager:info("Loaded ~p zones", [length(Zones)]), | ||
{ok, length(Zones)}; | ||
{error, Reason} -> | ||
lager:error("Failed to load zones: ~p", [Reason]), | ||
{err, Reason} | ||
end. |
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,86 @@ | ||
-module(erldns_zone_parser). | ||
|
||
-export([zones_to_erlang/1, zone_to_erlang/1]). | ||
|
||
-include("dns.hrl"). | ||
-include("erldns.hrl"). | ||
|
||
zones_to_erlang(Zones) -> zones_to_erlang(Zones, []). | ||
|
||
% Internal | ||
zones_to_erlang([], Zones) -> Zones; | ||
|
||
zones_to_erlang([Zone|Rest], Zones) -> | ||
ParsedZone = zone_to_erlang(Zone), | ||
zones_to_erlang(Rest, Zones ++ [ParsedZone]). | ||
|
||
%% Takes a JSON zone and turns it into the tuple {Name, Records}. | ||
zone_to_erlang([{<<"name">>, Name}, {<<"records">>, JsonRecords}]) -> | ||
Records = lists:map( | ||
fun(JsonRecord) -> | ||
json_record_to_erlang(JsonRecord) | ||
end, JsonRecords), | ||
|
||
FilteredRecords = lists:filter( | ||
fun(R) -> | ||
case R of | ||
{} -> false; | ||
_ -> true | ||
end | ||
end, Records), | ||
|
||
{Name, FilteredRecords}. | ||
|
||
% Internal converters | ||
json_record_to_erlang([{<<"name">>, Name}, {<<"type">>, <<"SOA">>}, {<<"data">>, [{<<"mname">>, Mname}, {<<"rname">>, Rname}, {<<"serial">>, Serial}, {<<"refresh">>, Refresh}, {<<"retry">>, Retry}, {<<"expire">>, Expire},{<<"minimum">>, Minimum}]}, {<<"ttl">>, Ttl}]) -> | ||
#dns_rr{name = Name, type = ?DNS_TYPE_SOA, data = #dns_rrdata_soa{mname = Mname, rname = Rname, serial = Serial, refresh = Refresh, retry = Retry, expire = Expire, minimum = Minimum}, ttl = Ttl}; | ||
|
||
json_record_to_erlang([{<<"name">>, Name}, {<<"type">>, <<"NS">>}, {<<"data">>, [{<<"dname">>, Dname}]}, {<<"ttl">>, Ttl}]) -> | ||
#dns_rr{name = Name, type = ?DNS_TYPE_NS, data = #dns_rrdata_ns{dname = Dname}, ttl = Ttl}; | ||
|
||
json_record_to_erlang([{<<"name">>, Name}, {<<"type">>, <<"A">>}, {<<"data">>, [{<<"ip">>, Ip}]}, {<<"ttl">>, Ttl}]) -> | ||
case inet_parse:address(binary_to_list(Ip)) of | ||
{ok, Address} -> | ||
#dns_rr{name = Name, type = ?DNS_TYPE_A, data = #dns_rrdata_a{ip = Address}, ttl = Ttl}; | ||
{error, Reason} -> | ||
lager:error("Failed to parse A record address ~p: ~p", [Ip, Reason]), | ||
{} | ||
end; | ||
|
||
json_record_to_erlang([{<<"name">>, Name}, {<<"type">>, <<"AAAA">>}, {<<"data">>, [{<<"ip">>, Ip}]}, {<<"ttl">>, Ttl}]) -> | ||
case inet_parse:address(binary_to_list(Ip)) of | ||
{ok, Address} -> | ||
#dns_rr{name = Name, type = ?DNS_TYPE_AAAA, data = #dns_rrdata_aaaa{ip = Address}, ttl = Ttl}; | ||
{error, Reason} -> | ||
lager:error("Failed to parse AAAA record address ~p: ~p", [Ip, Reason]), | ||
{} | ||
end; | ||
|
||
json_record_to_erlang([{<<"name">>, Name}, {<<"type">>, <<"CNAME">>}, {<<"data">>, [{<<"dname">>, Dname}]}, {<<"ttl">>, Ttl}]) -> | ||
#dns_rr{name = Name, type = ?DNS_TYPE_CNAME, data = #dns_rrdata_cname{dname = Dname}, ttl = Ttl}; | ||
|
||
json_record_to_erlang([{<<"name">>, Name}, {<<"type">>, <<"MX">>}, {<<"data">>, [{<<"preference">>, Preference}, {<<"exchange">>, Exchange}]}, {<<"ttl">>, Ttl}]) -> | ||
#dns_rr{name = Name, type = ?DNS_TYPE_MX, data = #dns_rrdata_mx{exchange = Exchange, preference = Preference}, ttl = Ttl}; | ||
|
||
json_record_to_erlang([{<<"name">>, Name}, {<<"type">>, <<"TXT">>}, {<<"data">>, [{<<"txt">>, Text}]}, {<<"ttl">>, Ttl}]) -> | ||
#dns_rr{name = Name, type = ?DNS_TYPE_TXT, data = #dns_rrdata_txt{txt = [Text]}, ttl = Ttl}; | ||
|
||
json_record_to_erlang([{<<"name">>, Name}, {<<"type">>, <<"SPF">>}, {<<"data">>, [{<<"spf">>, Spf}]}, {<<"ttl">>, Ttl}]) -> | ||
#dns_rr{name = Name, type = ?DNS_TYPE_SPF, data = #dns_rrdata_spf{spf = [Spf]}, ttl = Ttl}; | ||
|
||
json_record_to_erlang([{<<"name">>,Name},{<<"type">>,<<"PTR">>},{<<"data">>,[{<<"dname">>, Dname}]},{<<"ttl">>, Ttl}]) -> | ||
#dns_rr{name = Name, type = ?DNS_TYPE_PTR, data = #dns_rrdata_ptr{dname = Dname}, ttl = Ttl}; | ||
|
||
json_record_to_erlang([{<<"name">>,Name},{<<"type">>,<<"SSHFP">>},{<<"data">>,[{<<"alg">>,Alg},{<<"fptype">>,Fptype},{<<"fp">>,Fp}]},{<<"ttl">>,Ttl}]) -> | ||
#dns_rr{name = Name, type = ?DNS_TYPE_SSHFP, data = #dns_rrdata_sshfp{alg = Alg, fp_type = Fptype, fp = Fp}, ttl = Ttl}; | ||
|
||
json_record_to_erlang([{<<"name">>, Name}, {<<"type">>, <<"SRV">>}, {<<"data">>, [{<<"priority">>, Priority}, {<<"weight">>, Weight}, {<<"port">>, Port}, {<<"target">>, Target}]}, {<<"ttl">>, Ttl}]) -> | ||
#dns_rr{name = Name, type = ?DNS_TYPE_SRV, data = #dns_rrdata_srv{priority = Priority, weight = Weight, port = Port, target = Target}, ttl = Ttl}; | ||
|
||
json_record_to_erlang([{<<"name">>, Name}, {<<"type">>, <<"NAPTR">>}, {<<"data">>, [{<<"order">>, Order}, {<<"preference">>, Preference}, {<<"flags">>, Flags}, {<<"services">>, Services}, {<<"regexp">>, Regexp}, {<<"replacement">>, Replacement}]}, {<<"ttl">>, Ttl}]) -> | ||
#dns_rr{name = Name, type = ?DNS_TYPE_NAPTR, data = #dns_rrdata_naptr{order = Order, preference = Preference, flags = Flags, services = Services, regexp = Regexp, replacement = Replacement}, ttl = Ttl}; | ||
|
||
json_record_to_erlang(JsonRecord) -> | ||
lager:info("Unsupported record ~p", [JsonRecord]), | ||
{}. | ||
|