network-infra-utility 0.2.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +21 -0
- data/CHANGELOG.md +31 -4
- data/Gemfile +2 -0
- data/Gemfile.lock +70 -0
- data/Rakefile +1 -1
- data/bin/dns-query +834 -0
- data/bin/geo-doc +135 -0
- data/bin/geo-get +1 -1
- data/document/ASNum/346/250/241/345/235/227/345/212/237/350/203/275/350/257/264/346/230/216.md +242 -0
- data/document/DNSQuery/345/267/245/345/205/267/344/275/277/347/224/250/346/226/271/346/263/225.md +248 -0
- data/document/Geo/345/221/275/344/273/244/345/267/245/345/205/267/344/275/277/347/224/250/346/226/271/346/263/225.md +441 -0
- data/document/IP/346/250/241/345/235/227/345/212/237/350/203/275/350/257/264/346/230/216.md +297 -0
- data/document/MAC/346/250/241/345/235/227/345/212/237/350/203/275/350/257/264/346/230/216.md +296 -0
- data/document/SSH/350/277/236/346/216/245/345/256/242/346/210/267/347/253/257/344/275/277/347/224/250/346/226/271/346/263/225.md +764 -0
- data/network-infra-utility.gemspec +4 -2
- data/network.rb +3 -1
- data/service/geodb/GeoAPI.md +1 -0
- data/service/geodb/geodb.rb +278 -1
- data/service/ssh/README.md +942 -0
- data/service/ssh/bin/ssh-client +198 -0
- data/service/ssh/design/SSH/350/277/236/346/216/245/345/256/242/346/210/267/347/253/257/345/212/237/350/203/275/351/234/200/346/261/202/346/226/207/346/241/243.md +292 -0
- data/service/ssh/design/SSH/350/277/236/346/216/245/345/256/242/346/210/267/347/253/257/350/257/246/347/273/206/350/256/276/350/256/241/346/226/207/346/241/243.md +1521 -0
- data/service/ssh/design/SSH/350/277/236/346/216/245/345/256/242/346/210/267/347/253/257/350/275/257/344/273/266/350/256/276/350/256/241/346/226/207/346/241/243.md +2493 -0
- data/service/ssh/ext/ssh_core/bin/ssh_core.cmd +28 -0
- data/service/ssh/ext/ssh_core/config/sys.config +0 -0
- data/service/ssh/ext/ssh_core/config/vm.args +0 -0
- data/service/ssh/ext/ssh_core/local_deps/jsx/CHECKSUM +1 -0
- data/service/ssh/ext/ssh_core/local_deps/jsx/LICENSE +21 -0
- data/service/ssh/ext/ssh_core/local_deps/jsx/README.md +696 -0
- data/service/ssh/ext/ssh_core/local_deps/jsx/VERSION +1 -0
- data/service/ssh/ext/ssh_core/local_deps/jsx/contents.tar.gz +0 -0
- data/service/ssh/ext/ssh_core/local_deps/jsx/metadata.config +15 -0
- data/service/ssh/ext/ssh_core/local_deps/jsx/rebar.config +17 -0
- data/service/ssh/ext/ssh_core/local_deps/jsx/rebar.lock +1 -0
- data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx.app.src +10 -0
- data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx.erl +506 -0
- data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_config.erl +393 -0
- data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_config.hrl +18 -0
- data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_consult.erl +81 -0
- data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_decoder.erl +1909 -0
- data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_encoder.erl +116 -0
- data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_parser.erl +1214 -0
- data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_to_json.erl +408 -0
- data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_to_term.erl +389 -0
- data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_verify.erl +121 -0
- data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx.erl +506 -0
- data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_config.erl +393 -0
- data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_config.hrl +18 -0
- data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_consult.erl +81 -0
- data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_decoder.erl +1909 -0
- data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_encoder.erl +116 -0
- data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_parser.erl +1214 -0
- data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_to_json.erl +408 -0
- data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_to_term.erl +389 -0
- data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_verify.erl +121 -0
- data/service/ssh/ext/ssh_core/rebar.config +24 -0
- data/service/ssh/ext/ssh_core/rebar.lock +1 -0
- data/service/ssh/ext/ssh_core/src/ssh_auth_engine.erl +156 -0
- data/service/ssh/ext/ssh_core/src/ssh_channel_stm.erl +232 -0
- data/service/ssh/ext/ssh_core/src/ssh_codec.erl +83 -0
- data/service/ssh/ext/ssh_core/src/ssh_conn_sup.erl +48 -0
- data/service/ssh/ext/ssh_core/src/ssh_conn_worker.erl +535 -0
- data/service/ssh/ext/ssh_core/src/ssh_core.app.src +36 -0
- data/service/ssh/ext/ssh_core/src/ssh_core_app.erl +11 -0
- data/service/ssh/ext/ssh_core/src/ssh_core_sup.erl +46 -0
- data/service/ssh/ext/ssh_core/src/ssh_infra_sup.erl +104 -0
- data/service/ssh/ext/ssh_core/src/ssh_ipc.hrl +80 -0
- data/service/ssh/ext/ssh_core/src/ssh_ipc_coalesce.erl +94 -0
- data/service/ssh/ext/ssh_core/src/ssh_ipc_gateway.erl +467 -0
- data/service/ssh/ext/ssh_core/src/ssh_ipc_proto.erl +95 -0
- data/service/ssh/ext/ssh_core/src/ssh_jump_chain.erl +101 -0
- data/service/ssh/ext/ssh_core/src/ssh_keepalive_mgr.erl +222 -0
- data/service/ssh/ext/ssh_core/src/ssh_known_hosts_proxy.erl +67 -0
- data/service/ssh/ext/ssh_core/src/ssh_port_fwd.erl +225 -0
- data/service/ssh/ext/ssh_core/src/ssh_sftp_session.erl +250 -0
- data/service/ssh/ext/ssh_core/src/ssh_sftp_sup.erl +62 -0
- data/service/ssh/ext/ssh_core_rs/Cargo.lock +2345 -0
- data/service/ssh/ext/ssh_core_rs/Cargo.toml +30 -0
- data/service/ssh/ext/ssh_core_rs/bin/ssh_core_rs +34 -0
- data/service/ssh/ext/ssh_core_rs/bin/ssh_core_rs.cmd +40 -0
- data/service/ssh/ext/ssh_core_rs/src/channel.rs +296 -0
- data/service/ssh/ext/ssh_core_rs/src/coalesce.rs +143 -0
- data/service/ssh/ext/ssh_core_rs/src/codec.rs +71 -0
- data/service/ssh/ext/ssh_core_rs/src/conn.rs +628 -0
- data/service/ssh/ext/ssh_core_rs/src/gateway.rs +389 -0
- data/service/ssh/ext/ssh_core_rs/src/handler.rs +293 -0
- data/service/ssh/ext/ssh_core_rs/src/keepalive.rs +194 -0
- data/service/ssh/ext/ssh_core_rs/src/main.rs +351 -0
- data/service/ssh/ext/ssh_core_rs/src/portfwd.rs +378 -0
- data/service/ssh/ext/ssh_core_rs/src/proto.rs +198 -0
- data/service/ssh/ext/ssh_core_rs/src/sftp.rs +294 -0
- data/service/ssh/lib/network_infra_utility/ssh/automation/macro_engine.rb +213 -0
- data/service/ssh/lib/network_infra_utility/ssh/client.rb +257 -0
- data/service/ssh/lib/network_infra_utility/ssh/config/schema.rb +90 -0
- data/service/ssh/lib/network_infra_utility/ssh/config/settings.rb +103 -0
- data/service/ssh/lib/network_infra_utility/ssh/config/store.rb +90 -0
- data/service/ssh/lib/network_infra_utility/ssh/ipc/coalesce.rb +83 -0
- data/service/ssh/lib/network_infra_utility/ssh/ipc/errors.rb +36 -0
- data/service/ssh/lib/network_infra_utility/ssh/ipc/router.rb +212 -0
- data/service/ssh/lib/network_infra_utility/ssh/ipc/transport.rb +81 -0
- data/service/ssh/lib/network_infra_utility/ssh/security/host_key.rb +211 -0
- data/service/ssh/lib/network_infra_utility/ssh/security/vault.rb +211 -0
- data/service/ssh/lib/network_infra_utility/ssh/session/history.rb +56 -0
- data/service/ssh/lib/network_infra_utility/ssh/session/manager.rb +92 -0
- data/service/ssh/lib/network_infra_utility/ssh/session/session.rb +109 -0
- data/service/ssh/lib/network_infra_utility/ssh/session/tree.rb +95 -0
- data/service/ssh/lib/network_infra_utility/ssh/terminal/ansi_parser.rb +435 -0
- data/service/ssh/lib/network_infra_utility/ssh/terminal/buffer.rb +78 -0
- data/service/ssh/lib/network_infra_utility/ssh/terminal/emulator.rb +159 -0
- data/service/ssh/lib/network_infra_utility/ssh/terminal/logger.rb +195 -0
- data/service/ssh/lib/network_infra_utility/ssh/terminal/screen.rb +212 -0
- data/service/ssh/lib/network_infra_utility/ssh/terminal/theme.rb +127 -0
- data/service/ssh/lib/network_infra_utility/ssh/version.rb +7 -0
- data/service/ssh/lib/network_infra_utility/ssh.rb +44 -0
- data/support/basic/as_num.rb +221 -0
- data/support/basic/mac_address.rb +281 -0
- data/version.rb +1 -1
- metadata +138 -1
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
%% The MIT License
|
|
2
|
+
|
|
3
|
+
%% Copyright (c) 2010-2013 Alisdair Sullivan <alisdairsullivan@yahoo.ca>
|
|
4
|
+
|
|
5
|
+
%% Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
%% of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
%% in the Software without restriction, including without limitation the rights
|
|
8
|
+
%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
%% copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
%% furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
%% The above copyright notice and this permission notice shall be included in
|
|
13
|
+
%% all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
%% THE SOFTWARE.
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
-module(jsx_to_term).
|
|
25
|
+
|
|
26
|
+
-export([to_term/2]).
|
|
27
|
+
-export([init/1, handle_event/2]).
|
|
28
|
+
-export([
|
|
29
|
+
start_term/1,
|
|
30
|
+
start_object/1,
|
|
31
|
+
start_array/1,
|
|
32
|
+
finish/1,
|
|
33
|
+
insert/2,
|
|
34
|
+
get_key/1,
|
|
35
|
+
get_value/1
|
|
36
|
+
]).
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
-record(config, {
|
|
40
|
+
labels = binary,
|
|
41
|
+
return_maps = false
|
|
42
|
+
}).
|
|
43
|
+
|
|
44
|
+
-type config() :: proplists:proplist().
|
|
45
|
+
|
|
46
|
+
-spec to_term(Source::binary(), Config::jsx_config:options()) -> jsx:json_term() | {incomplete, jsx:decoder()}.
|
|
47
|
+
|
|
48
|
+
to_term(Source, Config) when is_list(Config) ->
|
|
49
|
+
(jsx:decoder(?MODULE, [return_maps] ++ Config, jsx_config:extract_config(Config)))(Source).
|
|
50
|
+
|
|
51
|
+
parse_config(Config) -> parse_config(Config, #config{}).
|
|
52
|
+
|
|
53
|
+
parse_config([{labels, Val}|Rest], Config)
|
|
54
|
+
when Val == binary; Val == atom; Val == existing_atom; Val == attempt_atom ->
|
|
55
|
+
parse_config(Rest, Config#config{labels = Val});
|
|
56
|
+
parse_config([labels|Rest], Config) ->
|
|
57
|
+
parse_config(Rest, Config#config{labels = binary});
|
|
58
|
+
parse_config([{return_maps, Val}|Rest], Config)
|
|
59
|
+
when Val == true; Val == false ->
|
|
60
|
+
parse_config(Rest, Config#config{return_maps = Val});
|
|
61
|
+
parse_config([return_maps|Rest], Config) ->
|
|
62
|
+
parse_config(Rest, Config#config{return_maps = true});
|
|
63
|
+
parse_config([{K, _}|Rest] = Options, Config) ->
|
|
64
|
+
case lists:member(K, jsx_config:valid_flags()) of
|
|
65
|
+
true -> parse_config(Rest, Config)
|
|
66
|
+
; false -> erlang:error(badarg, [Options, Config])
|
|
67
|
+
end;
|
|
68
|
+
parse_config([K|Rest] = Options, Config) ->
|
|
69
|
+
case lists:member(K, jsx_config:valid_flags()) of
|
|
70
|
+
true -> parse_config(Rest, Config)
|
|
71
|
+
; false -> erlang:error(badarg, [Options, Config])
|
|
72
|
+
end;
|
|
73
|
+
parse_config([], Config) ->
|
|
74
|
+
Config.
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
-type state() :: {list(), #config{}}.
|
|
78
|
+
-spec init(Config::config()) -> state().
|
|
79
|
+
|
|
80
|
+
init(Config) -> start_term(Config).
|
|
81
|
+
|
|
82
|
+
-spec handle_event(Event::any(), State::state()) -> state().
|
|
83
|
+
|
|
84
|
+
handle_event(end_json, State) -> get_value(State);
|
|
85
|
+
|
|
86
|
+
handle_event(start_object, State) -> start_object(State);
|
|
87
|
+
handle_event(end_object, State) -> finish(State);
|
|
88
|
+
|
|
89
|
+
handle_event(start_array, State) -> start_array(State);
|
|
90
|
+
handle_event(end_array, State) -> finish(State);
|
|
91
|
+
|
|
92
|
+
handle_event({key, Key}, {_, Config} = State) -> insert(format_key(Key, Config), State);
|
|
93
|
+
|
|
94
|
+
handle_event({_, Event}, State) -> insert(Event, State).
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
format_key(Key, Config) ->
|
|
98
|
+
case Config#config.labels of
|
|
99
|
+
binary -> Key
|
|
100
|
+
; atom -> binary_to_atom(Key, utf8)
|
|
101
|
+
; existing_atom -> binary_to_existing_atom(Key, utf8)
|
|
102
|
+
; attempt_atom ->
|
|
103
|
+
try binary_to_existing_atom(Key, utf8) of
|
|
104
|
+
Result -> Result
|
|
105
|
+
catch
|
|
106
|
+
error:badarg -> Key
|
|
107
|
+
end
|
|
108
|
+
end.
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
%% internal state is a stack and a config object
|
|
112
|
+
%% `{Stack, Config}`
|
|
113
|
+
%% the stack is a list of in progress objects/arrays
|
|
114
|
+
%% `[Current, Parent, Grandparent,...OriginalAncestor]`
|
|
115
|
+
%% an object has the representation on the stack of
|
|
116
|
+
%% `{object, [
|
|
117
|
+
%% {NthKey, NthValue},
|
|
118
|
+
%% {NMinus1Key, NthMinus1Value},
|
|
119
|
+
%% ...,
|
|
120
|
+
%% {FirstKey, FirstValue}
|
|
121
|
+
%% ]}`
|
|
122
|
+
%% or if returning maps
|
|
123
|
+
%% `{object, #{
|
|
124
|
+
%% FirstKey => FirstValue,
|
|
125
|
+
%% SecondKey => SecondValue,
|
|
126
|
+
%% ...,
|
|
127
|
+
%% NthKey => NthValue
|
|
128
|
+
%% }}`
|
|
129
|
+
%% or if there's a key with a yet to be matched value
|
|
130
|
+
%% `{object, Key, ...}`
|
|
131
|
+
%% an array looks like
|
|
132
|
+
%% `{array, [NthValue, NthMinus1Value,...FirstValue]}`
|
|
133
|
+
|
|
134
|
+
start_term(Config) when is_list(Config) -> {[], parse_config(Config)}.
|
|
135
|
+
|
|
136
|
+
%% allocate a new object on top of the stack
|
|
137
|
+
start_object({Stack, Config=#config{return_maps=true}}) ->
|
|
138
|
+
{[{object, #{}}] ++ Stack, Config};
|
|
139
|
+
start_object({Stack, Config}) ->
|
|
140
|
+
{[{object, []}] ++ Stack, Config}.
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
%% allocate a new array on top of the stack
|
|
144
|
+
start_array({Stack, Config}) -> {[{array, []}] ++ Stack, Config}.
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
%% finish an object or array and insert it into the parent object if it exists or
|
|
148
|
+
%% return it if it is the root object
|
|
149
|
+
finish({[{object, Map}], Config=#config{return_maps=true}}) -> {Map, Config};
|
|
150
|
+
finish({[{object, Map}|Rest], Config=#config{return_maps=true}}) -> insert(Map, {Rest, Config});
|
|
151
|
+
finish({[{object, []}], Config}) -> {[{}], Config};
|
|
152
|
+
finish({[{object, []}|Rest], Config}) -> insert([{}], {Rest, Config});
|
|
153
|
+
finish({[{object, Pairs}], Config}) -> {lists:reverse(Pairs), Config};
|
|
154
|
+
finish({[{object, Pairs}|Rest], Config}) -> insert(lists:reverse(Pairs), {Rest, Config});
|
|
155
|
+
finish({[{array, Values}], Config}) -> {lists:reverse(Values), Config};
|
|
156
|
+
finish({[{array, Values}|Rest], Config}) -> insert(lists:reverse(Values), {Rest, Config});
|
|
157
|
+
finish(_) -> erlang:error(badarg).
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
%% insert a value when there's no parent object or array
|
|
161
|
+
insert(Value, {[], Config}) -> {Value, Config};
|
|
162
|
+
%% insert a key or value into an object or array, autodetects the 'right' thing
|
|
163
|
+
insert(Key, {[{object, Map}|Rest], Config=#config{return_maps=true}}) ->
|
|
164
|
+
{[{object, Key, Map}] ++ Rest, Config};
|
|
165
|
+
insert(Key, {[{object, Pairs}|Rest], Config}) ->
|
|
166
|
+
{[{object, Key, Pairs}] ++ Rest, Config};
|
|
167
|
+
insert(Value, {[{object, Key, Map}|Rest], Config=#config{return_maps=true}}) ->
|
|
168
|
+
{[{object, maps:put(Key, Value, Map)}] ++ Rest, Config};
|
|
169
|
+
insert(Value, {[{object, Key, Pairs}|Rest], Config}) ->
|
|
170
|
+
{[{object, [{Key, Value}] ++ Pairs}] ++ Rest, Config};
|
|
171
|
+
insert(Value, {[{array, Values}|Rest], Config}) ->
|
|
172
|
+
{[{array, [Value] ++ Values}] ++ Rest, Config};
|
|
173
|
+
insert(_, _) -> erlang:error(badarg).
|
|
174
|
+
|
|
175
|
+
get_key({[{object, Key, _}|_], _}) -> Key;
|
|
176
|
+
get_key(_) -> erlang:error(badarg).
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
get_value({Value, _Config}) -> Value;
|
|
180
|
+
get_value(_) -> erlang:error(badarg).
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
%% eunit tests
|
|
185
|
+
|
|
186
|
+
-ifdef(TEST).
|
|
187
|
+
-include_lib("eunit/include/eunit.hrl").
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
config_test_() ->
|
|
191
|
+
[
|
|
192
|
+
{"empty config", ?_assertEqual(#config{}, parse_config([]))},
|
|
193
|
+
{"implicit binary labels", ?_assertEqual(#config{}, parse_config([labels]))},
|
|
194
|
+
{"binary labels", ?_assertEqual(#config{}, parse_config([{labels, binary}]))},
|
|
195
|
+
{"atom labels", ?_assertEqual(#config{labels=atom}, parse_config([{labels, atom}]))},
|
|
196
|
+
{"existing atom labels", ?_assertEqual(
|
|
197
|
+
#config{labels=existing_atom},
|
|
198
|
+
parse_config([{labels, existing_atom}])
|
|
199
|
+
)},
|
|
200
|
+
{"return_maps true", ?_assertEqual(
|
|
201
|
+
#config{return_maps=true},
|
|
202
|
+
parse_config([return_maps])
|
|
203
|
+
)},
|
|
204
|
+
{"invalid opt flag", ?_assertError(badarg, parse_config([error]))},
|
|
205
|
+
{"invalid opt tuple", ?_assertError(badarg, parse_config([{error, true}]))}
|
|
206
|
+
].
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
format_key_test_() ->
|
|
210
|
+
[
|
|
211
|
+
{"binary key", ?_assertEqual(<<"key">>, format_key(<<"key">>, #config{labels=binary}))},
|
|
212
|
+
{"atom key", ?_assertEqual(key, format_key(<<"key">>, #config{labels=atom}))},
|
|
213
|
+
{"existing atom key", ?_assertEqual(
|
|
214
|
+
key,
|
|
215
|
+
format_key(<<"key">>, #config{labels=existing_atom})
|
|
216
|
+
)},
|
|
217
|
+
{"nonexisting atom key", ?_assertError(
|
|
218
|
+
badarg,
|
|
219
|
+
format_key(<<"nonexistentatom">>, #config{labels=existing_atom})
|
|
220
|
+
)},
|
|
221
|
+
{"sloppy existing atom key", ?_assertEqual(
|
|
222
|
+
key,
|
|
223
|
+
format_key(<<"key">>, #config{labels=attempt_atom})
|
|
224
|
+
)},
|
|
225
|
+
{"nonexisting atom key", ?_assertEqual(
|
|
226
|
+
<<"nonexistentatom">>,
|
|
227
|
+
format_key(<<"nonexistentatom">>, #config{labels=attempt_atom})
|
|
228
|
+
)}
|
|
229
|
+
].
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
rep_manipulation_test_() ->
|
|
233
|
+
[
|
|
234
|
+
{"allocate a new context with option", ?_assertEqual(
|
|
235
|
+
{[], #config{labels=atom}},
|
|
236
|
+
start_term([{labels, atom}])
|
|
237
|
+
)},
|
|
238
|
+
{"allocate a new object on an empty stack", ?_assertEqual(
|
|
239
|
+
{[{object, []}], #config{}},
|
|
240
|
+
start_object({[], #config{}})
|
|
241
|
+
)},
|
|
242
|
+
{"allocate a new object on a stack", ?_assertEqual(
|
|
243
|
+
{[{object, []}, {object, []}], #config{}},
|
|
244
|
+
start_object({[{object, []}], #config{}})
|
|
245
|
+
)},
|
|
246
|
+
{"allocate a new array on an empty stack", ?_assertEqual(
|
|
247
|
+
{[{array, []}], #config{}},
|
|
248
|
+
start_array({[], #config{}})
|
|
249
|
+
)},
|
|
250
|
+
{"allocate a new array on a stack", ?_assertEqual(
|
|
251
|
+
{[{array, []}, {object, []}], #config{}},
|
|
252
|
+
start_array({[{object, []}], #config{}})
|
|
253
|
+
)},
|
|
254
|
+
{"insert a key into an object", ?_assertEqual(
|
|
255
|
+
{[{object, key, []}, junk], #config{}},
|
|
256
|
+
insert(key, {[{object, []}, junk], #config{}})
|
|
257
|
+
)},
|
|
258
|
+
{"get current key", ?_assertEqual(
|
|
259
|
+
key,
|
|
260
|
+
get_key({[{object, key, []}], #config{}})
|
|
261
|
+
)},
|
|
262
|
+
{"try to get non-key from object", ?_assertError(
|
|
263
|
+
badarg,
|
|
264
|
+
get_key({[{object, []}], #config{}})
|
|
265
|
+
)},
|
|
266
|
+
{"try to get key from array", ?_assertError(
|
|
267
|
+
badarg,
|
|
268
|
+
get_key({[{array, []}], #config{}})
|
|
269
|
+
)},
|
|
270
|
+
{"insert a value into an object", ?_assertEqual(
|
|
271
|
+
{[{object, [{key, value}]}, junk], #config{}},
|
|
272
|
+
insert(value, {[{object, key, []}, junk], #config{}})
|
|
273
|
+
)},
|
|
274
|
+
{"insert a value into an array", ?_assertEqual(
|
|
275
|
+
{[{array, [value]}, junk], #config{}},
|
|
276
|
+
insert(value, {[{array, []}, junk], #config{}})
|
|
277
|
+
)},
|
|
278
|
+
{"finish an object with no ancestor", ?_assertEqual(
|
|
279
|
+
{[{a, b}, {x, y}], #config{}},
|
|
280
|
+
finish({[{object, [{x, y}, {a, b}]}], #config{}})
|
|
281
|
+
)},
|
|
282
|
+
{"finish an empty object", ?_assertEqual(
|
|
283
|
+
{[{}], #config{}},
|
|
284
|
+
finish({[{object, []}], #config{}})
|
|
285
|
+
)},
|
|
286
|
+
{"finish an object with an ancestor", ?_assertEqual(
|
|
287
|
+
{[{object, [{key, [{a, b}, {x, y}]}, {foo, bar}]}], #config{}},
|
|
288
|
+
finish({[{object, [{x, y}, {a, b}]}, {object, key, [{foo, bar}]}], #config{}})
|
|
289
|
+
)},
|
|
290
|
+
{"finish an array with no ancestor", ?_assertEqual(
|
|
291
|
+
{[a, b, c], #config{}},
|
|
292
|
+
finish({[{array, [c, b, a]}], #config{}})
|
|
293
|
+
)},
|
|
294
|
+
{"finish an array with an ancestor", ?_assertEqual(
|
|
295
|
+
{[{array, [[a, b, c], d, e, f]}], #config{}},
|
|
296
|
+
finish({[{array, [c, b, a]}, {array, [d, e, f]}], #config{}})
|
|
297
|
+
)}
|
|
298
|
+
].
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
rep_manipulation_with_maps_test_() ->
|
|
302
|
+
[
|
|
303
|
+
{"allocate a new object on an empty stack", ?_assertEqual(
|
|
304
|
+
{[{object, #{}}], #config{return_maps=true}},
|
|
305
|
+
start_object({[], #config{return_maps=true}})
|
|
306
|
+
)},
|
|
307
|
+
{"allocate a new object on a stack", ?_assertEqual(
|
|
308
|
+
{[{object, #{}}, {object, #{}}], #config{return_maps=true}},
|
|
309
|
+
start_object({[{object, #{}}], #config{return_maps=true}})
|
|
310
|
+
)},
|
|
311
|
+
{"insert a key into an object", ?_assertEqual(
|
|
312
|
+
{[{object, key, #{}}, junk], #config{return_maps=true}},
|
|
313
|
+
insert(key, {[{object, #{}}, junk], #config{return_maps=true}})
|
|
314
|
+
)},
|
|
315
|
+
{"get current key", ?_assertEqual(
|
|
316
|
+
key,
|
|
317
|
+
get_key({[{object, key, #{}}], #config{return_maps=true}})
|
|
318
|
+
)},
|
|
319
|
+
{"try to get non-key from object", ?_assertError(
|
|
320
|
+
badarg,
|
|
321
|
+
get_key({[{object, #{}}], #config{return_maps=true}})
|
|
322
|
+
)},
|
|
323
|
+
{"insert a value into an object", ?_assertEqual(
|
|
324
|
+
{[{object, #{key => value}}, junk], #config{return_maps=true}},
|
|
325
|
+
insert(value, {[{object, key, #{}}, junk], #config{return_maps=true}})
|
|
326
|
+
)},
|
|
327
|
+
{"finish an object with no ancestor", ?_assertEqual(
|
|
328
|
+
{#{a => b, x => y}, #config{return_maps=true}},
|
|
329
|
+
finish({[{object, #{x => y, a => b}}], #config{return_maps=true}})
|
|
330
|
+
)},
|
|
331
|
+
{"finish an empty object", ?_assertEqual(
|
|
332
|
+
{#{}, #config{return_maps=true}},
|
|
333
|
+
finish({[{object, #{}}], #config{return_maps=true}})
|
|
334
|
+
)},
|
|
335
|
+
{"finish an object with an ancestor", ?_assertEqual(
|
|
336
|
+
{
|
|
337
|
+
[{object, #{key => #{a => b, x => y}, foo => bar}}],
|
|
338
|
+
#config{return_maps=true}
|
|
339
|
+
},
|
|
340
|
+
finish({
|
|
341
|
+
[{object, #{x => y, a => b}}, {object, key, #{foo => bar}}],
|
|
342
|
+
#config{return_maps=true}
|
|
343
|
+
})
|
|
344
|
+
)}
|
|
345
|
+
].
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
return_maps_test_() ->
|
|
349
|
+
[
|
|
350
|
+
{"an empty map", ?_assertEqual(
|
|
351
|
+
#{},
|
|
352
|
+
jsx:decode(<<"{}">>, [])
|
|
353
|
+
)},
|
|
354
|
+
{"an empty map", ?_assertEqual(
|
|
355
|
+
#{},
|
|
356
|
+
jsx:decode(<<"{}">>, [])
|
|
357
|
+
)},
|
|
358
|
+
{"an empty map", ?_assertEqual(
|
|
359
|
+
[{}],
|
|
360
|
+
jsx:decode(<<"{}">>, [{return_maps, false}])
|
|
361
|
+
)},
|
|
362
|
+
{"a small map", ?_assertEqual(
|
|
363
|
+
#{<<"awesome">> => true, <<"library">> => <<"jsx">>},
|
|
364
|
+
jsx:decode(<<"{\"library\": \"jsx\", \"awesome\": true}">>, [])
|
|
365
|
+
)},
|
|
366
|
+
{"a recursive map", ?_assertEqual(
|
|
367
|
+
#{<<"key">> => #{<<"key">> => true}},
|
|
368
|
+
jsx:decode(<<"{\"key\": {\"key\": true}}">>, [])
|
|
369
|
+
)},
|
|
370
|
+
{"a map inside a list", ?_assertEqual(
|
|
371
|
+
[#{}],
|
|
372
|
+
jsx:decode(<<"[{}]">>, [])
|
|
373
|
+
)}
|
|
374
|
+
].
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
handle_event_test_() ->
|
|
378
|
+
Data = jsx:test_cases(),
|
|
379
|
+
[
|
|
380
|
+
{
|
|
381
|
+
Title, ?_assertEqual(
|
|
382
|
+
Term,
|
|
383
|
+
lists:foldl(fun handle_event/2, init([]), Events ++ [end_json])
|
|
384
|
+
)
|
|
385
|
+
} || {Title, _, Term, Events} <- Data
|
|
386
|
+
].
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
-endif.
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
%% The MIT License
|
|
2
|
+
|
|
3
|
+
%% Copyright (c) 2010-2013 alisdair sullivan <alisdairsullivan@yahoo.ca>
|
|
4
|
+
|
|
5
|
+
%% Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
%% of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
%% in the Software without restriction, including without limitation the rights
|
|
8
|
+
%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
%% copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
%% furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
%% The above copyright notice and this permission notice shall be included in
|
|
13
|
+
%% all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
%% THE SOFTWARE.
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
-module(jsx_verify).
|
|
25
|
+
|
|
26
|
+
-export([is_json/2, is_term/2]).
|
|
27
|
+
-export([init/1, handle_event/2]).
|
|
28
|
+
|
|
29
|
+
-type config() :: proplists:proplist().
|
|
30
|
+
|
|
31
|
+
-spec is_json(Source::binary(), Config::jsx_config:options()) -> true | false | {incomplete, jsx:decoder()}.
|
|
32
|
+
|
|
33
|
+
is_json(Source, Config) when is_list(Config) ->
|
|
34
|
+
try (jsx:decoder(?MODULE, Config, jsx_config:extract_config(Config)))(Source)
|
|
35
|
+
catch error:badarg -> false
|
|
36
|
+
end.
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
-spec is_term(Source::jsx:json_term() | end_stream | end_json,
|
|
40
|
+
Config::jsx_config:options()) -> true | false | {incomplete, jsx:encoder()}.
|
|
41
|
+
|
|
42
|
+
is_term(Source, Config) when is_list(Config) ->
|
|
43
|
+
try (jsx:encoder(?MODULE, Config, jsx_config:extract_config(Config)))(Source)
|
|
44
|
+
catch error:badarg -> false
|
|
45
|
+
end.
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
parse_config(Config) -> parse_config(Config, []).
|
|
49
|
+
|
|
50
|
+
%% ignore deprecated flags
|
|
51
|
+
parse_config([no_repeated_keys|Rest], Config) ->
|
|
52
|
+
parse_config(Rest, Config);
|
|
53
|
+
parse_config([{repeated_keys, Val}|Rest], Config) when Val == true; Val == false ->
|
|
54
|
+
parse_config(Rest, Config);
|
|
55
|
+
parse_config([repeated_keys|Rest], Config) ->
|
|
56
|
+
parse_config(Rest, Config);
|
|
57
|
+
parse_config([{K, _}|Rest] = Options, Config) ->
|
|
58
|
+
case lists:member(K, jsx_config:valid_flags()) of
|
|
59
|
+
true -> parse_config(Rest, Config);
|
|
60
|
+
false -> erlang:error(badarg, [Options, Config])
|
|
61
|
+
end;
|
|
62
|
+
parse_config([K|Rest] = Options, Config) ->
|
|
63
|
+
case lists:member(K, jsx_config:valid_flags()) of
|
|
64
|
+
true -> parse_config(Rest, Config);
|
|
65
|
+
false -> erlang:error(badarg, [Options, Config])
|
|
66
|
+
end;
|
|
67
|
+
parse_config([], Config) ->
|
|
68
|
+
Config.
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
%% we don't actually need any state for this
|
|
72
|
+
-type state() :: [].
|
|
73
|
+
-spec init(Config::config()) -> state().
|
|
74
|
+
|
|
75
|
+
init(Config) -> parse_config(Config).
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
-spec handle_event(Event::any(), State::state()) -> state().
|
|
79
|
+
|
|
80
|
+
handle_event(end_json, _) -> true;
|
|
81
|
+
|
|
82
|
+
handle_event(_, State) -> State.
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
%% eunit tests
|
|
87
|
+
-ifdef(TEST).
|
|
88
|
+
-include_lib("eunit/include/eunit.hrl").
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
config_test_() ->
|
|
92
|
+
[
|
|
93
|
+
{"empty config", ?_assertEqual([], parse_config([]))},
|
|
94
|
+
{"no repeat keys", ?_assertEqual([], parse_config([no_repeated_keys]))},
|
|
95
|
+
{"bare repeated keys", ?_assertEqual([], parse_config([repeated_keys]))},
|
|
96
|
+
{"repeated keys true", ?_assertEqual(
|
|
97
|
+
[],
|
|
98
|
+
parse_config([{repeated_keys, true}])
|
|
99
|
+
)},
|
|
100
|
+
{"repeated keys false", ?_assertEqual(
|
|
101
|
+
[],
|
|
102
|
+
parse_config([{repeated_keys, false}])
|
|
103
|
+
)},
|
|
104
|
+
{"invalid opt flag", ?_assertError(badarg, parse_config([error]))},
|
|
105
|
+
{"invalid opt tuple", ?_assertError(badarg, parse_config([{error, true}]))}
|
|
106
|
+
].
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
handle_event_test_() ->
|
|
110
|
+
Data = jsx:test_cases() ++ jsx:special_test_cases(),
|
|
111
|
+
[
|
|
112
|
+
{
|
|
113
|
+
Title, ?_assertEqual(
|
|
114
|
+
true,
|
|
115
|
+
lists:foldl(fun handle_event/2, [], Events ++ [end_json])
|
|
116
|
+
)
|
|
117
|
+
} || {Title, _, _, Events} <- Data
|
|
118
|
+
].
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
-endif.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
%% Erlang/OTP 26+ required (ssh, public_key, crypto, kernel, stdlib)
|
|
2
|
+
{erl_opts, [
|
|
3
|
+
debug_info,
|
|
4
|
+
{src_dirs, ["src", "local_deps/jsx/src/src"]},
|
|
5
|
+
{platform_define, "win32", win32}
|
|
6
|
+
]}.
|
|
7
|
+
|
|
8
|
+
{deps, []}.
|
|
9
|
+
|
|
10
|
+
{shell, [
|
|
11
|
+
{apps, [ssh_core]}
|
|
12
|
+
]}.
|
|
13
|
+
|
|
14
|
+
{relx, [
|
|
15
|
+
{release, {ssh_core, "1.0.0"}, [ssh_core, runtime_tools, sasl]},
|
|
16
|
+
{mode, dev},
|
|
17
|
+
{include_erts, true},
|
|
18
|
+
{include_src, false},
|
|
19
|
+
{overlay, [
|
|
20
|
+
{mkdir, "{{output_dir}}/logs"},
|
|
21
|
+
{copy, "config/vm.args", "{{output_dir}}/releases/1.0.0/vm.args"},
|
|
22
|
+
{copy, "config/sys.config", "{{output_dir}}/releases/1.0.0/sys.config"}
|
|
23
|
+
]}
|
|
24
|
+
]}.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
[].
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
-module(ssh_auth_engine).
|
|
2
|
+
-behaviour(ssh_client_key_api).
|
|
3
|
+
|
|
4
|
+
%% Authentication engine — implements OTP ssh's key_cb behaviour.
|
|
5
|
+
%%
|
|
6
|
+
%% Key design (LLD §6.4, E1/E2/E7 fixes):
|
|
7
|
+
%% - OTP ssh has no ssh:auth_user/3 — auth is in ssh:connect options
|
|
8
|
+
%% - key_cb callbacks handle host key verification (via known_hosts_proxy)
|
|
9
|
+
%% - key_cb callbacks handle user private key loading
|
|
10
|
+
%% - known_hosts persistence is delegated to client via IPC
|
|
11
|
+
%% - Auth chain (multi-method fallback) creates multiple ssh:connect attempts
|
|
12
|
+
%%
|
|
13
|
+
%% OTP ssh 6.0.3 (OTP 29) key_cb calling convention:
|
|
14
|
+
%% call_KeyCb(F, Args, Opts) ->
|
|
15
|
+
%% apply(KeyCb, F, Args ++ [[{key_cb_private,KeyCbOpts}|UserOpts]])
|
|
16
|
+
%%
|
|
17
|
+
%% is_host_key new style (4-arg + opts => 5-arg total):
|
|
18
|
+
%% is_host_key(Public, [PeerName, IP], Port, Alg, Opts)
|
|
19
|
+
%% is_host_key old style (3-arg + opts => 4-arg total):
|
|
20
|
+
%% is_host_key(Public, PeerName, Alg, Opts)
|
|
21
|
+
%% add_host_key new style (3-arg + opts => 4-arg total):
|
|
22
|
+
%% add_host_key([PeerName, IP], Port, Public, Opts)
|
|
23
|
+
%% add_host_key old style (2-arg + opts => 3-arg total):
|
|
24
|
+
%% add_host_key(PeerName, Public, Opts)
|
|
25
|
+
|
|
26
|
+
-export([
|
|
27
|
+
%% ssh_client_key_api callbacks
|
|
28
|
+
add_host_key/4,
|
|
29
|
+
add_host_key/3,
|
|
30
|
+
is_host_key/5,
|
|
31
|
+
is_host_key/4,
|
|
32
|
+
user_key/2,
|
|
33
|
+
%% High-level auth
|
|
34
|
+
authenticate_chain/2
|
|
35
|
+
]).
|
|
36
|
+
|
|
37
|
+
-include("ssh_ipc.hrl").
|
|
38
|
+
|
|
39
|
+
%% ---------- ssh_client_key_api: Host key callbacks (OTP 6.0 — new style) ----------
|
|
40
|
+
|
|
41
|
+
%% @doc New style is_host_key (5-arg: Public, [PeerName, IP], Port, Alg, Opts).
|
|
42
|
+
%% Called by OTP 6.x first; if undef, OTP falls back to 4-arg old style.
|
|
43
|
+
-spec is_host_key(public_key:public_key(), [string()], inet:port_number(),
|
|
44
|
+
atom(), term()) -> boolean().
|
|
45
|
+
is_host_key(Key, [PeerName | _], Port, _Algorithm, _Opts) ->
|
|
46
|
+
case ssh_known_hosts_proxy:verify(PeerName, Port, Key) of
|
|
47
|
+
accepted -> true;
|
|
48
|
+
accepted_once -> true;
|
|
49
|
+
rejected -> false
|
|
50
|
+
end.
|
|
51
|
+
|
|
52
|
+
%% @doc New style add_host_key (4-arg: [PeerName, IP], Port, Public, Opts).
|
|
53
|
+
-spec add_host_key([string()], inet:port_number(), public_key:public_key(), term()) ->
|
|
54
|
+
ok | {error, term()}.
|
|
55
|
+
add_host_key([PeerName | _], Port, PublicKey, _Opts) ->
|
|
56
|
+
case ssh_known_hosts_proxy:verify(PeerName, Port, PublicKey) of
|
|
57
|
+
accepted -> ok;
|
|
58
|
+
accepted_once -> ok;
|
|
59
|
+
rejected -> {error, host_key_rejected}
|
|
60
|
+
end.
|
|
61
|
+
|
|
62
|
+
%% ---------- ssh_client_key_api: Host key callbacks (legacy old style) ----------
|
|
63
|
+
|
|
64
|
+
%% @doc Old style is_host_key (4-arg: Public, PeerName, Alg, Opts).
|
|
65
|
+
%% Called by older OTP when 5-arg version is undef.
|
|
66
|
+
-spec is_host_key(public_key:public_key(), string(), atom(), term()) -> boolean().
|
|
67
|
+
is_host_key(Key, PeerName, _Algorithm, _Opts) ->
|
|
68
|
+
case ssh_known_hosts_proxy:verify(PeerName, 22, Key) of
|
|
69
|
+
accepted -> true;
|
|
70
|
+
accepted_once -> true;
|
|
71
|
+
rejected -> false
|
|
72
|
+
end.
|
|
73
|
+
|
|
74
|
+
%% @doc Old style add_host_key (3-arg: PeerName, Public, Opts).
|
|
75
|
+
-spec add_host_key(string(), public_key:public_key(), term()) ->
|
|
76
|
+
ok | {error, term()}.
|
|
77
|
+
add_host_key(PeerName, PublicKey, _Opts) ->
|
|
78
|
+
case ssh_known_hosts_proxy:verify(PeerName, 22, PublicKey) of
|
|
79
|
+
accepted -> ok;
|
|
80
|
+
accepted_once -> ok;
|
|
81
|
+
rejected -> {error, host_key_rejected}
|
|
82
|
+
end.
|
|
83
|
+
|
|
84
|
+
%% ---------- ssh_client_key_api: User key callback ----------
|
|
85
|
+
|
|
86
|
+
%% @doc Called by OTP ssh to retrieve the user's private key for publickey auth.
|
|
87
|
+
-spec user_key(atom(), term()) -> {ok, term()} | {error, term()}.
|
|
88
|
+
user_key(Algorithm, Opts) ->
|
|
89
|
+
%% Algorithm: 'ssh-rsa' | 'ssh-ed25519' | 'ecdsa-sha2-nistp256' ...
|
|
90
|
+
%% Opts is a proplist; auth info stored under 'key_cb_private'
|
|
91
|
+
KeyCbOpts = proplists:get_value(key_cb_private, Opts, []),
|
|
92
|
+
AuthMap = case KeyCbOpts of
|
|
93
|
+
M when is_map(M) -> M;
|
|
94
|
+
_ -> #{}
|
|
95
|
+
end,
|
|
96
|
+
KeyPath = maps:get(<<"key_path">>, AuthMap, undefined),
|
|
97
|
+
Passphrase = maps:get(<<"passphrase">>, AuthMap, undefined),
|
|
98
|
+
case KeyPath of
|
|
99
|
+
undefined -> {error, no_key_path};
|
|
100
|
+
_ -> read_private_key(binary_to_list(KeyPath), Algorithm, Passphrase)
|
|
101
|
+
end.
|
|
102
|
+
|
|
103
|
+
%% ---------- Authentication chain ----------
|
|
104
|
+
|
|
105
|
+
%% @doc Multi-method authentication fallback chain.
|
|
106
|
+
%% OTP ssh integrates auth into ssh:connect, so each method requires a separate
|
|
107
|
+
%% ssh:connect attempt. V1.0 supports: publickey -> password fallback.
|
|
108
|
+
%%
|
|
109
|
+
%% Returns {ok, SshRef} | {error, Reason}
|
|
110
|
+
-spec authenticate_chain(map(), [atom()]) -> {ok, reference()} | {error, term()}.
|
|
111
|
+
authenticate_chain(_ConnectCtx, []) ->
|
|
112
|
+
{error, all_auth_methods_failed};
|
|
113
|
+
authenticate_chain(ConnectCtx, [Method | Rest]) ->
|
|
114
|
+
Opts = build_opts_for_method(ConnectCtx, Method),
|
|
115
|
+
Host = binary_to_list(maps:get(host, ConnectCtx)),
|
|
116
|
+
Port = maps:get(port, ConnectCtx, 22),
|
|
117
|
+
case ssh:connect(Host, Port, Opts, ?CONNECT_TIMEOUT_MS) of
|
|
118
|
+
{ok, Ref} -> {ok, Ref};
|
|
119
|
+
{error, _} -> authenticate_chain(ConnectCtx, Rest)
|
|
120
|
+
end.
|
|
121
|
+
|
|
122
|
+
%% ---------- Internal ----------
|
|
123
|
+
|
|
124
|
+
%% @doc Read a private key file (OpenSSH, PKCS8, SEC1).
|
|
125
|
+
read_private_key(Path, _Algorithm, Passphrase) ->
|
|
126
|
+
PwdOpt = case Passphrase of
|
|
127
|
+
undefined -> [];
|
|
128
|
+
Pwd -> [{passphrase, binary_to_list(Pwd)}]
|
|
129
|
+
end,
|
|
130
|
+
case public_key:read_keyfile(Path, PwdOpt) of
|
|
131
|
+
{ok, [Key | _]} -> {ok, Key};
|
|
132
|
+
{ok, Key} -> {ok, Key};
|
|
133
|
+
{error, Reason} -> {error, Reason}
|
|
134
|
+
end.
|
|
135
|
+
|
|
136
|
+
%% @doc Build ssh:connect options for a specific auth method.
|
|
137
|
+
build_opts_for_method(ConnectCtx, publickey) ->
|
|
138
|
+
AuthMap = maps:get(auth, ConnectCtx, #{}),
|
|
139
|
+
[
|
|
140
|
+
{user, binary_to_list(maps:get(user, ConnectCtx))},
|
|
141
|
+
{key_cb, {?MODULE, AuthMap}}
|
|
142
|
+
];
|
|
143
|
+
build_opts_for_method(ConnectCtx, password) ->
|
|
144
|
+
AuthMap = maps:get(auth, ConnectCtx, #{}),
|
|
145
|
+
[
|
|
146
|
+
{user, binary_to_list(maps:get(user, ConnectCtx))},
|
|
147
|
+
{password, binary_to_list(maps:get(<<"password">>, AuthMap))}
|
|
148
|
+
];
|
|
149
|
+
build_opts_for_method(ConnectCtx, keyboard_interactive) ->
|
|
150
|
+
AuthMap = maps:get(auth, ConnectCtx, #{}),
|
|
151
|
+
[
|
|
152
|
+
{user, binary_to_list(maps:get(user, ConnectCtx))},
|
|
153
|
+
{keyboard_interactive, fun(_Prompts, _Ssh) ->
|
|
154
|
+
{ok, [binary_to_list(maps:get(<<"password">>, AuthMap))]}
|
|
155
|
+
end}
|
|
156
|
+
].
|